Skip to content

Commit ad6380b

Browse files
authored
GH-111438: Add Support for Sharing Floats Between Interpreters (gh-111439)
This only affects users of the APIs in pycore_crossinterp.h (AKA _xxsubinterpretersmodule.c and _xxinterpchannels.c).
1 parent 2904d99 commit ad6380b

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

Lib/test/test__xxsubinterpreters.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def test_default_shareables(self):
102102
'spam',
103103
10,
104104
-10,
105+
100.0,
105106
]
106107
for obj in shareables:
107108
with self.subTest(obj):
@@ -129,7 +130,6 @@ class SubBytes(bytes):
129130
object,
130131
object(),
131132
Exception(),
132-
100.0,
133133
# user-defined types and objects
134134
Cheese,
135135
Cheese('Wensleydale'),
@@ -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_float(self):
193+
self._assert_values([0.0, 1.1, -1.0, 0.12345678, -0.12345678])
194+
192195

193196
class ModuleTests(TestBase):
194197

Lib/test/test_interpreters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,7 @@ def test_default_shareables(self):
778778
'spam',
779779
10,
780780
-10,
781+
100.0,
781782
]
782783
for obj in shareables:
783784
with self.subTest(obj):
@@ -805,7 +806,6 @@ class SubBytes(bytes):
805806
object,
806807
object(),
807808
Exception(),
808-
100.0,
809809
# user-defined types and objects
810810
Cheese,
811811
Cheese('Wensleydale'),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added support for sharing of float type with interpreters API.

Python/crossinterp.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,29 @@ _long_shared(PyThreadState *tstate, PyObject *obj,
585585
return 0;
586586
}
587587

588+
static PyObject *
589+
_new_float_object(_PyCrossInterpreterData *data)
590+
{
591+
double * value_ptr = data->data;
592+
return PyFloat_FromDouble(*value_ptr);
593+
}
594+
595+
static int
596+
_float_shared(PyThreadState *tstate, PyObject *obj,
597+
_PyCrossInterpreterData *data)
598+
{
599+
if (_PyCrossInterpreterData_InitWithSize(
600+
data, tstate->interp, sizeof(double), NULL,
601+
_new_float_object
602+
) < 0)
603+
{
604+
return -1;
605+
}
606+
double *shared = (double *)data->data;
607+
*shared = PyFloat_AsDouble(obj);
608+
return 0;
609+
}
610+
588611
static PyObject *
589612
_new_none_object(_PyCrossInterpreterData *data)
590613
{
@@ -624,4 +647,9 @@ _register_builtins_for_crossinterpreter_data(struct _xidregistry *xidregistry)
624647
if (_xidregistry_add_type(xidregistry, &PyUnicode_Type, _str_shared) != 0) {
625648
Py_FatalError("could not register str for cross-interpreter sharing");
626649
}
650+
651+
// float
652+
if (_xidregistry_add_type(xidregistry, &PyFloat_Type, _float_shared) != 0) {
653+
Py_FatalError("could not register float for cross-interpreter sharing");
654+
}
627655
}

0 commit comments

Comments
 (0)