Skip to content

Commit

Permalink
Merge branch 'trunk' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
glyph authored Mar 4, 2020
2 parents 5d33d07 + a24e934 commit 14d5941
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/twisted/words/newsfragments/9730.bugfix
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions src/twisted/words/test/test_domish.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,23 @@ def test_namespaceWithWhitespace(self):
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>"

Expand Down
11 changes: 9 additions & 2 deletions src/twisted/words/xish/domish.py
Original file line number Diff line number Diff line change
Expand Up @@ -807,11 +807,18 @@ def _onStartElement(self, name, attrs):
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)
Expand Down

0 comments on commit 14d5941

Please sign in to comment.