Mercurial > cpython-lru_cache-weakref
annotate Lib/statcache.py @ 19283:b865cd5c1925 legacy-trunk
Added comments before recently added/assigned slots in the type object,
so the backward compatibility issues will be easier to understand. I only
added comments indicating additions and assignments back to Python 2.0.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Wed, 15 Aug 2001 18:32:33 +0000 |
| parents | f68dbffff927 |
| children | e2f2187c3d2c |
| rev | line source |
|---|---|
|
11708
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
11707
diff
changeset
|
1 """Maintain a cache of stat() information on files. |
|
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
11707
diff
changeset
|
2 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
3286
diff
changeset
|
3 There are functions to reset the cache or to selectively remove items. |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
3286
diff
changeset
|
4 """ |
| 5 | 5 |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
6 import os as _os |
|
21
961696ef3ab7
Use 'stat' module instead of hardcoding information from <sys/stat.h>.
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
7 from stat import * |
| 5 | 8 |
| 16613 | 9 __all__ = ["stat","reset","forget","forget_prefix","forget_dir", |
| 10 "forget_except_prefix","isdir"] | |
| 11 | |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
12 # The cache. Keys are pathnames, values are os.stat outcomes. |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
13 # Remember that multiple threads may be calling this! So, e.g., that |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
14 # cache.has_key(path) returns 1 doesn't mean the cache will still contain |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
15 # path on the next line. Code defensively. |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
16 |
| 5 | 17 cache = {} |
| 18 | |
| 19 def stat(path): | |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
20 """Stat a file, possibly out of the cache.""" |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
21 ret = cache.get(path, None) |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
22 if ret is None: |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
23 cache[path] = ret = _os.stat(path) |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
24 return ret |
| 5 | 25 |
| 26 def reset(): | |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
27 """Clear the cache.""" |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
28 cache.clear() |
| 5 | 29 |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
30 # For thread saftey, always use forget() internally too. |
| 5 | 31 def forget(path): |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
32 """Remove a given item from the cache, if it exists.""" |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
33 try: |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
34 del cache[path] |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
35 except KeyError: |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
36 pass |
| 5 | 37 |
| 38 def forget_prefix(prefix): | |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
39 """Remove all pathnames with a given prefix.""" |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
40 for path in cache.keys(): |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
41 if path.startswith(prefix): |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
42 forget(path) |
| 5 | 43 |
| 44 def forget_dir(prefix): | |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
45 """Forget a directory and all entries except for entries in subdirs.""" |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
46 |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
47 # Remove trailing separator, if any. This is tricky to do in a |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
48 # x-platform way. For example, Windows accepts both / and \ as |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
49 # separators, and if there's nothing *but* a separator we want to |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
50 # preserve that this is the root. Only os.path has the platform |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
51 # knowledge we need. |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
52 from os.path import split, join |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
53 prefix = split(join(prefix, "xxx"))[0] |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
54 forget(prefix) |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
55 for path in cache.keys(): |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
56 # First check that the path at least starts with the prefix, so |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
57 # that when it doesn't we can avoid paying for split(). |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
58 if path.startswith(prefix) and split(path)[0] == prefix: |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
59 forget(path) |
| 5 | 60 |
| 61 def forget_except_prefix(prefix): | |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
62 """Remove all pathnames except with a given prefix. |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
63 |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
64 Normally used with prefix = '/' after a chdir(). |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
65 """ |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
66 |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
67 for path in cache.keys(): |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
68 if not path.startswith(prefix): |
|
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
69 forget(path) |
| 5 | 70 |
| 71 def isdir(path): | |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
72 """Return 1 if directory, else 0.""" |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
73 try: |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
74 st = stat(path) |
|
16317
861bf8dcfb58
SF bug #130306: statcache.py full of thread problems.
Tim Peters <tim.peters@gmail.com>
parents:
15872
diff
changeset
|
75 except _os.error: |
|
15872
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
76 return 0 |
|
3685ef2a55a4
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
15543
diff
changeset
|
77 return S_ISDIR(st[ST_MODE]) |
