bpo-43105: Importlib now resolves relative paths when creating module spec objects from file locations (GH-25121)

This commit is contained in:
Steve Dower 2021-04-07 01:02:07 +01:00 committed by GitHub
parent b57e045320
commit 04732ca993
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 2732 additions and 2527 deletions

View file

@ -173,6 +173,7 @@ class HelperFunctionsTests(unittest.TestCase):
pth_dir, pth_fn = self.make_pth("abc\x00def\n")
with captured_stderr() as err_out:
self.assertFalse(site.addpackage(pth_dir, pth_fn, set()))
self.maxDiff = None
self.assertEqual(err_out.getvalue(), "")
for path in sys.path:
if isinstance(path, str):
@ -408,55 +409,6 @@ class ImportSideEffectTests(unittest.TestCase):
"""Restore sys.path"""
sys.path[:] = self.sys_path
def test_abs_paths(self):
# Make sure all imported modules have their __file__ and __cached__
# attributes as absolute paths. Arranging to put the Lib directory on
# PYTHONPATH would cause the os module to have a relative path for
# __file__ if abs_paths() does not get run. sys and builtins (the
# only other modules imported before site.py runs) do not have
# __file__ or __cached__ because they are built-in.
try:
parent = os.path.relpath(os.path.dirname(os.__file__))
cwd = os.getcwd()
except ValueError:
# Failure to get relpath probably means we need to chdir
# to the same drive.
cwd, parent = os.path.split(os.path.dirname(os.__file__))
with change_cwd(cwd):
env = os.environ.copy()
env['PYTHONPATH'] = parent
code = ('import os, sys',
# use ASCII to avoid locale issues with non-ASCII directories
'os_file = os.__file__.encode("ascii", "backslashreplace")',
r'sys.stdout.buffer.write(os_file + b"\n")',
'os_cached = os.__cached__.encode("ascii", "backslashreplace")',
r'sys.stdout.buffer.write(os_cached + b"\n")')
command = '\n'.join(code)
# First, prove that with -S (no 'import site'), the paths are
# relative.
proc = subprocess.Popen([sys.executable, '-S', '-c', command],
env=env,
stdout=subprocess.PIPE)
stdout, stderr = proc.communicate()
self.assertEqual(proc.returncode, 0)
os__file__, os__cached__ = stdout.splitlines()[:2]
self.assertFalse(os.path.isabs(os__file__))
self.assertFalse(os.path.isabs(os__cached__))
# Now, with 'import site', it works.
proc = subprocess.Popen([sys.executable, '-c', command],
env=env,
stdout=subprocess.PIPE)
stdout, stderr = proc.communicate()
self.assertEqual(proc.returncode, 0)
os__file__, os__cached__ = stdout.splitlines()[:2]
self.assertTrue(os.path.isabs(os__file__),
"expected absolute path, got {}"
.format(os__file__.decode('ascii')))
self.assertTrue(os.path.isabs(os__cached__),
"expected absolute path, got {}"
.format(os__cached__.decode('ascii')))
def test_abs_paths_cached_None(self):
"""Test for __cached__ is None.