classification
Title: xml.etree.register_namespace dictionary changed size during iteration
Type: crash Stage:
Components: XML Versions: Python 3.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, maubp
Priority: normal Keywords:

Created on 2010-12-27 00:11 by maubp, last changed 2010-12-28 10:38 by georg.brandl. This issue is now closed.

Messages (2)
msg124688 - (view) Author: Peter (maubp) Date: 2010-12-27 00:11
The following was found testing the Biopython unit tests (latest code from git) against Python 3.2 beta 2 (compiled from source on 64 bit Linux Ubuntu). Reduced test case:

$ python3.2
Python 3.2b2 (r32b2:87398, Dec 26 2010, 19:01:30) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from xml.etree import ElementTree
>>> ElementTree.register_namespace("xs", "http://www.w3.org/2001/XMLSchema")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/peterjc/lib/python3.2/xml/etree/ElementTree.py", line 1071, in register_namespace
    for k, v in _namespace_map.items():
RuntimeError: dictionary changed size during iteration


Suggested fix, replace this:

def register_namespace(prefix, uri):
    if re.match("ns\d+$", prefix):
        raise ValueError("Prefix format reserved for internal use")
    for k, v in _namespace_map.items():
        if k == uri or v == prefix:
            del _namespace_map[k]
    _namespace_map[uri] = prefix


with something like this:

def register_namespace(prefix, uri):
    if re.match("ns\d+$", prefix):
        raise ValueError("Prefix format reserved for internal use")
    for k, v in list(_namespace_map.items()):
        if k == uri or v == prefix:
            del _namespace_map[k]
    _namespace_map[uri] = prefix


Note that cElementTree seems to be OK.

Note that Python 3.1 was not affected as it didn't even have register_namespace,

$ python3
Python 3.1.2 (r312:79147, Sep 27 2010, 09:57:50) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from xml.etree import ElementTree
>>> ElementTree.register_namespace("xs", "http://www.w3.org/2001/XMLSchema")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'register_namespace'
msg124781 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-12-28 10:38
Thanks, this should be fixed in r87526.
History
Date User Action Args
2010-12-28 10:38:45georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg124781

resolution: fixed
2010-12-27 00:11:54maubpcreate