-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
RuntimeError when URLopener.ftpcache is full #65662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is probably a pretty rare corner case, but a coworker reported this to me while testing code that does open several ftp connections to different files. |
Thanks for the report and patch. Would you mind adding a unit test? (Note that the “crash” type is for segfaults, not Python exceptions.) |
Ah, didn't know that about "crash". I wanted to add a test but hesitated only because that code is not well tested to begin with (perhaps, hence, this going unnoticed for so long). But I guess it could be done by mocking the ftpwrapper class. |
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. |
I've made a test for this patch with a very minimal mock ftpwrapper. We can see it fails on dictionary size change without Erik's fix: ====================================================================== Traceback (most recent call last):
File "/home/skyler/cpython/Lib/test/test_urllib.py", line 336, in test_ftp_cache_pruning
urlopen('ftp://localhost')
File "/home/skyler/cpython/Lib/test/test_urllib.py", line 45, in urlopen
return opener.open(url)
File "/home/skyler/cpython/Lib/urllib/request.py", line 1631, in open
return getattr(self, name)(url)
File "/home/skyler/cpython/Lib/urllib/request.py", line 1914, in open_ftp
for k in self.ftpcache.keys():
RuntimeError: dictionary changed size during iteration |
(I left some comments on Rietveld.) |
Addressed review comments |
New changeset b8f9ae84d211 by Benjamin Peterson in branch '3.4': New changeset 6f70a18313e5 by Benjamin Peterson in branch 'default': |
Thanks Skyler for finishing the job. I got busy/distracted. |
You're welcome, happy to help :) On Mon, Jun 9, 2014 at 8:31 AM, Erik Bray <report@bugs.python.org> wrote:
|
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: