GH-111438: Add Support for Sharing Floats Between Interpreters (gh-111439)

This only affects users of the APIs in pycore_crossinterp.h (AKA _xxsubinterpretersmodule.c and _xxinterpchannels.c).
This commit is contained in:
Anthony Shaw 2023-10-31 23:17:20 +09:00 committed by GitHub
parent 2904d99839
commit ad6380bc34
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 2 deletions

View file

@ -102,6 +102,7 @@ class IsShareableTests(unittest.TestCase):
'spam', 'spam',
10, 10,
-10, -10,
100.0,
] ]
for obj in shareables: for obj in shareables:
with self.subTest(obj): with self.subTest(obj):
@ -129,7 +130,6 @@ class IsShareableTests(unittest.TestCase):
object, object,
object(), object(),
Exception(), Exception(),
100.0,
# user-defined types and objects # user-defined types and objects
Cheese, Cheese,
Cheese('Wensleydale'), Cheese('Wensleydale'),
@ -189,6 +189,9 @@ class ShareableTypeTests(unittest.TestCase):
with self.assertRaises(OverflowError): with self.assertRaises(OverflowError):
_testinternalcapi.get_crossinterp_data(i) _testinternalcapi.get_crossinterp_data(i)
def test_float(self):
self._assert_values([0.0, 1.1, -1.0, 0.12345678, -0.12345678])
class ModuleTests(TestBase): class ModuleTests(TestBase):

View file

@ -778,6 +778,7 @@ class TestIsShareable(TestBase):
'spam', 'spam',
10, 10,
-10, -10,
100.0,
] ]
for obj in shareables: for obj in shareables:
with self.subTest(obj): with self.subTest(obj):
@ -805,7 +806,6 @@ class TestIsShareable(TestBase):
object, object,
object(), object(),
Exception(), Exception(),
100.0,
# user-defined types and objects # user-defined types and objects
Cheese, Cheese,
Cheese('Wensleydale'), Cheese('Wensleydale'),

View file

@ -0,0 +1 @@
Added support for sharing of float type with interpreters API.

View file

@ -585,6 +585,29 @@ _long_shared(PyThreadState *tstate, PyObject *obj,
return 0; return 0;
} }
static PyObject *
_new_float_object(_PyCrossInterpreterData *data)
{
double * value_ptr = data->data;
return PyFloat_FromDouble(*value_ptr);
}
static int
_float_shared(PyThreadState *tstate, PyObject *obj,
_PyCrossInterpreterData *data)
{
if (_PyCrossInterpreterData_InitWithSize(
data, tstate->interp, sizeof(double), NULL,
_new_float_object
) < 0)
{
return -1;
}
double *shared = (double *)data->data;
*shared = PyFloat_AsDouble(obj);
return 0;
}
static PyObject * static PyObject *
_new_none_object(_PyCrossInterpreterData *data) _new_none_object(_PyCrossInterpreterData *data)
{ {
@ -624,4 +647,9 @@ _register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry)
if (_xidregistry_add_type(xidregistry, &PyUnicode_Type, _str_shared) != 0) { if (_xidregistry_add_type(xidregistry, &PyUnicode_Type, _str_shared) != 0) {
Py_FatalError("could not register str for cross-interpreter sharing"); Py_FatalError("could not register str for cross-interpreter sharing");
} }
// float
if (_xidregistry_add_type(xidregistry, &PyFloat_Type, _float_shared) != 0) {
Py_FatalError("could not register float for cross-interpreter sharing");
}
} }