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
urllib: urlretrieve() does not close file objects on failure #49786
Comments
urllib.request.urlretrieve() does not close the file object created for This behaviour may result in orphaned temporary/incomplete files. It is Consider this code snippet demonstrating the described behaviour: On Windows, following output appears:
File exists! Attempting to delete.
Traceback (most recent call last):
File "test.py", line 6, in <module>
urllib.request.urlretrieve(NON_EXISTENT_URL, FILENAME)
File "C:\Program Files\Python\lib\urllib\request.py", line 134, in
urlretrieve
return _urlopener.retrieve(url, filename, reporthook, data)
File "C:\Program Files\Python\lib\urllib\request.py", line 1502, in
retrieve
block = fp.read(bs)
File "C:\Program Files\Python\lib\io.py", line 572, in read
self._checkClosed()
File "C:\Program Files\Python\lib\io.py", line 450, in _checkClosed
if msg is None else msg)
ValueError: I/O operation on closed file.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 10, in <module>
os.unlink(FILENAME)
WindowsError: [Error 32] The process cannot access the file because it
is being used by another process: 'nonexistent.html' As a quick fix it is possible to ensure closing both source and target Anyway, the proposed solution is still not the optimal one: ValueError Note: |
Fixed in r70521. |
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: