diff Lib/HTMLParser.py @ 67188:05cccd4257a2 2.7

Fix Issue10759 - HTMLParser.unescape() to handle malform charrefs.
author Senthil Kumaran <orsenthil@gmail.com>
date Tue, 28 Dec 2010 16:05:07 +0000
parents ebda6803f8d5
children 7d4dea76c476
line wrap: on
line diff
--- a/Lib/HTMLParser.py	Tue Dec 28 16:04:06 2010 +0000
+++ b/Lib/HTMLParser.py	Tue Dec 28 16:05:07 2010 +0000
@@ -367,13 +367,16 @@
             return s
         def replaceEntities(s):
             s = s.groups()[0]
-            if s[0] == "#":
-                s = s[1:]
-                if s[0] in ['x','X']:
-                    c = int(s[1:], 16)
-                else:
-                    c = int(s)
-                return unichr(c)
+            try:
+                if s[0] == "#":
+                    s = s[1:]
+                    if s[0] in ['x','X']:
+                        c = int(s[1:], 16)
+                    else:
+                        c = int(s)
+                    return unichr(c)
+            except ValueError:
+                return '&#'+s+';'
             else:
                 # Cannot use name2codepoint directly, because HTMLParser supports apos,
                 # which is not part of HTML 4