|
9 | 9 |
|
10 | 10 | CONCURRENT_INTERPRETERS_SUPPORT = sys.version_info >= (3, 14) and (
|
11 | 11 | sys.version_info != (3, 14, 0, "beta", 1)
|
12 |
| - or sys.version_info != (3, 14, 0, "beta", 2) |
| 12 | + and sys.version_info != (3, 14, 0, "beta", 2) |
13 | 13 | )
|
14 | 14 |
|
15 | 15 |
|
16 | 16 | def get_interpreters(*, modern: bool):
|
17 | 17 | if modern and CONCURRENT_INTERPRETERS_SUPPORT:
|
| 18 | + print(sys.version_info) |
18 | 19 | from concurrent import interpreters
|
19 | 20 |
|
20 | 21 | def create():
|
@@ -129,6 +130,65 @@ def test_independent_subinterpreters():
|
129 | 130 | )
|
130 | 131 |
|
131 | 132 |
|
| 133 | +@pytest.mark.skipif( |
| 134 | + sys.platform.startswith("emscripten"), reason="Requires loadable modules" |
| 135 | +) |
| 136 | +@pytest.mark.skipif(not CONCURRENT_INTERPRETERS_SUPPORT, reason="Requires 3.14.0b3+") |
| 137 | +def test_independent_subinterpreters_modern(): |
| 138 | + """Makes sure the internals object differs across independent subinterpreters. Modern (3.14+) syntax.""" |
| 139 | + |
| 140 | + sys.path.append(".") |
| 141 | + |
| 142 | + m = pytest.importorskip("mod_per_interpreter_gil") |
| 143 | + |
| 144 | + if not m.defined_PYBIND11_HAS_SUBINTERPRETER_SUPPORT: |
| 145 | + pytest.skip("Does not have subinterpreter support compiled in") |
| 146 | + |
| 147 | + from concurrent import interpreters |
| 148 | + |
| 149 | + code = """ |
| 150 | +import mod_per_interpreter_gil as m |
| 151 | +
|
| 152 | +values.put_nowait(m.internals_at()) |
| 153 | +""" |
| 154 | + |
| 155 | + with contextlib.closing(interpreters.create()) as interp1, contextlib.closing( |
| 156 | + interpreters.create() |
| 157 | + ) as interp2: |
| 158 | + with pytest.raises( |
| 159 | + interpreters.ExecutionFailed, |
| 160 | + match="does not support loading in subinterpreters", |
| 161 | + ): |
| 162 | + interp1.exec("import mod_shared_interpreter_gil") |
| 163 | + |
| 164 | + values = interpreters.create_queue() |
| 165 | + interp1.prepare_main(values=values) |
| 166 | + interp1.exec(code) |
| 167 | + res1 = values.get_nowait() |
| 168 | + |
| 169 | + interp2.prepare_main(values=values) |
| 170 | + interp2.exec(code) |
| 171 | + res2 = values.get_nowait() |
| 172 | + |
| 173 | + # do this while the two interpreters are active |
| 174 | + import mod_per_interpreter_gil as m2 |
| 175 | + |
| 176 | + assert m.internals_at() == m2.internals_at(), ( |
| 177 | + "internals should be the same within the main interpreter" |
| 178 | + ) |
| 179 | + |
| 180 | + assert res1 != m.internals_at(), "internals should differ from main interpreter" |
| 181 | + assert res2 != m.internals_at(), "internals should differ from main interpreter" |
| 182 | + assert res1 != res2, "internals should differ between interpreters" |
| 183 | + |
| 184 | + # do this after the two interpreters are destroyed and only one remains |
| 185 | + import mod_per_interpreter_gil as m3 |
| 186 | + |
| 187 | + assert m.internals_at() == m3.internals_at(), ( |
| 188 | + "internals should be the same within the main interpreter" |
| 189 | + ) |
| 190 | + |
| 191 | + |
132 | 192 | @pytest.mark.skipif(
|
133 | 193 | sys.platform.startswith("emscripten"), reason="Requires loadable modules"
|
134 | 194 | )
|
|
0 commit comments