Mercurial > cpython-lru_cache-weakref
annotate Lib/dircache.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | 743296ce052b |
| children | 65cf478950ff |
| rev | line source |
|---|---|
|
11708
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
11691
diff
changeset
|
1 """Read and cache directory listings. |
|
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
11691
diff
changeset
|
2 |
|
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
11691
diff
changeset
|
3 The listdir() routine returns a sorted list of the files in a directory, |
|
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
11691
diff
changeset
|
4 using a cache to avoid reading the directory more often than necessary. |
|
4a23cc49e2b0
Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents:
11691
diff
changeset
|
5 The annotate() routine appends slashes to directories.""" |
| 5 | 6 |
| 771 | 7 import os |
| 5 | 8 |
|
16957
cd6e4bc99295
Patch by Itamar S.T. (SF#305470): add reset() method.
Guido van Rossum <guido@python.org>
parents:
16070
diff
changeset
|
9 __all__ = ["listdir", "opendir", "annotate", "reset"] |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
15543
diff
changeset
|
10 |
| 5 | 11 cache = {} |
| 12 | |
|
16957
cd6e4bc99295
Patch by Itamar S.T. (SF#305470): add reset() method.
Guido van Rossum <guido@python.org>
parents:
16070
diff
changeset
|
13 def reset(): |
|
17055
743296ce052b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
16957
diff
changeset
|
14 """Reset the cache completely.""" |
|
743296ce052b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
16957
diff
changeset
|
15 global cache |
|
743296ce052b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
16957
diff
changeset
|
16 cache = {} |
|
16957
cd6e4bc99295
Patch by Itamar S.T. (SF#305470): add reset() method.
Guido van Rossum <guido@python.org>
parents:
16070
diff
changeset
|
17 |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
771
diff
changeset
|
18 def listdir(path): |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
771
diff
changeset
|
19 """List directory contents, using cache.""" |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
771
diff
changeset
|
20 try: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
771
diff
changeset
|
21 cached_mtime, list = cache[path] |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
771
diff
changeset
|
22 del cache[path] |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
771
diff
changeset
|
23 except KeyError: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
771
diff
changeset
|
24 cached_mtime, list = -1, [] |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
771
diff
changeset
|
25 try: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
771
diff
changeset
|
26 mtime = os.stat(path)[8] |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
771
diff
changeset
|
27 except os.error: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
771
diff
changeset
|
28 return [] |
|
15543
785155e2cfca
Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents:
11708
diff
changeset
|
29 if mtime != cached_mtime: |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
771
diff
changeset
|
30 try: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
771
diff
changeset
|
31 list = os.listdir(path) |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
771
diff
changeset
|
32 except os.error: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
771
diff
changeset
|
33 return [] |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
771
diff
changeset
|
34 list.sort() |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
771
diff
changeset
|
35 cache[path] = mtime, list |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
771
diff
changeset
|
36 return list |
| 5 | 37 |
| 38 opendir = listdir # XXX backward compatibility | |
| 39 | |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
771
diff
changeset
|
40 def annotate(head, list): |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
771
diff
changeset
|
41 """Add '/' suffixes to directories.""" |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
771
diff
changeset
|
42 for i in range(len(list)): |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
771
diff
changeset
|
43 if os.path.isdir(os.path.join(head, list[i])): |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
771
diff
changeset
|
44 list[i] = list[i] + '/' |
