Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Doc/library/concurrent.interpreters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ objects are either directly shared or copied efficiently. For example:
* :class:`str`
* :class:`int`
* :class:`float`
* :class:`complex`
* :class:`tuple` (of similarly supported objects)

There are a small number of Python types that actually share mutable
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test__interpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def test_default_shareables(self):
True,
False,
100.0,
1+2j,
(1, ('spam', 'eggs')),
]
for obj in shareables:
Expand Down Expand Up @@ -603,6 +604,7 @@ def test_shareable_types(self):
'spam',
b'spam',
42,
1+2j,
]
for obj in objects:
with self.subTest(obj):
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_crossinterp.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ def ignore_byteswarning():
-1.0,
0.12345678,
-0.12345678,
# complex
0j,
1+2j,
-1-2j,
]
TUPLE_EXCEPTION = (0, 1.0, EXCEPTION)
TUPLE_OBJECT = (0, 1.0, OBJECT)
Expand Down Expand Up @@ -1326,6 +1330,9 @@ def test_float(self):
-0.12345678,
])

def test_complex(self):
self.assert_roundtrip_equal([0j, 1+2j, -1-2j, 1.5+2.5j])

def test_tuple(self):
self.assert_roundtrip_equal([
(),
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_interpreters/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,7 @@ def test_stateless_func_returns_arg(self):
for arg in [
None,
10,
1+2j,
'spam!',
b'spam!',
(1, 2, 'spam!'),
Expand Down Expand Up @@ -1903,6 +1904,7 @@ def test_default_shareables(self):
True,
False,
100.0,
1+2j,
(),
(1, ('spam', 'eggs'), True),
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support sharing of :class:`complex` types in subinterpreters.
29 changes: 29 additions & 0 deletions Python/crossinterp_data_lookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,30 @@ _float_shared(PyThreadState *tstate, PyObject *obj, _PyXIData_t *xidata)
return 0;
}

// complex

static PyObject *
_new_complex_object(_PyXIData_t *xidata)
{
Py_complex * value_ptr = xidata->data;
return PyComplex_FromCComplex(*value_ptr);
}

static int
_complex_shared(PyThreadState *tstate, PyObject *obj, _PyXIData_t *xidata)
{
if (_PyXIData_InitWithSize(
xidata, tstate->interp, sizeof(Py_complex), NULL,
_new_complex_object
) < 0)
{
return -1;
}
Py_complex *shared = (Py_complex *)xidata->data;
*shared = PyComplex_AsCComplex(obj);
return 0;
}

// None

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

// complex
if (REGISTER(&PyComplex_Type, _complex_shared) != 0) {
Py_FatalError("could not register complex for cross-interpreter sharing");
}

// tuple
if (REGISTER_FALLBACK(&PyTuple_Type, _tuple_shared) != 0) {
Py_FatalError("could not register tuple for cross-interpreter sharing");
Expand Down
Loading