Skip to content

Commit 6a412c9

Browse files
authored
bpo-41900: C14N 2.0 serialisation failed for unprefixed attributes when a default namespace was defined. (pythonGH-22474)
1 parent d4b9edd commit 6a412c9

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

Lib/test/test_xml_etree.py

+8
Original file line numberDiff line numberDiff line change
@@ -3899,6 +3899,14 @@ def test_simple_roundtrip(self):
38993899
#self.assertEqual(c14n_roundtrip("<doc xmlns:x='http://example.com/x' xmlns='http://example.com/default'><b y:a1='1' xmlns='http://example.com/default' a3='3' xmlns:y='http://example.com/y' y:a2='2'/></doc>"),
39003900
#'<doc xmlns:x="http://example.com/x"><b xmlns:y="http://example.com/y" a3="3" y:a1="1" y:a2="2"></b></doc>')
39013901

3902+
# Namespace issues
3903+
xml = '<X xmlns="http://nps/a"><Y targets="abc,xyz"></Y></X>'
3904+
self.assertEqual(c14n_roundtrip(xml), xml)
3905+
xml = '<X xmlns="http://nps/a"><Y xmlns="http://nsp/b" targets="abc,xyz"></Y></X>'
3906+
self.assertEqual(c14n_roundtrip(xml), xml)
3907+
xml = '<X xmlns="http://nps/a"><Y xmlns:b="http://nsp/b" b:targets="abc,xyz"></Y></X>'
3908+
self.assertEqual(c14n_roundtrip(xml), xml)
3909+
39023910
def test_c14n_exclusion(self):
39033911
xml = textwrap.dedent("""\
39043912
<root xmlns:x="http://example.com/x">

Lib/xml/etree/ElementTree.py

+5
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,11 @@ def _qname(self, qname, uri=None):
18761876
self._declared_ns_stack[-1].append((uri, prefix))
18771877
return f'{prefix}:{tag}' if prefix else tag, tag, uri
18781878

1879+
if not uri:
1880+
# As soon as a default namespace is defined,
1881+
# anything that has no namespace (and thus, no prefix) goes there.
1882+
return tag, tag, uri
1883+
18791884
raise ValueError(f'Namespace "{uri}" is not declared in scope')
18801885

18811886
def data(self, data):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
C14N 2.0 serialisation in xml.etree.ElementTree failed for unprefixed attributes
2+
when a default namespace was defined.

0 commit comments

Comments
 (0)