|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import sys |
4 | | -import tempfile |
5 | 4 | from pathlib import Path |
6 | | -from textwrap import dedent |
7 | 5 |
|
8 | 6 |
|
9 | | -def test_virtualenv_py_race_condition_find_spec(): |
| 7 | +def test_virtualenv_py_race_condition_find_spec(tmp_path): |
10 | 8 | """Test that _Finder.find_spec handles NameError gracefully when _DISTUTILS_PATCH is not defined.""" |
11 | 9 | # 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: PLC0415 |
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 | | - def mock_old_exec(_x): |
79 | | - pass |
80 | | - |
81 | | - finder.exec_module(mock_old_exec, MockModule()) |
82 | | - |
83 | | - # Try to call load_module - this should not raise NameError |
84 | | - def mock_old_load(_name): |
85 | | - return MockModule() |
86 | | - |
87 | | - result = finder.load_module(mock_old_load, "distutils.dist") |
88 | | - assert result.__name__ == "distutils.dist" |
89 | | - |
90 | | - finally: |
91 | | - # Clean up |
92 | | - sys.path.remove(tmpdir) |
93 | | - if "_virtualenv_test" in sys.modules: |
94 | | - del sys.modules["_virtualenv_test"] |
| 10 | + venv_file = tmp_path / "_virtualenv_test.py" |
| 11 | + |
| 12 | + # Write a partial version of _virtualenv.py that has _Finder but not _DISTUTILS_PATCH |
| 13 | + # This simulates the state during a race condition where the file is being rewritten |
| 14 | + helper_file = Path(__file__).parent / "_test_race_condition_helper.py" |
| 15 | + partial_content = helper_file.read_text(encoding="utf-8") |
| 16 | + |
| 17 | + venv_file.write_text(partial_content, encoding="utf-8") |
| 18 | + |
| 19 | + sys.path.insert(0, str(tmp_path)) |
| 20 | + try: |
| 21 | + import _virtualenv_test # noqa: PLC0415 |
| 22 | + |
| 23 | + finder = _virtualenv_test.finder |
| 24 | + |
| 25 | + # Try to call find_spec - this should not raise NameError |
| 26 | + result = finder.find_spec("distutils.dist", None) |
| 27 | + assert result is None, "find_spec should return None when _DISTUTILS_PATCH is not defined" |
| 28 | + |
| 29 | + # Create a mock module object |
| 30 | + class MockModule: |
| 31 | + __name__ = "distutils.dist" |
| 32 | + |
| 33 | + # Try to call exec_module - this should not raise NameError |
| 34 | + def mock_old_exec(_x): |
| 35 | + pass |
| 36 | + |
| 37 | + finder.exec_module(mock_old_exec, MockModule()) |
| 38 | + |
| 39 | + # Try to call load_module - this should not raise NameError |
| 40 | + def mock_old_load(_name): |
| 41 | + return MockModule() |
| 42 | + |
| 43 | + result = finder.load_module(mock_old_load, "distutils.dist") |
| 44 | + assert result.__name__ == "distutils.dist" |
| 45 | + |
| 46 | + finally: |
| 47 | + sys.path.remove(str(tmp_path)) |
| 48 | + if "_virtualenv_test" in sys.modules: |
| 49 | + del sys.modules["_virtualenv_test"] |
95 | 50 |
|
96 | 51 |
|
97 | 52 | def test_virtualenv_py_normal_operation(): |
|
0 commit comments