Skip to content

Commit

Permalink
bpo-45948: Remove constructor discrepancy in C version of ElementTree…
Browse files Browse the repository at this point in the history
….XMLParser (pythonGH-31152)

Both implementations accept target=None now.
  • Loading branch information
jacobtylerwalls authored Feb 12, 2022
1 parent 9d9cfd6 commit 168fd64
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
9 changes: 9 additions & 0 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,15 @@ def end_ns(self, prefix):
('end-ns', ''),
])

def test_initialize_parser_without_target(self):
# Explicit None
parser = ET.XMLParser(target=None)
self.assertIsInstance(parser.target, ET.TreeBuilder)

# Implicit None
parser2 = ET.XMLParser()
self.assertIsInstance(parser2.target, ET.TreeBuilder)

def test_children(self):
# Test Element children iteration

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fixed a discrepancy in the C implementation of the
:mod:`xml.etree.ElementTree` module. Now, instantiating an
:class:`xml.etree.ElementTree.XMLParser` with a ``target=None``
keyword provides a default :class:`xml.etree.ElementTree.TreeBuilder`
target as the Python implementation does.
6 changes: 3 additions & 3 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -3637,15 +3637,15 @@ ignore_attribute_error(PyObject *value)
_elementtree.XMLParser.__init__
*
target: object = NULL
target: object = None
encoding: str(accept={str, NoneType}) = None
[clinic start generated code]*/

static int
_elementtree_XMLParser___init___impl(XMLParserObject *self, PyObject *target,
const char *encoding)
/*[clinic end generated code: output=3ae45ec6cdf344e4 input=53e35a829ae043e8]*/
/*[clinic end generated code: output=3ae45ec6cdf344e4 input=7e716dd6e4f3e439]*/
{
self->entity = PyDict_New();
if (!self->entity)
Expand All @@ -3670,7 +3670,7 @@ _elementtree_XMLParser___init___impl(XMLParserObject *self, PyObject *target,
(unsigned long)_Py_HashSecret.expat.hashsalt);
}

if (target) {
if (target != Py_None) {
Py_INCREF(target);
} else {
target = treebuilder_new(&TreeBuilder_Type, NULL, NULL);
Expand Down
4 changes: 2 additions & 2 deletions Modules/clinic/_elementtree.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 168fd64

Please sign in to comment.