Skip to content

Commit a0f2b66

Browse files
bpo-41900: C14N 2.0 serialisation failed for unprefixed attributes when a default namespace was defined. (GH-22474) (GH-22507)
(cherry picked from commit 6a412c9)
1 parent ebc8072 commit a0f2b66

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

Lib/test/test_xml_etree.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3894,6 +3894,14 @@ def test_simple_roundtrip(self):
38943894
#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>"),
38953895
#'<doc xmlns:x="http://example.com/x"><b xmlns:y="http://example.com/y" a3="3" y:a1="1" y:a2="2"></b></doc>')
38963896

3897+
# Namespace issues
3898+
xml = '<X xmlns="http://nps/a"><Y targets="abc,xyz"></Y></X>'
3899+
self.assertEqual(c14n_roundtrip(xml), xml)
3900+
xml = '<X xmlns="http://nps/a"><Y xmlns="http://nsp/b" targets="abc,xyz"></Y></X>'
3901+
self.assertEqual(c14n_roundtrip(xml), xml)
3902+
xml = '<X xmlns="http://nps/a"><Y xmlns:b="http://nsp/b" b:targets="abc,xyz"></Y></X>'
3903+
self.assertEqual(c14n_roundtrip(xml), xml)
3904+
38973905
def test_c14n_exclusion(self):
38983906
xml = textwrap.dedent("""\
38993907
<root xmlns:x="http://example.com/x">

Lib/xml/etree/ElementTree.py

Lines changed: 5 additions & 0 deletions
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):
Lines changed: 2 additions & 0 deletions
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)