summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorStefan Behnel <stefan_ml@behnel.de>2020-10-01 10:39:48 +0200
committerStefan Behnel <stefan_ml@behnel.de>2020-10-01 10:39:48 +0200
commit45aa5a1cf518ba529afb56a55150bcec683cf2e4 (patch)
treeb54022fb1ede8e6593a217b285b432a45499bb08 /src
parente77ab92a1cd65e59db98a00509640b63e37f8b3b (diff)
downloadpython-lxml-45aa5a1cf518ba529afb56a55150bcec683cf2e4.tar.gz
LP#1869455: C14N 2.0 serialisation failed for unprefixed attributes when a default namespace was defined.
Diffstat (limited to 'src')
-rw-r--r--src/lxml/serializer.pxi7
-rw-r--r--src/lxml/tests/test_elementtree.py8
2 files changed, 14 insertions, 1 deletions
diff --git a/src/lxml/serializer.pxi b/src/lxml/serializer.pxi
index 3a26f752..d66f59a7 100644
--- a/src/lxml/serializer.pxi
+++ b/src/lxml/serializer.pxi
@@ -1078,7 +1078,12 @@ cdef class C14NWriterTarget:
self._declared_ns_stack[-1].append((uri, prefix))
return f'{prefix}:{tag}' if prefix else tag, tag, uri
- raise ValueError(f'Namespace "{uri}" is not declared in scope')
+ 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}" of name "{tag}" is not declared in scope')
def data(self, data):
if not self._ignored_depth:
diff --git a/src/lxml/tests/test_elementtree.py b/src/lxml/tests/test_elementtree.py
index 48509ace..45c26cc0 100644
--- a/src/lxml/tests/test_elementtree.py
+++ b/src/lxml/tests/test_elementtree.py
@@ -4640,6 +4640,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):
c14n_roundtrip = self.c14n_roundtrip
xml = textwrap.dedent("""\