[Python-checkins] gh-95813: Improve HTMLParser from the view of inheritance (#95874)

ezio-melotti webhook-mailer at python.org
Thu Aug 18 07:16:52 EDT 2022


https://github.com/python/cpython/commit/157aef79b07e07bf115e49cdf5ff25e74c66354e
commit: 157aef79b07e07bf115e49cdf5ff25e74c66354e
branch: main
author: Dong-hee Na <donghee.na at python.org>
committer: ezio-melotti <ezio.melotti at gmail.com>
date: 2022-08-18T13:16:33+02:00
summary:

gh-95813: Improve HTMLParser from the view of inheritance (#95874)

* gh-95813: Improve HTMLParser from the view of inheritance

* gh-95813: Add unittest

* Address code review

files:
M Lib/html/parser.py
M Lib/test/test_htmlparser.py

diff --git a/Lib/html/parser.py b/Lib/html/parser.py
index bef0f4fe4bf7..13c95c34e505 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.
diff --git a/Lib/test/test_htmlparser.py b/Lib/test/test_htmlparser.py
index 12917755a560..b42a611c62c0 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_base_class_methods_called(self, super_reset_method, super_init_method):
+        with patch('_markupbase.ParserBase') as parser_base:
+            EventCollector()
+            super_init_method.assert_called_once()
+            super_reset_method.assert_called_once()
+
+
 if __name__ == "__main__":
     unittest.main()



More information about the Python-checkins mailing list