]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-41900: C14N 2.0 serialisation failed for unprefixed attributes when a default...
authorMiss Skeleton (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 3 Oct 2020 06:42:38 +0000 (23:42 -0700)
committerGitHub <noreply@github.com>
Sat, 3 Oct 2020 06:42:38 +0000 (08:42 +0200)
(cherry picked from commit 6a412c94b6b68e7e3632562dc7358a12ffd1447f)

Lib/test/test_xml_etree.py
Lib/xml/etree/ElementTree.py
Misc/NEWS.d/next/Library/2020-10-01-10-50-12.bpo-41900.Cho7oh.rst [new file with mode: 0644]

index b2492cda848f9e0ead7261938cbbf90c949d048a..341a3c7cd7660ce17e743e7c1f2e024d4f17e0c5 100644 (file)
@@ -3701,6 +3701,14 @@ class C14NTest(unittest.TestCase):
         #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>"),
         #'<doc xmlns:x="http://example.com/x"><b xmlns:y="http://example.com/y" a3="3" y:a1="1" y:a2="2"></b></doc>')
 
+        # Namespace issues
+        xml = '<X xmlns="http://nps/a"><Y targets="abc,xyz"></Y></X>'
+        self.assertEqual(c14n_roundtrip(xml), xml)
+        xml = '<X xmlns="http://nps/a"><Y xmlns="http://nsp/b" targets="abc,xyz"></Y></X>'
+        self.assertEqual(c14n_roundtrip(xml), xml)
+        xml = '<X xmlns="http://nps/a"><Y xmlns:b="http://nsp/b" b:targets="abc,xyz"></Y></X>'
+        self.assertEqual(c14n_roundtrip(xml), xml)
+
     def test_c14n_exclusion(self):
         xml = textwrap.dedent("""\
         <root xmlns:x="http://example.com/x">
index 645e999a0be6ca3c0bc4d59aa96411110a26b189..598b569ea958b8a8018b9fbca88b48360dd566dd 100644 (file)
@@ -1849,6 +1849,11 @@ class C14NWriterTarget:
                 self._declared_ns_stack[-1].append((uri, prefix))
                 return f'{prefix}:{tag}' if prefix else tag, tag, uri
 
+        if not uri:
+            # As soon as a default namespace is defined,
+            # anything that has no namespace (and thus, no prefix) goes there.
+            return tag, tag, uri
+
         raise ValueError(f'Namespace "{uri}" is not declared in scope')
 
     def data(self, data):
diff --git a/Misc/NEWS.d/next/Library/2020-10-01-10-50-12.bpo-41900.Cho7oh.rst b/Misc/NEWS.d/next/Library/2020-10-01-10-50-12.bpo-41900.Cho7oh.rst
new file mode 100644 (file)
index 0000000..6586c09
--- /dev/null
@@ -0,0 +1,2 @@
+C14N 2.0 serialisation in xml.etree.ElementTree failed for unprefixed attributes
+when a default namespace was defined.