[Python-checkins] python/dist/src/Misc NEWS,1.606,1.607
Thomas Heller
theller@python.net
08 Jan 2003 17:45:51 +0100
Skip Montanaro <skip@pobox.com> writes:
>
> theller> + - sys.path[0], if it contains a directory name, is now always an
> theller> + absolute pathname.
>
> I guess my next question is, why not make all paths absolute? (IOW, why is
> sys.path[0] special?)
As Guido has explained, it is special because it is created *after*
site.py has run.
> Perhaps site.py should contain something like
>
> # guard against current directory changes outside of our control, as
> # when an application changes directory when Python is embedded.
> import sys, os
> sys.path = [os.path.abspath(p) for p in sys.path]
> del sys, os
It already does, and additional code makes sure that the already
imported modules __file__ attributes are absolute paths:
<snip>
for m in sys.modules.values():
if hasattr(m, "__file__") and m.__file__:
m.__file__ = os.path.abspath(m.__file__)
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 = {}
dir = dircase = None # sys.path may be empty at this point
for dir in sys.path:
# Filter out duplicate paths (on case-insensitive file systems also
# if they only differ in case); turn relative paths into absolute
# paths.
dir, dircase = makepath(dir)
if not dircase in _dirs_in_sys_path:
L.append(dir)
_dirs_in_sys_path[dircase] = 1
sys.path[:] = L
del dir, dircase, L
</snip>