Skip to content

Commit 157aef7

Browse files
authored
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
1 parent c5bc67b commit 157aef7

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Lib/html/parser.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def __init__(self, *, convert_charrefs=True):
8989
If convert_charrefs is True (the default), all character references
9090
are automatically converted to the corresponding Unicode characters.
9191
"""
92+
super().__init__()
9293
self.convert_charrefs = convert_charrefs
9394
self.reset()
9495

@@ -98,7 +99,7 @@ def reset(self):
9899
self.lasttag = '???'
99100
self.interesting = interesting_normal
100101
self.cdata_elem = None
101-
_markupbase.ParserBase.reset(self)
102+
super().reset()
102103

103104
def feed(self, data):
104105
r"""Feed data to the parser.

Lib/test/test_htmlparser.py

+14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import pprint
55
import unittest
66

7+
from unittest.mock import patch
8+
79

810
class EventCollector(html.parser.HTMLParser):
911

@@ -787,5 +789,17 @@ def test_weird_chars_in_unquoted_attribute_values(self):
787789
('starttag', 'form',
788790
[('action', 'bogus|&#()value')])])
789791

792+
793+
class TestInheritance(unittest.TestCase):
794+
795+
@patch("_markupbase.ParserBase.__init__")
796+
@patch("_markupbase.ParserBase.reset")
797+
def test_base_class_methods_called(self, super_reset_method, super_init_method):
798+
with patch('_markupbase.ParserBase') as parser_base:
799+
EventCollector()
800+
super_init_method.assert_called_once()
801+
super_reset_method.assert_called_once()
802+
803+
790804
if __name__ == "__main__":
791805
unittest.main()

0 commit comments

Comments
 (0)