Skip to content

Commit ea22ea3

Browse files
Factor out _call_in_interpreter().
1 parent 435ce8b commit ea22ea3

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

Python/pystate.c

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,40 @@ _PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data)
12141214
return 0;
12151215
}
12161216

1217+
static void
1218+
_release_xidata(void *arg)
1219+
{
1220+
_PyCrossInterpreterData *data = (_PyCrossInterpreterData *)arg;
1221+
if (data->free != NULL) {
1222+
data->free(data->data);
1223+
}
1224+
Py_XDECREF(data->obj);
1225+
}
1226+
1227+
static void
1228+
_call_in_interpreter(PyInterpreterState *interp,
1229+
void (*func)(void *), void *arg)
1230+
{
1231+
/* We would use Py_AddPendingCall() if it weren't specific to the
1232+
* main interpreter (see bpo-33608). In the meantime we take a
1233+
* naive approach.
1234+
*/
1235+
PyThreadState *save_tstate = NULL;
1236+
if (interp != PyThreadState_Get()->interp) {
1237+
// XXX Using the "head" thread isn't strictly correct.
1238+
PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
1239+
// XXX Possible GILState issues?
1240+
save_tstate = PyThreadState_Swap(tstate);
1241+
}
1242+
1243+
func(arg);
1244+
1245+
// Switch back.
1246+
if (save_tstate != NULL) {
1247+
PyThreadState_Swap(save_tstate);
1248+
}
1249+
}
1250+
12171251
void
12181252
_PyCrossInterpreterData_Release(_PyCrossInterpreterData *data)
12191253
{
@@ -1232,24 +1266,8 @@ _PyCrossInterpreterData_Release(_PyCrossInterpreterData *data)
12321266
return;
12331267
}
12341268

1235-
PyThreadState *save_tstate = NULL;
1236-
if (interp != PyThreadState_Get()->interp) {
1237-
// XXX Using the "head" thread isn't strictly correct.
1238-
PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
1239-
// XXX Possible GILState issues?
1240-
save_tstate = PyThreadState_Swap(tstate);
1241-
}
1242-
12431269
// "Release" the data and/or the object.
1244-
if (data->free != NULL) {
1245-
data->free(data->data);
1246-
}
1247-
Py_XDECREF(data->obj);
1248-
1249-
// Switch back.
1250-
if (save_tstate != NULL) {
1251-
PyThreadState_Swap(save_tstate);
1252-
}
1270+
_call_in_interpreter(interp, _release_xidata, data);
12531271
}
12541272

12551273
PyObject *

0 commit comments

Comments
 (0)