~sschwarzer/ftputil#38: 
If cache size is zero, FTPHost.listdir causes an IndexError

If the cache size for an FTPHost instance is explicitly set to zero, a following listdir call raises an IndexError:

>>> import ftputil
>>> host = ftputil.FTPHost("ftp.debian.org", "anonymous", "foo@example.com")
>>> host.stat_cache.resize(0)
>>> host.listdir(host.curdir)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "ftputil.py", line 795, in listdir
    return self._stat.listdir(path)
  File "ftp_stat.py", line 565, in listdir
    return self.__call_with_parser_retry(self._real_listdir, path)
  File "ftp_stat.py", line 543, in __call_with_parser_retry
    result = method(*args, **kwargs)
  File "ftp_stat.py", line 422, in _real_listdir
    self._lstat_cache[loop_path] = stat_result
  File "ftp_stat_cache.py", line 168, in __setitem__
    self._cache[path] = stat_result
  File "lrucache.py", line 164, in __setitem__
    lru = heappop(self.__heap)
IndexError: index out of range

On the other hand, disabling the cache explicitly doesn't raise an exception:

>>> host.stat_cache.disable()
>>> host.listdir(host.curdir)
['debian']
Status
RESOLVED FIXED
Submitter
schwa (unverified)
Assigned to
No-one
Submitted
14 years ago
Updated
14 years ago
Labels
bug library

schwa (unverified) 14 years ago · edit

One can notice a corresponding behavior in the lrucache module itself:

>>> import lrucache
>>> c = lrucache.LRUCache()
>>> c.size = 0
>>> c[1] = 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "lrucache.py", line 164, in __setitem__
    lru = heappop(self.__heap)
IndexError: index out of range

I guess, it's a question of philosophy if trying to store an item in a zero-sized cache should be considered a bug. In my opinion, this should be handled transparently, as if the cache was full. It shouldn't raise an exception, but perhaps effectively discard the to-be cache entry (in the same way older entries are discarded from the cache when it's full and new entries arrive).

schwa (unverified) 14 years ago · edit

Fixed in changeset 882.

schwa (unverified) 13 years ago · edit

I changed the policy for zero size caches in d0d66598c347f51deeb7010cd7ba849171d10690. They're no longer allowed.

Using a cache size of zero never was intuitive. Actually, it was only used to implicitly clear the cache. This now can be done with a new clear method.

Register here or Log in to comment, or comment via email.