From f7a3bd19c7246087a537cbc7530d2fc316bea6fd Mon Sep 17 00:00:00 2001 From: "Felix C. Stegerman" Date: Mon, 26 Oct 2020 15:58:44 +0100 Subject: [PATCH 1/3] bpo-42151: don't set specified_attributes=1 in pure Python ElementTree Make the pure Python implementation of xml.etree.ElementTree behave the same as the C implementation (_elementree) with regards to default attribute values (by not setting specified_attributes=1). --- Lib/xml/etree/ElementTree.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py index 7a269001d6e18f6..168418e466c4522 100644 --- a/Lib/xml/etree/ElementTree.py +++ b/Lib/xml/etree/ElementTree.py @@ -1560,7 +1560,6 @@ def __init__(self, *, target=None, encoding=None): # Configure pyexpat: buffering, new-style attribute handling. parser.buffer_text = 1 parser.ordered_attributes = 1 - parser.specified_attributes = 1 self._doctype = None self.entity = {} try: @@ -1580,7 +1579,6 @@ def _setevents(self, events_queue, events_to_report): for event_name in events_to_report: if event_name == "start": parser.ordered_attributes = 1 - parser.specified_attributes = 1 def handler(tag, attrib_in, event=event_name, append=append, start=self._start): append((event, start(tag, attrib_in))) From 1739e878d45c60dd37d076bef461b37924139039 Mon Sep 17 00:00:00 2001 From: "Felix C. Stegerman" Date: Mon, 26 Oct 2020 16:17:18 +0100 Subject: [PATCH 2/3] bpo-42151: add ElementTree test for default attribute values --- Lib/test/test_xml_etree.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index fd4a38527fde066..fcb1f7fdfbbde54 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -108,6 +108,19 @@ &entity; """ +ATTLIST_XML = """\ + + + + + +]> + +&qux; + +""" + def checkwarnings(*filters, quiet=False): def decorator(test): def newtest(*args, **kwargs): @@ -1354,6 +1367,12 @@ def test_tree_write_attribute_order(self): self.assertEqual(serialize(root, method='html'), '') + def test_attlist_default(self): + # Test default attribute values; See BPO 42151. + root = ET.fromstring(ATTLIST_XML) + self.assertEqual(root[0].attrib, + {'{http://www.w3.org/XML/1998/namespace}lang': 'eng'}) + class XMLPullParserTest(unittest.TestCase): From a325328166aa2fee744d4ca08cf77dce2fa142ec Mon Sep 17 00:00:00 2001 From: "Felix C. Stegerman" Date: Mon, 26 Oct 2020 18:01:50 +0100 Subject: [PATCH 3/3] bpo-42151: add NEWS entry --- .../next/Library/2020-10-26-18-01-09.bpo-42151.et5f7s.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-10-26-18-01-09.bpo-42151.et5f7s.rst diff --git a/Misc/NEWS.d/next/Library/2020-10-26-18-01-09.bpo-42151.et5f7s.rst b/Misc/NEWS.d/next/Library/2020-10-26-18-01-09.bpo-42151.et5f7s.rst new file mode 100644 index 000000000000000..6361f2c088d2df0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-10-26-18-01-09.bpo-42151.et5f7s.rst @@ -0,0 +1,3 @@ +Make the pure Python implementation of :mod:`xml.etree.ElementTree` behave +the same as the C implementation (:mod:`_elementree`) regarding default +attribute values (by not setting ``specified_attributes=1``).