All the error constants in `xml.parsers.expat.errors` are strings. However, when expat raises an ExpatError exception, ExpatError.code attribute is a number. There seems to be no way of associating ExpatError with a corresponding error code from `xml.parsers.expat.errors. Following code snippet should print "Ignore empty file" but in Python 2.6 it raises ExpatError. from xml.etree import ElementTree from xml.parsers import expat try: ElementTree.parse('') except expat.ExpatError as e: if e.code == expat.errors.XML_ERROR_NO_ELEMENTS: print "Ignore empty file" else: raise
After talking to Fred, I'll add a "codes" attribute mapping the error constant strings to their codes. Changing the constants to integers would be very bad for b/w compatibility.