annotate Lib/dircache.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 33d4e03b23ba
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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."""
46838
33d4e03b23ba Deprecate the dircache module for 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 30470
diff changeset
6 from warnings import warnpy3k
33d4e03b23ba Deprecate the dircache module for 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 30470
diff changeset
7 warnpy3k("the dircache module has been removed in Python 3.0", stacklevel=2)
33d4e03b23ba Deprecate the dircache module for 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 30470
diff changeset
8 del warnpy3k
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
771
2e82dd0f8a9e posix -> os
Guido van Rossum <guido@python.org>
parents: 521
diff changeset
10 import os
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
16957
cd6e4bc99295 Patch by Itamar S.T. (SF#305470): add reset() method.
Guido van Rossum <guido@python.org>
parents: 16070
diff changeset
12 __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
13
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 cache = {}
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
16957
cd6e4bc99295 Patch by Itamar S.T. (SF#305470): add reset() method.
Guido van Rossum <guido@python.org>
parents: 16070
diff changeset
16 def reset():
17055
743296ce052b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 16957
diff changeset
17 """Reset the cache completely."""
743296ce052b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 16957
diff changeset
18 global cache
743296ce052b Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 16957
diff changeset
19 cache = {}
16957
cd6e4bc99295 Patch by Itamar S.T. (SF#305470): add reset() method.
Guido van Rossum <guido@python.org>
parents: 16070
diff changeset
20
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 771
diff changeset
21 def listdir(path):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 771
diff changeset
22 """List directory contents, using cache."""
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 771
diff changeset
23 try:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 771
diff changeset
24 cached_mtime, list = cache[path]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 771
diff changeset
25 del cache[path]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 771
diff changeset
26 except KeyError:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 771
diff changeset
27 cached_mtime, list = -1, []
30470
1e1e685d69ef Patch #707167: Pass dircache exceptions to the caller. Fixes #682813.
Martin v. Löwis <martin@v.loewis.de>
parents: 23625
diff changeset
28 mtime = os.stat(path).st_mtime
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 11708
diff changeset
29 if mtime != cached_mtime:
30470
1e1e685d69ef Patch #707167: Pass dircache exceptions to the caller. Fixes #682813.
Martin v. Löwis <martin@v.loewis.de>
parents: 23625
diff changeset
30 list = os.listdir(path)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 771
diff changeset
31 list.sort()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 771
diff changeset
32 cache[path] = mtime, list
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 771
diff changeset
33 return list
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 opendir = listdir # XXX backward compatibility
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 771
diff changeset
37 def annotate(head, list):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 771
diff changeset
38 """Add '/' suffixes to directories."""
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 771
diff changeset
39 for i in range(len(list)):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 771
diff changeset
40 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
41 list[i] = list[i] + '/'