]> granicus.if.org Git - python/commitdiff
Fix Issue10759 - html.parser.unescape() fails on HTML entities with incorrect syntax
authorSenthil Kumaran <orsenthil@gmail.com>
Tue, 28 Dec 2010 15:55:16 +0000 (15:55 +0000)
committerSenthil Kumaran <orsenthil@gmail.com>
Tue, 28 Dec 2010 15:55:16 +0000 (15:55 +0000)
Lib/html/parser.py
Lib/test/test_htmlparser.py

index 8d275ab315858f4c49b8ef593e9bb66dd41c0637..21ebbc3eaf078989ab9a1bfb6dd20746a333fe9a 100644 (file)
@@ -434,13 +434,16 @@ class HTMLParser(_markupbase.ParserBase):
             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 chr(c)
+            try:
+                if s[0] == "#":
+                    s = s[1:]
+                    if s[0] in ['x','X']:
+                        c = int(s[1:], 16)
+                    else:
+                        c = int(s)
+                    return chr(c)
+            except ValueError:
+                return '&#'+ s +';'
             else:
                 # Cannot use name2codepoint directly, because HTMLParser
                 # supports apos, which is not part of HTML 4
index beaf6b63a239ca5892bd47b200ecab34ee8ef3a6..5ecd016084dbf3a05f74615118258f9e2205043d 100644 (file)
@@ -356,6 +356,11 @@ class HTMLParserTolerantTestCase(TestCaseBase):
                                 [('action', 'bogus|&#()value')])],
                         collector = self.collector)
 
+    def test_unescape_function(self):
+        p = html.parser.HTMLParser()
+        self.assertEqual(p.unescape('&#bad;'),'&#bad;')
+        self.assertEqual(p.unescape('&#0038;'),'&')
+
 
 def test_main():
     support.run_unittest(HTMLParserTestCase, HTMLParserTolerantTestCase)