Skip to content

Commit 25909b0

Browse files
committed
Make Python path handling more robust
1 parent fd29471 commit 25909b0

File tree

1 file changed

+27
-45
lines changed

1 file changed

+27
-45
lines changed

tests/test_multiple_interpreters/test_multiple_interpreters.py

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import pytest
1111

12+
import pybind11_tests
13+
1214
# 3.14.0b3+, though sys.implementation.supports_isolated_interpreters is being added in b4
1315
# Can be simplified when we drop support for the first three betas
1416
CONCURRENT_INTERPRETERS_SUPPORT = (
@@ -84,7 +86,7 @@ def run_string(
8486
def test_independent_subinterpreters():
8587
"""Makes sure the internals object differs across independent subinterpreters"""
8688

87-
sys.path.append(".")
89+
sys.path.insert(0, os.path.dirname(pybind11_tests.__file__))
8890

8991
run_string, create = get_interpreters(modern=True)
9092

@@ -135,7 +137,7 @@ def test_independent_subinterpreters():
135137
def test_independent_subinterpreters_modern():
136138
"""Makes sure the internals object differs across independent subinterpreters. Modern (3.14+) syntax."""
137139

138-
sys.path.append(".")
140+
sys.path.insert(0, os.path.dirname(pybind11_tests.__file__))
139141

140142
m = pytest.importorskip("mod_per_interpreter_gil")
141143

@@ -181,7 +183,7 @@ def test_independent_subinterpreters_modern():
181183
def test_dependent_subinterpreters():
182184
"""Makes sure the internals object differs across subinterpreters"""
183185

184-
sys.path.append(".")
186+
sys.path.insert(0, os.path.dirname(pybind11_tests.__file__))
185187

186188
run_string, create = get_interpreters(modern=False)
187189

@@ -209,11 +211,11 @@ def test_dependent_subinterpreters():
209211

210212

211213
PREAMBLE_CODE = textwrap.dedent(
212-
"""
214+
f"""
213215
def test():
214216
import sys
215217
216-
sys.path.append('.')
218+
sys.path.insert(0, {os.path.dirname(pybind11_tests.__file__)!r})
217219
218220
import collections
219221
import mod_per_interpreter_gil_with_singleton as m
@@ -316,7 +318,7 @@ def test_import_in_subinterpreter_after_main():
316318
with contextlib.ExitStack() as stack:
317319
interps = [
318320
stack.enter_context(contextlib.closing(interpreters.create()))
319-
for _ in range(4)
321+
for _ in range(8)
320322
]
321323
random.shuffle(interps)
322324
for interp in interps:
@@ -369,7 +371,7 @@ def test_import_in_subinterpreter_before_main():
369371
with contextlib.ExitStack() as stack:
370372
interps = [
371373
stack.enter_context(contextlib.closing(interpreters.create()))
372-
for _ in range(4)
374+
for _ in range(8)
373375
]
374376
for interp in interps:
375377
interp.call(test)
@@ -395,7 +397,7 @@ def test_import_in_subinterpreter_before_main():
395397
with contextlib.ExitStack() as stack:
396398
interps = [
397399
stack.enter_context(contextlib.closing(interpreters.create()))
398-
for _ in range(4)
400+
for _ in range(8)
399401
]
400402
for interp in interps:
401403
interp.call(test)
@@ -417,41 +419,21 @@ def test_import_in_subinterpreter_before_main():
417419
def test_import_in_subinterpreter_concurrently():
418420
"""Tests that importing a module in multiple subinterpreters concurrently works correctly"""
419421
check_script_success_in_subprocess(
420-
"""
421-
import gc
422-
import sys
423-
from concurrent.futures import InterpreterPoolExecutor, as_completed
424-
425-
sys.path.append('.')
426-
427-
def test():
428-
import collections
429-
import mod_per_interpreter_gil_with_singleton as m
430-
431-
objects = m.get_objects_in_singleton()
432-
assert objects == [
433-
type(None),
434-
tuple,
435-
list,
436-
dict,
437-
collections.OrderedDict,
438-
collections.defaultdict,
439-
collections.deque,
440-
]
441-
442-
assert hasattr(m, 'MyClass')
443-
assert hasattr(m, 'MyGlobalError')
444-
assert hasattr(m, 'MyLocalError')
445-
assert hasattr(m, 'MyEnum')
446-
447-
futures = future = None
448-
with InterpreterPoolExecutor(max_workers=16) as executor:
449-
futures = [executor.submit(test) for _ in range(32)]
450-
for future in as_completed(futures):
451-
future.result()
452-
del futures, future, executor
453-
454-
for _ in range(5):
455-
gc.collect()
456-
"""
422+
PREAMBLE_CODE
423+
+ textwrap.dedent(
424+
"""
425+
import gc
426+
from concurrent.futures import InterpreterPoolExecutor, as_completed
427+
428+
futures = future = None
429+
with InterpreterPoolExecutor(max_workers=16) as executor:
430+
futures = [executor.submit(test) for _ in range(32)]
431+
for future in as_completed(futures):
432+
future.result()
433+
del futures, future, executor
434+
435+
for _ in range(5):
436+
gc.collect()
437+
"""
438+
)
457439
)

0 commit comments

Comments
 (0)