Skip to content

GH-111435 sharing of bool type in sub interpreters #111436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Lib/test/test__xxsubinterpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ def test_default_shareables(self):
'spam',
10,
-10,
True,
False,
100.0,
]
for obj in shareables:
Expand All @@ -121,8 +123,6 @@ class SubBytes(bytes):

not_shareables = [
# singletons
True,
False,
NotImplemented,
...,
# builtin types and objects
Expand Down Expand Up @@ -189,6 +189,9 @@ def test_non_shareable_int(self):
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])

Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_interpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,8 @@ def test_default_shareables(self):
'spam',
10,
-10,
True,
False,
100.0,
]
for obj in shareables:
Expand All @@ -797,8 +799,6 @@ class SubBytes(bytes):

not_shareables = [
# singletons
True,
False,
NotImplemented,
...,
# builtin types and objects
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added support for sharing of bool type with interpreters API.
25 changes: 25 additions & 0 deletions Python/crossinterp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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");
Expand Down