Skip to content

Commit

Permalink
add support for sharing floats
Browse files Browse the repository at this point in the history
  • Loading branch information
tonybaloney committed Oct 29, 2023
1 parent 66bea25 commit 9a6041b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Lib/test/test__xxsubinterpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def test_default_shareables(self):
'spam',
10,
-10,
100.0,
]
for obj in shareables:
with self.subTest(obj):
Expand Down Expand Up @@ -129,7 +130,6 @@ class SubBytes(bytes):
object,
object(),
Exception(),
100.0,
# user-defined types and objects
Cheese,
Cheese('Wensleydale'),
Expand Down Expand Up @@ -189,6 +189,9 @@ def test_non_shareable_int(self):
with self.assertRaises(OverflowError):
_testcapi.get_crossinterp_data(i)

def test_float(self):
self._assert_values([0.0, 1.1, -1.0, 0.12345678, -0.12345678])


class ModuleTests(TestBase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added support for sharing of float type with interpreters API.
28 changes: 28 additions & 0 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -2933,6 +2933,29 @@ _str_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), obj,
_new_float_object
) < 0)
{
return -1;
}
double * shared = (double*)data->data;
*shared = PyFloat_AsDouble(obj);
return 0;
}

static PyObject *
_new_long_object(_PyCrossInterpreterData *data)
{
Expand Down Expand Up @@ -2999,6 +3022,11 @@ _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");
}
}


Expand Down

0 comments on commit 9a6041b

Please sign in to comment.