Skip to content

gh-151626: Fix tests that fail when PYTHONPYCACHEPREFIX is set#151952

Merged
gpshead merged 3 commits into
python:mainfrom
zangjiucheng:gh-151626-fix
Jul 1, 2026
Merged

gh-151626: Fix tests that fail when PYTHONPYCACHEPREFIX is set#151952
gpshead merged 3 commits into
python:mainfrom
zangjiucheng:gh-151626-fix

Conversation

@zangjiucheng

@zangjiucheng zangjiucheng commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Several tests in test_inspect, test_import, test_importlib.test_util,
    test_py_compile, and test_compileall assume the default __pycache__
    bytecode layout and fail when the suite is run with PYTHONPYCACHEPREFIX set.
  • Fix: neutralize sys.pycache_prefix (and PYTHONPYCACHEPREFIX for
    subprocess-based tests) where that assumption is made so tests are environment-independent.
  • The key pattern for subprocess tests: assert_python_ok() runs with -I
    (isolated mode), which ignores PYTHONPYCACHEPREFIX, so the expected cached
    path must be computed with sys.pycache_prefix=None to match.

…mpile, and test_inspect to neutralize PYTHONPYCACHEPREFIX
@vstinner

Copy link
Copy Markdown
Member

test_inspect fails on Windows. Example:


FAIL: test_details_option_with_aliased_target (test.test_inspect.test_inspect.TestModuleCLI.test_details_option_with_aliased_target)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\a\cpython\cpython\Lib\test\test_inspect\test_inspect.py", line 6676, in test_details_option_with_aliased_target
    self.assertEqual(output_lines, expected_lines)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: Lists differ: ['Tar[116 chars], 'Cached: None', 'Line: 98', ''] != ['Tar[116 chars], 'Cached: __pycache__\\frozencpython-316.pyc', 'Line: 98', '']

First differing element 3:
'Cached: None'
'Cached: __pycache__\\frozencpython-316.pyc'

  ['Target: ntpath:join (looked up as "os:path.join")',
   'Origin: frozen',
   'Source: D:\\a\\cpython\\cpython\\Lib\\ntpath.py',
-  'Cached: None',
+  'Cached: __pycache__\\frozencpython-316.pyc',
   'Line: 98',
   '']

@zangjiucheng

Copy link
Copy Markdown
Contributor Author

I will resolve this today :)

zangjiucheng and others added 2 commits June 23, 2026 09:32
…d_target

On Windows, os.path resolves to ntpath which is a frozen module
(origin='frozen', cached=None). Calling cache_from_source('frozen')
fabricates a nonsensical path that mismatches the subprocess output of
None. Guard the cache_from_source call on cached being non-None.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

@gpshead gpshead left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice test suite cleanup, thanks!

@gpshead gpshead added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Jul 1, 2026
@gpshead gpshead merged commit 564c58c into python:main Jul 1, 2026
61 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @zangjiucheng for the PR, and @gpshead for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Sorry, @zangjiucheng and @gpshead, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 564c58c718bc3de9cf3d9bc20cdc06317411c1cd 3.14

@bedevere-app

bedevere-app Bot commented Jul 1, 2026

Copy link
Copy Markdown

GH-152736 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jul 1, 2026
@miss-islington-app

Copy link
Copy Markdown

Sorry, @zangjiucheng and @gpshead, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 564c58c718bc3de9cf3d9bc20cdc06317411c1cd 3.13

@gpshead gpshead removed needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Jul 1, 2026
gpshead pushed a commit that referenced this pull request Jul 1, 2026
…GH-151952) (#152736)

gh-151626: Fix tests that fail when PYTHONPYCACHEPREFIX is set (GH-151952)

Fix tests in test_compileall, test_import, test_importlib, test_py_compile, and test_inspect to neutralize PYTHONPYCACHEPREFIX
(cherry picked from commit 564c58c)

Co-authored-by: Jiucheng(Oliver) <git.jiucheng@gmail.com>
@bedevere-app

bedevere-app Bot commented Jul 1, 2026

Copy link
Copy Markdown

GH-152744 is a backport of this pull request to the 3.14 branch.

gpshead added a commit that referenced this pull request Jul 1, 2026
…GH-151952) (#152744)

Fix tests in test_compileall, test_import, test_importlib, test_py_compile, and test_inspect to neutralize PYTHONPYCACHEPREFIX
(cherry picked from commit 564c58c)

Co-authored-by: Jiucheng(Oliver) <git.jiucheng@gmail.com>
gpshead added a commit that referenced this pull request Jul 1, 2026
…GH-151952) (GH-152744) (#152748)

[3.14] gh-151626: Fix tests that fail when PYTHONPYCACHEPREFIX is set (GH-151952) (GH-152744)

Fix tests in test_compileall, test_import, test_importlib, test_py_compile, and test_inspect to neutralize PYTHONPYCACHEPREFIX
(cherry picked from commit 564c58c)
(cherry picked from commit 7087839)

Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
Co-authored-by: Jiucheng(Oliver) <git.jiucheng@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants