Beispiel #1
0
 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})
Beispiel #2
0
 def test_send_recv_nowait_same_interpreter(self):
     interp = interpreters.create()
     interp.run(
         dedent("""
         from test.support import interpreters
         r, s = interpreters.create_channel()
         orig = b'spam'
         s.send_nowait(orig)
         obj = r.recv_nowait()
         assert obj == orig, 'expected: obj == orig'
         # When going back to the same interpreter we get the same object.
         assert obj is not orig, 'expected: obj is not orig'
         """))
Beispiel #3
0
 def test_send_recv_same_interpreter(self):
     interp = interpreters.create()
     out = _run_output(
         interp,
         dedent("""
         from test.support import interpreters
         r, s = interpreters.create_channel()
         orig = b'spam'
         s.send(orig)
         obj = r.recv()
         assert obj is not orig
         assert obj == orig
         """))
    def test_in_thread(self):
        interp = interpreters.create()
        script, file = _captured_script('print("it worked!", end="")')
        with file:
            def f():
                interp.run(script)

            t = threading.Thread(target=f)
            t.start()
            t.join()
            out = file.read()

        self.assertEqual(out, 'it worked!')
Beispiel #5
0
    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}))
Beispiel #6
0
    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}))
Beispiel #7
0
    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_fork(self):
        interp = interpreters.create()
        import tempfile
        with tempfile.NamedTemporaryFile('w+') as file:
            file.write('')
            file.flush()

            expected = 'spam spam spam spam spam'
            script = dedent(f"""
                import os
                try:
                    os.fork()
                except RuntimeError:
                    with open('{file.name}', 'w') as out:
                        out.write('{expected}')
                """)
            interp.run(script)

            file.seek(0)
            content = file.read()
            self.assertEqual(content, expected)
    def test_send_recv_nowait_different_interpreters(self):
        r1, s1 = interpreters.create_channel()
        r2, s2 = interpreters.create_channel()
        orig1 = b'spam'
        s1.send_nowait(orig1)
        out = _run_output(
            interpreters.create(),
            dedent(f"""
                obj1 = r.recv_nowait()
                assert obj1 == b'spam', 'expected: obj1 == orig1'
                # When going to another interpreter we get a copy.
                assert id(obj1) != {id(orig1)}, 'expected: obj1 is not orig1'
                orig2 = b'eggs'
                print(id(orig2))
                s.send_nowait(orig2)
                """),
            channels=dict(r=r1, s=s2),
            )
        obj2 = r2.recv_nowait()

        self.assertEqual(obj2, b'eggs')
        self.assertNotEqual(id(obj2), int(out))
Beispiel #10
0
    def test_send_recv_nowait_same_interpreter(self):
        interp = interpreters.create()
        out = _run_output(
            interp,
            dedent("""
            from test.support import interpreters
            r, s = interpreters.create_channel()
            orig = b'spam'
            s.send(orig)
            obj = r.recv_nowait()
            assert obj is not orig
            assert obj == orig
            """))

        r, s = interpreters.create_channel()

        def f():
            while True:
                try:
                    obj = r.recv_nowait()
                    break
                except _interpreters.ChannelEmptyError:
                    time.sleep(0.1)
            s.send(obj)
 def test_bytes_for_script(self):
     interp = interpreters.create()
     with self.assertRaises(TypeError):
         interp.run(b'print("spam")')
 def test_bad_script(self):
     interp = interpreters.create()
     with self.assertRaises(TypeError):
         interp.run(10)
 def test_already_running(self):
     interp = interpreters.create()
     with _running(interp):
         with self.assertRaises(RuntimeError):
             interp.run('print("spam")')
 def test_already_destroyed(self):
     interp = interpreters.create()
     interp.close()
     with self.assertRaises(RuntimeError):
         interp.close()
 def test_equality(self):
     interp1 = interpreters.create()
     interp2 = interpreters.create()
     self.assertEqual(interp1, interp1)
     self.assertNotEqual(interp1, interp2)
 def test_subinterpreter_isolated_explicit(self):
     interp1 = interpreters.create(isolated=True)
     interp2 = interpreters.create(isolated=False)
     self.assertTrue(interp1.isolated)
     self.assertFalse(interp2.isolated)
Beispiel #17
0
 def setUp(self):
     super().setUp()
     self.interp = interpreters.create()
     self._fs = None
 def test_in_main(self):
     interp = interpreters.create()
     self.assertIsInstance(interp, interpreters.Interpreter)
     self.assertIn(interp, interpreters.list_all())
Beispiel #19
0
 def test_in_main(self):
     interp = interpreters.create()
     lst = interpreters.list_all()
     self.assertEqual(interp.id, lst[1].id)
 def f():
     nonlocal interp
     interp = interpreters.create()
     lock.acquire()
     lock.release()
 def test_subinterpreter_isolated_default(self):
     interp = interpreters.create()
     self.assertFalse(interp.isolated)