Skip to content

Commit 897f14d

Browse files
authored
gh-89653: PEP 670: Convert PyCell macros to functions (#92653)
Convert the following macros to static inline functions: * PyCell_GET() * PyCell_SET() Limited C API version 3.12 no longer casts arguments. Fix also usage of PyCell_SET(): only delete the old value after setting the new value.
1 parent da5727a commit 897f14d

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

Include/cpython/cellobject.h

+17-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,23 @@ PyAPI_FUNC(PyObject *) PyCell_New(PyObject *);
2121
PyAPI_FUNC(PyObject *) PyCell_Get(PyObject *);
2222
PyAPI_FUNC(int) PyCell_Set(PyObject *, PyObject *);
2323

24-
#define PyCell_GET(op) (((PyCellObject *)(op))->ob_ref)
25-
#define PyCell_SET(op, v) _Py_RVALUE(((PyCellObject *)(op))->ob_ref = (v))
24+
static inline PyObject* PyCell_GET(PyObject *op) {
25+
assert(PyCell_Check(op));
26+
PyCellObject *cell = _Py_CAST(PyCellObject*, op);
27+
return cell->ob_ref;
28+
}
29+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030c0000
30+
# define PyCell_GET(op) PyCell_GET(_PyObject_CAST(op))
31+
#endif
32+
33+
static inline void PyCell_SET(PyObject *op, PyObject *value) {
34+
assert(PyCell_Check(op));
35+
PyCellObject *cell = _Py_CAST(PyCellObject*, op);
36+
cell->ob_ref = value;
37+
}
38+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030c0000
39+
# define PyCell_SET(op, value) PyCell_SET(_PyObject_CAST(op), (value))
40+
#endif
2641

2742
#ifdef __cplusplus
2843
}

Objects/cellobject.c

+7-8
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,21 @@ PyCell_Get(PyObject *op)
5656
PyErr_BadInternalCall();
5757
return NULL;
5858
}
59-
Py_XINCREF(((PyCellObject*)op)->ob_ref);
60-
return PyCell_GET(op);
59+
PyObject *value = PyCell_GET(op);
60+
return Py_XNewRef(value);
6161
}
6262

6363
int
64-
PyCell_Set(PyObject *op, PyObject *obj)
64+
PyCell_Set(PyObject *op, PyObject *value)
6565
{
66-
PyObject* oldobj;
6766
if (!PyCell_Check(op)) {
6867
PyErr_BadInternalCall();
6968
return -1;
7069
}
71-
oldobj = PyCell_GET(op);
72-
Py_XINCREF(obj);
73-
PyCell_SET(op, obj);
74-
Py_XDECREF(oldobj);
70+
PyObject *old_value = PyCell_GET(op);
71+
Py_XINCREF(value);
72+
PyCell_SET(op, value);
73+
Py_XDECREF(old_value);
7574
return 0;
7675
}
7776

Objects/frameobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1086,9 +1086,9 @@ _PyFrame_LocalsToFast(_PyInterpreterFrame *frame, int clear)
10861086
if (cell != NULL) {
10871087
oldvalue = PyCell_GET(cell);
10881088
if (value != oldvalue) {
1089-
Py_XDECREF(oldvalue);
10901089
Py_XINCREF(value);
10911090
PyCell_SET(cell, value);
1091+
Py_XDECREF(oldvalue);
10921092
}
10931093
}
10941094
else if (value != oldvalue) {

0 commit comments

Comments
 (0)