Skip to content
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 create() and destroy().
  • Loading branch information
ericsnowcurrently committed Dec 5, 2017
commit 457567f7dc0c2a86204466fbb56350fcf5c87ce0
13 changes: 12 additions & 1 deletion Doc/library/_interpreters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,18 @@ support multiple interpreters.
It defines the following functions:


.. XXX TBD
.. function:: create()

Initialize a new Python interpreter and return its identifier. The
interpreter will be created in the current thread and will remain
idle until something is run in it.


.. function:: destroy(id)

Finalize and destroy the identified interpreter.

.. XXX must not be running?


**Caveats:**
Expand Down
157 changes: 156 additions & 1 deletion Lib/test/test__interpreters.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,165 @@
import threading
import unittest

from test import support
interpreters = support.import_module('_interpreters')


class InterpretersTests(unittest.TestCase):
pass

def setUp(self):
self.ids = []
self.lock = threading.Lock()

def tearDown(self):
for id in self.ids:
try:
interpreters.destroy(id)
except RuntimeError:
pass # already destroyed

def _create(self):
id = interpreters.create()
self.ids.append(id)
return id

def test_create_in_main(self):
id = interpreters.create()
self.ids.append(id)

self.assertIn(id, interpreters._enumerate())

def test_create_unique_id(self):
seen = set()
for _ in range(100):
id = self._create()
interpreters.destroy(id)
seen.add(id)

self.assertEqual(len(seen), 100)

def test_create_in_thread(self):
id = None
def f():
nonlocal id
id = interpreters.create()
self.ids.append(id)
self.lock.acquire()
self.lock.release()

t = threading.Thread(target=f)
with self.lock:
t.start()
t.join()
self.assertIn(id, interpreters._enumerate())

@unittest.skip('waiting for run_string()')
def test_create_in_subinterpreter(self):
raise NotImplementedError

@unittest.skip('waiting for run_string()')
def test_create_in_threaded_subinterpreter(self):
raise NotImplementedError

def test_create_after_destroy_all(self):
before = set(interpreters._enumerate())
# Create 3 subinterpreters.
ids = []
for _ in range(3):
id = interpreters.create()
ids.append(id)
# Now destroy them.
for id in ids:
interpreters.destroy(id)
# Finally, create another.
id = interpreters.create()
self.ids.append(id)
self.assertEqual(set(interpreters._enumerate()), before | {id})

def test_create_after_destroy_some(self):
before = set(interpreters._enumerate())
# Create 3 subinterpreters.
id1 = interpreters.create()
id2 = interpreters.create()
self.ids.append(id2)
id3 = interpreters.create()
# Now destroy 2 of them.
interpreters.destroy(id1)
interpreters.destroy(id3)
# Finally, create another.
id = interpreters.create()
self.ids.append(id)
self.assertEqual(set(interpreters._enumerate()), before | {id, id2})

def test_destroy_one(self):
id1 = self._create()
id2 = self._create()
id3 = self._create()
self.assertIn(id2, interpreters._enumerate())
interpreters.destroy(id2)
self.assertNotIn(id2, interpreters._enumerate())
self.assertIn(id1, interpreters._enumerate())
self.assertIn(id3, interpreters._enumerate())

def test_destroy_all(self):
before = set(interpreters._enumerate())
ids = set()
for _ in range(3):
id = self._create()
ids.add(id)
self.assertEqual(set(interpreters._enumerate()), before | ids)
for id in ids:
interpreters.destroy(id)
self.assertEqual(set(interpreters._enumerate()), before)

def test_destroy_main(self):
main, = interpreters._enumerate()
with self.assertRaises(RuntimeError):
interpreters.destroy(main)

def f():
with self.assertRaises(RuntimeError):
interpreters.destroy(main)

t = threading.Thread(target=f)
t.start()
t.join()

def test_destroy_already_destroyed(self):
id = interpreters.create()
interpreters.destroy(id)
with self.assertRaises(RuntimeError):
interpreters.destroy(id)

def test_destroy_does_not_exist(self):
with self.assertRaises(RuntimeError):
interpreters.destroy(1_000_000)

def test_destroy_bad_id(self):
with self.assertRaises(RuntimeError):
interpreters.destroy(-1)

@unittest.skip('waiting for run_string()')
def test_destroy_from_current(self):
raise NotImplementedError

@unittest.skip('waiting for run_string()')
def test_destroy_from_sibling(self):
raise NotImplementedError

def test_destroy_from_other_thread(self):
id = interpreters.create()
self.ids.append(id)
def f():
interpreters.destroy(id)

t = threading.Thread(target=f)
t.start()
t.join()

@unittest.skip('waiting for run_string()')
def test_destroy_still_running(self):
raise NotImplementedError


if __name__ == "__main__":
Expand Down
167 changes: 166 additions & 1 deletion Modules/_interpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,173 @@
#include "Python.h"


static PyObject *
_get_id(PyInterpreterState *interp)
{
unsigned long id = PyInterpreterState_GetID(interp);
if (id == 0 && PyErr_Occurred() != NULL)
return NULL;
return PyLong_FromUnsignedLong(id);
}

static PyInterpreterState *
_look_up(PyObject *requested_id)
{
PyObject * id;
PyInterpreterState *interp;

interp = PyInterpreterState_Head();
while (interp != NULL) {
id = _get_id(interp);
if (id == NULL)
return NULL;
if (requested_id == id)
return interp;
interp = PyInterpreterState_Next(interp);
}

PyErr_Format(PyExc_RuntimeError,
"unrecognized interpreter ID %R", requested_id);
return NULL;
}

static PyObject *
_get_current(void)
{
PyThreadState *tstate;
PyInterpreterState *interp;

tstate = PyThreadState_Get();
if (tstate == NULL)
return NULL;
interp = tstate->interp;

// get ID
return _get_id(interp);
}


/* module level code ********************************************************/

// XXX track count?

static PyObject *
interp_create(PyObject *self, PyObject *args)
{
if (!PyArg_UnpackTuple(args, "create", 0, 0))
return NULL;

// Create and initialize the new interpreter.
PyThreadState *tstate, *save_tstate;
save_tstate = PyThreadState_Swap(NULL);
tstate = Py_NewInterpreter();
PyThreadState_Swap(save_tstate);
if (tstate == NULL) {
/* Since no new thread state was created, there is no exception to
propagate; raise a fresh one after swapping in the old thread
state. */
PyErr_SetString(PyExc_RuntimeError, "interpreter creation failed");
return NULL;
}
return _get_id(tstate->interp);
}

PyDoc_STRVAR(create_doc,
"create() -> ID\n\
\n\
Create a new interpreter and return a unique generated ID.");


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

// Ensure the ID is not the current interpreter.
PyObject *current = _get_current();
if (current == NULL)
return NULL;
if (PyObject_RichCompareBool(id, current, Py_EQ) != 0) {
PyErr_SetString(PyExc_RuntimeError,
"cannot destroy the current interpreter");
return NULL;
}

// Look up the interpreter.
PyInterpreterState *interp = _look_up(id);
if (interp == NULL)
return NULL;

// Destroy the interpreter.
//PyInterpreterState_Delete(interp);
PyThreadState *tstate, *save_tstate;
tstate = PyInterpreterState_ThreadHead(interp); // XXX Is this the right one?
save_tstate = PyThreadState_Swap(tstate);
// XXX Stop current execution?
Py_EndInterpreter(tstate); // XXX Handle possible errors?
PyThreadState_Swap(save_tstate);

Py_RETURN_NONE;
}

PyDoc_STRVAR(destroy_doc,
"destroy(ID)\n\
\n\
Destroy the identified interpreter.\n\
\n\
Attempting to destroy the current interpreter results in a RuntimeError.\n\
So does an unrecognized ID.");


static PyObject *
interp_enumerate(PyObject *self)
{
PyObject *ids, *id;
PyInterpreterState *interp;

// XXX Handle multiple main interpreters.

ids = PyList_New(0);
if (ids == NULL)
return NULL;

interp = PyInterpreterState_Head();
while (interp != NULL) {
id = _get_id(interp);
if (id == NULL)
return NULL;
// insert at front of list
if (PyList_Insert(ids, 0, id) < 0)
return NULL;

interp = PyInterpreterState_Next(interp);
}

return ids;
}

PyDoc_STRVAR(enumerate_doc,
"enumerate() -> [ID]\n\
\n\
Return a list containing the ID of every existing interpreter.");


static PyMethodDef module_functions[] = {
{NULL, NULL} /* sentinel */
{"create", (PyCFunction)interp_create,
METH_VARARGS, create_doc},
{"destroy", (PyCFunction)interp_destroy,
METH_VARARGS, destroy_doc},

{"_enumerate", (PyCFunction)interp_enumerate,
METH_NOARGS, enumerate_doc},

{NULL, NULL} /* sentinel */
};


Expand Down