summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1997-06-06 21:11:11 (GMT)
committerGuido van Rossum <guido@python.org>1997-06-06 21:11:11 (GMT)
commitf668d17e010ea5a09846d2e6a994511ca7ad28eb (patch)
tree8881a73b609687cd860ed30c676c9316589e9cf7 /Lib
parent2966b322055158184eebc72bb892ef2ccc42f0d2 (diff)
downloadcpython-f668d17e010ea5a09846d2e6a994511ca7ad28eb.zip
cpython-f668d17e010ea5a09846d2e6a994511ca7ad28eb.tar.gz
cpython-f668d17e010ea5a09846d2e6a994511ca7ad28eb.tar.bz2
Clear the ftp cache when it contains more than 10 entries.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/urllib.py23
1 files changed, 21 insertions, 2 deletions
diff --git a/Lib/urllib.py b/Lib/urllib.py
index 173f83b..d9040cc 100644
--- a/Lib/urllib.py
+++ b/Lib/urllib.py
@@ -28,7 +28,9 @@ import os
import sys
-__version__ = '1.6'
+__version__ = '1.7'
+
+MAXFTPCACHE = 10 # Trim the ftp cache beyond this size
# Helper for non-unix systems
if os.name == 'mac':
@@ -317,6 +319,13 @@ class URLopener:
dirs, file = dirs[:-1], dirs[-1]
if dirs and not dirs[0]: dirs = dirs[1:]
key = (user, host, port, string.joinfields(dirs, '/'))
+ if len(self.ftpcache) > MAXFTPCACHE:
+ # Prune the cache, rather arbitrarily
+ for k in self.ftpcache.keys():
+ if k != key:
+ v = self.ftpcache[k]
+ del self.ftpcache[k]
+ v.close()
try:
if not self.ftpcache.has_key(key):
self.ftpcache[key] = \
@@ -506,7 +515,17 @@ class ftpwrapper:
if file: cmd = 'LIST ' + file
else: cmd = 'LIST'
conn = self.ftp.transfercmd(cmd)
- return addclosehook(conn.makefile('rb'), self.ftp.voidresp)
+ return addclosehook(conn.makefile('rb'), self.endtransfer)
+ def endtransfer(self):
+ try:
+ self.ftp.voidresp()
+ except ftperrors():
+ pass
+ def close(self):
+ try:
+ self.ftp.close()
+ except ftperrors():
+ pass
# Base class for addinfo and addclosehook
class addbase: