Skip to content

Commit 230e8e9

Browse files
authored
GH-111435: Add Support for Sharing True and False Between Interpreters (gh-111436)
This only affects users of the APIs in pycore_crossinterp.h (AKA _xxsubinterpretersmodule.c and _xxinterpchannels.c).
1 parent 9322ce9 commit 230e8e9

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

Lib/test/test__xxsubinterpreters.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ def test_default_shareables(self):
102102
'spam',
103103
10,
104104
-10,
105+
True,
106+
False,
105107
100.0,
106108
]
107109
for obj in shareables:
@@ -121,8 +123,6 @@ class SubBytes(bytes):
121123

122124
not_shareables = [
123125
# singletons
124-
True,
125-
False,
126126
NotImplemented,
127127
...,
128128
# builtin types and objects
@@ -189,6 +189,9 @@ def test_non_shareable_int(self):
189189
with self.assertRaises(OverflowError):
190190
_testinternalcapi.get_crossinterp_data(i)
191191

192+
def test_bool(self):
193+
self._assert_values([True, False])
194+
192195
def test_float(self):
193196
self._assert_values([0.0, 1.1, -1.0, 0.12345678, -0.12345678])
194197

Lib/test/test_interpreters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,8 @@ def test_default_shareables(self):
778778
'spam',
779779
10,
780780
-10,
781+
True,
782+
False,
781783
100.0,
782784
]
783785
for obj in shareables:
@@ -797,8 +799,6 @@ class SubBytes(bytes):
797799

798800
not_shareables = [
799801
# singletons
800-
True,
801-
False,
802802
NotImplemented,
803803
...,
804804
# builtin types and objects
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added support for sharing of bool type with interpreters API.

Python/crossinterp.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,26 @@ _none_shared(PyThreadState *tstate, PyObject *obj,
693693
return 0;
694694
}
695695

696+
static PyObject *
697+
_new_bool_object(_PyCrossInterpreterData *data)
698+
{
699+
if (data->data){
700+
Py_RETURN_TRUE;
701+
}
702+
Py_RETURN_FALSE;
703+
}
704+
705+
static int
706+
_bool_shared(PyThreadState *tstate, PyObject *obj,
707+
_PyCrossInterpreterData *data)
708+
{
709+
_PyCrossInterpreterData_Init(data, tstate->interp,
710+
(void *) (Py_IsTrue(obj) ? (uintptr_t) 1 : (uintptr_t) 0), NULL,
711+
_new_bool_object);
712+
// data->obj and data->free remain NULL
713+
return 0;
714+
}
715+
696716
static void
697717
_register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry)
698718
{
@@ -716,6 +736,11 @@ _register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry)
716736
Py_FatalError("could not register str for cross-interpreter sharing");
717737
}
718738

739+
// bool
740+
if (_xidregistry_add_type(xidregistry, &PyBool_Type, _bool_shared) != 0) {
741+
Py_FatalError("could not register bool for cross-interpreter sharing");
742+
}
743+
719744
// float
720745
if (_xidregistry_add_type(xidregistry, &PyFloat_Type, _float_shared) != 0) {
721746
Py_FatalError("could not register float for cross-interpreter sharing");

0 commit comments

Comments
 (0)