close the file even if an exception occurs #5536
This commit is contained in:
parent
8ed2520536
commit
b364bfe2f4
|
@ -233,6 +233,7 @@ class URLopener:
|
|||
except IOError, msg:
|
||||
pass
|
||||
fp = self.open(url, data)
|
||||
try:
|
||||
headers = fp.info()
|
||||
if filename:
|
||||
tfp = open(filename, 'wb')
|
||||
|
@ -246,6 +247,7 @@ class URLopener:
|
|||
(fd, filename) = tempfile.mkstemp(suffix)
|
||||
self.__tempfiles.append(filename)
|
||||
tfp = os.fdopen(fd, 'wb')
|
||||
try:
|
||||
result = filename, headers
|
||||
if self.tempcache is not None:
|
||||
self.tempcache[url] = result
|
||||
|
@ -266,8 +268,10 @@ class URLopener:
|
|||
blocknum += 1
|
||||
if reporthook:
|
||||
reporthook(blocknum, bs, size)
|
||||
fp.close()
|
||||
finally:
|
||||
tfp.close()
|
||||
finally:
|
||||
fp.close()
|
||||
del fp
|
||||
del tfp
|
||||
|
||||
|
|
Loading…
Reference in New Issue