def test_basic(self): interp1 = interpreters.create() interp2 = interpreters.create() interp3 = interpreters.create() self.assertEqual(4, len(interpreters.list_all())) interp2.close() self.assertEqual(3, len(interpreters.list_all()))
def test_basic(self): main = interpreters.get_main() interp1 = interpreters.create() interp2 = interpreters.create() interp3 = interpreters.create() self.assertEqual(set(interpreters.list_all()), {main, interp1, interp2, interp3}) interp2.close() self.assertEqual(set(interpreters.list_all()), {main, interp1, interp3})
def test_in_subinterpreter(self): main, = interpreters.list_all() interp = interpreters.create() out = _run_output(interp, dedent(""" from test.support import interpreters interp = interpreters.create() print(interp.id) """)) interp2 = interpreters.Interpreter(int(out)) self.assertEqual(interpreters.list_all(), [main, interp, interp2])
def test_all(self): before = set(interpreters.list_all()) interps = set() for _ in range(3): interp = interpreters.create() interps.add(interp) self.assertEqual(set(interpreters.list_all()), before | interps) for interp in interps: interp.close() self.assertEqual(set(interpreters.list_all()), before)
def test_from_sibling(self): main, = interpreters.list_all() interp1 = interpreters.create() script = dedent(f""" from test.support import interpreters interp2 = interpreters.create() interp2.close() """) interp1.run(script) self.assertEqual(len(set(interpreters.list_all())), len({main, interp1}))
def test_after_destroy_some(self): before = set(interpreters.list_all()) # Create 3 subinterpreters. interp1 = interpreters.create() interp2 = interpreters.create() interp3 = interpreters.create() # Now destroy 2 of them. interp1.close() interp2.close() # Finally, create another. interp = interpreters.create() self.assertEqual(set(interpreters.list_all()), before | {interp3, interp})
def test_after_destroy_all(self): before = set(interpreters.list_all()) # Create 3 subinterpreters. interp_lst = [] for _ in range(3): interps = interpreters.create() interp_lst.append(interps) # Now destroy them. for interp in interp_lst: interp.close() # Finally, create another. interp = interpreters.create() self.assertEqual(set(interpreters.list_all()), before | {interp})
def test_from_current(self): main, = interpreters.list_all() interp = interpreters.create() out = _run_output(interp, dedent(f""" from test.support import interpreters interp = interpreters.Interpreter({int(interp.id)}) try: interp.close() except RuntimeError: print('failed') """)) self.assertEqual(out.strip(), 'failed') self.assertEqual(set(interpreters.list_all()), {main, interp})
def test_from_sibling(self): main, = interpreters.list_all() interp1 = interpreters.create() interp2 = interpreters.create() self.assertEqual(set(interpreters.list_all()), {main, interp1, interp2}) interp1.run(dedent(f""" from test.support import interpreters interp2 = interpreters.Interpreter(int({interp2.id})) interp2.close() interp3 = interpreters.create() interp3.close() """)) self.assertEqual(set(interpreters.list_all()), {main, interp1})
def test_in_subinterpreter(self): main, = interpreters.list_all() interp = interpreters.create() out = _run_output( interp, dedent(""" from test.support import interpreters interp = interpreters.create() print(interp) """)) interp2 = out.strip() self.assertEqual(len(set(interpreters.list_all())), len({main, interp, interp2}))
def test_from_current(self): main, = interpreters.list_all() interp = interpreters.create() script = dedent(f""" from test.support import interpreters try: main = interpreters.get_current() main.close() except RuntimeError: pass """) interp.run(script) self.assertEqual(len(set(interpreters.list_all())), len({main, interp}))
def test_still_running(self): main, = interpreters.list_all() interp = interpreters.create() with _running(interp): with self.assertRaises(RuntimeError): interp.close() self.assertTrue(interp.is_running())
def clean_up_interpreters(): for interp in interpreters.list_all(): if interp.id == 0: # main continue try: interp.close() except RuntimeError: pass # already destroyed
def test_after_creating(self): main = interpreters.get_current() first = interpreters.create() second = interpreters.create() ids = [] for interp in interpreters.list_all(): ids.append(interp.id) self.assertEqual(ids, [main.id, first.id, second.id])
def test_main(self): main, = interpreters.list_all() with self.assertRaises(RuntimeError): main.close() def f(): with self.assertRaises(RuntimeError): main.close() t = threading.Thread(target=f) t.start() t.join()
def test_in_thread(self): lock = threading.Lock() interp = None def f(): nonlocal interp interp = interpreters.create() lock.acquire() lock.release() t = threading.Thread(target=f) with lock: t.start() t.join() self.assertIn(interp, interpreters.list_all())
def test_in_thread(self): lock = threading.Lock() id = None interp = interpreters.create() lst = interpreters.list_all() def f(): nonlocal id id = interp.id lock.acquire() lock.release() t = threading.Thread(target=f) with lock: t.start() t.join() self.assertEqual(interp.id, lst[1].id)
def test_in_main(self): interp = interpreters.create() self.assertIsInstance(interp, interpreters.Interpreter) self.assertIn(interp, interpreters.list_all())
def test_in_main(self): interp = interpreters.create() lst = interpreters.list_all() self.assertEqual(interp.id, lst[1].id)
def test_initial(self): interps = interpreters.list_all() self.assertEqual(1, len(interps))