]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.9] bpo-34480: fix bug where match variable is used prior to being defined (GH...
authorMarek Suscak <marek.suscak@gmail.com>
Mon, 16 May 2022 16:19:04 +0000 (18:19 +0200)
committerGitHub <noreply@github.com>
Mon, 16 May 2022 16:19:04 +0000 (18:19 +0200)
Co-authored-by: Ezio Melotti <ezio.melotti@gmail.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Ɓukasz Langa <lukasz@langa.pl>
Lib/_markupbase.py
Lib/test/test_htmlparser.py
Misc/NEWS.d/next/Library/2022-04-09-11-28-49.bpo-34480.Pw-GJ6.rst [new file with mode: 0644]

index 2af5f1c23b6066284938fb1cc697bc1fc2fea6ea..7091eb635b37f2c4405057ad7dfc0c0a7f9618e0 100644 (file)
@@ -157,6 +157,7 @@ class ParserBase:
             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:
index 12917755a56017e31e6a5ba2c241bd01673e823a..44a76a445d8a5d349a5ffb228595538709def72e 100644 (file)
@@ -787,5 +787,27 @@ class AttributesTestCase(TestCaseBase):
                             ('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 (file)
index 0000000..748df89
--- /dev/null
@@ -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.