@@ -1205,7 +1205,6 @@ _PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data)
1205
1205
}
1206
1206
1207
1207
// Fill in the blanks and validate the result.
1208
- Py_XINCREF (data -> obj );
1209
1208
data -> interp = interp -> id ;
1210
1209
if (_check_xidata (data ) != 0 ) {
1211
1210
_PyCrossInterpreterData_Release (data );
@@ -1215,6 +1214,40 @@ _PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data)
1215
1214
return 0 ;
1216
1215
}
1217
1216
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
+
1218
1251
void
1219
1252
_PyCrossInterpreterData_Release (_PyCrossInterpreterData * data )
1220
1253
{
@@ -1233,24 +1266,8 @@ _PyCrossInterpreterData_Release(_PyCrossInterpreterData *data)
1233
1266
return ;
1234
1267
}
1235
1268
1236
- PyThreadState * save_tstate = NULL ;
1237
- if (interp != PyThreadState_Get ()-> interp ) {
1238
- // XXX Using the "head" thread isn't strictly correct.
1239
- PyThreadState * tstate = PyInterpreterState_ThreadHead (interp );
1240
- // XXX Possible GILState issues?
1241
- save_tstate = PyThreadState_Swap (tstate );
1242
- }
1243
-
1244
1269
// "Release" the data and/or the object.
1245
- if (data -> free != NULL ) {
1246
- data -> free (data -> data );
1247
- }
1248
- Py_XDECREF (data -> obj );
1249
-
1250
- // Switch back.
1251
- if (save_tstate != NULL ) {
1252
- PyThreadState_Swap (save_tstate );
1253
- }
1270
+ _call_in_interpreter (interp , _release_xidata , data );
1254
1271
}
1255
1272
1256
1273
PyObject *
@@ -1355,6 +1372,7 @@ _bytes_shared(PyObject *obj, _PyCrossInterpreterData *data)
1355
1372
return -1 ;
1356
1373
}
1357
1374
data -> data = (void * )shared ;
1375
+ Py_INCREF (obj );
1358
1376
data -> obj = obj ; // Will be "released" (decref'ed) when data released.
1359
1377
data -> new_object = _new_bytes_object ;
1360
1378
data -> free = PyMem_Free ;
@@ -1382,6 +1400,7 @@ _str_shared(PyObject *obj, _PyCrossInterpreterData *data)
1382
1400
shared -> buffer = PyUnicode_DATA (obj );
1383
1401
shared -> len = PyUnicode_GET_LENGTH (obj ) - 1 ;
1384
1402
data -> data = (void * )shared ;
1403
+ Py_INCREF (obj );
1385
1404
data -> obj = obj ; // Will be "released" (decref'ed) when data released.
1386
1405
data -> new_object = _new_str_object ;
1387
1406
data -> free = PyMem_Free ;
0 commit comments