1010@pytest .mark .skipif (
1111 sys .platform .startswith ("emscripten" ), reason = "Requires loadable modules"
1212)
13- def test_interpreters ():
14- """Makes sure the internals object differs across subinterpreters"""
13+ def test_independent_subinterpreters ():
14+ """Makes sure the internals object differs across independent subinterpreters"""
1515
1616 sys .path .append ("." )
1717
@@ -36,6 +36,8 @@ def test_interpreters():
3636 interp1 = interpreters .create ()
3737 interp2 = interpreters .create ()
3838 try :
39+ res0 = interpreters .run_string (interp1 , "import mod_test_interpreters2" )
40+
3941 pipei , pipeo = os .pipe ()
4042 interpreters .run_string (interp1 , code , shared = {"pipeo" : pipeo })
4143 with open (pipei , "rb" ) as f :
@@ -56,6 +58,7 @@ def test_interpreters():
5658 interpreters .destroy (interp1 )
5759 interpreters .destroy (interp2 )
5860
61+ assert "does not support loading in subinterpreters" in res0 .msg , "cannot use shared_gil in a default subinterpreter"
5962 assert res1 != m .internals_at (), "internals should differ from main interpreter"
6063 assert res2 != m .internals_at (), "internals should differ from main interpreter"
6164 assert res1 != res2 , "internals should differ between interpreters"
@@ -66,3 +69,53 @@ def test_interpreters():
6669 assert m .internals_at () == m3 .internals_at (), (
6770 "internals should be the same within the main interpreter"
6871 )
72+
73+ @pytest .mark .skipif (
74+ sys .platform .startswith ("emscripten" ), reason = "Requires loadable modules"
75+ )
76+ def test_dependent_subinterpreters ():
77+ """Makes sure the internals object differs across subinterpreters"""
78+
79+ sys .path .append ("." )
80+
81+ if sys .version_info >= (3 , 14 ):
82+ import interpreters
83+ elif sys .version_info >= (3 , 13 ):
84+ import _interpreters as interpreters
85+ elif sys .version_info >= (3 , 12 ):
86+ import _xxsubinterpreters as interpreters
87+ else :
88+ pytest .skip ("Test requires a the interpreters stdlib module" )
89+
90+ import mod_test_interpreters2 as m
91+
92+ code = """
93+ import mod_test_interpreters2 as m
94+ import pickle
95+ with open(pipeo, 'wb') as f:
96+ pickle.dump(m.internals_at(), f)
97+ """
98+
99+ interp1 = interpreters .create ("legacy" )
100+ try :
101+ pipei , pipeo = os .pipe ()
102+ interpreters .run_string (interp1 , code , shared = {"pipeo" : pipeo })
103+ with open (pipei , "rb" ) as f :
104+ res1 = pickle .load (f )
105+
106+ # do this while the two interpreters are active
107+ import mod_test_interpreters2 as m2
108+
109+ assert m .internals_at () == m2 .internals_at (), (
110+ "internals should be the same within the main interpreter"
111+ )
112+ finally :
113+ interpreters .destroy (interp1 )
114+
115+ assert res1 != m .internals_at (), "internals should differ from main interpreter"
116+
117+ # do this after the two interpreters are destroyed and only one remains
118+ import mod_test_interpreters2 as m3
119+ assert m .internals_at () == m3 .internals_at (), (
120+ "internals should be the same within the main interpreter"
121+ )
0 commit comments