Skip to content

Commit 6740479

Browse files
authored
gh-157047: Support sharing of complex types in subinterpreters (#157051)
* gh-157047: Support sharing of complex types in subinterpreters
1 parent 9d0179d commit 6740479

6 files changed

Lines changed: 42 additions & 0 deletions

File tree

‎Doc/library/concurrent.interpreters.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ objects are either directly shared or copied efficiently. For example:
189189
* :class:`str`
190190
* :class:`int`
191191
* :class:`float`
192+
* :class:`complex`
192193
* :class:`tuple` (of similarly supported objects)
193194

194195
There are a small number of Python types that actually share mutable

‎Lib/test/test__interpreters.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def test_default_shareables(self):
104104
True,
105105
False,
106106
100.0,
107+
1+2j,
107108
(1, ('spam', 'eggs')),
108109
]
109110
for obj in shareables:
@@ -603,6 +604,7 @@ def test_shareable_types(self):
603604
'spam',
604605
b'spam',
605606
42,
607+
1+2j,
606608
]
607609
for obj in objects:
608610
with self.subTest(obj):

‎Lib/test/test_crossinterp.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ def ignore_byteswarning():
124124
-1.0,
125125
0.12345678,
126126
-0.12345678,
127+
# complex
128+
0j,
129+
1+2j,
130+
-1-2j,
127131
]
128132
TUPLE_EXCEPTION = (0, 1.0, EXCEPTION)
129133
TUPLE_OBJECT = (0, 1.0, OBJECT)
@@ -1326,6 +1330,9 @@ def test_float(self):
13261330
-0.12345678,
13271331
])
13281332

1333+
def test_complex(self):
1334+
self.assert_roundtrip_equal([0j, 1+2j, -1-2j, 1.5+2.5j])
1335+
13291336
def test_tuple(self):
13301337
self.assert_roundtrip_equal([
13311338
(),

‎Lib/test/test_interpreters/test_api.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,7 @@ def test_stateless_func_returns_arg(self):
13461346
for arg in [
13471347
None,
13481348
10,
1349+
1+2j,
13491350
'spam!',
13501351
b'spam!',
13511352
(1, 2, 'spam!'),
@@ -1943,6 +1944,7 @@ def test_default_shareables(self):
19431944
True,
19441945
False,
19451946
100.0,
1947+
1+2j,
19461948
(),
19471949
(1, ('spam', 'eggs'), True),
19481950
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added support for sharing of :class:`complex` type with interpreters API.

‎Python/crossinterp_data_lookup.h‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,30 @@ _float_shared(PyThreadState *tstate, PyObject *obj, _PyXIData_t *xidata)
555555
return 0;
556556
}
557557

558+
// complex
559+
560+
static PyObject *
561+
_new_complex_object(_PyXIData_t *xidata)
562+
{
563+
Py_complex *value_ptr = xidata->data;
564+
return PyComplex_FromCComplex(*value_ptr);
565+
}
566+
567+
static int
568+
_complex_shared(PyThreadState *tstate, PyObject *obj, _PyXIData_t *xidata)
569+
{
570+
if (_PyXIData_InitWithSize(
571+
xidata, tstate->interp, sizeof(Py_complex), NULL,
572+
_new_complex_object
573+
) < 0)
574+
{
575+
return -1;
576+
}
577+
Py_complex *shared = (Py_complex *)xidata->data;
578+
*shared = PyComplex_AsCComplex(obj);
579+
return 0;
580+
}
581+
558582
// None
559583

560584
static PyObject *
@@ -825,6 +849,11 @@ _register_builtins_for_crossinterpreter_data(dlregistry_t *xidregistry)
825849
Py_FatalError("could not register float for cross-interpreter sharing");
826850
}
827851

852+
// complex
853+
if (REGISTER(&PyComplex_Type, _complex_shared) != 0) {
854+
Py_FatalError("could not register complex for cross-interpreter sharing");
855+
}
856+
828857
// tuple
829858
if (REGISTER_FALLBACK(&PyTuple_Type, _tuple_shared) != 0) {
830859
Py_FatalError("could not register tuple for cross-interpreter sharing");

0 commit comments

Comments
 (0)