mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Merged revisions 70521 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r70521 | benjamin.peterson | 2009-03-22 12:45:11 -0500 (Sun, 22 Mar 2009) | 1 line close the file even if an exception occurs #5536 ........
This commit is contained in:
parent
218f462a0f
commit
373498fc67
2 changed files with 42 additions and 35 deletions
|
@ -233,41 +233,45 @@ class URLopener:
|
||||||
except IOError, msg:
|
except IOError, msg:
|
||||||
pass
|
pass
|
||||||
fp = self.open(url, data)
|
fp = self.open(url, data)
|
||||||
headers = fp.info()
|
try:
|
||||||
if filename:
|
headers = fp.info()
|
||||||
tfp = open(filename, 'wb')
|
if filename:
|
||||||
else:
|
tfp = open(filename, 'wb')
|
||||||
import tempfile
|
else:
|
||||||
garbage, path = splittype(url)
|
import tempfile
|
||||||
garbage, path = splithost(path or "")
|
garbage, path = splittype(url)
|
||||||
path, garbage = splitquery(path or "")
|
garbage, path = splithost(path or "")
|
||||||
path, garbage = splitattr(path or "")
|
path, garbage = splitquery(path or "")
|
||||||
suffix = os.path.splitext(path)[1]
|
path, garbage = splitattr(path or "")
|
||||||
(fd, filename) = tempfile.mkstemp(suffix)
|
suffix = os.path.splitext(path)[1]
|
||||||
self.__tempfiles.append(filename)
|
(fd, filename) = tempfile.mkstemp(suffix)
|
||||||
tfp = os.fdopen(fd, 'wb')
|
self.__tempfiles.append(filename)
|
||||||
result = filename, headers
|
tfp = os.fdopen(fd, 'wb')
|
||||||
if self.tempcache is not None:
|
try:
|
||||||
self.tempcache[url] = result
|
result = filename, headers
|
||||||
bs = 1024*8
|
if self.tempcache is not None:
|
||||||
size = -1
|
self.tempcache[url] = result
|
||||||
read = 0
|
bs = 1024*8
|
||||||
blocknum = 0
|
size = -1
|
||||||
if reporthook:
|
read = 0
|
||||||
if "content-length" in headers:
|
blocknum = 0
|
||||||
size = int(headers["Content-Length"])
|
if reporthook:
|
||||||
reporthook(blocknum, bs, size)
|
if "content-length" in headers:
|
||||||
while 1:
|
size = int(headers["Content-Length"])
|
||||||
block = fp.read(bs)
|
reporthook(blocknum, bs, size)
|
||||||
if block == "":
|
while 1:
|
||||||
break
|
block = fp.read(bs)
|
||||||
read += len(block)
|
if block == "":
|
||||||
tfp.write(block)
|
break
|
||||||
blocknum += 1
|
read += len(block)
|
||||||
if reporthook:
|
tfp.write(block)
|
||||||
reporthook(blocknum, bs, size)
|
blocknum += 1
|
||||||
fp.close()
|
if reporthook:
|
||||||
tfp.close()
|
reporthook(blocknum, bs, size)
|
||||||
|
finally:
|
||||||
|
tfp.close()
|
||||||
|
finally:
|
||||||
|
fp.close()
|
||||||
del fp
|
del fp
|
||||||
del tfp
|
del tfp
|
||||||
|
|
||||||
|
|
|
@ -92,6 +92,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.
|
||||||
|
|
||||||
- Fix Decimal.__format__ bug that swapped the meanings of the '<' and
|
- Fix Decimal.__format__ bug that swapped the meanings of the '<' and
|
||||||
'>' alignment characters.
|
'>' alignment characters.
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue