MCPcopy Create free account
hub / github.com/pybind/pybind11 / get_interpreters

Function get_interpreters

tests/test_multiple_interpreters.py:32–83  ·  view source on GitHub ↗
(*, modern: bool)

Source from the content-addressed store, hash-verified

30
31
32def get_interpreters(*, modern: bool):
33 if modern and CONCURRENT_INTERPRETERS_SUPPORT:
34 from concurrent import interpreters
35
36 def create():
37 return contextlib.closing(interpreters.create())
38
39 def run_string(
40 interp: interpreters.Interpreter,
41 code: str,
42 *,
43 shared: dict[str, object] | None = None,
44 ) -> Exception | None:
45 if shared:
46 interp.prepare_main(**shared)
47 try:
48 interp.exec(code)
49 return None
50 except interpreters.ExecutionFailed as err:
51 return err
52
53 return run_string, create
54
55 if sys.version_info >= (3, 12):
56 interpreters = pytest.importorskip(
57 "_interpreters" if sys.version_info >= (3, 13) else "_xxsubinterpreters"
58 )
59
60 @contextlib.contextmanager
61 def create(config: str = ""):
62 try:
63 if config:
64 interp = interpreters.create(config)
65 else:
66 interp = interpreters.create()
67 except TypeError:
68 pytest.skip(f"interpreters module needs to support {config} config")
69
70 try:
71 yield interp
72 finally:
73 interpreters.destroy(interp)
74
75 def run_string(
76 interp: int, code: str, shared: dict[str, object] | None = None
77 ) -> Exception | None:
78 kwargs = {"shared": shared} if shared else {}
79 return interpreters.run_string(interp, code, **kwargs)
80
81 return run_string, create
82
83 pytest.skip("Test requires the interpreters stdlib module")
84
85
86@pytest.mark.skipif(

Calls

no outgoing calls

Tested by

no test coverage detected