8000 Do not rely on channels in test__xxsubinterpreters. · python/cpython@2afbdc6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2afbdc6

Browse files
Do not rely on channels in test__xxsubinterpreters.
1 parent b21ed6a commit 2afbdc6

File tree

2 files changed

+60
-16
lines changed

2 files changed

+60
-16
lines changed

Lib/test/test__xxsubinterpreters.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import time
1010
import unittest
1111

12+
import _testcapi
1213
from test import support
1314
from test.support import import_helper
1415
from test.support import script_helper
@@ -144,31 +145,20 @@ class SubBytes(bytes):
144145

145146
class ShareableTypeTests(unittest.TestCase):
146147

147-
# XXX Stop depending on _xxinterpchannels here.
148-
channels = import_helper.import_module('_xxinterpchannels')
149-
150-
def setUp(self):
151-
super().setUp()
152-
self.cid = self.channels.create()
153-
154-
def tearDown(self):
155-
self.channels.destroy(self.cid)
156-
super().tearDown()
157-
158148
def _assert_values(self, values):
159149
for obj in values:
160150
with self.subTest(obj):
161-
self.channels.send(self.cid, obj)
162-
got = self.channels.recv(self.cid)
151+
xid = _testcapi.get_crossinterp_data(obj)
152+
got = _testcapi.restore_crossinterp_data(xid)
163153

164154
self.assertEqual(got, obj)
165155
self.assertIs(type(got), type(obj))
166156

167157
def test_singletons(self):
168158
for obj in [None]:
169159
with self.subTest(obj):
170-
self.channels.send(self.cid, obj)
171-
got = self.channels.recv(self.cid)
160+
xid = _testcapi.get_crossinterp_data(obj)
161+
got = _testcapi.restore_crossinterp_data(xid)
172162

173163
# XXX What about between interpreters?
174164
self.assertIs(got, obj)
@@ -199,7 +189,7 @@ def test_non_shareable_int(self):
199189
for i in ints:
200190
with self.subTest(i):
201191
with self.assertRaises(OverflowError):
202-
self.channels.send(self.cid, i)
192+
_testcapi.get_crossinterp_data(i)
203193

204194

205195
class ModuleTests(TestBase):

Modules/_testcapimodule.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,58 @@ run_in_subinterp_with_config(PyObject *self, PyObject *args, PyObject *kwargs)
16391639
return PyLong_FromLong(r);
16401640
}
16411641

1642+
static void
1643+
_xid_capsule_destructor(PyObject *capsule)
1644+
{
1645+
_PyCrossInterpreterData *data = \
1646+
(_PyCrossInterpreterData *)PyCapsule_GetPointer(capsule, NULL);
1647+
if (data != NULL) {
1648+
assert(_PyCrossInterpreterData_Release(data) == 0);
1649+
PyMem_Free(data);
1650+
}
1651+
}
1652+
1653+
static PyObject *
1654+
get_crossinterp_data(PyObject *self, PyObject *args)
1655+
{
1656+
PyObject *obj = NULL;
1657+
if (!PyArg_ParseTuple(args, "O:get_crossinterp_data", &obj)) {
1658+
return NULL;
1659+
}
1660+
1661+
_PyCrossInterpreterData *data = PyMem_NEW(_PyCrossInterpreterData, 1);
1662+
if (data == NULL) {
1663+
PyErr_NoMemory();
1664+
return NULL;
1665+
}
1666+
if (_PyObject_GetCrossInterpreterData(obj, data) != 0) {
1667+
PyMem_Free(data);
1668+
return NULL;
1669+
}
1670+
PyObject *capsule = PyCapsule_New(data, NULL, _xid_capsule_destructor);
1671+
if (capsule == NULL) {
1672+
assert(_PyCrossInterpreterData_Release(data) == 0);
1673+
PyMem_Free(data);
1674+
}
1675+
return capsule;
1676+
}
1677+
1678+
static PyObject *
1679+
restore_crossinterp_data(PyObject *self, PyObject *args)
1680+
{
1681+
PyObject *capsule = NULL;
1682+
if (!PyArg_ParseTuple(args, "O:restore_crossinterp_data", &capsule)) {
1683+
return NULL;
1684+
}
1685+
1686+
_PyCrossInterpreterData *data = \
1687+
(_PyCrossInterpreterData *)PyCapsule_GetPointer(capsule, NULL);
1688+
if (data == NULL) {
1689+
return NULL;
1690+
}
1691+
return _PyCrossInterpreterData_NewObject(data);
1692+
}
1693+
16421694
static void
16431695
slot_tp_del(PyObject *self)
16441696
{
@@ -3306,6 +3358,8 @@ static PyMethodDef TestMethods[] = {
33063358
{"run_in_subinterp_with_config",
33073359
_PyCFunction_CAST(run_in_subinterp_with_config),
33083360
METH_VARARGS | METH_KEYWORDS},
3361+
{"get_crossinterp_data", get_crossinterp_data, METH_VARARGS},
3362+
{"restore_crossinterp_data", restore_crossinterp_data, METH_VARARGS},
33093363
{"with_tp_del", with_tp_del, METH_VARARGS},
33103364
{"create_cfunction", create_cfunction, METH_NOARGS},
33113365
{"call_in_temporary_c_thread", call_in_temporary_c_thread, METH_VARARGS,

0 commit comments

Comments
 (0)
0