mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
One patch from Sjoerd and one from Jack.
Sjoerd: add separate administration of temporary files created y URLopener.retrieve() so cleanup can properly remove them. The old code removed everything in tempcache which was a bad idea if the user had passed a non-temp file into it. (I added a line to delete the tempcache in cleanup() -- it still seems to make sense.) Jack: in basejoin(), interpret relative paths starting in "../". This is necessary if the server uses symbolic links.
This commit is contained in:
parent
f01dff7e93
commit
2b3fd76cc7
1 changed files with 14 additions and 7 deletions
|
@ -82,7 +82,7 @@ def urlcleanup():
|
||||||
ftpcache = {}
|
ftpcache = {}
|
||||||
class URLopener:
|
class URLopener:
|
||||||
|
|
||||||
tempcache = None # So close() in __del__() won't fail
|
__tempfiles = []
|
||||||
|
|
||||||
# Constructor
|
# Constructor
|
||||||
def __init__(self, proxies=None):
|
def __init__(self, proxies=None):
|
||||||
|
@ -110,14 +110,15 @@ class URLopener:
|
||||||
self.cleanup()
|
self.cleanup()
|
||||||
|
|
||||||
def cleanup(self):
|
def cleanup(self):
|
||||||
if self.tempcache:
|
if self.__tempfiles:
|
||||||
import os
|
import os
|
||||||
for url in self.tempcache.keys():
|
for file in self.__tempfiles:
|
||||||
try:
|
try:
|
||||||
os.unlink(self.tempcache[url][0])
|
os.unlink(file)
|
||||||
except os.error:
|
except os.error:
|
||||||
pass
|
pass
|
||||||
del self.tempcache[url]
|
URLopener.__tempfiles = []
|
||||||
|
self.tempcache = None
|
||||||
|
|
||||||
# Add a header to be used by the HTTP interface only
|
# Add a header to be used by the HTTP interface only
|
||||||
# e.g. u.addheader('Accept', 'sound/basic')
|
# e.g. u.addheader('Accept', 'sound/basic')
|
||||||
|
@ -182,6 +183,7 @@ class URLopener:
|
||||||
if not filename:
|
if not filename:
|
||||||
import tempfile
|
import tempfile
|
||||||
filename = tempfile.mktemp()
|
filename = tempfile.mktemp()
|
||||||
|
self.__tempfiles.append(filename)
|
||||||
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
|
||||||
|
@ -622,9 +624,14 @@ def basejoin(base, url):
|
||||||
# Interpret ../ (important because of symlinks)
|
# Interpret ../ (important because of symlinks)
|
||||||
while basepath and path[:3] == '../':
|
while basepath and path[:3] == '../':
|
||||||
path = path[3:]
|
path = path[3:]
|
||||||
i = string.rfind(basepath, '/')
|
i = string.rfind(basepath[:-1], '/')
|
||||||
if i > 0:
|
if i > 0:
|
||||||
basepath = basepath[:i-1]
|
basepath = basepath[:i+1]
|
||||||
|
elif i == 0:
|
||||||
|
basepath = '/'
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
basepath = ''
|
||||||
|
|
||||||
path = basepath + path
|
path = basepath + path
|
||||||
if type and host: return type + '://' + host + path
|
if type and host: return type + '://' + host + path
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue