]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-42151: don't set specified_attributes=1 in pure Python ElementTree (GH-22987)
authorFelix C. Stegerman <flx@obfusk.net>
Wed, 24 Feb 2021 02:25:31 +0000 (03:25 +0100)
committerGitHub <noreply@github.com>
Wed, 24 Feb 2021 02:25:31 +0000 (11:25 +0900)
Lib/test/test_xml_etree.py
Lib/xml/etree/ElementTree.py
Misc/NEWS.d/next/Library/2020-10-26-18-01-09.bpo-42151.et5f7s.rst [new file with mode: 0644]

index fd4a38527fde0660c8442ce128c2fba720c22149..fcb1f7fdfbbde54b18cb0e5998a08d85f0c09c94 100644 (file)
@@ -108,6 +108,19 @@ EXTERNAL_ENTITY_XML = """\
 <document>&entity;</document>
 """
 
+ATTLIST_XML = """\
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE Foo [
+<!ELEMENT foo (bar*)>
+<!ELEMENT bar (#PCDATA)*>
+<!ATTLIST bar xml:lang CDATA "eng">
+<!ENTITY qux "quux">
+]>
+<foo>
+<bar>&qux;</bar>
+</foo>
+"""
+
 def checkwarnings(*filters, quiet=False):
     def decorator(test):
         def newtest(*args, **kwargs):
@@ -1354,6 +1367,12 @@ class ElementTreeTest(unittest.TestCase):
         self.assertEqual(serialize(root, method='html'),
                 '<cirriculum status="public" company="example"></cirriculum>')
 
+    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):
 
index 7a269001d6e18f60790f5565d69b301622139469..168418e466c45223fde8957ca997a956b298afbb 100644 (file)
@@ -1560,7 +1560,6 @@ class XMLParser:
         # 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 @@ class XMLParser:
         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)))
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 (file)
index 0000000..6361f2c
--- /dev/null
@@ -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``).