From 774ab32fecb362ffe86b0e9d482a4cf629f49ba2 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Thu, 11 Aug 2022 14:24:03 +0900 Subject: [PATCH 1/3] gh-95813: Improve HTMLParser from the view of inheritance --- Lib/html/parser.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/html/parser.py b/Lib/html/parser.py index bef0f4fe4bf776..13c95c34e505c8 100644 --- a/Lib/html/parser.py +++ b/Lib/html/parser.py @@ -89,6 +89,7 @@ def __init__(self, *, convert_charrefs=True): If convert_charrefs is True (the default), all character references are automatically converted to the corresponding Unicode characters. """ + super().__init__() self.convert_charrefs = convert_charrefs self.reset() @@ -98,7 +99,7 @@ def reset(self): self.lasttag = '???' self.interesting = interesting_normal self.cdata_elem = None - _markupbase.ParserBase.reset(self) + super().reset() def feed(self, data): r"""Feed data to the parser. From 86c1fe4858b176dc442bd84de3c24c533d5ef952 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Thu, 11 Aug 2022 14:57:49 +0900 Subject: [PATCH 2/3] gh-95813: Add unittest --- Lib/test/test_htmlparser.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Lib/test/test_htmlparser.py b/Lib/test/test_htmlparser.py index 12917755a56017..d9b4f3332c349b 100644 --- a/Lib/test/test_htmlparser.py +++ b/Lib/test/test_htmlparser.py @@ -4,6 +4,8 @@ import pprint import unittest +from unittest.mock import patch + class EventCollector(html.parser.HTMLParser): @@ -787,5 +789,17 @@ def test_weird_chars_in_unquoted_attribute_values(self): ('starttag', 'form', [('action', 'bogus|&#()value')])]) + +class TestInheritance(unittest.TestCase): + + @patch("_markupbase.ParserBase.__init__") + @patch("_markupbase.ParserBase.reset") + def test_init(self, super_init_method, super_reset_method): + with patch('_markupbase.ParserBase') as parser_base: + a = html.parser.HTMLParser() + self.assertTrue(super_init_method.called) + self.assertTrue(super_reset_method.called) + + if __name__ == "__main__": unittest.main() From cfd22e7ee18ef4a20d6dde2601b5fb2f94ff660c Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Wed, 17 Aug 2022 13:15:35 +0900 Subject: [PATCH 3/3] Address code review --- Lib/test/test_htmlparser.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_htmlparser.py b/Lib/test/test_htmlparser.py index d9b4f3332c349b..b42a611c62c0aa 100644 --- a/Lib/test/test_htmlparser.py +++ b/Lib/test/test_htmlparser.py @@ -794,11 +794,11 @@ class TestInheritance(unittest.TestCase): @patch("_markupbase.ParserBase.__init__") @patch("_markupbase.ParserBase.reset") - def test_init(self, super_init_method, super_reset_method): + def test_base_class_methods_called(self, super_reset_method, super_init_method): with patch('_markupbase.ParserBase') as parser_base: - a = html.parser.HTMLParser() - self.assertTrue(super_init_method.called) - self.assertTrue(super_reset_method.called) + EventCollector() + super_init_method.assert_called_once() + super_reset_method.assert_called_once() if __name__ == "__main__":