]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-111438: Add Support for Sharing Floats Between Interpreters (gh-111439)
authorAnthony Shaw <anthony.p.shaw@gmail.com>
Tue, 31 Oct 2023 14:17:20 +0000 (23:17 +0900)
committerGitHub <noreply@github.com>
Tue, 31 Oct 2023 14:17:20 +0000 (08:17 -0600)
This only affects users of the APIs in pycore_crossinterp.h (AKA _xxsubinterpretersmodule.c and _xxinterpchannels.c).

Lib/test/test__xxsubinterpreters.py
Lib/test/test_interpreters.py
Misc/NEWS.d/next/Core and Builtins/2023-10-29-12-33-33.gh-issue-111438.bHTLLl.rst [new file with mode: 0644]
Python/crossinterp.c

index f0b9101f0a20bc568004477b66eaf9e0f4c8d315..5dce3e5f4031c1df50b6618b0f9c0002523b4807 100644 (file)
@@ -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):
 
index e124a7cc7259a290db3ab4eeac4a415bba2286da..7a2f489408b96488a7750a82e96cba10fd864cb2 100644 (file)
@@ -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 (file)
index 0000000..b181977
--- /dev/null
@@ -0,0 +1 @@
+Added support for sharing of float type with interpreters API.
index 74f1d6ecef1329bbb6ce47164e255c4a0367d24e..17c476ba4369c473aa44ad4ba69d556da51db55a 100644 (file)
@@ -585,6 +585,29 @@ _long_shared(PyThreadState *tstate, PyObject *obj,
     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 *
 _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) {
         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");
+    }
 }