summaryrefslogtreecommitdiffstats
path: root/Lib/site.py
diff options
context:
space:
mode:
authorFred Drake <fdrake@acm.org>2001-07-20 20:06:17 (GMT)
committerFred Drake <fdrake@acm.org>2001-07-20 20:06:17 (GMT)
commit7f5296e7c06f5a6010960f34ae7db89d8902cddb (patch)
treea7f28c35494f1daf58d35a85eb6faef632b322a7 /Lib/site.py
parent5a3e4cb0a2b922bbbcaf3e3afa9693b57e189ea4 (diff)
downloadcpython-7f5296e7c06f5a6010960f34ae7db89d8902cddb.zip
cpython-7f5296e7c06f5a6010960f34ae7db89d8902cddb.tar.gz
cpython-7f5296e7c06f5a6010960f34ae7db89d8902cddb.tar.bz2
Make the add*() helper functions more robust for use after intialization
is complete: recompute _dirs_in_sys_path each time these functions are entered after module initialization is complete, and reset before returning to user code. This closes SF patch #442983.
Diffstat (limited to 'Lib/site.py')
-rw-r--r--Lib/site.py39
1 files changed, 33 insertions, 6 deletions
diff --git a/Lib/site.py b/Lib/site.py
index 9dfe6ce..be53065 100644
--- a/Lib/site.py
+++ b/Lib/site.py
@@ -77,16 +77,16 @@ del m
# This ensures that the initial path provided by the interpreter contains
# only absolute pathnames, even if we're running from the build directory.
L = []
-dirs_in_sys_path = {}
+_dirs_in_sys_path = {}
for dir in sys.path:
# Filter out paths that don't exist, but leave in the empty string
# since it's a special case.
if dir and not os.path.isdir(dir):
continue
dir, dircase = makepath(dir)
- if not dirs_in_sys_path.has_key(dircase):
+ if not _dirs_in_sys_path.has_key(dircase):
L.append(dir)
- dirs_in_sys_path[dircase] = 1
+ _dirs_in_sys_path[dircase] = 1
sys.path[:] = L
del dir, L
@@ -99,9 +99,24 @@ if os.name == "posix" and os.path.basename(sys.path[-1]) == "Modules":
sys.path.append(s)
del get_platform, s
+def _init_pathinfo():
+ global _dirs_in_sys_path
+ _dirs_in_sys_path = d = {}
+ for dir in sys.path:
+ if dir and not os.path.isdir(dir):
+ continue
+ dir, dircase = makepath(dir)
+ d[dircase] = 1
+
def addsitedir(sitedir):
+ global _dirs_in_sys_path
+ if _dirs_in_sys_path is None:
+ _init_pathinfo()
+ reset = 1
+ else:
+ reset = 0
sitedir, sitedircase = makepath(sitedir)
- if not dirs_in_sys_path.has_key(sitedircase):
+ if not _dirs_in_sys_path.has_key(sitedircase):
sys.path.append(sitedir) # Add path component
try:
names = os.listdir(sitedir)
@@ -111,8 +126,16 @@ def addsitedir(sitedir):
for name in names:
if name[-4:] == endsep + "pth":
addpackage(sitedir, name)
+ if reset:
+ _dirs_in_sys_path = None
def addpackage(sitedir, name):
+ global _dirs_in_sys_path
+ if _dirs_in_sys_path is None:
+ _init_pathinfo()
+ reset = 1
+ else:
+ reset = 0
fullname = os.path.join(sitedir, name)
try:
f = open(fullname)
@@ -130,9 +153,11 @@ def addpackage(sitedir, name):
if dir[-1] == '\n':
dir = dir[:-1]
dir, dircase = makepath(sitedir, dir)
- if not dirs_in_sys_path.has_key(dircase) and os.path.exists(dir):
+ if not _dirs_in_sys_path.has_key(dircase) and os.path.exists(dir):
sys.path.append(dir)
- dirs_in_sys_path[dircase] = 1
+ _dirs_in_sys_path[dircase] = 1
+ if reset:
+ _dirs_in_sys_path = None
prefixes = [sys.prefix]
if sys.exec_prefix != sys.prefix:
@@ -151,6 +176,8 @@ for prefix in prefixes:
if os.path.isdir(sitedir):
addsitedir(sitedir)
+_dirs_in_sys_path = None
+
# Define new built-ins 'quit' and 'exit'.
# These are simply strings that display a hint on how to exit.