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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy