Skip to content

test_py_info_cache_invalidation_on_py_info_change alters py_info.py, invalidating .pyc files #2933

@mgorny

Description

@mgorny

Issue

The following test alters py_info.py on installed virtualenv. While it restores the original content afterwards, the operation changes mtime and therefore invalidates .pyc files.

def test_py_info_cache_invalidation_on_py_info_change(mocker, session_app_data):
# 1. Get a PythonInfo object for the current executable, this will cache it.
PythonInfo.from_exe(sys.executable, session_app_data)
# 2. Spy on _run_subprocess
spy = mocker.spy(cached_py_info, "_run_subprocess")
# 3. Modify the content of py_info.py
py_info_script = Path(cached_py_info.__file__).parent / "py_info.py"
original_content = py_info_script.read_text(encoding="utf-8")
try:
# 4. Clear the in-memory cache
mocker.patch.dict(cached_py_info._CACHE, {}, clear=True) # noqa: SLF001
py_info_script.write_text(original_content + "\n# a comment", encoding="utf-8")
# 5. Get the PythonInfo object again
info = PythonInfo.from_exe(sys.executable, session_app_data)
# 6. Assert that _run_subprocess was called again
if is_macos_brew(info):
assert spy.call_count in {2, 3}
else:
assert spy.call_count == 2
finally:
# Restore the original content
py_info_script.write_text(original_content, encoding="utf-8")

 * QA Notice: This package installs one or more compiled Python modules whose
 * .py files have different timestamps than recorded:
 * 
 *   /usr/lib/pypy3.11/site-packages/virtualenv/discovery/__pycache__/py_info.pypy311.opt-1.pyc
 *   /usr/lib/pypy3.11/site-packages/virtualenv/discovery/__pycache__/py_info.pypy311.opt-2.pyc
 *   /usr/lib/pypy3.11/site-packages/virtualenv/discovery/__pycache__/py_info.pypy311.pyc
 *   /usr/lib/python3.11/site-packages/virtualenv/discovery/__pycache__/py_info.cpython-311.opt-1.pyc
 *   /usr/lib/python3.11/site-packages/virtualenv/discovery/__pycache__/py_info.cpython-311.opt-2.pyc
 *   /usr/lib/python3.11/site-packages/virtualenv/discovery/__pycache__/py_info.cpython-311.pyc
 *   /usr/lib/python3.12/site-packages/virtualenv/discovery/__pycache__/py_info.cpython-312.opt-1.pyc
 *   /usr/lib/python3.12/site-packages/virtualenv/discovery/__pycache__/py_info.cpython-312.opt-2.pyc
 *   /usr/lib/python3.12/site-packages/virtualenv/discovery/__pycache__/py_info.cpython-312.pyc
 *   /usr/lib/python3.13/site-packages/virtualenv/discovery/__pycache__/py_info.cpython-313.opt-1.pyc
 *   /usr/lib/python3.13/site-packages/virtualenv/discovery/__pycache__/py_info.cpython-313.opt-2.pyc
 *   /usr/lib/python3.13/site-packages/virtualenv/discovery/__pycache__/py_info.cpython-313.pyc
 *   /usr/lib/python3.14/site-packages/virtualenv/discovery/__pycache__/py_info.cpython-314.opt-1.pyc
 *   /usr/lib/python3.14/site-packages/virtualenv/discovery/__pycache__/py_info.cpython-314.opt-2.pyc
 *   /usr/lib/python3.14/site-packages/virtualenv/discovery/__pycache__/py_info.cpython-314.pyc

Environment

Provide at least:

  • OS: Gentoo Linux amd64
  • pip list of the host python where virtualenv is installed: n/a (very long)

Output of the virtual environment creation

n/a

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions