Skip to content

Commit a93c51e

Browse files
authoredFeb 6, 2020
bpo-39573: Use Py_REFCNT() macro (GH-18388)
Replace direct acccess to PyObject.ob_refcnt with usage of the Py_REFCNT() macro.
1 parent 446463f commit a93c51e

9 files changed

+42
-38
lines changed
 

‎Modules/_functoolsmodule.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ functools_reduce(PyObject *self, PyObject *args)
649649
for (;;) {
650650
PyObject *op2;
651651

652-
if (args->ob_refcnt > 1) {
652+
if (Py_REFCNT(args) > 1) {
653653
Py_DECREF(args);
654654
if ((args = PyTuple_New(2)) == NULL)
655655
goto Fail;
@@ -666,7 +666,7 @@ functools_reduce(PyObject *self, PyObject *args)
666666
result = op2;
667667
else {
668668
/* Update the args tuple in-place */
669-
assert(args->ob_refcnt == 1);
669+
assert(Py_REFCNT(args) == 1);
670670
Py_XSETREF(_PyTuple_ITEMS(args)[0], result);
671671
Py_XSETREF(_PyTuple_ITEMS(args)[1], op2);
672672
if ((result = PyObject_Call(func, args, NULL)) == NULL) {

‎Modules/_testcapimodule.c

+9-7
Original file line numberDiff line numberDiff line change
@@ -3550,8 +3550,8 @@ slot_tp_del(PyObject *self)
35503550
PyObject *error_type, *error_value, *error_traceback;
35513551

35523552
/* Temporarily resurrect the object. */
3553-
assert(self->ob_refcnt == 0);
3554-
self->ob_refcnt = 1;
3553+
assert(Py_REFCNT(self) == 0);
3554+
Py_REFCNT(self) = 1;
35553555

35563556
/* Save the current exception, if any. */
35573557
PyErr_Fetch(&error_type, &error_value, &error_traceback);
@@ -3573,17 +3573,19 @@ slot_tp_del(PyObject *self)
35733573
/* Undo the temporary resurrection; can't use DECREF here, it would
35743574
* cause a recursive call.
35753575
*/
3576-
assert(self->ob_refcnt > 0);
3577-
if (--self->ob_refcnt == 0)
3578-
return; /* this is the normal path out */
3576+
assert(Py_REFCNT(self) > 0);
3577+
if (--Py_REFCNT(self) == 0) {
3578+
/* this is the normal path out */
3579+
return;
3580+
}
35793581

35803582
/* __del__ resurrected it! Make it look like the original Py_DECREF
35813583
* never happened.
35823584
*/
35833585
{
3584-
Py_ssize_t refcnt = self->ob_refcnt;
3586+
Py_ssize_t refcnt = Py_REFCNT(self);
35853587
_Py_NewReference(self);
3586-
self->ob_refcnt = refcnt;
3588+
Py_REFCNT(self) = refcnt;
35873589
}
35883590
assert(!PyType_IS_GC(Py_TYPE(self)) || _PyObject_GC_IS_TRACKED(self));
35893591
/* If Py_REF_DEBUG macro is defined, _Py_NewReference() increased

‎Objects/enumobject.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ enum_next_long(enumobject *en, PyObject* next_item)
122122
}
123123
en->en_longindex = stepped_up;
124124

125-
if (result->ob_refcnt == 1) {
125+
if (Py_REFCNT(result) == 1) {
126126
Py_INCREF(result);
127127
old_index = PyTuple_GET_ITEM(result, 0);
128128
old_item = PyTuple_GET_ITEM(result, 1);
@@ -167,7 +167,7 @@ enum_next(enumobject *en)
167167
}
168168
en->en_index++;
169169

170-
if (result->ob_refcnt == 1) {
170+
if (Py_REFCNT(result) == 1) {
171171
Py_INCREF(result);
172172
old_index = PyTuple_GET_ITEM(result, 0);
173173
old_item = PyTuple_GET_ITEM(result, 1);

‎Objects/fileobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ PyFile_GetLine(PyObject *f, int n)
8585
"EOF when reading a line");
8686
}
8787
else if (s[len-1] == '\n') {
88-
if (result->ob_refcnt == 1)
88+
if (Py_REFCNT(result) == 1)
8989
_PyBytes_Resize(&result, len-1);
9090
else {
9191
PyObject *v;

‎Objects/object.c

+18-18
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ _Py_GetRefTotal(void)
5858
Py_ssize_t total = _Py_RefTotal;
5959
o = _PySet_Dummy;
6060
if (o != NULL)
61-
total -= o->ob_refcnt;
61+
total -= Py_REFCNT(o);
6262
return total;
6363
}
6464

@@ -206,32 +206,32 @@ PyObject_CallFinalizer(PyObject *self)
206206
int
207207
PyObject_CallFinalizerFromDealloc(PyObject *self)
208208
{
209-
if (self->ob_refcnt != 0) {
209+
if (Py_REFCNT(self) != 0) {
210210
_PyObject_ASSERT_FAILED_MSG(self,
211211
"PyObject_CallFinalizerFromDealloc called "
212212
"on object with a non-zero refcount");
213213
}
214214

215215
/* Temporarily resurrect the object. */
216-
self->ob_refcnt = 1;
216+
Py_REFCNT(self) = 1;
217217

218218
PyObject_CallFinalizer(self);
219219

220220
_PyObject_ASSERT_WITH_MSG(self,
221-
self->ob_refcnt > 0,
221+
Py_REFCNT(self) > 0,
222222
"refcount is too small");
223223

224224
/* Undo the temporary resurrection; can't use DECREF here, it would
225225
* cause a recursive call. */
226-
if (--self->ob_refcnt == 0) {
226+
if (--Py_REFCNT(self) == 0) {
227227
return 0; /* this is the normal path out */
228228
}
229229

230230
/* tp_finalize resurrected it! Make it look like the original Py_DECREF
231231
* never happened. */
232-
Py_ssize_t refcnt = self->ob_refcnt;
232+
Py_ssize_t refcnt = Py_REFCNT(self);
233233
_Py_NewReference(self);
234-
self->ob_refcnt = refcnt;
234+
Py_REFCNT(self) = refcnt;
235235

236236
_PyObject_ASSERT(self,
237237
(!PyType_IS_GC(Py_TYPE(self))
@@ -263,12 +263,12 @@ PyObject_Print(PyObject *op, FILE *fp, int flags)
263263
Py_END_ALLOW_THREADS
264264
}
265265
else {
266-
if (op->ob_refcnt <= 0) {
266+
if (Py_REFCNT(op) <= 0) {
267267
/* XXX(twouters) cast refcount to long until %zd is
268268
universally available */
269269
Py_BEGIN_ALLOW_THREADS
270270
fprintf(fp, "<refcnt %ld at %p>",
271-
(long)op->ob_refcnt, (void *)op);
271+
(long)Py_REFCNT(op), (void *)op);
272272
Py_END_ALLOW_THREADS
273273
}
274274
else {
@@ -363,7 +363,7 @@ _PyObject_Dump(PyObject* op)
363363
fprintf(stderr, "object address : %p\n", (void *)op);
364364
/* XXX(twouters) cast refcount to long until %zd is
365365
universally available */
366-
fprintf(stderr, "object refcount : %ld\n", (long)op->ob_refcnt);
366+
fprintf(stderr, "object refcount : %ld\n", (long)Py_REFCNT(op));
367367
fflush(stderr);
368368

369369
PyTypeObject *type = Py_TYPE(op);
@@ -1010,7 +1010,7 @@ PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value)
10101010
return err;
10111011
}
10121012
Py_DECREF(name);
1013-
_PyObject_ASSERT(name, name->ob_refcnt >= 1);
1013+
_PyObject_ASSERT(name, Py_REFCNT(name) >= 1);
10141014
if (tp->tp_getattr == NULL && tp->tp_getattro == NULL)
10151015
PyErr_Format(PyExc_TypeError,
10161016
"'%.100s' object has no attributes "
@@ -1829,7 +1829,7 @@ _Py_NewReference(PyObject *op)
18291829
void
18301830
_Py_ForgetReference(PyObject *op)
18311831
{
1832-
if (op->ob_refcnt < 0) {
1832+
if (Py_REFCNT(op) < 0) {
18331833
_PyObject_ASSERT_FAILED_MSG(op, "negative refcnt");
18341834
}
18351835

@@ -1867,7 +1867,7 @@ _Py_PrintReferences(FILE *fp)
18671867
PyObject *op;
18681868
fprintf(fp, "Remaining objects:\n");
18691869
for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
1870-
fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", (void *)op, op->ob_refcnt);
1870+
fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", (void *)op, Py_REFCNT(op));
18711871
if (PyObject_Print(op, fp, 0) != 0)
18721872
PyErr_Clear();
18731873
putc('\n', fp);
@@ -1884,7 +1884,7 @@ _Py_PrintReferenceAddresses(FILE *fp)
18841884
fprintf(fp, "Remaining object addresses:\n");
18851885
for (op = refchain._ob_next; op != &refchain; op = op->_ob_next)
18861886
fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", (void *)op,
1887-
op->ob_refcnt, Py_TYPE(op)->tp_name);
1887+
Py_REFCNT(op), Py_TYPE(op)->tp_name);
18881888
}
18891889

18901890
PyObject *
@@ -2025,7 +2025,7 @@ _PyTrash_deposit_object(PyObject *op)
20252025

20262026
_PyObject_ASSERT(op, PyObject_IS_GC(op));
20272027
_PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
2028-
_PyObject_ASSERT(op, op->ob_refcnt == 0);
2028+
_PyObject_ASSERT(op, Py_REFCNT(op) == 0);
20292029
_PyGCHead_SET_PREV(_Py_AS_GC(op), gcstate->trash_delete_later);
20302030
gcstate->trash_delete_later = op;
20312031
}
@@ -2037,7 +2037,7 @@ _PyTrash_thread_deposit_object(PyObject *op)
20372037
PyThreadState *tstate = _PyThreadState_GET();
20382038
_PyObject_ASSERT(op, PyObject_IS_GC(op));
20392039
_PyObject_ASSERT(op, !_PyObject_GC_IS_TRACKED(op));
2040-
_PyObject_ASSERT(op, op->ob_refcnt == 0);
2040+
_PyObject_ASSERT(op, Py_REFCNT(op) == 0);
20412041
_PyGCHead_SET_PREV(_Py_AS_GC(op), tstate->trash_delete_later);
20422042
tstate->trash_delete_later = op;
20432043
}
@@ -2064,7 +2064,7 @@ _PyTrash_destroy_chain(void)
20642064
* assorted non-release builds calling Py_DECREF again ends
20652065
* up distorting allocation statistics.
20662066
*/
2067-
_PyObject_ASSERT(op, op->ob_refcnt == 0);
2067+
_PyObject_ASSERT(op, Py_REFCNT(op) == 0);
20682068
++gcstate->trash_delete_nesting;
20692069
(*dealloc)(op);
20702070
--gcstate->trash_delete_nesting;
@@ -2102,7 +2102,7 @@ _PyTrash_thread_destroy_chain(void)
21022102
* assorted non-release builds calling Py_DECREF again ends
21032103
* up distorting allocation statistics.
21042104
*/
2105-
_PyObject_ASSERT(op, op->ob_refcnt == 0);
2105+
_PyObject_ASSERT(op, Py_REFCNT(op) == 0);
21062106
(*dealloc)(op);
21072107
assert(tstate->trash_delete_nesting == 1);
21082108
}

‎Objects/tupleobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ int
153153
PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
154154
{
155155
PyObject **p;
156-
if (!PyTuple_Check(op) || op->ob_refcnt != 1) {
156+
if (!PyTuple_Check(op) || Py_REFCNT(op) != 1) {
157157
Py_XDECREF(newitem);
158158
PyErr_BadInternalCall();
159159
return -1;

‎Objects/typeobject.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -1173,8 +1173,9 @@ subtype_dealloc(PyObject *self)
11731173
}
11741174
if (type->tp_del) {
11751175
type->tp_del(self);
1176-
if (self->ob_refcnt > 0)
1176+
if (Py_REFCNT(self) > 0) {
11771177
return;
1178+
}
11781179
}
11791180

11801181
/* Find the nearest base with a different tp_dealloc */
@@ -1239,7 +1240,7 @@ subtype_dealloc(PyObject *self)
12391240
if (type->tp_del) {
12401241
_PyObject_GC_TRACK(self);
12411242
type->tp_del(self);
1242-
if (self->ob_refcnt > 0) {
1243+
if (Py_REFCNT(self) > 0) {
12431244
/* Resurrected */
12441245
goto endlabel;
12451246
}

‎Objects/weakrefobject.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,8 @@ PyObject_ClearWeakRefs(PyObject *object)
952952

953953
if (object == NULL
954954
|| !PyType_SUPPORTS_WEAKREFS(Py_TYPE(object))
955-
|| object->ob_refcnt != 0) {
955+
|| Py_REFCNT(object) != 0)
956+
{
956957
PyErr_BadInternalCall();
957958
return;
958959
}
@@ -975,8 +976,9 @@ PyObject_ClearWeakRefs(PyObject *object)
975976
current->wr_callback = NULL;
976977
clear_weakref(current);
977978
if (callback != NULL) {
978-
if (((PyObject *)current)->ob_refcnt > 0)
979+
if (Py_REFCNT((PyObject *)current) > 0) {
979980
handle_callback(current, callback);
981+
}
980982
Py_DECREF(callback);
981983
}
982984
}
@@ -993,8 +995,7 @@ PyObject_ClearWeakRefs(PyObject *object)
993995
for (i = 0; i < count; ++i) {
994996
PyWeakReference *next = current->wr_next;
995997

996-
if (((PyObject *)current)->ob_refcnt > 0)
997-
{
998+
if (Py_REFCNT((PyObject *)current) > 0) {
998999
Py_INCREF(current);
9991000
PyTuple_SET_ITEM(tuple, i * 2, (PyObject *) current);
10001001
PyTuple_SET_ITEM(tuple, i * 2 + 1, current->wr_callback);

‎Python/sysmodule.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1656,7 +1656,7 @@ static Py_ssize_t
16561656
sys_getrefcount_impl(PyObject *module, PyObject *object)
16571657
/*[clinic end generated code: output=5fd477f2264b85b2 input=bf474efd50a21535]*/
16581658
{
1659-
return object->ob_refcnt;
1659+
return Py_REFCNT(object);
16601660
}
16611661

16621662
#ifdef Py_REF_DEBUG

0 commit comments

Comments
 (0)
Please sign in to comment.