]> granicus.if.org Git - python/commitdiff
Merged revisions 87542 via svnmerge from
authorSenthil Kumaran <orsenthil@gmail.com>
Tue, 28 Dec 2010 16:10:56 +0000 (16:10 +0000)
committerSenthil Kumaran <orsenthil@gmail.com>
Tue, 28 Dec 2010 16:10:56 +0000 (16:10 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r87542 | senthil.kumaran | 2010-12-28 23:55:16 +0800 (Tue, 28 Dec 2010) | 3 lines

  Fix Issue10759 - html.parser.unescape() fails on HTML entities with incorrect syntax
........

Lib/html/parser.py
Lib/test/test_htmlparser.py

index c2c7f6bf5da5fcab99caf17e19a7a7daafed0c83..3f68e189c4ce193df37bd1eeaf2b985e6346667b 100644 (file)
@@ -367,13 +367,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 e982218dba11fe3e1f2ac12faf7840c4bbed2e04..661d41dce9c9e7ca12201c2675ec051627bb785f 100755 (executable)
@@ -319,6 +319,10 @@ DOCTYPE html [
         self._run_check("<html foo='&euro;&amp;&#97;&#x61;&unsupported;'>", [
                 ("starttag", "html", [("foo", "\u20AC&aa&unsupported;")])
                 ])
+    def test_unescape_function(self):
+        p = html.parser.HTMLParser()
+        self.assertEqual(p.unescape('&#bad;'),'&#bad;')
+        self.assertEqual(p.unescape('&#0038;'),'&')
 
 
 def test_main():