summaryrefslogtreecommitdiffstats
path: root/Lib/urllib/request.py
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2009-03-26 21:49:58 (GMT)
committerBenjamin Peterson <benjamin@python.org>2009-03-26 21:49:58 (GMT)
commit5f28b7b7978ad96fef83e2b41017bdd7ab8a082f (patch)
tree511fa303a1132e525b05b2288a009781d103dc67 /Lib/urllib/request.py
parentb476d597921ab0ef52de1f56a5efa71c65727114 (diff)
downloadcpython-5f28b7b7978ad96fef83e2b41017bdd7ab8a082f.zip
cpython-5f28b7b7978ad96fef83e2b41017bdd7ab8a082f.tar.gz
cpython-5f28b7b7978ad96fef83e2b41017bdd7ab8a082f.tar.bz2
Merged revisions 70518,70521,70590,70594-70595 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r70518 | matthias.klose | 2009-03-22 08:08:22 -0500 (Sun, 22 Mar 2009) | 2 lines - Fix comment macro in python.man ........ 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 ........ r70590 | skip.montanaro | 2009-03-24 19:52:11 -0500 (Tue, 24 Mar 2009) | 1 line clarify the type of data returned ........ r70594 | marc-andre.lemburg | 2009-03-25 14:44:58 -0500 (Wed, 25 Mar 2009) | 9 lines Remove the sys.version_info shortcut, since they cause the APIs to return different information than the _sys_version() output used in previous Python versions. This also fixes issue5561: platform.python_version_tuple returns tuple of ints, should be strings Added more tests for the various platform functions. ........ r70595 | marc-andre.lemburg | 2009-03-25 14:45:33 -0500 (Wed, 25 Mar 2009) | 3 lines News item for the platform.py fix (r70594). ........
Diffstat (limited to 'Lib/urllib/request.py')
-rw-r--r--Lib/urllib/request.py74
1 files changed, 39 insertions, 35 deletions
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
index b86d8f2..c789ffc 100644
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -1474,41 +1474,45 @@ class URLopener:
except IOError as msg:
pass
fp = self.open(url, data)
- headers = fp.info()
- if filename:
- tfp = open(filename, 'wb')
- else:
- import tempfile
- garbage, path = splittype(url)
- garbage, path = splithost(path or "")
- path, garbage = splitquery(path or "")
- path, garbage = splitattr(path or "")
- suffix = os.path.splitext(path)[1]
- (fd, filename) = tempfile.mkstemp(suffix)
- self.__tempfiles.append(filename)
- tfp = os.fdopen(fd, 'wb')
- result = filename, headers
- if self.tempcache is not None:
- self.tempcache[url] = result
- bs = 1024*8
- size = -1
- read = 0
- blocknum = 0
- if reporthook:
- if "content-length" in headers:
- size = int(headers["Content-Length"])
- reporthook(blocknum, bs, size)
- while 1:
- block = fp.read(bs)
- if not block:
- break
- read += len(block)
- tfp.write(block)
- blocknum += 1
- if reporthook:
- reporthook(blocknum, bs, size)
- fp.close()
- tfp.close()
+ try:
+ headers = fp.info()
+ if filename:
+ tfp = open(filename, 'wb')
+ else:
+ import tempfile
+ garbage, path = splittype(url)
+ garbage, path = splithost(path or "")
+ path, garbage = splitquery(path or "")
+ path, garbage = splitattr(path or "")
+ suffix = os.path.splitext(path)[1]
+ (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
+ bs = 1024*8
+ size = -1
+ read = 0
+ blocknum = 0
+ if reporthook:
+ if "content-length" in headers:
+ size = int(headers["Content-Length"])
+ reporthook(blocknum, bs, size)
+ while 1:
+ block = fp.read(bs)
+ if not block:
+ break
+ read += len(block)
+ tfp.write(block)
+ blocknum += 1
+ if reporthook:
+ reporthook(blocknum, bs, size)
+ finally:
+ tfp.close()
+ finally:
+ fp.close()
del fp
del tfp