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

Function test_independent_subinterpreters

tests/test_multiple_interpreters.py:89–133  ·  view source on GitHub ↗

Makes sure the internals object differs across independent subinterpreters

()

Source from the content-addressed store, hash-verified

87 sys.platform.startswith("emscripten"), reason="Requires loadable modules"
88)
89def test_independent_subinterpreters():
90 """Makes sure the internals object differs across independent subinterpreters"""
91
92 sys.path.insert(0, os.path.dirname(pybind11_tests.__file__))
93
94 run_string, create = get_interpreters(modern=True)
95
96 import mod_per_interpreter_gil as m
97
98 if not m.defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT:
99 pytest.skip("Does not have subinterpreter support compiled in")
100
101 code = textwrap.dedent(
102 """
103 import mod_per_interpreter_gil as m
104 import pickle
105 with open(pipeo, 'wb') as f:
106 pickle.dump(m.internals_at(), f)
107 """
108 ).strip()
109
110 with create() as interp1, create() as interp2:
111 try:
112 res0 = run_string(interp1, "import mod_shared_interpreter_gil")
113 if res0 is not None:
114 res0 = str(res0)
115 except Exception as e:
116 res0 = str(e)
117
118 pipei, pipeo = os.pipe()
119 run_string(interp1, code, shared={"pipeo": pipeo})
120 with open(pipei, "rb") as f:
121 res1 = pickle.load(f)
122
123 pipei, pipeo = os.pipe()
124 run_string(interp2, code, shared={"pipeo": pipeo})
125 with open(pipei, "rb") as f:
126 res2 = pickle.load(f)
127
128 assert "does not support loading in subinterpreters" in res0, (
129 "cannot use shared_gil in a default subinterpreter"
130 )
131 assert res1 != m.internals_at(), "internals should differ from main interpreter"
132 assert res2 != m.internals_at(), "internals should differ from main interpreter"
133 assert res1 != res2, "internals should differ between interpreters"
134
135
136@pytest.mark.skipif(

Callers

nothing calls this directly

Calls 6

get_interpretersFunction · 0.85
createFunction · 0.85
run_stringFunction · 0.85
strClass · 0.85
insertMethod · 0.80
loadMethod · 0.45

Tested by

no test coverage detected