From ad6380bc340900e0977ce54928b0d3e166c7cf99 Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Tue, 31 Oct 2023 23:17:20 +0900 Subject: 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). --- Lib/test/test__xxsubinterpreters.py | 5 +++- Lib/test/test_interpreters.py | 2 +- .../2023-10-29-12-33-33.gh-issue-111438.bHTLLl.rst | 1 + Python/crossinterp.c | 28 ++++++++++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-10-29-12-33-33.gh-issue-111438.bHTLLl.rst diff --git a/Lib/test/test__xxsubinterpreters.py b/Lib/test/test__xxsubinterpreters.py index f0b9101..5dce3e5 100644 --- a/Lib/test/test__xxsubinterpreters.py +++ b/Lib/test/test__xxsubinterpreters.py @@ -102,6 +102,7 @@ class IsShareableTests(unittest.TestCase): 'spam', 10, -10, + 100.0, ] for obj in shareables: with self.subTest(obj): @@ -129,7 +130,6 @@ class IsShareableTests(unittest.TestCase): object, object(), Exception(), - 100.0, # user-defined types and objects Cheese, Cheese('Wensleydale'), @@ -189,6 +189,9 @@ class ShareableTypeTests(unittest.TestCase): with self.assertRaises(OverflowError): _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): diff --git a/Lib/test/test_interpreters.py b/Lib/test/test_interpreters.py index e124a7c..7a2f489 100644 --- a/Lib/test/test_interpreters.py +++ b/Lib/test/test_interpreters.py @@ -778,6 +778,7 @@ class TestIsShareable(TestBase): 'spam', 10, -10, + 100.0, ] for obj in shareables: with self.subTest(obj): @@ -805,7 +806,6 @@ class TestIsShareable(TestBase): object, object(), Exception(), - 100.0, # user-defined types and objects Cheese, Cheese('Wensleydale'), diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-10-29-12-33-33.gh-issue-111438.bHTLLl.rst b/Misc/NEWS.d/next/Core and Builtins/2023-10-29-12-33-33.gh-issue-111438.bHTLLl.rst new file mode 100644 index 0000000..b181977 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-10-29-12-33-33.gh-issue-111438.bHTLLl.rst @@ -0,0 +1 @@ +Added support for sharing of float type with interpreters API. diff --git a/Python/crossinterp.c b/Python/crossinterp.c index 74f1d6e..17c476b 100644 --- a/Python/crossinterp.c +++ b/Python/crossinterp.c @@ -586,6 +586,29 @@ _long_shared(PyThreadState *tstate, PyObject *obj, } 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 * _new_none_object(_PyCrossInterpreterData *data) { // XXX Singleton refcounts are problematic across interpreters... @@ -624,4 +647,9 @@ _register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry) if (_xidregistry_add_type(xidregistry, &PyUnicode_Type, _str_shared) != 0) { 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"); + } } -- cgit v0.12