Skip to content

Commit 25fd52a

Browse files
Revert "Remove unused refcounts in singletons within CPython/Objects"
This reverts commit 5af0167.
1 parent 02cf9af commit 25fd52a

26 files changed

+96
-33
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_NotImplemented
674+
#define Py_RETURN_NOTIMPLEMENTED return Py_NewRef(Py_NotImplemented)
675675

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

Objects/abstract.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
124124
return -1;
125125
}
126126
else if (result == Py_NotImplemented) {
127+
Py_DECREF(result);
127128
return defaultvalue;
128129
}
129130
if (!PyLong_Check(result)) {
@@ -885,20 +886,23 @@ binary_op1(PyObject *v, PyObject *w, const int op_slot
885886
x = slotw(v, w);
886887
if (x != Py_NotImplemented)
887888
return x;
889+
Py_DECREF(x); /* can't do it */
888890
slotw = NULL;
889891
}
890892
x = slotv(v, w);
891893
assert(_Py_CheckSlotResult(v, op_name, x != NULL));
892894
if (x != Py_NotImplemented) {
893895
return x;
894896
}
897+
Py_DECREF(x); /* can't do it */
895898
}
896899
if (slotw) {
897900
PyObject *x = slotw(v, w);
898901
assert(_Py_CheckSlotResult(w, op_name, x != NULL));
899902
if (x != Py_NotImplemented) {
900903
return x;
901904
}
905+
Py_DECREF(x); /* can't do it */
902906
}
903907
Py_RETURN_NOTIMPLEMENTED;
904908
}
@@ -926,6 +930,8 @@ binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
926930
{
927931
PyObject *result = BINARY_OP1(v, w, op_slot, op_name);
928932
if (result == Py_NotImplemented) {
933+
Py_DECREF(result);
934+
929935
if (op_slot == NB_SLOT(nb_rshift) &&
930936
PyCFunction_CheckExact(v) &&
931937
strcmp(((PyCFunctionObject *)v)->m_ml->ml_name, "print") == 0)
@@ -989,20 +995,23 @@ ternary_op(PyObject *v,
989995
if (x != Py_NotImplemented) {
990996
return x;
991997
}
998+
Py_DECREF(x); /* can't do it */
992999
slotw = NULL;
9931000
}
9941001
x = slotv(v, w, z);
9951002
assert(_Py_CheckSlotResult(v, op_name, x != NULL));
9961003
if (x != Py_NotImplemented) {
9971004
return x;
9981005
}
1006+
Py_DECREF(x); /* can't do it */
9991007
}
10001008
if (slotw) {
10011009
PyObject *x = slotw(v, w, z);
10021010
assert(_Py_CheckSlotResult(w, op_name, x != NULL));
10031011
if (x != Py_NotImplemented) {
10041012
return x;
10051013
}
1014+
Py_DECREF(x); /* can't do it */
10061015
}
10071016

10081017
PyNumberMethods *mz = Py_TYPE(z)->tp_as_number;
@@ -1017,6 +1026,7 @@ ternary_op(PyObject *v,
10171026
if (x != Py_NotImplemented) {
10181027
return x;
10191028
}
1029+
Py_DECREF(x); /* can't do it */
10201030
}
10211031
}
10221032

@@ -1063,6 +1073,7 @@ PyNumber_Add(PyObject *v, PyObject *w)
10631073
if (result != Py_NotImplemented) {
10641074
return result;
10651075
}
1076+
Py_DECREF(result);
10661077

10671078
PySequenceMethods *m = Py_TYPE(v)->tp_as_sequence;
10681079
if (m && m->sq_concat) {
@@ -1100,6 +1111,7 @@ PyNumber_Multiply(PyObject *v, PyObject *w)
11001111
if (result == Py_NotImplemented) {
11011112
PySequenceMethods *mv = Py_TYPE(v)->tp_as_sequence;
11021113
PySequenceMethods *mw = Py_TYPE(w)->tp_as_sequence;
1114+
Py_DECREF(result);
11031115
if (mv && mv->sq_repeat) {
11041116
return sequence_repeat(mv->sq_repeat, v, w);
11051117
}
@@ -1179,6 +1191,7 @@ binary_iop1(PyObject *v, PyObject *w, const int iop_slot, const int op_slot
11791191
if (x != Py_NotImplemented) {
11801192
return x;
11811193
}
1194+
Py_DECREF(x);
11821195
}
11831196
}
11841197
#ifdef NDEBUG
@@ -1200,6 +1213,7 @@ binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot,
12001213
{
12011214
PyObject *result = BINARY_IOP1(v, w, iop_slot, op_slot, op_name);
12021215
if (result == Py_NotImplemented) {
1216+
Py_DECREF(result);
12031217
return binop_type_error(v, w, op_name);
12041218
}
12051219
return result;
@@ -1217,6 +1231,7 @@ ternary_iop(PyObject *v, PyObject *w, PyObject *z, const int iop_slot, const int
12171231
if (x != Py_NotImplemented) {
12181232
return x;
12191233
}
1234+
Py_DECREF(x);
12201235
}
12211236
}
12221237
return ternary_op(v, w, z, op_slot, op_name);
@@ -1246,6 +1261,7 @@ PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
12461261
NB_SLOT(nb_add), "+=");
12471262
if (result == Py_NotImplemented) {
12481263
PySequenceMethods *m = Py_TYPE(v)->tp_as_sequence;
1264+
Py_DECREF(result);
12491265
if (m != NULL) {
12501266
binaryfunc func = m->sq_inplace_concat;
12511267
if (func == NULL)
@@ -1270,6 +1286,7 @@ PyNumber_InPlaceMultiply(PyObject *v, PyObject *w)
12701286
ssizeargfunc f = NULL;
12711287
PySequenceMethods *mv = Py_TYPE(v)->tp_as_sequence;
12721288
PySequenceMethods *mw = Py_TYPE(w)->tp_as_sequence;
1289+
Py_DECREF(result);
12731290
if (mv != NULL) {
12741291
f = mv->sq_inplace_repeat;
12751292
if (f == NULL)
@@ -1753,6 +1770,7 @@ PySequence_Concat(PyObject *s, PyObject *o)
17531770
PyObject *result = BINARY_OP1(s, o, NB_SLOT(nb_add), "+");
17541771
if (result != Py_NotImplemented)
17551772
return result;
1773+
Py_DECREF(result);
17561774
}
17571775
return type_error("'%.200s' object can't be concatenated", s);
17581776
}
@@ -1783,6 +1801,7 @@ PySequence_Repeat(PyObject *o, Py_ssize_t count)
17831801
Py_DECREF(n);
17841802
if (result != Py_NotImplemented)
17851803
return result;
1804+
Py_DECREF(result);
17861805
}
17871806
return type_error("'%.200s' object can't be repeated", o);
17881807
}
@@ -1811,6 +1830,7 @@ PySequence_InPlaceConcat(PyObject *s, PyObject *o)
18111830
NB_SLOT(nb_add), "+=");
18121831
if (result != Py_NotImplemented)
18131832
return result;
1833+
Py_DECREF(result);
18141834
}
18151835
return type_error("'%.200s' object can't be concatenated", s);
18161836
}
@@ -1844,6 +1864,7 @@ PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
18441864
Py_DECREF(n);
18451865
if (result != Py_NotImplemented)
18461866
return result;
1867+
Py_DECREF(result);
18471868
}
18481869
return type_error("'%.200s' object can't be repeated", o);
18491870
}

Objects/boolobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ PyObject *PyBool_FromLong(long ok)
2222
result = Py_True;
2323
else
2424
result = Py_False;
25+
Py_INCREF(result);
2526
return result;
2627
}
2728

Objects/bytearrayobject.c

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

21242125
buf = PyByteArray_AS_STRING(self);

Objects/classobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ 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);
262263
return res;
263264
}
264265

Objects/codeobject.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,7 @@ 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);
896897
line = Py_None;
897898
}
898899
else {
@@ -1457,6 +1458,7 @@ code_richcompare(PyObject *self, PyObject *other, int op)
14571458
res = Py_False;
14581459

14591460
done:
1461+
Py_INCREF(res);
14601462
return res;
14611463
}
14621464

Objects/complexobject.c

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

634+
Py_INCREF(res);
633635
return res;
634636

635637
Unimplemented:

Objects/descrobject.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,12 +1677,15 @@ 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);
16801681
get = pold->prop_get ? pold->prop_get : Py_None;
16811682
}
16821683
if (set == NULL || set == Py_None) {
1684+
Py_XDECREF(set);
16831685
set = pold->prop_set ? pold->prop_set : Py_None;
16841686
}
16851687
if (del == NULL || del == Py_None) {
1688+
Py_XDECREF(del);
16861689
del = pold->prop_del ? pold->prop_del : Py_None;
16871690
}
16881691
if (pold->getter_doc && get != Py_None) {

Objects/dictobject.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3219,6 +3219,7 @@ dict_richcompare(PyObject *v, PyObject *w, int op)
32193219
}
32203220
else
32213221
res = Py_NotImplemented;
3222+
Py_INCREF(res);
32223223
return res;
32233224
}
32243225

@@ -4693,6 +4694,7 @@ dictview_richcompare(PyObject *self, PyObject *other, int op)
46934694
if (ok < 0)
46944695
return NULL;
46954696
result = ok ? Py_True : Py_False;
4697+
Py_INCREF(result);
46964698
return result;
46974699
}
46984700

Objects/enumobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ 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);
362363
PyErr_Format(PyExc_TypeError,
363364
"'%.200s' object is not reversible",
364365
Py_TYPE(seq)->tp_name);

Objects/exceptions.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -554,12 +554,11 @@ 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-
Py_INCREF(value);
560-
} else {
559+
else
561560
value = Py_None;
562-
}
561+
Py_INCREF(value);
563562
self->value = value;
564563
return 0;
565564
}
@@ -1249,7 +1248,7 @@ exception_group_projection(PyObject *eg, PyObject *keep)
12491248
}
12501249

12511250
PyObject *result = split_result.match ?
1252-
split_result.match : Py_None;
1251+
split_result.match : Py_NewRef(Py_None);
12531252
assert(split_result.rest == NULL);
12541253
return result;
12551254
}
@@ -1294,7 +1293,7 @@ _PyExc_PrepReraiseStar(PyObject *orig, PyObject *excs)
12941293
Py_ssize_t numexcs = PyList_GET_SIZE(excs);
12951294

12961295
if (numexcs == 0) {
1297-
return Py_None;
1296+
return Py_NewRef(Py_None);
12981297
}
12991298

13001299
if (!_PyBaseExceptionGroup_Check(orig)) {
@@ -1537,12 +1536,11 @@ ImportError_reduce(PyImportErrorObject *self, PyObject *Py_UNUSED(ignored))
15371536
if (state == NULL)
15381537
return NULL;
15391538
args = ((PyBaseExceptionObject *)self)->args;
1540-
if (state == Py_None) {
1539+
if (state == Py_None)
15411540
res = PyTuple_Pack(2, Py_TYPE(self), args);
1542-
} else {
1541+
else
15431542
res = PyTuple_Pack(3, Py_TYPE(self), args, state);
1544-
Py_DECREF(state);
1545-
}
1543+
Py_DECREF(state);
15461544
return res;
15471545
}
15481546

@@ -1970,6 +1968,7 @@ OSError_reduce(PyOSErrorObject *self, PyObject *Py_UNUSED(ignored))
19701968
* So, to recreate filename2, we need to pass in
19711969
* winerror as well.
19721970
*/
1971+
Py_INCREF(Py_None);
19731972
PyTuple_SET_ITEM(args, 3, Py_None);
19741973

19751974
/* filename2 */

Objects/floatobject.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ convert_to_double(PyObject **v, double *dbl)
349349
}
350350
}
351351
else {
352+
Py_INCREF(Py_NotImplemented);
352353
*v = Py_NotImplemented;
353354
return -1;
354355
}
@@ -881,6 +882,7 @@ float_is_integer_impl(PyObject *self)
881882
PyExc_ValueError);
882883
return NULL;
883884
}
885+
Py_INCREF(o);
884886
return o;
885887
}
886888

Objects/frameobject.c

Lines changed: 4 additions & 4 deletions
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-
Py_INCREF(globals);
73-
return globals;
71+
if (globals == NULL) {
72+
globals = Py_None;
7473
}
75-
Py_RETURN_NONE;
74+
Py_INCREF(globals);
75+
return globals;
7676
}
7777

7878
static PyObject *

Objects/funcobject.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ _PyFunction_FromConstructor(PyFrameConstructor *constr)
3030
op->func_defaults = NULL;
3131
op->func_kwdefaults = NULL;
3232
op->func_closure = NULL;
33+
Py_INCREF(Py_None);
3334
op->func_doc = Py_None;
3435
op->func_dict = NULL;
3536
op->func_weakreflist = NULL;
@@ -68,8 +69,9 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
6869
PyObject *doc;
6970
if (PyTuple_Size(consts) >= 1) {
7071
doc = PyTuple_GetItem(consts, 0);
71-
if (!PyUnicode_Check(doc))
72+
if (!PyUnicode_Check(doc)) {
7273
doc = Py_None;
74+
}
7375
}
7476
else {
7577
doc = Py_None;

Objects/genobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ 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);
196197
return PYGEN_RETURN;
197198
}
198199
return PYGEN_ERROR;

Objects/listobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,7 @@ 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);
20962097
return PyObject_RichCompareBool(v, w, Py_LT);
20972098
}
20982099
if (res_obj == NULL)

Objects/memoryobject.c

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

2919+
Py_XINCREF(res);
29192920
return res;
29202921
}
29212922

0 commit comments

Comments
 (0)