Skip to content

Commit 435ce8b

Browse files
Move incref responsibility to getdata funcs.
1 parent 28c15d9 commit 435ce8b

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

Include/internal/pystate.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ struct _xid;
8080

8181
// _PyCrossInterpreterData is similar to Py_buffer as an effectively
8282
// opaque struct that holds data outside the object machinery. This
83-
// is necessary to pass between interpreters in the same process.
83+
// is necessary to pass safely between interpreters in the same process.
8484
typedef struct _xid {
8585
// data is the cross-interpreter-safe derivation of a Python object
8686
// (see _PyObject_GetCrossInterpreterData). It will be NULL if the
@@ -89,8 +89,9 @@ typedef struct _xid {
8989
// obj is the Python object from which the data was derived. This
9090
// is non-NULL only if the data remains bound to the object in some
9191
// way, such that the object must be "released" (via a decref) when
92-
// the data is released. In that case it is automatically
93-
// incref'ed (to match the automatic decref when releaed).
92+
// the data is released. In that case the code that sets the field,
93+
// likely a registered "crossinterpdatafunc", is responsible for
94+
// ensuring it owns the reference (i.e. incref).
9495
PyObject *obj;
9596
// interp is the ID of the owning interpreter of the original
9697
// object. It corresponds to the active interpreter when

Modules/_xxsubinterpretersmodule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,6 +1712,7 @@ _channelid_shared(PyObject *obj, _PyCrossInterpreterData *data)
17121712
xid->resolve = ((channelid *)obj)->resolve;
17131713

17141714
data->data = xid;
1715+
Py_INCREF(obj);
17151716
data->obj = obj;
17161717
data->new_object = _channelid_from_xid;
17171718
data->free = PyMem_Free;

Python/pystate.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,6 @@ _PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data)
12051205
}
12061206

12071207
// Fill in the blanks and validate the result.
1208-
Py_XINCREF(data->obj);
12091208
data->interp = interp->id;
12101209
if (_check_xidata(data) != 0) {
12111210
_PyCrossInterpreterData_Release(data);
@@ -1355,6 +1354,7 @@ _bytes_shared(PyObject *obj, _PyCrossInterpreterData *data)
13551354
return -1;
13561355
}
13571356
data->data = (void *)shared;
1357+
Py_INCREF(obj);
13581358
data->obj = obj; // Will be "released" (decref'ed) when data released.
13591359
data->new_object = _new_bytes_object;
13601360
data->free = PyMem_Free;
@@ -1382,6 +1382,7 @@ _str_shared(PyObject *obj, _PyCrossInterpreterData *data)
13821382
shared->buffer = PyUnicode_DATA(obj);
13831383
shared->len = PyUnicode_GET_LENGTH(obj) - 1;
13841384
data->data = (void *)shared;
1385+
Py_INCREF(obj);
13851386
data->obj = obj; // Will be "released" (decref'ed) when data released.
13861387
data->new_object = _new_str_object;
13871388
data->free = PyMem_Free;

0 commit comments

Comments
 (0)