Skip to content

Commit 5af0167

Browse files
Remove unused refcounts in singletons within CPython/Objects
1 parent f7287b2 commit 5af0167

26 files changed

+33
-96
lines changed

Include/object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
671671
#define Py_NotImplemented (&_Py_NotImplementedStruct)
672672

673673
/* Macro for returning Py_NotImplemented from a function */
674-
#define Py_RETURN_NOTIMPLEMENTED return Py_NewRef(Py_NotImplemented)
674+
#define Py_RETURN_NOTIMPLEMENTED return Py_NotImplemented
675675

676676
/* Rich comparison opcodes */
677677
#define Py_LT 0

Objects/abstract.c

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
124124
return -1;
125125
}
126126
else if (result == Py_NotImplemented) {
127-
Py_DECREF(result);
128127
return defaultvalue;
129128
}
130129
if (!PyLong_Check(result)) {
@@ -883,23 +882,20 @@ binary_op1(PyObject *v, PyObject *w, const int op_slot
883882
x = slotw(v, w);
884883
if (x != Py_NotImplemented)
885884
return x;
886-
Py_DECREF(x); /* can't do it */
887885
slotw = NULL;
888886
}
889887
x = slotv(v, w);
890888
assert(_Py_CheckSlotResult(v, op_name, x != NULL));
891889
if (x != Py_NotImplemented) {
892890
return x;
893891
}
894-
Py_DECREF(x); /* can't do it */
895892
}
896893
if (slotw) {
897894
PyObject *x = slotw(v, w);
898895
assert(_Py_CheckSlotResult(w, op_name, x != NULL));
899896
if (x != Py_NotImplemented) {
900897
return x;
901898
}
902-
Py_DECREF(x); /* can't do it */
903899
}
904900
Py_RETURN_NOTIMPLEMENTED;
905901
}
@@ -927,8 +923,6 @@ binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
927923
{
928924
PyObject *result = BINARY_OP1(v, w, op_slot, op_name);
929925
if (result == Py_NotImplemented) {
930-
Py_DECREF(result);
931-
932926
if (op_slot == NB_SLOT(nb_rshift) &&
933927
PyCFunction_CheckExact(v) &&
934928
strcmp(((PyCFunctionObject *)v)->m_ml->ml_name, "print") == 0)
@@ -992,23 +986,20 @@ ternary_op(PyObject *v,
992986
if (x != Py_NotImplemented) {
993987
return x;
994988
}
995-
Py_DECREF(x); /* can't do it */
996989
slotw = NULL;
997990
}
998991
x = slotv(v, w, z);
999992
assert(_Py_CheckSlotResult(v, op_name, x != NULL));
1000993
if (x != Py_NotImplemented) {
1001994
return x;
1002995
}
1003-
Py_DECREF(x); /* can't do it */
1004996
}
1005997
if (slotw) {
1006998
PyObject *x = slotw(v, w, z);
1007999
assert(_Py_CheckSlotResult(w, op_name, x != NULL));
10081000
if (x != Py_NotImplemented) {
10091001
return x;
10101002
}
1011-
Py_DECREF(x); /* can't do it */
10121003
}
10131004

10141005
PyNumberMethods *mz = Py_TYPE(z)->tp_as_number;
@@ -1023,7 +1014,6 @@ ternary_op(PyObject *v,
10231014
if (x != Py_NotImplemented) {
10241015
return x;
10251016
}
1026-
Py_DECREF(x); /* can't do it */
10271017
}
10281018
}
10291019

@@ -1070,7 +1060,6 @@ PyNumber_Add(PyObject *v, PyObject *w)
10701060
if (result != Py_NotImplemented) {
10711061
return result;
10721062
}
1073-
Py_DECREF(result);
10741063

10751064
PySequenceMethods *m = Py_TYPE(v)->tp_as_sequence;
10761065
if (m && m->sq_concat) {
@@ -1108,7 +1097,6 @@ PyNumber_Multiply(PyObject *v, PyObject *w)
11081097
if (result == Py_NotImplemented) {
11091098
PySequenceMethods *mv = Py_TYPE(v)->tp_as_sequence;
11101099
PySequenceMethods *mw = Py_TYPE(w)->tp_as_sequence;
1111-
Py_DECREF(result);
11121100
if (mv && mv->sq_repeat) {
11131101
return sequence_repeat(mv->sq_repeat, v, w);
11141102
}
@@ -1188,7 +1176,6 @@ binary_iop1(PyObject *v, PyObject *w, const int iop_slot, const int op_slot
11881176
if (x != Py_NotImplemented) {
11891177
return x;
11901178
}
1191-
Py_DECREF(x);
11921179
}
11931180
}
11941181
#ifdef NDEBUG
@@ -1210,7 +1197,6 @@ binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot,
12101197
{
12111198
PyObject *result = BINARY_IOP1(v, w, iop_slot, op_slot, op_name);
12121199
if (result == Py_NotImplemented) {
1213-
Py_DECREF(result);
12141200
return binop_type_error(v, w, op_name);
12151201
}
12161202
return result;
@@ -1228,7 +1214,6 @@ ternary_iop(PyObject *v, PyObject *w, PyObject *z, const int iop_slot, const int
12281214
if (x != Py_NotImplemented) {
12291215
return x;
12301216
}
1231-
Py_DECREF(x);
12321217
}
12331218
}
12341219
return ternary_op(v, w, z, op_slot, op_name);
@@ -1258,7 +1243,6 @@ PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
12581243
NB_SLOT(nb_add), "+=");
12591244
if (result == Py_NotImplemented) {
12601245
PySequenceMethods *m = Py_TYPE(v)->tp_as_sequence;
1261-
Py_DECREF(result);
12621246
if (m != NULL) {
12631247
binaryfunc func = m->sq_inplace_concat;
12641248
if (func == NULL)
@@ -1283,7 +1267,6 @@ PyNumber_InPlaceMultiply(PyObject *v, PyObject *w)
12831267
ssizeargfunc f = NULL;
12841268
PySequenceMethods *mv = Py_TYPE(v)->tp_as_sequence;
12851269
PySequenceMethods *mw = Py_TYPE(w)->tp_as_sequence;
1286-
Py_DECREF(result);
12871270
if (mv != NULL) {
12881271
f = mv->sq_inplace_repeat;
12891272
if (f == NULL)
@@ -1767,7 +1750,6 @@ PySequence_Concat(PyObject *s, PyObject *o)
17671750
PyObject *result = BINARY_OP1(s, o, NB_SLOT(nb_add), "+");
17681751
if (result != Py_NotImplemented)
17691752
return result;
1770-
Py_DECREF(result);
17711753
}
17721754
return type_error("'%.200s' object can't be concatenated", s);
17731755
}
@@ -1798,7 +1780,6 @@ PySequence_Repeat(PyObject *o, Py_ssize_t count)
17981780
Py_DECREF(n);
17991781
if (result != Py_NotImplemented)
18001782
return result;
1801-
Py_DECREF(result);
18021783
}
18031784
return type_error("'%.200s' object can't be repeated", o);
18041785
}
@@ -1827,7 +1808,6 @@ PySequence_InPlaceConcat(PyObject *s, PyObject *o)
18271808
NB_SLOT(nb_add), "+=");
18281809
if (result != Py_NotImplemented)
18291810
return result;
1830-
Py_DECREF(result);
18311811
}
18321812
return type_error("'%.200s' object can't be concatenated", s);
18331813
}
@@ -1861,7 +1841,6 @@ PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
18611841
Py_DECREF(n);
18621842
if (result != Py_NotImplemented)
18631843
return result;
1864-
Py_DECREF(result);
18651844
}
18661845
return type_error("'%.200s' object can't be repeated", o);
18671846
}

Objects/boolobject.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ PyObject *PyBool_FromLong(long ok)
2222
result = Py_True;
2323
else
2424
result = Py_False;
25-
Py_INCREF(result);
2625
return result;
2726
}
2827

Objects/bytearrayobject.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2119,7 +2119,6 @@ _common_reduce(PyByteArrayObject *self, int proto)
21192119
}
21202120
if (dict == NULL) {
21212121
dict = Py_None;
2122-
Py_INCREF(dict);
21232122
}
21242123

21252124
buf = PyByteArray_AS_STRING(self);

Objects/classobject.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ method_richcompare(PyObject *self, PyObject *other, int op)
259259
res = eq ? Py_True : Py_False;
260260
else
261261
res = eq ? Py_False : Py_True;
262-
Py_INCREF(res);
263262
return res;
264263
}
265264

Objects/codeobject.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,6 @@ lineiter_next(lineiterator *li)
893893
start = PyLong_FromLong(bounds->ar_start);
894894
end = PyLong_FromLong(bounds->ar_end);
895895
if (bounds->ar_line < 0) {
896-
Py_INCREF(Py_None);
897896
line = Py_None;
898897
}
899898
else {
@@ -1458,7 +1457,6 @@ code_richcompare(PyObject *self, PyObject *other, int op)
14581457
res = Py_False;
14591458

14601459
done:
1461-
Py_INCREF(res);
14621460
return res;
14631461
}
14641462

Objects/complexobject.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,6 @@ to_complex(PyObject **pobj, Py_complex *pc)
449449
pc->real = PyFloat_AsDouble(obj);
450450
return 0;
451451
}
452-
Py_INCREF(Py_NotImplemented);
453452
*pobj = Py_NotImplemented;
454453
return -1;
455454
}
@@ -631,7 +630,6 @@ complex_richcompare(PyObject *v, PyObject *w, int op)
631630
else
632631
res = Py_False;
633632

634-
Py_INCREF(res);
635633
return res;
636634

637635
Unimplemented:

Objects/descrobject.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,15 +1677,12 @@ property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del)
16771677
return NULL;
16781678

16791679
if (get == NULL || get == Py_None) {
1680-
Py_XDECREF(get);
16811680
get = pold->prop_get ? pold->prop_get : Py_None;
16821681
}
16831682
if (set == NULL || set == Py_None) {
1684-
Py_XDECREF(set);
16851683
set = pold->prop_set ? pold->prop_set : Py_None;
16861684
}
16871685
if (del == NULL || del == Py_None) {
1688-
Py_XDECREF(del);
16891686
del = pold->prop_del ? pold->prop_del : Py_None;
16901687
}
16911688
if (pold->getter_doc && get != Py_None) {

Objects/dictobject.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2933,7 +2933,6 @@ dict_richcompare(PyObject *v, PyObject *w, int op)
29332933
}
29342934
else
29352935
res = Py_NotImplemented;
2936-
Py_INCREF(res);
29372936
return res;
29382937
}
29392938

@@ -4339,7 +4338,6 @@ dictview_richcompare(PyObject *self, PyObject *other, int op)
43394338
if (ok < 0)
43404339
return NULL;
43414340
result = ok ? Py_True : Py_False;
4342-
Py_INCREF(result);
43434341
return result;
43444342
}
43454343

Objects/enumobject.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,6 @@ reversed_new_impl(PyTypeObject *type, PyObject *seq)
359359

360360
reversed_meth = _PyObject_LookupSpecial(seq, &_Py_ID(__reversed__));
361361
if (reversed_meth == Py_None) {
362-
Py_DECREF(reversed_meth);
363362
PyErr_Format(PyExc_TypeError,
364363
"'%.200s' object is not reversible",
365364
Py_TYPE(seq)->tp_name);

0 commit comments

Comments
 (0)