[issue21463] RuntimeError when URLopener.ftpcache is full

Jessica McKellar report at bugs.python.org
Sat Jun 7 06:25:00 CEST 2014


Jessica McKellar added the comment:

I want to state explicitly what the error is for some new contributors who might pick this up at a sprint this weekend:

The issue is that you can't change a dictionary while iterating over it:

>>> d = {"a": "b"}
>>> for elt in d.keys():
...     del d[elt]
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: dictionary changed size during iteration

In Python 2, d.keys() produced a copy of the list of keys, and we delete items from the dictionary while iterating over that copy, which is safe. In Python 3, d.keys() produces a view object* instead, and mutating the dictionary while iterating over it is unsafe and raises an error.

The patch makes a copy of the keys before iterating over it so that it is safe in Python 3.

* https://docs.python.org/3/library/stdtypes.html#dict-views

----------
nosy: +jesstess

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


More information about the Python-bugs-list mailing list