Skip to content

Commit 84f1e16

Browse files
committed
fix(test_inspect): add method to handle cached paths for frozen modules, fix NEWS.d with no test target
1 parent fdc59f3 commit 84f1e16

2 files changed

Lines changed: 19 additions & 25 deletions

File tree

Lib/test/test_inspect/test_inspect.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6520,6 +6520,19 @@ class TestModuleCLI(unittest.TestCase):
65206520
NO_SOURCE_ERROR = "No source code available for defining module"
65216521
NO_SOURCE_TARGET_ERROR = "Failed to retrieve source code for given target"
65226522

6523+
@staticmethod
6524+
def _expected_cached(module):
6525+
# assert_python_ok() runs the subprocess in isolated mode (-I), which
6526+
# ignores PYTHONPYCACHEPREFIX, so compute the expected cached path the
6527+
# same way (i.e. without any pycache prefix) to stay independent of the
6528+
# environment the test suite is run in. Modules without a cached path
6529+
# (e.g. frozen modules such as ntpath/importlib.machinery on Windows)
6530+
# report None, so preserve that.
6531+
if module.__spec__.cached is None:
6532+
return None
6533+
with support.swap_attr(sys, 'pycache_prefix', None):
6534+
return importlib.util.cache_from_source(module.__spec__.origin)
6535+
65236536
def test_only_source(self):
65246537
module = importlib.import_module('unittest')
65256538
rc, out, err = assert_python_ok('-m', 'inspect',
@@ -6577,12 +6590,7 @@ def test_details_option_with_package(self):
65776590
args = support.optim_args_from_interpreter_flags()
65786591
rc, out, err = assert_python_ok(*args, '-m', 'inspect',
65796592
module_name, '--details')
6580-
# assert_python_ok() runs the subprocess in isolated mode (-I), which
6581-
# ignores PYTHONPYCACHEPREFIX, so compute the expected cached path the
6582-
# same way (i.e. without any pycache prefix) to stay independent of the
6583-
# environment the test suite is run in.
6584-
with support.swap_attr(sys, 'pycache_prefix', None):
6585-
cached = importlib.util.cache_from_source(module.__spec__.origin)
6593+
cached = self._expected_cached(module)
65866594
# Full rendering check on the expected output
65876595
expected_lines = [
65886596
f"Target: {module.__name__}", # No aliasing
@@ -6626,12 +6634,7 @@ def test_details_option_with_data_target(self):
66266634
args = support.optim_args_from_interpreter_flags()
66276635
rc, out, err = assert_python_ok(*args, '-m', 'inspect',
66286636
cli_target, '--details')
6629-
# assert_python_ok() runs the subprocess in isolated mode (-I), which
6630-
# ignores PYTHONPYCACHEPREFIX, so compute the expected cached path the
6631-
# same way (i.e. without any pycache prefix) to stay independent of the
6632-
# environment the test suite is run in.
6633-
with support.swap_attr(sys, 'pycache_prefix', None):
6634-
cached = importlib.util.cache_from_source(module.__spec__.origin)
6637+
cached = self._expected_cached(module)
66356638
# Full rendering check on the expected output
66366639
# The error is only informational when reading source details
66376640
expected_lines = [
@@ -6657,16 +6660,7 @@ def test_details_option_with_aliased_target(self):
66576660
args = support.optim_args_from_interpreter_flags()
66586661
rc, out, err = assert_python_ok(*args, '-m', 'inspect',
66596662
cli_target, '--details')
6660-
# assert_python_ok() runs the subprocess in isolated mode (-I), which
6661-
# ignores PYTHONPYCACHEPREFIX, so compute the expected cached path the
6662-
# same way (i.e. without any pycache prefix) to stay independent of the
6663-
# environment the test suite is run in. For frozen modules (e.g.
6664-
# ntpath on Windows) there is no cached path; keep it None.
6665-
if module.__spec__.cached is not None:
6666-
with support.swap_attr(sys, 'pycache_prefix', None):
6667-
cached = importlib.util.cache_from_source(module.__spec__.origin)
6668-
else:
6669-
cached = None
6663+
cached = self._expected_cached(module)
66706664
# Full rendering check on the expected output
66716665
expected_lines = [
66726666
f'Target: {defining_target} (looked up as "{cli_target}")',
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Fix several tests in :mod:`test.test_inspect`, :mod:`test.test_import`,
2-
:mod:`test.test_importlib`, :mod:`test.test_py_compile` and
3-
:mod:`test.test_compileall` that failed when the test suite was run with
1+
Fix several tests in ``test.test_inspect``, ``test.test_import``,
2+
``test.test_importlib``, ``test.test_py_compile`` and
3+
``test.test_compileall`` that failed when the test suite was run with
44
:envvar:`PYTHONPYCACHEPREFIX` set. These tests now neutralize the pycache
55
prefix where they assume the default ``__pycache__`` bytecode layout.

0 commit comments

Comments
 (0)