I also meant to say regarding the non self-closing tags.
IIRC A lot of the elif's were to reduce length of processing time in pairing routine, be removing a lot of the elements needed to check, those (HR BR IMG) as you say should be self closing
The previous elif would be catching the non-self-closing tags but not the end tag, thus causing the mismatch (which you probably realise)
One way to change it to not catch them would be add a 1 and a space to each of the tests...
Code:
elif entity[:4] == '<hr ' or entity[:4] == '<br ' or entity[:5] == '<img ':
this_entity.e_type = 3
Edit: But the elif with the '/>' would be catching them anyway.