Skip to content

Commit

Permalink
pythongh-76785: Support Running Some Functions in Subinterpreters (py…
Browse files Browse the repository at this point in the history
…thongh-110251)

This specifically refers to `test.support.interpreters.Interpreter.run()`.
  • Loading branch information
ericsnowcurrently authored and Glyphack committed Jan 27, 2024
1 parent 56a89cb commit b0dcb10
Show file tree
Hide file tree
Showing 3 changed files with 439 additions and 26 deletions.
18 changes: 16 additions & 2 deletions Lib/test/support/interpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,26 @@ def close(self):
"""
return _interpreters.destroy(self._id)

# XXX Rename "run" to "exec"?
def run(self, src_str, /, *, channels=None):
"""Run the given source code in the interpreter.
This blocks the current Python thread until done.
This is essentially the same as calling the builtin "exec"
with this interpreter, using the __dict__ of its __main__
module as both globals and locals.
There is no return value.
If the code raises an unhandled exception then a RunFailedError
is raised, which summarizes the unhandled exception. The actual
exception is discarded because objects cannot be shared between
interpreters.
This blocks the current Python thread until done. During
that time, the previous interpreter is allowed to run
in other threads.
"""
_interpreters.run_string(self._id, src_str, channels)
_interpreters.exec(self._id, src_str, channels)


def create_channel():
Expand Down
105 changes: 105 additions & 0 deletions Lib/test/test__xxsubinterpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,5 +925,110 @@ def f():
self.assertEqual(retcode, 0)


class RunFuncTests(TestBase):

def setUp(self):
super().setUp()
self.id = interpreters.create()

def test_success(self):
r, w = os.pipe()
def script():
global w
import contextlib
with open(w, 'w', encoding="utf-8") as spipe:
with contextlib.redirect_stdout(spipe):
print('it worked!', end='')
interpreters.run_func(self.id, script, shared=dict(w=w))

with open(r, encoding="utf-8") as outfile:
out = outfile.read()

self.assertEqual(out, 'it worked!')

def test_in_thread(self):
r, w = os.pipe()
def script():
global w
import contextlib
with open(w, 'w', encoding="utf-8") as spipe:
with contextlib.redirect_stdout(spipe):
print('it worked!', end='')
def f():
interpreters.run_func(self.id, script, shared=dict(w=w))
t = threading.Thread(target=f)
t.start()
t.join()

with open(r, encoding="utf-8") as outfile:
out = outfile.read()

self.assertEqual(out, 'it worked!')

def test_code_object(self):
r, w = os.pipe()

def script():
global w
import contextlib
with open(w, 'w', encoding="utf-8") as spipe:
with contextlib.redirect_stdout(spipe):
print('it worked!', end='')
code = script.__code__
interpreters.run_func(self.id, code, shared=dict(w=w))

with open(r, encoding="utf-8") as outfile:
out = outfile.read()

self.assertEqual(out, 'it worked!')

def test_closure(self):
spam = True
def script():
assert spam

with self.assertRaises(ValueError):
interpreters.run_func(self.id, script)

# XXX This hasn't been fixed yet.
@unittest.expectedFailure
def test_return_value(self):
def script():
return 'spam'
with self.assertRaises(ValueError):
interpreters.run_func(self.id, script)

def test_args(self):
with self.subTest('args'):
def script(a, b=0):
assert a == b
with self.assertRaises(ValueError):
interpreters.run_func(self.id, script)

with self.subTest('*args'):
def script(*args):
assert not args
with self.assertRaises(ValueError):
interpreters.run_func(self.id, script)

with self.subTest('**kwargs'):
def script(**kwargs):
assert not kwargs
with self.assertRaises(ValueError):
interpreters.run_func(self.id, script)

with self.subTest('kwonly'):
def script(*, spam=True):
assert spam
with self.assertRaises(ValueError):
interpreters.run_func(self.id, script)

with self.subTest('posonly'):
def script(spam, /):
assert spam
with self.assertRaises(ValueError):
interpreters.run_func(self.id, script)


if __name__ == '__main__':
unittest.main()
Loading

0 comments on commit b0dcb10

Please sign in to comment.