[Python-checkins] [3.9] bpo-34480: fix bug where match variable is used prior to being defined (GH-17643) (GH-32256)

ambv webhook-mailer at python.org
Mon May 16 12:19:11 EDT 2022


https://github.com/python/cpython/commit/518b2389673833887b26a8474eeb9530d17e2b8d
commit: 518b2389673833887b26a8474eeb9530d17e2b8d
branch: 3.9
author: Marek Suscak <marek.suscak at gmail.com>
committer: ambv <lukasz at langa.pl>
date: 2022-05-16T18:19:04+02:00
summary:

[3.9] bpo-34480: fix bug where match variable is used prior to being defined (GH-17643) (GH-32256)

Co-authored-by: Ezio Melotti <ezio.melotti at gmail.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Łukasz Langa <lukasz at langa.pl>

files:
A Misc/NEWS.d/next/Library/2022-04-09-11-28-49.bpo-34480.Pw-GJ6.rst
M Lib/_markupbase.py
M Lib/test/test_htmlparser.py

diff --git a/Lib/_markupbase.py b/Lib/_markupbase.py
index 2af5f1c23b606..7091eb635b37f 100644
--- a/Lib/_markupbase.py
+++ b/Lib/_markupbase.py
@@ -157,6 +157,7 @@ def parse_marked_section(self, i, report=1):
             match= _msmarkedsectionclose.search(rawdata, i+3)
         else:
             self.error('unknown status keyword %r in marked section' % rawdata[i+3:j])
+            match = None
         if not match:
             return -1
         if report:
diff --git a/Lib/test/test_htmlparser.py b/Lib/test/test_htmlparser.py
index 12917755a5601..44a76a445d8a5 100644
--- a/Lib/test/test_htmlparser.py
+++ b/Lib/test/test_htmlparser.py
@@ -787,5 +787,27 @@ def test_weird_chars_in_unquoted_attribute_values(self):
                             ('starttag', 'form',
                                 [('action', 'bogus|&#()value')])])
 
+    def test_invalid_keyword_error_exception(self):
+        # bpo-34480: check that subclasses that define an
+        # error method that raises an exception work
+        class InvalidMarkupException(Exception):
+            pass
+        class MyHTMLParser(html.parser.HTMLParser):
+            def error(self, message):
+                raise InvalidMarkupException(message)
+        parser = MyHTMLParser()
+        with self.assertRaises(InvalidMarkupException):
+            parser.feed('<![invalid>')
+
+    def test_invalid_keyword_error_pass(self):
+        # bpo-34480: check that subclasses that define an
+        # error method that doesn't raise an exception work
+        class MyHTMLParser(html.parser.HTMLParser):
+            def error(self, message):
+                pass
+        parser = MyHTMLParser()
+        self.assertEqual(parser.feed('<![invalid>'), None)
+
+
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2022-04-09-11-28-49.bpo-34480.Pw-GJ6.rst b/Misc/NEWS.d/next/Library/2022-04-09-11-28-49.bpo-34480.Pw-GJ6.rst
new file mode 100644
index 0000000000000..748df89b07e3f
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-04-09-11-28-49.bpo-34480.Pw-GJ6.rst
@@ -0,0 +1,3 @@
+Fix a bug where :mod:`_markupbase` raised an :exc:`UnboundLocalError`
+when an invalid keyword was found in marked section. Patch by Marek
+Suscak.



More information about the Python-checkins mailing list