Commit b364bfe2 authored by Benjamin Peterson's avatar Benjamin Peterson

close the file even if an exception occurs #5536

parent 8ed25205
...@@ -233,6 +233,7 @@ class URLopener: ...@@ -233,6 +233,7 @@ class URLopener:
except IOError, msg: except IOError, msg:
pass pass
fp = self.open(url, data) fp = self.open(url, data)
try:
headers = fp.info() headers = fp.info()
if filename: if filename:
tfp = open(filename, 'wb') tfp = open(filename, 'wb')
...@@ -246,6 +247,7 @@ class URLopener: ...@@ -246,6 +247,7 @@ class URLopener:
(fd, filename) = tempfile.mkstemp(suffix) (fd, filename) = tempfile.mkstemp(suffix)
self.__tempfiles.append(filename) self.__tempfiles.append(filename)
tfp = os.fdopen(fd, 'wb') tfp = os.fdopen(fd, 'wb')
try:
result = filename, headers result = filename, headers
if self.tempcache is not None: if self.tempcache is not None:
self.tempcache[url] = result self.tempcache[url] = result
...@@ -266,8 +268,10 @@ class URLopener: ...@@ -266,8 +268,10 @@ class URLopener:
blocknum += 1 blocknum += 1
if reporthook: if reporthook:
reporthook(blocknum, bs, size) reporthook(blocknum, bs, size)
fp.close() finally:
tfp.close() tfp.close()
finally:
fp.close()
del fp del fp
del tfp del tfp
......
...@@ -188,6 +188,9 @@ Core and Builtins ...@@ -188,6 +188,9 @@ Core and Builtins
Library Library
------- -------
- Issue #5536: urllib.urlretrieve makes sure to close the file it's writing to
even if an exception occurs.
- Issue #5381: Added object_pairs_hook to the json module. This allows - Issue #5381: Added object_pairs_hook to the json module. This allows
OrderedDicts to be built by the decoder. OrderedDicts to be built by the decoder.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment