Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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 May 24, 2017
commit f605ae7cae69b0c188274b603ee33405c4b7c0f9
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 @@ -417,6 +417,34 @@ merged into the execution namespace before the code is executed.\n\
See PyRun_SimpleStrings.");


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 @@ -427,6 +455,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