[New-bugs-announce] [issue10777] xml.etree.register_namespace dictionary changed size during iteration

Peter report at bugs.python.org
Mon Dec 27 01:12:09 CET 2010


New submission from Peter <p.j.a.cock at googlemail.com>:

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'

----------
components: XML
messages: 124688
nosy: maubp
priority: normal
severity: normal
status: open
title: xml.etree.register_namespace dictionary changed size during iteration
type: crash
versions: Python 3.2

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10777>
_______________________________________


More information about the New-bugs-announce mailing list