8000 gh-134939: Add the interpreters Module by ericsnowcurrently · Pull Request #133958 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-134939: Add the interpreters Module #133958

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

Merged
merged 17 commits into from
Jun 11, 2025
Merged
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
Hide the channels module.
  • Loading branch information
ericsnowcurrently committed Jun 3, 2025
commit eb7f7eb7b5d8a91791567ac0d87f7c295d8428d4
4 changes: 4 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ Doc/howto/clinic.rst @erlend-aasland
Lib/interpreters/ @ericsnowcurrently
Modules/_interp*module.c @ericsnowcurrently
Lib/test/test_interpreters/ @ericsnowcurrently
Lib/test/test__interp*.py @ericsnowcurrently
Lib/test/support/channels.py @ericsnowcurrently
Lib/concurrent/futures/interpreter.py @ericsnowcurrently
Doc/library/interpreters.rst @ericsnowcurrently

# Android
**/*Android* @mhsmith @freakboy3742
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import time
import _interpchannels as _channels
from . import _crossinterp
from interpreters import _crossinterp

# aliases:
from _interpchannels import (
ChannelError, ChannelNotFoundError, ChannelClosedError, # noqa: F401
ChannelEmptyError, ChannelNotEmptyError, # noqa: F401
)
from ._crossinterp import (
from interpreters._crossinterp import (
UNBOUND_ERROR, UNBOUND_REMOVE,
)

Expand Down
14 changes: 7 additions & 7 deletions Lib/test/test_interpreters/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Raise SkipTest if subinterpreters not supported.
_channels = import_helper.import_module('_interpchannels')
import interpreters
from interpreters import channels
from test.support import channels
from .utils import _run_output, TestBase


Expand Down Expand Up @@ -171,7 +171,7 @@ def test_send_recv_main(self):
def test_send_recv_same_interpreter(self):
interp = interpreters.create()
interp.exec(dedent("""
from interpreters import channels
from test.support import channels
r, s = channels.create()
orig = b'spam'
s.send_nowait(orig)
Expand Down Expand Up @@ -244,7 +244,7 @@ def test_send_recv_nowait_main_with_default(self):
def test_send_recv_nowait_same_interpreter(self):
interp = interpreters.create()
interp.exec(dedent("""
from interpreters import channels
from test.support import channels
r, s = channels.create()
orig = b'spam'
s.send_nowait(orig)
Expand Down Expand Up @@ -387,7 +387,7 @@ def common(rch, sch, unbound=None, presize=0):
interp = interpreters.create()

_run_output(interp, dedent(f"""
from interpreters import channels
from test.support import channels
sch = channels.SendChannel({sch.id})
obj1 = b'spam'
obj2 = b'eggs'
8000 Expand Down Expand Up @@ -482,7 +482,7 @@ def test_send_cleared_with_subinterpreter_mixed(self):
self.assertEqual(_channels.get_count(rch.id), 0)

_run_output(interp, dedent(f"""
from interpreters import channels
from test.support import channels
sch = channels.SendChannel({sch.id})
sch.send_nowait(1, unbounditems=channels.UNBOUND)
sch.send_nowait(2, unbounditems=channels.UNBOUND_ERROR)
Expand Down Expand Up @@ -518,15 +518,15 @@ def test_send_cleared_with_subinterpreter_multiple(self):

sch.send_nowait(1)
_run_output(interp1, dedent(f"""
from interpreters import channels
from test.support import channels
rch = channels.RecvChannel({rch.id})
sch = channels.SendChannel({sch.id})
obj1 = rch.recv()
sch.send_nowait(2, unbounditems=channels.UNBOUND)
sch.send_nowait(obj1, unbounditems=channels.UNBOUND_REMOVE)
"""))
_run_output(interp2, dedent(f"""
from interpreters import channels
from test.support import channels
rch = channels.RecvChannel({rch.id})
sch = channels.SendChannel({sch.id})
obj2 = rch.recv()
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2516,12 +2516,12 @@ def setUpClass(cls):
import interpreters
except ModuleNotFoundError:
raise unittest.SkipTest('subinterpreters required')
import interpreters.channels # noqa: F401
import test.support.channels # noqa: F401

@cpython_only
@no_rerun('channels (and queues) might have a refleak; see gh-122199')
def test_static_types_inherited_slots(self):
rch, sch = interpreters.channels.create()
rch, sch = test.support.channels.create()

script = textwrap.dedent("""
import test.support
Expand Down
19 changes: 16 additions & 3 deletions Modules/_interpchannelsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,21 @@ wait_for_lock(PyThread_type_lock mutex, PY_TIMEOUT_T timeout)
return 0;
}

static int
ensure_highlevel_module_loaded(void)
{
PyObject *highlevel = PyImport_ImportModule("interpreters.channels");
if (highlevel == NULL) {
PyErr_Clear();
highlevel = PyImport_ImportModule("test.support.channels");
if (highlevel == NULL) {
return -1;
}
}
Py_DECREF(highlevel);
return 0;
}


/* module state *************************************************************/

Expand Down Expand Up @@ -2742,11 +2757,9 @@ _get_current_channelend_type(int end)
}
if (cls == NULL) {
// Force the module to be loaded, to register the type.
PyObject *highlevel = PyImport_ImportModule("interpreters.channels");
if (highlevel == NULL) {
if (ensure_highlevel_module_loaded() < 0) {
return NULL;
}
Py_DECREF(highlevel);
if (end == CHANNEL_SEND) {
cls = state->send_channel_type;
}
48DF Expand Down
Loading
0