|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pytest |
| 4 | +import os |
| 5 | +import pickle |
| 6 | + |
| 7 | +def test_interpreters(): |
| 8 | + """Makes sure the internals object differs across subinterpreters""" |
| 9 | + |
| 10 | + import mod_test_interpreters as m |
| 11 | + |
| 12 | + i = None |
| 13 | + try: |
| 14 | + # 3.14+ |
| 15 | + import interpreters as i |
| 16 | + except ImportError: |
| 17 | + try: |
| 18 | + # 3.13 |
| 19 | + import _interpreters as i |
| 20 | + except ImportError: |
| 21 | + try: |
| 22 | + # 3.12 |
| 23 | + import _xxsubinterpreters as i |
| 24 | + except ImportError: |
| 25 | + # <= 3.11 |
| 26 | + pytest.skip("Test requires a the interpreters stdlib module") |
| 27 | + return |
| 28 | + |
| 29 | + code = """ |
| 30 | +import mod_test_interpreters as m |
| 31 | +import pickle |
| 32 | +with open(pipeo, 'wb') as f: |
| 33 | + pickle.dump(m.internals_at(), f) |
| 34 | +""" |
| 35 | + |
| 36 | + interp1 = i.create() |
| 37 | + interp2 = i.create() |
| 38 | + try: |
| 39 | + pipei, pipeo = os.pipe() |
| 40 | + i.run_string(interp1, code, shared={'pipeo':pipeo}) |
| 41 | + with open(pipei, 'rb') as f: |
| 42 | + res1 = pickle.load(f) |
| 43 | + |
| 44 | + pipei, pipeo = os.pipe() |
| 45 | + i.run_string(interp2, code, shared={'pipeo':pipeo}) |
| 46 | + with open(pipei, 'rb') as f: |
| 47 | + res2 = pickle.load(f) |
| 48 | + |
| 49 | + # do this while the two interpreters are active |
| 50 | + import mod_test_interpreters as m2 |
| 51 | + print(dir(m)) |
| 52 | + print(dir(m2)) |
| 53 | + assert m.internals_at() == m2.internals_at(), "internals should be the same within the main interpreter" |
| 54 | + finally: |
| 55 | + i.destroy(interp1) |
| 56 | + i.destroy(interp2) |
| 57 | + |
| 58 | + assert res1 != m.internals_at(), "internals should differ from main interpreter" |
| 59 | + assert res2 != m.internals_at(), "internals should differ from main interpreter" |
| 60 | + assert res1 != res2, "internals should differ between interpreters" |
0 commit comments