Skip to content

Commit b91d59b

Browse files
committed
wip: replace use of PyCell_GET/SET
1 parent 09c240f commit b91d59b

File tree

3 files changed

+53
-29
lines changed

3 files changed

+53
-29
lines changed

Objects/frameobject.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "pycore_moduleobject.h" // _PyModule_GetDict()
99
#include "pycore_modsupport.h" // _PyArg_CheckPositional()
1010
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
11+
#include "pycore_cell.h" // PyCell_GetRef() PyCell_SetTakeRef()
1112
#include "pycore_opcode_metadata.h" // _PyOpcode_Deopt, _PyOpcode_Caches
1213

1314

@@ -187,11 +188,8 @@ framelocalsproxy_setitem(PyObject *self, PyObject *key, PyObject *value)
187188
}
188189
}
189190
if (cell != NULL) {
190-
PyObject *oldvalue_o = PyCell_GET(cell);
191-
if (value != oldvalue_o) {
192-
PyCell_SET(cell, Py_XNewRef(value));
193-
Py_XDECREF(oldvalue_o);
194-
}
191+
Py_XINCREF(value);
192+
PyCell_SetTakeRef((PyCellObject *)cell, value);
195193
} else if (value != PyStackRef_AsPyObjectBorrow(oldvalue)) {
196194
PyStackRef_XCLOSE(fast[i]);
197195
fast[i] = PyStackRef_FromPyObjectNew(value);
@@ -1987,19 +1985,22 @@ frame_get_var(_PyInterpreterFrame *frame, PyCodeObject *co, int i,
19871985
if (kind & CO_FAST_FREE) {
19881986
// The cell was set by COPY_FREE_VARS.
19891987
assert(value != NULL && PyCell_Check(value));
1990-
value = PyCell_GET(value);
1988+
value = PyCell_GetRef((PyCellObject *)value);
19911989
}
19921990
else if (kind & CO_FAST_CELL) {
19931991
if (value != NULL) {
19941992
if (PyCell_Check(value)) {
19951993
assert(!_PyFrame_IsIncomplete(frame));
1996-
value = PyCell_GET(value);
1994+
value = PyCell_GetRef((PyCellObject *)value);
19971995
}
19981996
// (likely) Otherwise it is an arg (kind & CO_FAST_LOCAL),
19991997
// with the initial value set when the frame was created...
20001998
// (unlikely) ...or it was set via the f_locals proxy.
20011999
}
20022000
}
2001+
else {
2002+
Py_INCREF(value);
2003+
}
20032004
}
20042005
*pvalue = value;
20052006
return 1;
@@ -2076,14 +2077,14 @@ PyFrame_GetVar(PyFrameObject *frame_obj, PyObject *name)
20762077
continue;
20772078
}
20782079

2079-
PyObject *value; // borrowed reference
2080+
PyObject *value;
20802081
if (!frame_get_var(frame, co, i, &value)) {
20812082
break;
20822083
}
20832084
if (value == NULL) {
20842085
break;
20852086
}
2086-
return Py_NewRef(value);
2087+
return value;
20872088
}
20882089

20892090
PyErr_Format(PyExc_NameError, "variable %R does not exist", name);

Objects/typeobject.c

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "pycore_typeobject.h" // struct type_cache
2020
#include "pycore_unionobject.h" // _Py_union_type_or
2121
#include "pycore_weakref.h" // _PyWeakref_GET_REF()
22+
#include "pycore_cell.h" // PyCell_GetRef()
2223
#include "opcode.h" // MAKE_CELL
2324

2425
#include <stddef.h> // ptrdiff_t
@@ -11658,23 +11659,28 @@ super_init_without_args(_PyInterpreterFrame *cframe, PyTypeObject **type_p,
1165811659

1165911660
assert(_PyFrame_GetCode(cframe)->co_nlocalsplus > 0);
1166011661
PyObject *firstarg = PyStackRef_AsPyObjectBorrow(_PyFrame_GetLocalsArray(cframe)[0]);
11662+
if (firstarg == NULL) {
11663+
PyErr_SetString(PyExc_RuntimeError, "super(): arg[0] deleted");
11664+
return -1;
11665+
}
1166111666
// The first argument might be a cell.
11662-
if (firstarg != NULL && (_PyLocals_GetKind(co->co_localspluskinds, 0) & CO_FAST_CELL)) {
11663-
// "firstarg" is a cell here unless (very unlikely) super()
11664-
// was called from the C-API before the first MAKE_CELL op.
11665-
if (_PyInterpreterFrame_LASTI(cframe) >= 0) {
11666-
// MAKE_CELL and COPY_FREE_VARS have no quickened forms, so no need
11667-
// to use _PyOpcode_Deopt here:
11668-
assert(_PyCode_CODE(co)[0].op.code == MAKE_CELL ||
11669-
_PyCode_CODE(co)[0].op.code == COPY_FREE_VARS);
11670-
assert(PyCell_Check(firstarg));
11671-
firstarg = PyCell_GET(firstarg);
11667+
// "firstarg" is a cell here unless (very unlikely) super()
11668+
// was called from the C-API before the first MAKE_CELL op.
11669+
if ((_PyLocals_GetKind(co->co_localspluskinds, 0) & CO_FAST_CELL) &&
11670+
(_PyInterpreterFrame_LASTI(cframe) >= 0)) {
11671+
// MAKE_CELL and COPY_FREE_VARS have no quickened forms, so no need
11672+
// to use _PyOpcode_Deopt here:
11673+
assert(_PyCode_CODE(co)[0].op.code == MAKE_CELL ||
11674+
_PyCode_CODE(co)[0].op.code == COPY_FREE_VARS);
11675+
assert(PyCell_Check(firstarg));
11676+
firstarg = PyCell_GetRef((PyCellObject *)firstarg);
11677+
if (firstarg == NULL) {
11678+
PyErr_SetString(PyExc_RuntimeError, "super(): arg[0] deleted");
11679+
return -1;
1167211680
}
1167311681
}
11674-
if (firstarg == NULL) {
11675-
PyErr_SetString(PyExc_RuntimeError,
11676-
"super(): arg[0] deleted");
11677-
return -1;
11682+
else {
11683+
Py_INCREF(firstarg);
1167811684
}
1167911685

1168011686
// Look for __class__ in the free vars.
@@ -11689,18 +11695,22 @@ super_init_without_args(_PyInterpreterFrame *cframe, PyTypeObject **type_p,
1168911695
if (cell == NULL || !PyCell_Check(cell)) {
1169011696
PyErr_SetString(PyExc_RuntimeError,
1169111697
"super(): bad __class__ cell");
11698+
Py_DECREF(firstarg);
1169211699
return -1;
1169311700
}
11694-
type = (PyTypeObject *) PyCell_GET(cell);
11701+
type = (PyTypeObject *) PyCell_GetRef((PyCellObject *)cell);
1169511702
if (type == NULL) {
1169611703
PyErr_SetString(PyExc_RuntimeError,
1169711704
"super(): empty __class__ cell");
11705+
Py_DECREF(firstarg);
1169811706
return -1;
1169911707
}
1170011708
if (!PyType_Check(type)) {
1170111709
PyErr_Format(PyExc_RuntimeError,
1170211710
"super(): __class__ is not a type (%s)",
1170311711
Py_TYPE(type)->tp_name);
11712+
Py_DECREF(type);
11713+
Py_DECREF(firstarg);
1170411714
return -1;
1170511715
}
1170611716
break;
@@ -11709,6 +11719,7 @@ super_init_without_args(_PyInterpreterFrame *cframe, PyTypeObject **type_p,
1170911719
if (type == NULL) {
1171011720
PyErr_SetString(PyExc_RuntimeError,
1171111721
"super(): __class__ cell not found");
11722+
Py_DECREF(firstarg);
1171211723
return -1;
1171311724
}
1171411725

@@ -11755,16 +11766,23 @@ super_init_impl(PyObject *self, PyTypeObject *type, PyObject *obj) {
1175511766
return -1;
1175611767
}
1175711768
}
11769+
else {
11770+
Py_INCREF(type);
11771+
Py_XINCREF(obj);
11772+
}
1175811773

11759-
if (obj == Py_None)
11774+
if (obj == Py_None) {
11775+
Py_DECREF(obj);
1176011776
obj = NULL;
11777+
}
1176111778
if (obj != NULL) {
1176211779
obj_type = supercheck(type, obj);
11763-
if (obj_type == NULL)
11780+
if (obj_type == NULL) {
11781+
Py_DECREF(obj);
1176411782
return -1;
11765-
Py_INCREF(obj);
11783+
}
1176611784
}
11767-
Py_XSETREF(su->type, (PyTypeObject*)Py_NewRef(type));
11785+
Py_XSETREF(su->type, (PyTypeObject*)type);
1176811786
Py_XSETREF(su->obj, obj);
1176911787
Py_XSETREF(su->obj_type, obj_type);
1177011788
return 0;

Python/bltinmodule.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "pycore_pythonrun.h" // _Py_SourceAsString()
1414
#include "pycore_sysmodule.h" // _PySys_GetAttr()
1515
#include "pycore_tuple.h" // _PyTuple_FromArray()
16+
#include "pycore_cell.h" // PyCell_GetRef()
1617

1718
#include "clinic/bltinmodule.c.h"
1819

@@ -209,7 +210,7 @@ builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
209210
PyObject *margs[3] = {name, bases, ns};
210211
cls = PyObject_VectorcallDict(meta, margs, 3, mkw);
211212
if (cls != NULL && PyType_Check(cls) && PyCell_Check(cell)) {
212-
PyObject *cell_cls = PyCell_GET(cell);
213+
PyObject *cell_cls = PyCell_GetRef((PyCellObject *)cell);
213214
if (cell_cls != cls) {
214215
if (cell_cls == NULL) {
215216
const char *msg =
@@ -221,9 +222,13 @@ builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
221222
"__class__ set to %.200R defining %.200R as %.200R";
222223
PyErr_Format(PyExc_TypeError, msg, cell_cls, name, cls);
223224
}
225+
Py_XDECREF(cell_cls);
224226
Py_SETREF(cls, NULL);
225227
goto error;
226228
}
229+
else {
230+
Py_DECREF(cell_cls);
231+
}
227232
}
228233
}
229234
error:

0 commit comments

Comments
 (0)