gh-145056: Accept frozendict in xml.etree (#145508) · python/cpython@7bdfce0 · GitHub
Skip to content

Commit 7bdfce0

Browse files
authored
gh-145056: Accept frozendict in xml.etree (#145508)
Element and SubElement of xml.etree.ElementTree now also accept frozendict for attrib. Export _PyDict_CopyAsDict() function.
1 parent c0ecf21 commit 7bdfce0

5 files changed

Lines changed: 36 additions & 14 deletions

File tree

Doc/library/xml.etree.elementtree.rst

Lines changed: 6 additions & 0 deletions

Include/internal/pycore_dict.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,8 @@ extern void _PyDict_Clear_LockHeld(PyObject *op);
160160
PyAPI_FUNC(void) _PyDict_EnsureSharedOnRead(PyDictObject *mp);
161161
#endif
162162

163-
extern PyObject* _PyDict_CopyAsDict(PyObject *op);
163+
// Export for '_elementtree' shared extension
164+
PyAPI_FUNC(PyObject*) _PyDict_CopyAsDict(PyObject *op);
164165

165166
#define DKIX_EMPTY (-1)
166167
#define DKIX_DUMMY (-2) /* Used internally */

Lib/test/test_xml_etree.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4472,17 +4472,24 @@ def test_issue14818(self):
44724472
ET.Element('a', dict(href="#"), id="foo"),
44734473
ET.Element('a', href="#", id="foo"),
44744474
ET.Element('a', dict(href="#", id="foo"), href="#", id="foo"),
4475+
ET.Element('a', frozendict(href="#", id="foo")),
4476+
ET.Element('a', frozendict(href="#"), id="foo"),
4477+
ET.Element('a', attrib=frozendict(href="#", id="foo")),
44754478
]
44764479
for e in elements:
44774480
self.assertEqual(e.tag, 'a')
44784481
self.assertEqual(e.attrib, dict(href="#", id="foo"))
44794482

44804483
e2 = ET.SubElement(elements[0], 'foobar', attrib={'key1': 'value1'})
44814484
self.assertEqual(e2.attrib['key1'], 'value1')
4485+
e3 = ET.SubElement(elements[0], 'foobar',
4486+
attrib=frozendict({'key1': 'value1'}))
4487+
self.assertEqual(e3.attrib['key1'], 'value1')
44824488

4483-
with self.assertRaisesRegex(TypeError, 'must be dict, not str'):
4489+
errmsg = 'must be dict or frozendict, not str'
4490+
with self.assertRaisesRegex(TypeError, errmsg):
44844491
ET.Element('a', "I'm not a dict")
4485-
with self.assertRaisesRegex(TypeError, 'must be dict, not str'):
4492+
with self.assertRaisesRegex(TypeError, errmsg):
44864493
ET.Element('a', attrib="I'm not a dict")
44874494

44884495
# --------------------------------------------------------------------

Lib/xml/etree/ElementTree.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ class Element:
165165
"""
166166

167167
def __init__(self, tag, attrib={}, **extra):
168-
if not isinstance(attrib, dict):
169-
raise TypeError("attrib must be dict, not %s" % (
168+
if not isinstance(attrib, (dict, frozendict)):
169+
raise TypeError("attrib must be dict or frozendict, not %s" % (
170170
attrib.__class__.__name__,))
171171
self.tag = tag
172172
self.attrib = {**attrib, **extra}

Modules/_elementtree.c

Lines changed: 17 additions & 9 deletions

0 commit comments

Comments
 (0)