From 1b3dfe641f9b2b1c10c8eb82e65c103b6e8c0550 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Sun, 19 Feb 2017 15:26:37 +0900 Subject: [PATCH 1/2] bpo-29592: remove abs_paths() from site.py It seems redundant for now. --- Lib/site.py | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/Lib/site.py b/Lib/site.py index 0fc92009e19af3..a4c11ed54926fe 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -96,22 +96,6 @@ def makepath(*paths): return dir, os.path.normcase(dir) -def abs_paths(): - """Set all module __file__ and __cached__ attributes to an absolute path""" - for m in set(sys.modules.values()): - if (getattr(getattr(m, '__loader__', None), '__module__', None) not in - ('_frozen_importlib', '_frozen_importlib_external')): - continue # don't mess with a PEP 302-supplied __file__ - try: - m.__file__ = os.path.abspath(m.__file__) - except (AttributeError, OSError): - pass - try: - m.__cached__ = os.path.abspath(m.__cached__) - except (AttributeError, OSError): - pass - - def removeduppaths(): """ Remove duplicate entries from sys.path along with making them absolute""" @@ -522,7 +506,6 @@ def main(): """ global ENABLE_USER_SITE - abs_paths() known_paths = removeduppaths() known_paths = venv(known_paths) if ENABLE_USER_SITE is None: From 3632140fa2d2c8dc89d3cc7cea2b3a75212f83fb Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Sun, 19 Feb 2017 23:15:18 +0900 Subject: [PATCH 2/2] Call abs_paths() only if removeduppaths() changed sys.path --- Lib/site.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Lib/site.py b/Lib/site.py index a4c11ed54926fe..4f96ca91170c54 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -96,6 +96,22 @@ def makepath(*paths): return dir, os.path.normcase(dir) +def abs_paths(): + """Set all module __file__ and __cached__ attributes to an absolute path""" + for m in set(sys.modules.values()): + if (getattr(getattr(m, '__loader__', None), '__module__', None) not in + ('_frozen_importlib', '_frozen_importlib_external')): + continue # don't mess with a PEP 302-supplied __file__ + try: + m.__file__ = os.path.abspath(m.__file__) + except (AttributeError, OSError): + pass + try: + m.__cached__ = os.path.abspath(m.__cached__) + except (AttributeError, OSError): + pass + + def removeduppaths(): """ Remove duplicate entries from sys.path along with making them absolute""" @@ -506,7 +522,13 @@ def main(): """ global ENABLE_USER_SITE + orig_path = sys.path[:] known_paths = removeduppaths() + if orig_path != sys.path: + # removeduppaths() might make sys.path absolute. + # fix __file__ and __cached__ of already imported modules too. + abs_paths() + known_paths = venv(known_paths) if ENABLE_USER_SITE is None: ENABLE_USER_SITE = check_enableusersite()