Skip to content

Commit 52467b1

Browse files
committed
Issue #14007: make XMLParser a real subclassable type exported from _elementtree. +cleanups
1 parent 7e0229e commit 52467b1

File tree

3 files changed

+174
-115
lines changed

3 files changed

+174
-115
lines changed

Doc/library/xml.etree.elementtree.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -646,8 +646,8 @@ ElementTree Objects
646646

647647
Loads an external XML section into this element tree. *source* is a file
648648
name or :term:`file object`. *parser* is an optional parser instance.
649-
If not given, the standard XMLParser parser is used. Returns the section
650-
root element.
649+
If not given, the standard :class:`XMLParser` parser is used. Returns the
650+
section root element.
651651

652652

653653
.. method:: write(file, encoding="us-ascii", xml_declaration=None, method="xml")
@@ -767,9 +767,9 @@ XMLParser Objects
767767
:class:`Element` structure builder for XML source data, based on the expat
768768
parser. *html* are predefined HTML entities. This flag is not supported by
769769
the current implementation. *target* is the target object. If omitted, the
770-
builder uses an instance of the standard TreeBuilder class. *encoding* [1]_
771-
is optional. If given, the value overrides the encoding specified in the
772-
XML file.
770+
builder uses an instance of the standard :class:`TreeBuilder` class.
771+
*encoding* [1]_ is optional. If given, the value overrides the encoding
772+
specified in the XML file.
773773

774774

775775
.. method:: close()

Lib/test/test_xml_etree.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,6 +2028,34 @@ def close(self):
20282028
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'))
20292029

20302030

2031+
class XMLParserTest(unittest.TestCase):
2032+
sample1 = '<file><line>22</line></file>'
2033+
2034+
def _check_sample_element(self, e):
2035+
self.assertEqual(e.tag, 'file')
2036+
self.assertEqual(e[0].tag, 'line')
2037+
self.assertEqual(e[0].text, '22')
2038+
2039+
def test_constructor_args(self):
2040+
# Positional args. The first (html) is not supported, but should be
2041+
# nevertheless correctly accepted.
2042+
parser = ET.XMLParser(None, ET.TreeBuilder(), 'utf-8')
2043+
parser.feed(self.sample1)
2044+
self._check_sample_element(parser.close())
2045+
2046+
# Now as keyword args.
2047+
parser2 = ET.XMLParser(encoding='utf-8', html=[{}], target=ET.TreeBuilder())
2048+
parser2.feed(self.sample1)
2049+
self._check_sample_element(parser2.close())
2050+
2051+
def test_subclass(self):
2052+
class MyParser(ET.XMLParser):
2053+
pass
2054+
parser = MyParser()
2055+
parser.feed(self.sample1)
2056+
self._check_sample_element(parser.close())
2057+
2058+
20312059
class NoAcceleratorTest(unittest.TestCase):
20322060
# Test that the C accelerator was not imported for pyET
20332061
def test_correct_import_pyET(self):
@@ -2245,6 +2273,7 @@ def test_main(module=pyET):
22452273
ElementTreeTest,
22462274
NamespaceParseTest,
22472275
TreeBuilderTest,
2276+
XMLParserTest,
22482277
KeywordArgsTest]
22492278
if module is pyET:
22502279
# Run the tests specific to the Python implementation

0 commit comments

Comments
 (0)