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

+1-1
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

-21
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

-1
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

-1
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

-1
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

-2
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

-2
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

-3
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

-2
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

-1
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);

Objects/exceptions.c

+10-9
Original file line numberDiff line numberDiff line change
@@ -554,11 +554,12 @@ StopIteration_init(PyStopIterationObject *self, PyObject *args, PyObject *kwds)
554554
if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
555555
return -1;
556556
Py_CLEAR(self->value);
557-
if (size > 0)
557+
if (size > 0) {
558558
value = PyTuple_GET_ITEM(args, 0);
559-
else
559+
Py_INCREF(value);
560+
} else {
560561
value = Py_None;
561-
Py_INCREF(value);
562+
}
562563
self->value = value;
563564
return 0;
564565
}
@@ -1248,7 +1249,7 @@ exception_group_projection(PyObject *eg, PyObject *keep)
12481249
}
12491250

12501251
PyObject *result = split_result.match ?
1251-
split_result.match : Py_NewRef(Py_None);
1252+
split_result.match : Py_None;
12521253
assert(split_result.rest == NULL);
12531254
return result;
12541255
}
@@ -1293,7 +1294,7 @@ _PyExc_PrepReraiseStar(PyObject *orig, PyObject *excs)
12931294
Py_ssize_t numexcs = PyList_GET_SIZE(excs);
12941295

12951296
if (numexcs == 0) {
1296-
return Py_NewRef(Py_None);
1297+
return Py_None;
12971298
}
12981299

12991300
if (!_PyBaseExceptionGroup_Check(orig)) {
@@ -1536,11 +1537,12 @@ ImportError_reduce(PyImportErrorObject *self, PyObject *Py_UNUSED(ignored))
15361537
if (state == NULL)
15371538
return NULL;
15381539
args = ((PyBaseExceptionObject *)self)->args;
1539-
if (state == Py_None)
1540+
if (state == Py_None) {
15401541
res = PyTuple_Pack(2, Py_TYPE(self), args);
1541-
else
1542+
} else {
15421543
res = PyTuple_Pack(3, Py_TYPE(self), args, state);
1543-
Py_DECREF(state);
1544+
Py_DECREF(state);
1545+
}
15441546
return res;
15451547
}
15461548

@@ -1968,7 +1970,6 @@ OSError_reduce(PyOSErrorObject *self, PyObject *Py_UNUSED(ignored))
19681970
* So, to recreate filename2, we need to pass in
19691971
* winerror as well.
19701972
*/
1971-
Py_INCREF(Py_None);
19721973
PyTuple_SET_ITEM(args, 3, Py_None);
19731974

19741975
/* filename2 */

Objects/floatobject.c

-2
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ convert_to_double(PyObject **v, double *dbl)
349349
}
350350
}
351351
else {
352-
Py_INCREF(Py_NotImplemented);
353352
*v = Py_NotImplemented;
354353
return -1;
355354
}
@@ -882,7 +881,6 @@ float_is_integer_impl(PyObject *self)
882881
PyExc_ValueError);
883882
return NULL;
884883
}
885-
Py_INCREF(o);
886884
return o;
887885
}
888886

Objects/frameobject.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ static PyObject *
6868
frame_getglobals(PyFrameObject *f, void *closure)
6969
{
7070
PyObject *globals = f->f_frame->f_globals;
71-
if (globals == NULL) {
72-
globals = Py_None;
71+
if (globals != NULL) {
72+
Py_INCREF(globals);
73+
return globals;
7374
}
74-
Py_INCREF(globals);
75-
return globals;
75+
Py_RETURN_NONE;
7676
}
7777

7878
static PyObject *

Objects/funcobject.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ _PyFunction_FromConstructor(PyFrameConstructor *constr)
3030
op->func_defaults = NULL;
3131
op->func_kwdefaults = NULL;
3232
op->func_closure = NULL;
33-
Py_INCREF(Py_None);
3433
op->func_doc = Py_None;
3534
op->func_dict = NULL;
3635
op->func_weakreflist = NULL;
@@ -69,9 +68,8 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
6968
PyObject *doc;
7069
if (PyTuple_Size(consts) >= 1) {
7170
doc = PyTuple_GetItem(consts, 0);
72-
if (!PyUnicode_Check(doc)) {
71+
if (!PyUnicode_Check(doc))
7372
doc = Py_None;
74-
}
7573
}
7674
else {
7775
doc = Py_None;

Objects/genobject.c

-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult,
193193
/* `gen` is an exhausted generator:
194194
only return value if called from send(). */
195195
*presult = Py_None;
196-
Py_INCREF(*presult);
197196
return PYGEN_RETURN;
198197
}
199198
return PYGEN_ERROR;

Objects/listobject.c

-1
Original file line numberDiff line numberDiff line change
@@ -2093,7 +2093,6 @@ unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms)
20932093
res_obj = (*(ms->key_richcompare))(v, w, Py_LT);
20942094

20952095
if (res_obj == Py_NotImplemented) {
2096-
Py_DECREF(res_obj);
20972096
return PyObject_RichCompareBool(v, w, Py_LT);
20982097
}
20992098
if (res_obj == NULL)

Objects/memoryobject.c

-1
Original file line numberDiff line numberDiff line change
@@ -2916,7 +2916,6 @@ memory_richcompare(PyObject *v, PyObject *w, int op)
29162916
unpacker_free(unpack_v);
29172917
unpacker_free(unpack_w);
29182918

2919-
Py_XINCREF(res);
29202919
return res;
29212920
}
29222921

0 commit comments

Comments
 (0)