[Python-checkins] cpython (2.7): #13987: HTMLParser is now able to handle EOFs in the middle of a construct.

ezio.melotti python-checkins at python.org
Wed Feb 15 11:44:34 CET 2012


http://hg.python.org/cpython/rev/11a31eb5da93
changeset:   74945:11a31eb5da93
branch:      2.7
user:        Ezio Melotti <ezio.melotti at gmail.com>
date:        Wed Feb 15 12:44:23 2012 +0200
summary:
  #13987: HTMLParser is now able to handle EOFs in the middle of a construct.

files:
  Lib/HTMLParser.py           |  13 ++++++++++---
  Lib/test/test_htmlparser.py |  16 ++++++++--------
  Misc/NEWS                   |   3 +++
  3 files changed, 21 insertions(+), 11 deletions(-)


diff --git a/Lib/HTMLParser.py b/Lib/HTMLParser.py
--- a/Lib/HTMLParser.py
+++ b/Lib/HTMLParser.py
@@ -170,9 +170,16 @@
                 else:
                     break
                 if k < 0:
-                    if end:
-                        self.error("EOF in middle of construct")
-                    break
+                    if not end:
+                        break
+                    k = rawdata.find('>', i + 1)
+                    if k < 0:
+                        k = rawdata.find('<', i + 1)
+                        if k < 0:
+                            k = i + 1
+                    else:
+                        k += 1
+                    self.handle_data(rawdata[i:k])
                 i = self.updatepos(i, k)
             elif startswith("&#", i):
                 match = charref.match(rawdata, i)
diff --git a/Lib/test/test_htmlparser.py b/Lib/test/test_htmlparser.py
--- a/Lib/test/test_htmlparser.py
+++ b/Lib/test/test_htmlparser.py
@@ -204,16 +204,16 @@
     def test_starttag_junk_chars(self):
         self._run_check("</>", [])
         self._run_check("</$>", [('comment', '$')])
-        self._parse_error("</")
-        self._parse_error("</a")
+        self._run_check("</", [('data', '</')])
+        self._run_check("</a", [('data', '</a')])
         self._parse_error("<a<a>")
         self._run_check("</a<a>", [('endtag', 'a<a')])
-        self._parse_error("<!")
-        self._parse_error("<a")
-        self._parse_error("<a foo='bar'")
-        self._parse_error("<a foo='bar")
-        self._parse_error("<a foo='>'")
-        self._parse_error("<a foo='>")
+        self._run_check("<!", [('data', '<!')])
+        self._run_check("<a", [('data', '<a')])
+        self._run_check("<a foo='bar'", [('data', "<a foo='bar'")])
+        self._run_check("<a foo='bar", [('data', "<a foo='bar")])
+        self._run_check("<a foo='>'", [('data', "<a foo='>'")])
+        self._run_check("<a foo='>", [('data', "<a foo='>")])
 
     def test_valid_doctypes(self):
         # from http://www.w3.org/QA/2002/04/valid-dtd-list.html
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -93,6 +93,9 @@
 Library
 -------
 
+- Issue #13987: HTMLParser is now able to handle EOFs in the middle of a
+  construct.
+
 - Issue #13015: Fix a possible reference leak in defaultdict.__repr__.
   Patch by Suman Saha.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list