From f668d17e010ea5a09846d2e6a994511ca7ad28eb Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Fri, 6 Jun 1997 21:11:11 +0000 Subject: Clear the ftp cache when it contains more than 10 entries. --- Lib/urllib.py | 23 +++++++++++++++++++++-- 1 file 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: -- cgit v0.12