From 105835c63ba6f241863dbb18969a8068e82a4a95 Mon Sep 17 00:00:00 2001 From: Marek Suscak Date: Sat, 2 Apr 2022 21:18:02 +0200 Subject: [PATCH 1/5] Fix bpo-31844 --- Lib/_markupbase.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/_markupbase.py b/Lib/_markupbase.py index 2af5f1c23b6066..7091eb635b37f2 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: From f94036bcca7f54633aa5859fc735003ea664f408 Mon Sep 17 00:00:00 2001 From: Marek Suscak Date: Sat, 2 Apr 2022 21:29:55 +0200 Subject: [PATCH 2/5] Add tests --- Lib/test/test_htmlparser.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Lib/test/test_htmlparser.py b/Lib/test/test_htmlparser.py index 12917755a56017..23ab2366b40c45 100644 --- a/Lib/test/test_htmlparser.py +++ b/Lib/test/test_htmlparser.py @@ -787,5 +787,31 @@ def test_weird_chars_in_unquoted_attribute_values(self): ('starttag', 'form', [('action', 'bogus|&#()value')])]) + def test_invalid_keyword_error_exception(self): + class InvalidMarkupException(Exception): + pass + + class MyHTMLParser(html.parser.HTMLParser): + + def error(self, message): + raise InvalidMarkupException(message) + + parser = MyHTMLParser() + with self.assertRaises(InvalidMarkupException): + parser.feed('') + + def test_invalid_keyword_error_pass(self): + class MyHTMLParser(html.parser.HTMLParser): + + def error(self, message): + pass + + parser = MyHTMLParser() + self.assertEqual( + parser.feed(''), + None + ) + + if __name__ == "__main__": unittest.main() From ff38a95e48cad22013539d3f996b2a3b7f063877 Mon Sep 17 00:00:00 2001 From: Ezio Melotti Date: Wed, 6 Apr 2022 10:40:51 +0200 Subject: [PATCH 3/5] Add comments pointing to the bpo issue and remove whitespace. --- Lib/test/test_htmlparser.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/Lib/test/test_htmlparser.py b/Lib/test/test_htmlparser.py index 23ab2366b40c45..44a76a445d8a5d 100644 --- a/Lib/test/test_htmlparser.py +++ b/Lib/test/test_htmlparser.py @@ -788,29 +788,25 @@ def test_weird_chars_in_unquoted_attribute_values(self): [('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('') 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(''), - None - ) + self.assertEqual(parser.feed(''), None) if __name__ == "__main__": From 53755e1f491f798ae687839d7715e4663b59cca8 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 9 Apr 2022 11:28:49 +0000 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2022-04-09-11-28-49.bpo-34480.Pw-GJ6.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-04-09-11-28-49.bpo-34480.Pw-GJ6.rst 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 00000000000000..7e922cd91cf36a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-04-09-11-28-49.bpo-34480.Pw-GJ6.rst @@ -0,0 +1 @@ +Fix a bug where `match` variable is used prior to being defined when an invalid markup is encountered and :meth:`ParserBase.error` is implemented as a NOP in a child class of :class:`ParserBase`. From 950fb5728cd1341e68da3c286da6a93cf4aff358 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Mon, 16 May 2022 17:31:03 +0200 Subject: [PATCH 5/5] Reword NEWS entry to be more high-level --- .../next/Library/2022-04-09-11-28-49.bpo-34480.Pw-GJ6.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 index 7e922cd91cf36a..748df89b07e3fc 100644 --- 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 @@ -1 +1,3 @@ -Fix a bug where `match` variable is used prior to being defined when an invalid markup is encountered and :meth:`ParserBase.error` is implemented as a NOP in a child class of :class:`ParserBase`. +Fix a bug where :mod:`_markupbase` raised an :exc:`UnboundLocalError` +when an invalid keyword was found in marked section. Patch by Marek +Suscak.