Skip to content

Commit 3632140

Browse files
committed
Call abs_paths() only if removeduppaths() changed sys.path
1 parent 1b3dfe6 commit 3632140

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Lib/site.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,22 @@ def makepath(*paths):
9696
return dir, os.path.normcase(dir)
9797

9898

99+
def abs_paths():
100+
"""Set all module __file__ and __cached__ attributes to an absolute path"""
101+
for m in set(sys.modules.values()):
102+
if (getattr(getattr(m, '__loader__', None), '__module__', None) not in
103+
('_frozen_importlib', '_frozen_importlib_external')):
104+
continue # don't mess with a PEP 302-supplied __file__
105+
try:
106+
m.__file__ = os.path.abspath(m.__file__)
107+
except (AttributeError, OSError):
108+
pass
109+
try:
110+
m.__cached__ = os.path.abspath(m.__cached__)
111+
except (AttributeError, OSError):
112+
pass
113+
114+
99115
def removeduppaths():
100116
""" Remove duplicate entries from sys.path along with making them
101117
absolute"""
@@ -506,7 +522,13 @@ def main():
506522
"""
507523
global ENABLE_USER_SITE
508524

525+
orig_path = sys.path[:]
509526
known_paths = removeduppaths()
527+
if orig_path != sys.path:
528+
# removeduppaths() might make sys.path absolute.
529+
# fix __file__ and __cached__ of already imported modules too.
530+
abs_paths()
531+
510532
known_paths = venv(known_paths)
511533
if ENABLE_USER_SITE is None:
512534
ENABLE_USER_SITE = check_enableusersite()

0 commit comments

Comments
 (0)