8000 gh-76785: Multiple Interpreters in the Stdlib (PEP 554) by nanjekyejoannah · Pull Request #18817 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-76785: Multiple Interpreters in the Stdlib (PEP 554) #18817

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

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
create stdlib interpreters module and _interpreters internal module
  • Loading branch information
nanjekyejoannah committed Sep 10, 2019
commit 331be62729bad0037af12e484fe693685d22495b
9 changes: 9 additions & 0 deletions Doc/library/interpreters.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
:mod:`interpreters` --- High-level Sub-interpreters Module
==========================================================

.. module:: interpreters
:synopsis: High-level Sub-interpreters Module.

**Source code:** :source:`Lib/interpreters.py`

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the module going to be provisional still in 3.9 or is this going to be part of the stable API? If it's going to be provisional, I think it could include a note in the header to explain that.

--------------
Empty file added Lib/interpreters.py
Empty file.
56 changes: 28 additions & 28 deletions Lib/test/test__xxsubinterpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from test.support import script_helper


interpreters = support.import_module('_xxsubinterpreters')
interpreters = support.import_module('_interpreters')


##################################
Expand Down Expand Up @@ -446,7 +446,7 @@ def test_subinterpreter(self):
main = interpreters.get_main()
interp = interpreters.create()
out = _run_output(interp, dedent("""
import _xxsubinterpreters as _interpreters
import _interpreters
cur = _interpreters.get_current()
print(cur)
assert isinstance(cur, _interpreters.InterpreterID)
Expand All @@ -469,7 +469,7 @@ def test_from_subinterpreter(self):
[expected] = interpreters.list_all()
interp = interpreters.create()
out = _run_output(interp, dedent("""
import _xxsubinterpreters as _interpreters
import _interpreters
main = _interpreters.get_main()
print(main)
assert isinstance(main, _interpreters.InterpreterID)
Expand All @@ -495,7 +495,7 @@ def test_subinterpreter(self):
def test_from_subinterpreter(self):
interp = interpreters.create()
out = _run_output(interp, dedent(f"""
import _xxsubinterpreters as _interpreters
import _interpreters
if _interpreters.is_running({interp}):
print(True)
else:
Expand Down Expand Up @@ -616,7 +616,7 @@ def test_in_subinterpreter(self):
main, = interpreters.list_all()
id1 = interpreters.create()
out = _run_output(id1, dedent("""
import _xxsubinterpreters as _interpreters
import _interpreters
id = _interpreters.create()
print(id)
assert isinstance(id, _interpreters.InterpreterID)
Expand All @@ -632,7 +632,7 @@ def test_in_threaded_subinterpreter(self):
def f():
nonlocal id2
out = _run_output(id1, dedent("""
import _xxsubinterpreters as _interpreters
import _interpreters
id = _interpreters.create()
print(id)
"""))
Expand Down Expand Up @@ -726,7 +726,7 @@ def test_from_current(self):
main, = interpreters.list_all()
id = interpreters.create()
script = dedent(f"""
import _xxsubinterpreters as _interpreters
import _interpreters
try:
_interpreters.destroy({id})
except RuntimeError:
Expand All @@ -741,7 +741,7 @@ def test_from_sibling(self):
id1 = interpreters.create()
id2 = interpreters.create()
script = dedent(f"""
import _xxsubinterpreters as _interpreters
import _interpreters
_interpreters.destroy({id2})
""")
interpreters.run_string(id1, script)
Expand Down Expand Up @@ -1057,7 +1057,7 @@ def test_still_running_at_exit(self):
script = dedent(f"""
from textwrap import dedent
import threading
import _xxsubinterpreters as _interpreters
import _interpreters
id = _interpreters.create()
def f():
_interpreters.run_string(id, dedent('''
Expand Down Expand Up @@ -1191,15 +1191,15 @@ def test_sequential_ids(self):
def test_ids_global(self):
id1 = interpreters.create()
out = _run_output(id1, dedent("""
import _xxsubinterpreters as _interpreters
import _interpreters
cid = _interpreters.channel_create()
print(cid)
"""))
cid1 = int(out.strip())

id2 = interpreters.create()
out = _run_output(id2, dedent("""
import _xxsubinterpreters as _interpreters
import _interpreters
cid = _interpreters.channel_create()
print(cid)
"""))
Expand All @@ -1221,7 +1221,7 @@ def test_send_recv_main(self):
def test_send_recv_same_interpreter(self):
id1 = interpreters.create()
out = _run_output(id1, dedent("""
import _xxsubinterpreters as _interpreters
import _interpreters
cid = _interpreters.channel_create()
orig = b'spam'
_interpreters.channel_send(cid, orig)
Expand All @@ -1234,7 +1234,7 @@ def test_send_recv_different_interpreters(self):
cid = interpreters.channel_create()
id1 = interpreters.create()
out = _run_output(id1, dedent(f"""
import _xxsubinterpreters as _interpreters
import _interpreters
_interpreters.channel_send({cid}, b'spam')
"""))
obj = interpreters.channel_recv(cid)
Expand Down Expand Up @@ -1270,7 +1270,7 @@ def f():
nonlocal out
out = _run_output(id1, dedent(f"""
import time
import _xxsubinterpreters as _interpreters
import _interpreters
while True:
try:
obj = _interpreters.channel_recv({cid})
Expand Down Expand Up @@ -1307,7 +1307,7 @@ def test_run_string_arg_unresolved(self):
interp = interpreters.create()

out = _run_output(interp, dedent("""
import _xxsubinterpreters as _interpreters
import _interpreters
print(cid.end)
_interpreters.channel_send(cid, b'spam')
"""),
Expand All @@ -1327,7 +1327,7 @@ def test_run_string_arg_resolved(self):
interp = interpreters.create()

out = _run_output(interp, dedent("""
import _xxsubinterpreters as _interpreters
import _interpreters
print(chan.id.end)
_interpreters.channel_send(chan.id, b'spam')
"""),
Expand Down Expand Up @@ -1355,11 +1355,11 @@ def test_close_multiple_users(self):
id1 = interpreters.create()
id2 = interpreters.create()
interpreters.run_string(id1, dedent(f"""
import _xxsubinterpreters as _interpreters
import _interpreters
_interpreters.channel_send({cid}, b'spam')
"""))
interpreters.run_string(id2, dedent(f"""
import _xxsubinterpreters as _interpreters
import _interpreters
_interpreters.channel_recv({cid})
"""))
interpreters.channel_close(cid)
Expand Down Expand Up @@ -1498,7 +1498,7 @@ def test_close_by_unassociated_interp(self):
interpreters.channel_send(cid, b'spam')
interp = interpreters.create()
interpreters.run_string(interp, dedent(f"""
import _xxsubinterpreters as _interpreters
import _interpreters
_interpreters.channel_close({cid}, force=True)
"""))
with self.assertRaises(interpreters.ChannelClosedError):
Expand Down Expand Up @@ -1579,11 +1579,11 @@ def test_multiple_users(self):
id1 = interpreters.create()
id2 = interpreters.create()
interpreters.run_string(id1, dedent(f"""
import _xxsubinterpreters as _interpreters
import _interpreters
_interpreters.channel_send({cid}, b'spam')
"""))
out = _run_output(id2, dedent(f"""
import _xxsubinterpreters as _interpreters
import _interpreters
obj = _interpreters.channel_recv({cid})
_interpreters.channel_release({cid})
print(repr(obj))
Expand Down Expand Up @@ -1637,7 +1637,7 @@ def test_by_unassociated_interp(self):
interpreters.channel_send(cid, b'spam')
interp = interpreters.create()
interpreters.run_string(interp, dedent(f"""
import _xxsubinterpreters as _interpreters
import _interpreters
_interpreters.channel_release({cid})
"""))
obj = interpreters.channel_recv(cid)
Expand All @@ -1652,7 +1652,7 @@ def test_close_if_unassociated(self):
cid = interpreters.channel_create()
interp = interpreters.create()
interpreters.run_string(interp, dedent(f"""
import _xxsubinterpreters as _interpreters
import _interpreters
obj = _interpreters.channel_send({cid}, b'spam')
_interpreters.channel_release({cid})
"""))
Expand Down Expand Up @@ -1756,12 +1756,12 @@ def _new_channel(self, creator):
else:
ch = interpreters.channel_create()
run_interp(creator.id, f"""
import _xxsubinterpreters
cid = _xxsubinterpreters.channel_create()
import _interpreters
cid = _interpreters.channel_create()
# We purposefully send back an int to avoid tying the
# channel to the other interpreter.
_xxsubinterpreters.channel_send({ch}, int(cid))
del _xxsubinterpreters
_interpreters.channel_send({ch}, int(cid))
del _interpreters
""")
self._cid = interpreters.channel_recv(ch)
return self._cid
Expand All @@ -1788,7 +1788,7 @@ def _prep_interpreter(self, interp):
if interp.name == 'main':
return
run_interp(interp.id, f"""
import _xxsubinterpreters as interpreters
import _interpreters as interpreters
import test.test__xxsubinterpreters as helpers
ChannelState = helpers.ChannelState
try:
Expand Down
18 changes: 9 additions & 9 deletions Modules/_xxsubinterpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ channel_exceptions_init(PyObject *ns)
// XXX Move the exceptions into per-module memory?

// A channel-related operation failed.
ChannelError = PyErr_NewException("_xxsubinterpreters.ChannelError",
ChannelError = PyErr_NewException("_interpreters.ChannelError",
PyExc_RuntimeError, NULL);
if (ChannelError == NULL) {
return -1;
Expand All @@ -304,7 +304,7 @@ channel_exceptions_init(PyObject *ns)

// An operation tried to use a channel that doesn't exist.
ChannelNotFoundError = PyErr_NewException(
"_xxsubinterpreters.ChannelNotFoundError", ChannelError, NULL);
"_interpreters.ChannelNotFoundError", ChannelError, NULL);
if (ChannelNotFoundError == NULL) {
return -1;
}
Expand All @@ -314,7 +314,7 @@ channel_exceptions_init(PyObject *ns)

// An operation tried to use a closed channel.
ChannelClosedError = PyErr_NewException(
"_xxsubinterpreters.ChannelClosedError", ChannelError, NULL);
"_interpreters.ChannelClosedError", ChannelError, NULL);
if (ChannelClosedError == NULL) {
return -1;
}
Expand All @@ -324,7 +324,7 @@ channel_exceptions_init(PyObject *ns)

// An operation tried to pop from an empty channel.
ChannelEmptyError = PyErr_NewException(
"_xxsubinterpreters.ChannelEmptyError", ChannelError, NULL);
"_interpreters.ChannelEmptyError", ChannelError, NULL);
if (ChannelEmptyError == NULL) {
return -1;
}
Expand All @@ -334,7 +334,7 @@ channel_exceptions_init(PyObject *ns)

// An operation tried to close a non-empty channel.
ChannelNotEmptyError = PyErr_NewException(
"_xxsubinterpreters.ChannelNotEmptyError", ChannelError, NULL);
"_interpreters.ChannelNotEmptyError", ChannelError, NULL);
if (ChannelNotEmptyError == NULL) {
return -1;
}
Expand Down Expand Up @@ -1736,7 +1736,7 @@ PyDoc_STRVAR(channelid_doc,

static PyTypeObject ChannelIDtype = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"_xxsubinterpreters.ChannelID", /* tp_name */
"_interpreters.ChannelID", /* tp_name */
sizeof(channelid), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)channelid_dealloc, /* tp_dealloc */
Expand Down Expand Up @@ -1793,7 +1793,7 @@ interp_exceptions_init(PyObject *ns)

if (RunFailedError == NULL) {
// An uncaught exception came out of interp_run_string().
RunFailedError = PyErr_NewException("_xxsubinterpreters.RunFailedError",
RunFailedError = PyErr_NewException("_interpreters.RunFailedError",
PyExc_RuntimeError, NULL);
if (RunFailedError == NULL) {
return -1;
Expand Down Expand Up @@ -2519,7 +2519,7 @@ The 'interpreters' module provides a more convenient interface.");

static struct PyModuleDef interpretersmodule = {
PyModuleDef_HEAD_INIT,
"_xxsubinterpreters", /* m_name */
"_interpreters", /* m_name */
module_doc, /* m_doc */
-1, /* m_size */
module_functions, /* m_methods */
Expand All @@ -2531,7 +2531,7 @@ static struct PyModuleDef interpretersmodule = {


PyMODINIT_FUNC
PyInit__xxsubinterpreters(void)
PyInit__interpreters(void)
{
if (_init_globals() != 0) {
return NULL;
Expand Down
4 changes: 2 additions & 2 deletions PC/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extern PyObject* PyInit__codecs(void);
extern PyObject* PyInit__weakref(void);
/* XXX: These two should really be extracted to standalone extensions. */
extern PyObject* PyInit_xxsubtype(void);
extern PyObject* PyInit__xxsubinterpreters(void);
extern PyObject* PyInit__interpreters(void);
extern PyObject* PyInit__random(void);
extern PyObject* PyInit_itertools(void);
extern PyObject* PyInit__collections(void);
Expand Down Expand Up @@ -133,7 +133,7 @@ struct _inittab _PyImport_Inittab[] = {
{"_json", PyInit__json},

{"xxsubtype", PyInit_xxsubtype},
{"_xxsubinterpreters", PyInit__xxsubinterpreters},
{"_interpreters", PyInit__interpreters},
#ifdef _Py_HAVE_ZLIB
{"zlib", PyInit_zlib},
#endif
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ def detect_simple_extensions(self):
self.add(Extension('syslog', ['syslogmodule.c']))

# Python interface to subinterpreter C-API.
self.add(Extension('_xxsubinterpreters', ['_xxsubinterpretersmodule.c']))
self.add(Extension('_interpreters', ['_xxsubinterpretersmodule.c']))

#
# Here ends the simple stuff. From here on, modules need certain
Expand Down
0