Merge pull request #1198 from goffi-contrib/9730-goffi-contrib-fix-xmpp-parsing-python-3.8

Author: goffi-contrib

Reviewer: glyph

Fixes: ticket:9730

Fix parsing of namespaced attributes with Python 3.8 in twisted.words.
This commit is contained in:
Glyph 2020-02-29 22:08:27 -08:00 committed by GitHub
commit a24e9345c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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)