]> granicus.if.org Git - python/commitdiff
Clear the ftp cache when it contains more than 10 entries.
authorGuido van Rossum <guido@python.org>
Fri, 6 Jun 1997 21:11:11 +0000 (21:11 +0000)
committerGuido van Rossum <guido@python.org>
Fri, 6 Jun 1997 21:11:11 +0000 (21:11 +0000)
Lib/urllib.py

index 173f83b29aaea139e64002b21e0e7693b71cf4fa..d9040cc99e82908a66831074724648d9853f00e0 100644 (file)
@@ -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: