|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import sys |
| 4 | +import tempfile |
| 5 | +from pathlib import Path |
| 6 | +from textwrap import dedent |
| 7 | + |
| 8 | + |
| 9 | +def test_virtualenv_py_race_condition_find_spec(): |
| 10 | + """Test that _Finder.find_spec handles NameError gracefully when _DISTUTILS_PATCH is not defined.""" |
| 11 | + # Create a temporary file with partial _virtualenv.py content (simulating race condition) |
| 12 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 13 | + venv_file = Path(tmpdir) / "_virtualenv_test.py" |
| 14 | + |
| 15 | + # Write a partial version of _virtualenv.py that has _Finder but not _DISTUTILS_PATCH |
| 16 | + # This simulates the state during a race condition where the file is being rewritten |
| 17 | + partial_content = dedent(""" |
| 18 | + import sys |
| 19 | + |
| 20 | + class _Finder: |
| 21 | + fullname = None |
| 22 | + lock = [] |
| 23 | + |
| 24 | + def find_spec(self, fullname, path, target=None): |
| 25 | + # This should handle the NameError gracefully |
| 26 | + try: |
| 27 | + distutils_patch = _DISTUTILS_PATCH # noqa: F821 |
| 28 | + except NameError: |
| 29 | + return None |
| 30 | + if fullname in distutils_patch and self.fullname is None: |
| 31 | + return None |
| 32 | + return None |
| 33 | + |
| 34 | + @staticmethod |
| 35 | + def exec_module(old, module): |
| 36 | + old(module) |
| 37 | + try: |
| 38 | + distutils_patch = _DISTUTILS_PATCH # noqa: F821 |
| 39 | + except NameError: |
| 40 | + return |
| 41 | + if module.__name__ in distutils_patch: |
| 42 | + pass # Would call patch_dist(module) |
| 43 | + |
| 44 | + @staticmethod |
| 45 | + def load_module(old, name): |
| 46 | + module = old(name) |
| 47 | + try: |
| 48 | + distutils_patch = _DISTUTILS_PATCH # noqa: F821 |
| 49 | + except NameError: |
| 50 | + return module |
| 51 | + if module.__name__ in distutils_patch: |
| 52 | + pass # Would call patch_dist(module) |
| 53 | + return module |
| 54 | + |
| 55 | + finder = _Finder() |
| 56 | + """) |
| 57 | + |
| 58 | + venv_file.write_text(partial_content, encoding="utf-8") |
| 59 | + |
| 60 | + # Add the directory to sys.path temporarily |
| 61 | + sys.path.insert(0, tmpdir) |
| 62 | + try: |
| 63 | + # Import the module |
| 64 | + import _virtualenv_test # noqa: F401 |
| 65 | + |
| 66 | + # Get the finder instance |
| 67 | + finder = _virtualenv_test.finder |
| 68 | + |
| 69 | + # Try to call find_spec - this should not raise NameError |
| 70 | + result = finder.find_spec("distutils.dist", None) |
| 71 | + assert result is None, "find_spec should return None when _DISTUTILS_PATCH is not defined" |
| 72 | + |
| 73 | + # Create a mock module object |
| 74 | + class MockModule: |
| 75 | + __name__ = "distutils.dist" |
| 76 | + |
| 77 | + # Try to call exec_module - this should not raise NameError |
| 78 | + mock_old_exec = lambda x: None # noqa: E731 |
| 79 | + finder.exec_module(mock_old_exec, MockModule()) |
| 80 | + |
| 81 | + # Try to call load_module - this should not raise NameError |
| 82 | + mock_old_load = lambda name: MockModule() # noqa: E731 |
| 83 | + result = finder.load_module(mock_old_load, "distutils.dist") |
| 84 | + assert result.__name__ == "distutils.dist" |
| 85 | + |
| 86 | + finally: |
| 87 | + # Clean up |
| 88 | + sys.path.remove(tmpdir) |
| 89 | + if "_virtualenv_test" in sys.modules: |
| 90 | + del sys.modules["_virtualenv_test"] |
| 91 | + |
| 92 | + |
| 93 | +def test_virtualenv_py_normal_operation(): |
| 94 | + """Test that the fix doesn't break normal operation when _DISTUTILS_PATCH is defined.""" |
| 95 | + # Read the actual _virtualenv.py file |
| 96 | + virtualenv_py_path = ( |
| 97 | + Path(__file__).parent.parent.parent.parent.parent |
| 98 | + / "src" |
| 99 | + / "virtualenv" |
| 100 | + / "create" |
| 101 | + / "via_global_ref" |
| 102 | + / "_virtualenv.py" |
| 103 | + ) |
| 104 | + |
| 105 | + if not virtualenv_py_path.exists(): |
| 106 | + return # Skip if we can't find the file |
| 107 | + |
| 108 | + content = virtualenv_py_path.read_text(encoding="utf-8") |
| 109 | + |
| 110 | + # Verify the fix is present |
| 111 | + assert "try:" in content |
| 112 | + assert "distutils_patch = _DISTUTILS_PATCH" in content |
| 113 | + assert "except NameError:" in content |
| 114 | + assert "return None" in content or "return" in content |
0 commit comments