diff --git a/src/twisted/words/newsfragments/9730.bugfix b/src/twisted/words/newsfragments/9730.bugfix
new file mode 100644
index 000000000..5c91305c8
--- /dev/null
+++ b/src/twisted/words/newsfragments/9730.bugfix
@@ -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
diff --git a/src/twisted/words/test/test_domish.py b/src/twisted/words/test/test_domish.py
index a8f8fa76b..cd16e3a4d 100644
--- a/src/twisted/words/test/test_domish.py
+++ b/src/twisted/words/test/test_domish.py
@@ -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"""
+ test
+ """
+
+ # 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""
diff --git a/src/twisted/words/xish/domish.py b/src/twisted/words/xish/domish.py
index 2063c410a..fc49285f5 100644
--- a/src/twisted/words/xish/domish.py
+++ b/src/twisted/words/xish/domish.py
@@ -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)