@@ -512,25 +512,29 @@ def _convertEntities(self, match):
512512 entities with the appropriate Unicode characters. If HTML
513513 entities are being converted, any unrecognized entities are
514514 escaped."""
515- x = match .group (1 )
516- if self .convertHTMLEntities and x in name2codepoint :
517- return unichr (name2codepoint [x ])
518- elif x in self .XML_ENTITIES_TO_SPECIAL_CHARS :
519- if self .convertXMLEntities :
520- return self .XML_ENTITIES_TO_SPECIAL_CHARS [x ]
521- else :
522- return u'&%s;' % x
523- elif len (x ) > 0 and x [0 ] == '#' :
524- # Handle numeric entities
525- if len (x ) > 1 and x [1 ] == 'x' :
526- return unichr (int (x [2 :], 16 ))
527- else :
528- return unichr (int (x [1 :]))
515+ try :
516+ x = match .group (1 )
517+ if self .convertHTMLEntities and x in name2codepoint :
518+ return unichr (name2codepoint [x ])
519+ elif x in self .XML_ENTITIES_TO_SPECIAL_CHARS :
520+ if self .convertXMLEntities :
521+ return self .XML_ENTITIES_TO_SPECIAL_CHARS [x ]
522+ else :
523+ return u'&%s;' % x
524+ elif len (x ) > 0 and x [0 ] == '#' :
525+ # Handle numeric entities
526+ if len (x ) > 1 and x [1 ] == 'x' :
527+ return unichr (int (x [2 :], 16 ))
528+ else :
529+ return unichr (int (x [1 :]))
529530
530- elif self .escapeUnrecognizedEntities :
531- return u'&%s;' % x
532- else :
533- return u'&%s;' % x
531+ elif self .escapeUnrecognizedEntities :
532+ return u'&%s;' % x
533+
534+ except ValueError : # e.g. ValueError: unichr() arg not in range(0x10000)
535+ pass
536+
537+ return u'&%s;' % x
534538
535539 def __init__ (self , parser , name , attrs = None , parent = None ,
536540 previous = None ):
0 commit comments