(Hopefully) suppress transient refleaks in test_httpservers.
[python.git] / Lib / dircache.py
blob7e4f0b508ac74e47495622472b2382370fa3d9d9
1 """Read and cache directory listings.
3 The listdir() routine returns a sorted list of the files in a directory,
4 using a cache to avoid reading the directory more often than necessary.
5 The annotate() routine appends slashes to directories."""
6 from warnings import warnpy3k
7 warnpy3k("the dircache module has been removed in Python 3.0", stacklevel=2)
8 del warnpy3k
10 import os
12 __all__ = ["listdir", "opendir", "annotate", "reset"]
14 cache = {}
16 def reset():
17 """Reset the cache completely."""
18 global cache
19 cache = {}
21 def listdir(path):
22 """List directory contents, using cache."""
23 try:
24 cached_mtime, list = cache[path]
25 del cache[path]
26 except KeyError:
27 cached_mtime, list = -1, []
28 mtime = os.stat(path).st_mtime
29 if mtime != cached_mtime:
30 list = os.listdir(path)
31 list.sort()
32 cache[path] = mtime, list
33 return list
35 opendir = listdir # XXX backward compatibility
37 def annotate(head, list):
38 """Add '/' suffixes to directories."""
39 for i in range(len(list)):
40 if os.path.isdir(os.path.join(head, list[i])):
41 list[i] = list[i] + '/'