diff options
author | Fred Drake <fdrake@acm.org> | 2000-09-28 16:52:36 (GMT) |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 2000-09-28 16:52:36 (GMT) |
commit | 38cb9f1f174415d3b37fbaeb5d152d65525839d2 (patch) | |
tree | e4dab6e52b796e35df535cb1f99718e4a119d5b2 /Lib/site.py | |
parent | b4e460ac4b5a5b7e72b0eb6e8d21af179464c3d7 (diff) | |
download | cpython-38cb9f1f174415d3b37fbaeb5d152d65525839d2.zip cpython-38cb9f1f174415d3b37fbaeb5d152d65525839d2.tar.gz cpython-38cb9f1f174415d3b37fbaeb5d152d65525839d2.tar.bz2 |
Convert all entries on sys.path to absolute paths, and also update the
__file__ attributes of already-imported modules to be absolute. This helps
robustify the interpreter against os.chdir() calls from the application.
Only remove setdefaultencoding() from sys if it exists; if this module is
run as a script (since there is a _test() function that gets run), it broke
because the script attempts to remove it again after the import of site
has already done so. This allows the module to be run as a script again.
makepath(): New function, standardizes all pathname normalization in one
place.
Diffstat (limited to 'Lib/site.py')
-rw-r--r-- | Lib/site.py | 39 |
1 files changed, 31 insertions, 8 deletions
diff --git a/Lib/site.py b/Lib/site.py index a9f5480..c92e98d 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -59,7 +59,28 @@ ImportError exception, it is silently ignored. import sys, os +def makepath(*paths): + dir = os.path.join(*paths) + return os.path.normcase(os.path.abspath(dir)) + +L = sys.modules.values() +for m in L: + if hasattr(m, "__file__"): + m.__file__ = makepath(m.__file__) +del m, L + +# This ensures that the initial path provided by the interpreter contains +# only absolute pathnames, even if we're running from the build directory. +L = [] +for dir in sys.path: + dir = makepath(dir) + if dir not in L: + L.append(dir) +sys.path[:] = L +del dir, L + def addsitedir(sitedir): + sitedir = makepath(sitedir) if sitedir not in sys.path: sys.path.append(sitedir) # Add path component try: @@ -86,7 +107,7 @@ def addpackage(sitedir, name): continue if dir[-1] == '\n': dir = dir[:-1] - dir = os.path.join(sitedir, dir) + dir = makepath(sitedir, dir) if dir not in sys.path and os.path.exists(dir): sys.path.append(dir) @@ -96,11 +117,11 @@ if sys.exec_prefix != sys.prefix: for prefix in prefixes: if prefix: if os.sep == '/': - sitedirs = [os.path.join(prefix, - "lib", - "python" + sys.version[:3], - "site-packages"), - os.path.join(prefix, "lib", "site-python")] + sitedirs = [makepath(prefix, + "lib", + "python" + sys.version[:3], + "site-packages"), + makepath(prefix, "lib", "site-python")] else: sitedirs = [prefix] for sitedir in sitedirs: @@ -202,9 +223,11 @@ except ImportError: # # Remove sys.setdefaultencoding() so that users cannot change the -# encoding after initialization. +# encoding after initialization. The test for presence is needed when +# this module is run as a script, becuase this code is executed twice. # -del sys.setdefaultencoding +if hasattr(sys, "setdefaultencoding"): + del sys.setdefaultencoding def _test(): print "sys.path = [" |