8000 bpo-30439: Add some helpful low-level functions for subinterpreters. by ericsnowcurrently · Pull Request #1802 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-30439: Add some helpful low-level functions for subinterpreters. #1802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
8eb5adf
Add the _interpreters module to the stdlib.
ericsnowcurrently Dec 14, 2016
457567f
Add create() and destroy().
ericsnowcurrently Dec 29, 2016
e6af9f3
Finish nearly all the create/destroy tests.
ericsnowcurrently Jan 1, 2017
335967e
Add run_string().
ericsnowcurrently Dec 29, 2016
d2df973
Get tricky tests working.
ericsnowcurrently Jan 2, 2017
b70a496
Add a test for a still running interpreter when main exits.
ericsnowcurrently Jan 2, 2017
6f2d28f
Add run_string_unrestricted().
ericsnowcurrently Jan 4, 2017
80a931b
Exit out of the child process.
ericsnowcurrently Jan 4, 2017
6aa7d9c
Resolve several TODOs.
ericsnowcurrently Jan 4, 2017
083e13c
Set up the execution namespace before switching threads.
ericsnowcurrently Jan 4, 2017
ce081a7
Run in a copy of __main__.
ericsnowcurrently Jan 4, 2017
ec05cf5
Close stdin and stdout after the proc finishes.
ericsnowcurrently Jan 4, 2017
fe90466
Clean up a test.
ericsnowcurrently Jan 4, 2017
9082017
Chain exceptions during cleanup.
ericsnowcurrently Jan 4, 2017
21865d4
Finish the module docs.
ericsnowcurrently Jan 4, 2017
0342a4f
Fix docs.
ericsnowcurrently May 23, 2017
e9d9b04
Fix includes.
ericsnowcurrently Dec 5, 2017
722ae94
Add _interpreters.is_shareable().
ericsnowcurrently Nov 28, 2017
061ae13
Add _PyObject_CheckShareable().
ericsnowcurrently Dec 4, 2017
9a365ec
Add _PyCrossInterpreterData.
ericsnowcurrently Dec 4, 2017
8f299d4
Use the shared data in run() safely.
ericsnowcurrently Dec 7, 2017
92029a1
Do not use a copy of the __main__ ns.
ericsnowcurrently Dec 7, 2017
52c9c2f
Never return the execution namespace.
ericsnowcurrently Dec 8, 2017
a797883
Group sharing-related code.
ericsnowcurrently Dec 8, 2017
777838a
Fix a refcount.
ericsnowcurrently Dec 8, 2017
ab8f175
Add get_current() and enumerate().
ericsnowcurrently Dec 29, 2016
c50c51c
Add is_running().
ericsnowcurrently Dec 29, 2016
271d20d
Add get_main().
ericsnowcurrently Jan 4, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add is_running().
  • Loading branch information
ericsnowcurrently committed Dec 12, 2017
commit c50c51c728d735fe32d661bc6c62a1f971e8f262
6 changes: 6 additions & 0 deletions Doc/library/_interpreters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ It defines the following functions:
Return the ID of the currently running interpreter.


.. function:: is_running(id)

Return whether or not the identified interpreter is currently
running any code.


.. function:: create()

Initialize a new Python interpreter and return its identifier. The
Expand Down
30 changes: 30 additions & 0 deletions Lib/test/test__interpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,36 @@ def test_sub(self):
self.assertEqual(id2, id1)


class IsRunningTests(TestBase):

def test_main_running(self):
main, = interpreters.enumerate()
sub = interpreters.create()
main_running = interpreters.is_running(main)
sub_running = interpreters.is_running(sub)

self.assertTrue(main_running)
self.assertFalse(sub_running)

def test_sub_running(self):
main, = interpreters.enumerate()
sub1 = interpreters.create()
sub2 = interpreters.create()
ns = interpreters.run_string_unrestricted(sub1, dedent(f"""
import _interpreters
main = _interpreters.is_running({main})
sub1 = _interpreters.is_running({sub1})
sub2 = _interpreters.is_running({sub2})
"""))
main_running = ns['main']
sub1_running = ns['sub1']
sub2_running = ns['sub2']

self.assertTrue(main_running)
self.assertTrue(sub1_running)
self.assertFalse(sub2_running)


class CreateTests(TestBase):

def test_in_main(self):
Expand Down
30 changes: 30 additions & 0 deletions Modules/_interpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,34 @@ Return True if the object's data may be shared between interpreters and\n\
False otherwise.");


static PyObject *
interp_is_running(PyObject *self, PyObject *args)
{
PyObject *id;
if (!PyArg_UnpackTuple(args, "is_running", 1, 1, &id))
return NULL;
if (!PyLong_Check(id)) {
PyErr_SetString(PyExc_TypeError, "ID must be an int");
return NULL;
}

PyInterpreterState *interp = _look_up(id);
if (interp == NULL)
return NULL;
int is_running = _is_running(interp);
if (is_running < 0)
return NULL;
if (is_running)
Py_RETURN_TRUE;
Py_RETURN_FALSE;
}

PyDoc_STRVAR(is_running_doc,
"is_running(id) -> bool\n\
\n\
Return whether or not the identified interpreter is running.");


static PyMethodDef module_functions[] = {
{"create", (PyCFunction)interp_create,
METH_VARARGS, create_doc},
Expand All @@ -543,6 +571,8 @@ static PyMethodDef module_functions[] = {
METH_NOARGS, enumerate_doc},
{"get_current", (PyCFunction)interp_get_current,
METH_NOARGS, get_current_doc},
{"is_running", (PyCFunction)interp_is_running,
METH_VARARGS, is_running_doc},

{"run_string", (PyCFunction)interp_run_string,
METH_VARARGS, run_string_doc},
Expand Down
0