Skip to content

bpo-30439: Add a basic high-level interpreters module. #1803

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
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 May 23, 2017
commit 84920ad6558c7fad4ba84b9f6eb7c330f95f8560
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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy