8000 gh-101524: Split Up the _xxsubinterpreters Module by ericsnowcurrently · Pull Request #101526 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-101524: Split Up the _xxsubinterpreters Module #101526

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
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 8000
Next Next commit
Do not rely on channels in test__xxsubinterpreters.
  • Loading branch information
ericsnowcurrently committed Feb 3, 2023
commit 2afbdc62c4cbd4d78df5aee1ae8a07c2d5706252
22 changes: 6 additions & 16 deletions Lib/test/test__xxsubinterpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import time
import unittest

import _testcapi
from test import support
from test.support import import_helper
from test.support import script_helper
Expand Down Expand Up @@ -144,31 +145,20 @@ class SubBytes(bytes):

class ShareableTypeTests(unittest.TestCase):

# XXX Stop depending on _xxinterpchannels here.
channels = import_helper.import_module('_xxinterpchannels')

def setUp(self):
super().setUp()
self.cid = self.channels.create()

def tearDown(self):
self.channels.destroy(self.cid)
super().tearDown()

def _assert_values(self, values):
for obj in values:
with self.subTest(obj):
self.channels.send(self.cid, obj)
got = self.channels.recv(self.cid)
xid = _testcapi.get_crossinterp_data(obj)
got = _testcapi.restore_crossinterp_data(xid)

self.assertEqual(got, obj)
self.assertIs(type(got), type(obj))

def test_singletons(self):
for obj in [None]:
with self.subTest(obj):
self.channels.send(self.cid, obj)
got = self.channels.recv(self.cid)
xid = _testcapi.get_crossinterp_data(obj)
got = _testcapi.restore_crossinterp_data(xid)

# XXX What about between interpreters?
self.assertIs(got, obj)
Expand Down Expand Up @@ -199,7 +189,7 @@ def test_non_shareable_int(self):
for i in ints:
with self.subTest(i):
with self.assertRaises(OverflowError):
self.channels.send(self.cid, i)
_testcapi.get_crossinterp_data(i)


class ModuleTests(TestBase):
Expand Down
54 changes: 54 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1639,6 +1639,58 @@ run_in_subinterp_with_config(PyObject *self, PyObject *args, PyObject *kwargs)
return PyLong_FromLong(r);
}

static void
_xid_capsule_destructor(PyObject *capsule)
{
_PyCrossInterpreterData *data = \
(_PyCrossInterpreterData *)PyCapsule_GetPointer(capsule, NULL);
if (data != NULL) {
assert(_PyCrossInterpreterData_Release(data) == 0);
PyMem_Free(data);
}
}

static PyObject *
get_crossinterp_data(PyObject *self, PyObject *args)
{
PyObject *obj = NULL;
if (!PyArg_ParseTuple(args, "O:get_crossinterp_data", &obj)) {
return NULL;
}

_PyCrossInterpreterData *data = PyMem_NEW(_PyCrossInterpreterData, 1);
if (data == NULL) {
PyErr_NoMemory();
return NULL;
}
if (_PyObject_GetCrossInterpreterData(obj, data) != 0) {
PyMem_Free(data);
return NULL;
}
PyObject *capsule = PyCapsule_New(data, NULL, _xid_capsule_destructor);
if (capsule == NULL) {
assert(_PyCrossInterpreterData_Release(data) == 0);
PyMem_Free(data);
}
return capsule;
}

static PyObject *
restore_crossinterp_data(PyObject *self, PyObject *args)
{
PyObject *capsule = NULL;
if (!PyArg_ParseTuple(args, "O:restore_crossinterp_data", &capsule)) {
return NULL;
}

_PyCrossInterpreterData *data = \
(_PyCrossInterpreterData *)PyCapsule_GetPointer(capsule, NULL);
if (data == NULL) {
return NULL;
}
return _PyCrossInterpreterData_NewObject(data);
}

static void
slot_tp_del(PyObject *self)
{
Expand Down Expand Up @@ -3306,6 +3358,8 @@ static PyMethodDef TestMethods[] = {
{"run_in_subinterp_with_config",
_PyCFunction_CAST(run_in_subinterp_with_config),
METH_VARARGS | METH_KEYWORDS},
{"get_crossinterp_data", get_crossinterp_data, METH_VARARGS},
{"restore_crossinterp_data", restore_crossinterp_data, METH_VARARGS},
{"with_tp_del", with_tp_del, METH_VARARGS},
{"create_cfunction", create_cfunction, METH_NOARGS},
{"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_VARARGS,
Expand Down
0