Fix parsing of namespaced attributes with Python 3.8 in twisted.words.xish.domish.ExpatElementStream

This commit is contained in:
Jérôme Poisson 2019-11-17 19:48:53 +01:00
parent 56a81939b5
commit 7ca563c625
3 changed files with 27 additions and 2 deletions

View File

@ -0,0 +1 @@
Fixed parsing of streams with Python 3.8 when there are spaces in namespaces or namespaced attributes in twisted.words.xish.domish.ExpatElementStream

View File

@ -350,6 +350,23 @@ class DomishStreamTestsMixin:
self.elements[0].attributes, {(" bar baz ", "baz"): "quux"})
def test_attributesWithNamespaces(self):
"""
Attributes with namespace are parsed without Exception.
(https://twistedmatrix.com/trac/ticket/9730 regression test)
"""
xml = b"""<root xmlns:test='http://example.org' xml:lang='en'>
<test:test>test</test:test>
</root>"""
# with Python 3.8 and without #9730 fix, the following error would
# happen at next line:
# ``RuntimeError: dictionary keys changed during iteration``
self.stream.parse(xml)
self.assertEqual(self.elements[0].uri, "http://example.org")
def testChildPrefix(self):
xml = b"<root xmlns='testns' xmlns:foo='testns2'><foo:child/></root>"

View File

@ -807,11 +807,18 @@ class ExpatElementStream:
qname = ('', name)
# Process attributes
newAttrs = {}
toDelete = []
for k, v in attrs.items():
if " " in k:
aqname = k.rsplit(" ", 1)
attrs[(aqname[0], aqname[1])] = v
del attrs[k]
newAttrs[(aqname[0], aqname[1])] = v
toDelete.append(k)
attrs.update(newAttrs)
for k in toDelete:
del attrs[k]
# Construct the new element
e = Element(qname, self.defaultNsStack[-1], attrs, self.localPrefixes)