]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-111435: Add Support for Sharing True and False Between Interpreters (gh-111436)
authorAnthony Shaw <anthony.p.shaw@gmail.com>
Thu, 2 Nov 2023 00:09:01 +0000 (09:09 +0900)
committerGitHub <noreply@github.com>
Thu, 2 Nov 2023 00:09:01 +0000 (00:09 +0000)
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-11-35-21.gh-issue-111435.ageUWQ.rst [new file with mode: 0644]
Python/crossinterp.c

index 5dce3e5f4031c1df50b6618b0f9c0002523b4807..ae7dfa19acc519381a1835a7319ad37451dacb6f 100644 (file)
@@ -102,6 +102,8 @@ class IsShareableTests(unittest.TestCase):
                 'spam',
                 10,
                 -10,
+                True,
+                False,
                 100.0,
                 ]
         for obj in shareables:
@@ -121,8 +123,6 @@ class IsShareableTests(unittest.TestCase):
 
         not_shareables = [
                 # singletons
-                True,
-                False,
                 NotImplemented,
                 ...,
                 # builtin types and objects
@@ -189,6 +189,9 @@ class ShareableTypeTests(unittest.TestCase):
                 with self.assertRaises(OverflowError):
                     _testinternalcapi.get_crossinterp_data(i)
 
+    def test_bool(self):
+        self._assert_values([True, False])
+
     def test_float(self):
         self._assert_values([0.0, 1.1, -1.0, 0.12345678, -0.12345678])
 
index 7a2f489408b96488a7750a82e96cba10fd864cb2..74f86088b455905ba63507434780bc65016624b0 100644 (file)
@@ -778,6 +778,8 @@ class TestIsShareable(TestBase):
                 'spam',
                 10,
                 -10,
+                True,
+                False,
                 100.0,
                 ]
         for obj in shareables:
@@ -797,8 +799,6 @@ class TestIsShareable(TestBase):
 
         not_shareables = [
                 # singletons
-                True,
-                False,
                 NotImplemented,
                 ...,
                 # builtin types and objects
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-10-29-11-35-21.gh-issue-111435.ageUWQ.rst b/Misc/NEWS.d/next/Core and Builtins/2023-10-29-11-35-21.gh-issue-111435.ageUWQ.rst
new file mode 100644 (file)
index 0000000..95044dc
--- /dev/null
@@ -0,0 +1 @@
+Added support for sharing of bool type with interpreters API.
index 00eccbdf979504a8fe6cff49328f94d6ad67ff8f..5e3bf4cb6aa238b44c0b0660ee76aec23b163046 100644 (file)
@@ -693,6 +693,26 @@ _none_shared(PyThreadState *tstate, PyObject *obj,
     return 0;
 }
 
+static PyObject *
+_new_bool_object(_PyCrossInterpreterData *data)
+{
+    if (data->data){
+        Py_RETURN_TRUE;
+    }
+    Py_RETURN_FALSE;
+}
+
+static int
+_bool_shared(PyThreadState *tstate, PyObject *obj,
+             _PyCrossInterpreterData *data)
+{
+    _PyCrossInterpreterData_Init(data, tstate->interp,
+            (void *) (Py_IsTrue(obj) ? (uintptr_t) 1 : (uintptr_t) 0), NULL,
+            _new_bool_object);
+    // data->obj and data->free remain NULL
+    return 0;
+}
+
 static void
 _register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry)
 {
@@ -716,6 +736,11 @@ _register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry)
         Py_FatalError("could not register str for cross-interpreter sharing");
     }
 
+    // bool
+    if (_xidregistry_add_type(xidregistry, &PyBool_Type, _bool_shared) != 0) {
+        Py_FatalError("could not register bool 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");