From 25fd52a053c42d0a35c8c7c552f091d557496570 Mon Sep 17 00:00:00 2001 From: Eddie Elizondo Date: Tue, 8 Mar 2022 16:15:01 -0800 Subject: [PATCH] Revert "Remove unused refcounts in singletons within CPython/Objects" This reverts commit 5af0167a59c0a84bbc742808df839a13a63a03c8. --- Include/object.h | 2 +- Objects/abstract.c | 21 +++++++++++++++++++++ Objects/boolobject.c | 1 + Objects/bytearrayobject.c | 1 + Objects/classobject.c | 1 + Objects/codeobject.c | 2 ++ Objects/complexobject.c | 2 ++ Objects/descrobject.c | 3 +++ Objects/dictobject.c | 2 ++ Objects/enumobject.c | 1 + Objects/exceptions.c | 19 +++++++++---------- Objects/floatobject.c | 2 ++ Objects/frameobject.c | 8 ++++---- Objects/funcobject.c | 4 +++- Objects/genobject.c | 1 + Objects/listobject.c | 1 + Objects/memoryobject.c | 1 + Objects/methodobject.c | 10 +++++----- Objects/object.c | 20 +++++++++++--------- Objects/odictobject.c | 1 + Objects/rangeobject.c | 3 +++ Objects/setobject.c | 1 + Objects/sliceobject.c | 5 +++-- Objects/stringlib/unicode_format.h | 1 + Objects/typeobject.c | 13 ++++++++++++- Objects/unicodeobject.c | 3 +++ 26 files changed, 96 insertions(+), 33 deletions(-) diff --git a/Include/object.h b/Include/object.h index 933fdb63487330..f3f85815b4bbfd 100644 --- a/Include/object.h +++ b/Include/object.h @@ -671,7 +671,7 @@ PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */ #define Py_NotImplemented (&_Py_NotImplementedStruct) /* Macro for returning Py_NotImplemented from a function */ -#define Py_RETURN_NOTIMPLEMENTED return Py_NotImplemented +#define Py_RETURN_NOTIMPLEMENTED return Py_NewRef(Py_NotImplemented) /* Rich comparison opcodes */ #define Py_LT 0 diff --git a/Objects/abstract.c b/Objects/abstract.c index 5d7056d82854f7..79f5a5f760f8e2 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -124,6 +124,7 @@ PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue) return -1; } else if (result == Py_NotImplemented) { + Py_DECREF(result); return defaultvalue; } if (!PyLong_Check(result)) { @@ -885,6 +886,7 @@ binary_op1(PyObject *v, PyObject *w, const int op_slot x = slotw(v, w); if (x != Py_NotImplemented) return x; + Py_DECREF(x); /* can't do it */ slotw = NULL; } x = slotv(v, w); @@ -892,6 +894,7 @@ binary_op1(PyObject *v, PyObject *w, const int op_slot if (x != Py_NotImplemented) { return x; } + Py_DECREF(x); /* can't do it */ } if (slotw) { PyObject *x = slotw(v, w); @@ -899,6 +902,7 @@ binary_op1(PyObject *v, PyObject *w, const int op_slot if (x != Py_NotImplemented) { return x; } + Py_DECREF(x); /* can't do it */ } Py_RETURN_NOTIMPLEMENTED; } @@ -926,6 +930,8 @@ binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name) { PyObject *result = BINARY_OP1(v, w, op_slot, op_name); if (result == Py_NotImplemented) { + Py_DECREF(result); + if (op_slot == NB_SLOT(nb_rshift) && PyCFunction_CheckExact(v) && strcmp(((PyCFunctionObject *)v)->m_ml->ml_name, "print") == 0) @@ -989,6 +995,7 @@ ternary_op(PyObject *v, if (x != Py_NotImplemented) { return x; } + Py_DECREF(x); /* can't do it */ slotw = NULL; } x = slotv(v, w, z); @@ -996,6 +1003,7 @@ ternary_op(PyObject *v, if (x != Py_NotImplemented) { return x; } + Py_DECREF(x); /* can't do it */ } if (slotw) { PyObject *x = slotw(v, w, z); @@ -1003,6 +1011,7 @@ ternary_op(PyObject *v, if (x != Py_NotImplemented) { return x; } + Py_DECREF(x); /* can't do it */ } PyNumberMethods *mz = Py_TYPE(z)->tp_as_number; @@ -1017,6 +1026,7 @@ ternary_op(PyObject *v, if (x != Py_NotImplemented) { return x; } + Py_DECREF(x); /* can't do it */ } } @@ -1063,6 +1073,7 @@ PyNumber_Add(PyObject *v, PyObject *w) if (result != Py_NotImplemented) { return result; } + Py_DECREF(result); PySequenceMethods *m = Py_TYPE(v)->tp_as_sequence; if (m && m->sq_concat) { @@ -1100,6 +1111,7 @@ PyNumber_Multiply(PyObject *v, PyObject *w) if (result == Py_NotImplemented) { PySequenceMethods *mv = Py_TYPE(v)->tp_as_sequence; PySequenceMethods *mw = Py_TYPE(w)->tp_as_sequence; + Py_DECREF(result); if (mv && mv->sq_repeat) { return sequence_repeat(mv->sq_repeat, v, w); } @@ -1179,6 +1191,7 @@ binary_iop1(PyObject *v, PyObject *w, const int iop_slot, const int op_slot if (x != Py_NotImplemented) { return x; } + Py_DECREF(x); } } #ifdef NDEBUG @@ -1200,6 +1213,7 @@ binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot, { PyObject *result = BINARY_IOP1(v, w, iop_slot, op_slot, op_name); if (result == Py_NotImplemented) { + Py_DECREF(result); return binop_type_error(v, w, op_name); } return result; @@ -1217,6 +1231,7 @@ ternary_iop(PyObject *v, PyObject *w, PyObject *z, const int iop_slot, const int if (x != Py_NotImplemented) { return x; } + Py_DECREF(x); } } return ternary_op(v, w, z, op_slot, op_name); @@ -1246,6 +1261,7 @@ PyNumber_InPlaceAdd(PyObject *v, PyObject *w) NB_SLOT(nb_add), "+="); if (result == Py_NotImplemented) { PySequenceMethods *m = Py_TYPE(v)->tp_as_sequence; + Py_DECREF(result); if (m != NULL) { binaryfunc func = m->sq_inplace_concat; if (func == NULL) @@ -1270,6 +1286,7 @@ PyNumber_InPlaceMultiply(PyObject *v, PyObject *w) ssizeargfunc f = NULL; PySequenceMethods *mv = Py_TYPE(v)->tp_as_sequence; PySequenceMethods *mw = Py_TYPE(w)->tp_as_sequence; + Py_DECREF(result); if (mv != NULL) { f = mv->sq_inplace_repeat; if (f == NULL) @@ -1753,6 +1770,7 @@ PySequence_Concat(PyObject *s, PyObject *o) PyObject *result = BINARY_OP1(s, o, NB_SLOT(nb_add), "+"); if (result != Py_NotImplemented) return result; + Py_DECREF(result); } return type_error("'%.200s' object can't be concatenated", s); } @@ -1783,6 +1801,7 @@ PySequence_Repeat(PyObject *o, Py_ssize_t count) Py_DECREF(n); if (result != Py_NotImplemented) return result; + Py_DECREF(result); } return type_error("'%.200s' object can't be repeated", o); } @@ -1811,6 +1830,7 @@ PySequence_InPlaceConcat(PyObject *s, PyObject *o) NB_SLOT(nb_add), "+="); if (result != Py_NotImplemented) return result; + Py_DECREF(result); } return type_error("'%.200s' object can't be concatenated", s); } @@ -1844,6 +1864,7 @@ PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count) Py_DECREF(n); if (result != Py_NotImplemented) return result; + Py_DECREF(result); } return type_error("'%.200s' object can't be repeated", o); } diff --git a/Objects/boolobject.c b/Objects/boolobject.c index d6a4a41014851e..d86958aff9ccb8 100644 --- a/Objects/boolobject.c +++ b/Objects/boolobject.c @@ -22,6 +22,7 @@ PyObject *PyBool_FromLong(long ok) result = Py_True; else result = Py_False; + Py_INCREF(result); return result; } diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index 60f4644b744213..0ebb2ece39d5d3 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -2119,6 +2119,7 @@ _common_reduce(PyByteArrayObject *self, int proto) } if (dict == NULL) { dict = Py_None; + Py_INCREF(dict); } buf = PyByteArray_AS_STRING(self); diff --git a/Objects/classobject.c b/Objects/classobject.c index 2f32491fdf436c..3b1c25394f152a 100644 --- a/Objects/classobject.c +++ b/Objects/classobject.c @@ -259,6 +259,7 @@ method_richcompare(PyObject *self, PyObject *other, int op) res = eq ? Py_True : Py_False; else res = eq ? Py_False : Py_True; + Py_INCREF(res); return res; } diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 20dad082af6310..5279f6ce170648 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -893,6 +893,7 @@ lineiter_next(lineiterator *li) start = PyLong_FromLong(bounds->ar_start); end = PyLong_FromLong(bounds->ar_end); if (bounds->ar_line < 0) { + Py_INCREF(Py_None); line = Py_None; } else { @@ -1457,6 +1458,7 @@ code_richcompare(PyObject *self, PyObject *other, int op) res = Py_False; done: + Py_INCREF(res); return res; } diff --git a/Objects/complexobject.c b/Objects/complexobject.c index 99fd16bb9d9f3d..9bd68d50c30ae0 100644 --- a/Objects/complexobject.c +++ b/Objects/complexobject.c @@ -449,6 +449,7 @@ to_complex(PyObject **pobj, Py_complex *pc) pc->real = PyFloat_AsDouble(obj); return 0; } + Py_INCREF(Py_NotImplemented); *pobj = Py_NotImplemented; return -1; } @@ -630,6 +631,7 @@ complex_richcompare(PyObject *v, PyObject *w, int op) else res = Py_False; + Py_INCREF(res); return res; Unimplemented: diff --git a/Objects/descrobject.c b/Objects/descrobject.c index c53d8237f5fe58..2d4cfb5b7aeb8f 100644 --- a/Objects/descrobject.c +++ b/Objects/descrobject.c @@ -1677,12 +1677,15 @@ property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del) return NULL; if (get == NULL || get == Py_None) { + Py_XDECREF(get); get = pold->prop_get ? pold->prop_get : Py_None; } if (set == NULL || set == Py_None) { + Py_XDECREF(set); set = pold->prop_set ? pold->prop_set : Py_None; } if (del == NULL || del == Py_None) { + Py_XDECREF(del); del = pold->prop_del ? pold->prop_del : Py_None; } if (pold->getter_doc && get != Py_None) { diff --git a/Objects/dictobject.c b/Objects/dictobject.c index 2f7666baf3a4ae..635a738985c01d 100644 --- a/Objects/dictobject.c +++ b/Objects/dictobject.c @@ -3219,6 +3219,7 @@ dict_richcompare(PyObject *v, PyObject *w, int op) } else res = Py_NotImplemented; + Py_INCREF(res); return res; } @@ -4693,6 +4694,7 @@ dictview_richcompare(PyObject *self, PyObject *other, int op) if (ok < 0) return NULL; result = ok ? Py_True : Py_False; + Py_INCREF(result); return result; } diff --git a/Objects/enumobject.c b/Objects/enumobject.c index 5384edba42509b..d84bac6f4c9af2 100644 --- a/Objects/enumobject.c +++ b/Objects/enumobject.c @@ -359,6 +359,7 @@ reversed_new_impl(PyTypeObject *type, PyObject *seq) reversed_meth = _PyObject_LookupSpecial(seq, &_Py_ID(__reversed__)); if (reversed_meth == Py_None) { + Py_DECREF(reversed_meth); PyErr_Format(PyExc_TypeError, "'%.200s' object is not reversible", Py_TYPE(seq)->tp_name); diff --git a/Objects/exceptions.c b/Objects/exceptions.c index f00a65d06538b9..9dbbd40f1de1c4 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -554,12 +554,11 @@ StopIteration_init(PyStopIterationObject *self, PyObject *args, PyObject *kwds) if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) return -1; Py_CLEAR(self->value); - if (size > 0) { + if (size > 0) value = PyTuple_GET_ITEM(args, 0); - Py_INCREF(value); - } else { + else value = Py_None; - } + Py_INCREF(value); self->value = value; return 0; } @@ -1249,7 +1248,7 @@ exception_group_projection(PyObject *eg, PyObject *keep) } PyObject *result = split_result.match ? - split_result.match : Py_None; + split_result.match : Py_NewRef(Py_None); assert(split_result.rest == NULL); return result; } @@ -1294,7 +1293,7 @@ _PyExc_PrepReraiseStar(PyObject *orig, PyObject *excs) Py_ssize_t numexcs = PyList_GET_SIZE(excs); if (numexcs == 0) { - return Py_None; + return Py_NewRef(Py_None); } if (!_PyBaseExceptionGroup_Check(orig)) { @@ -1537,12 +1536,11 @@ ImportError_reduce(PyImportErrorObject *self, PyObject *Py_UNUSED(ignored)) if (state == NULL) return NULL; args = ((PyBaseExceptionObject *)self)->args; - if (state == Py_None) { + if (state == Py_None) res = PyTuple_Pack(2, Py_TYPE(self), args); - } else { + else res = PyTuple_Pack(3, Py_TYPE(self), args, state); - Py_DECREF(state); - } + Py_DECREF(state); return res; } @@ -1970,6 +1968,7 @@ OSError_reduce(PyOSErrorObject *self, PyObject *Py_UNUSED(ignored)) * So, to recreate filename2, we need to pass in * winerror as well. */ + Py_INCREF(Py_None); PyTuple_SET_ITEM(args, 3, Py_None); /* filename2 */ diff --git a/Objects/floatobject.c b/Objects/floatobject.c index d404f64a9764fb..91ca848bf26e8a 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -349,6 +349,7 @@ convert_to_double(PyObject **v, double *dbl) } } else { + Py_INCREF(Py_NotImplemented); *v = Py_NotImplemented; return -1; } @@ -881,6 +882,7 @@ float_is_integer_impl(PyObject *self) PyExc_ValueError); return NULL; } + Py_INCREF(o); return o; } diff --git a/Objects/frameobject.c b/Objects/frameobject.c index 14382711e01e36..eb7fdb30cd75e6 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -68,11 +68,11 @@ static PyObject * frame_getglobals(PyFrameObject *f, void *closure) { PyObject *globals = f->f_frame->f_globals; - if (globals != NULL) { - Py_INCREF(globals); - return globals; + if (globals == NULL) { + globals = Py_None; } - Py_RETURN_NONE; + Py_INCREF(globals); + return globals; } static PyObject * diff --git a/Objects/funcobject.c b/Objects/funcobject.c index f1394bc79d153c..deacfd55dd2866 100644 --- a/Objects/funcobject.c +++ b/Objects/funcobject.c @@ -30,6 +30,7 @@ _PyFunction_FromConstructor(PyFrameConstructor *constr) op->func_defaults = NULL; op->func_kwdefaults = NULL; op->func_closure = NULL; + Py_INCREF(Py_None); op->func_doc = Py_None; op->func_dict = NULL; op->func_weakreflist = NULL; @@ -68,8 +69,9 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname PyObject *doc; if (PyTuple_Size(consts) >= 1) { doc = PyTuple_GetItem(consts, 0); - if (!PyUnicode_Check(doc)) + if (!PyUnicode_Check(doc)) { doc = Py_None; + } } else { doc = Py_None; diff --git a/Objects/genobject.c b/Objects/genobject.c index 48a81efa3daad8..bfa1ea5c45f66e 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -193,6 +193,7 @@ gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult, /* `gen` is an exhausted generator: only return value if called from send(). */ *presult = Py_None; + Py_INCREF(*presult); return PYGEN_RETURN; } return PYGEN_ERROR; diff --git a/Objects/listobject.c b/Objects/listobject.c index d5542402ecc9d8..783ae88a17f3be 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -2093,6 +2093,7 @@ unsafe_object_compare(PyObject *v, PyObject *w, MergeState *ms) res_obj = (*(ms->key_richcompare))(v, w, Py_LT); if (res_obj == Py_NotImplemented) { + Py_DECREF(res_obj); return PyObject_RichCompareBool(v, w, Py_LT); } if (res_obj == NULL) diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c index 1c4255f3af6cd1..45fe8985c2adb4 100644 --- a/Objects/memoryobject.c +++ b/Objects/memoryobject.c @@ -2916,6 +2916,7 @@ memory_richcompare(PyObject *v, PyObject *w, int op) unpacker_free(unpack_v); unpacker_free(unpack_w); + Py_XINCREF(res); return res; } diff --git a/Objects/methodobject.c b/Objects/methodobject.c index e63119b46c5d54..93fac22ec437c1 100644 --- a/Objects/methodobject.c +++ b/Objects/methodobject.c @@ -258,11 +258,10 @@ meth_get__self__(PyCFunctionObject *m, void *closure) PyObject *self; self = PyCFunction_GET_SELF(m); - if (self != NULL) { - Py_INCREF(self); - return self; - } - Py_RETURN_NONE; + if (self == NULL) + self = Py_None; + Py_INCREF(self); + return self; } static PyGetSetDef meth_getsets [] = { @@ -315,6 +314,7 @@ meth_richcompare(PyObject *self, PyObject *other, int op) res = eq ? Py_True : Py_False; else res = eq ? Py_False : Py_True; + Py_INCREF(res); return res; } diff --git a/Objects/object.c b/Objects/object.c index c57cb418030f30..7dc5e2d7184421 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -674,16 +674,19 @@ do_richcompare(PyThreadState *tstate, PyObject *v, PyObject *w, int op) res = (*f)(w, v, _Py_SwappedOp[op]); if (res != Py_NotImplemented) return res; + Py_DECREF(res); } if ((f = Py_TYPE(v)->tp_richcompare) != NULL) { res = (*f)(v, w, op); if (res != Py_NotImplemented) return res; + Py_DECREF(res); } if (!checked_reverse_op && (f = Py_TYPE(w)->tp_richcompare) != NULL) { res = (*f)(w, v, _Py_SwappedOp[op]); if (res != Py_NotImplemented) return res; + Py_DECREF(res); } /* If neither object implements it, provide a sensible default for == and !=, but raise an exception for ordering. */ @@ -702,6 +705,7 @@ do_richcompare(PyThreadState *tstate, PyObject *v, PyObject *w, int op) Py_TYPE(w)->tp_name); return NULL; } + Py_INCREF(res); return res; } @@ -748,12 +752,11 @@ PyObject_RichCompareBool(PyObject *v, PyObject *w, int op) res = PyObject_RichCompare(v, w, op); if (res == NULL) return -1; - if (PyBool_Check(res)) { + if (PyBool_Check(res)) ok = (res == Py_True); - } else { + else ok = PyObject_IsTrue(res); - Py_DECREF(res); - } + Py_DECREF(res); return ok; } @@ -1706,9 +1709,9 @@ PyTypeObject _PyNone_Type = { }; PyObject _Py_NoneStruct = { - _PyObject_EXTRA_INIT - _Py_IMMORTAL_REFCNT, - &_PyNone_Type + _PyObject_EXTRA_INIT + _Py_IMMORTAL_REFCNT, + &_PyNone_Type }; /* NotImplemented is an object that can be used to signal that an @@ -1809,8 +1812,7 @@ PyTypeObject _PyNotImplemented_Type = { PyObject _Py_NotImplementedStruct = { _PyObject_EXTRA_INIT - _Py_IMMORTAL_REFCNT, - &_PyNotImplemented_Type + 1, &_PyNotImplemented_Type }; PyStatus diff --git a/Objects/odictobject.c b/Objects/odictobject.c index d2c56b22e3d5ab..c207593ab79f72 100644 --- a/Objects/odictobject.c +++ b/Objects/odictobject.c @@ -1507,6 +1507,7 @@ odict_richcompare(PyObject *v, PyObject *w, int op) return NULL; res = (eq == (op == Py_EQ)) ? Py_True : Py_False; + Py_INCREF(res); return res; } else { Py_RETURN_NOTIMPLEMENTED; diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index f7314059fde056..5d583b2edf0e94 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -527,6 +527,8 @@ range_hash(rangeobject *r) if (cmp_result == -1) goto end; if (cmp_result == 1) { + Py_INCREF(Py_None); + Py_INCREF(Py_None); PyTuple_SET_ITEM(t, 1, Py_None); PyTuple_SET_ITEM(t, 2, Py_None); } @@ -537,6 +539,7 @@ range_hash(rangeobject *r) if (cmp_result == -1) goto end; if (cmp_result == 1) { + Py_INCREF(Py_None); PyTuple_SET_ITEM(t, 2, Py_None); } else { diff --git a/Objects/setobject.c b/Objects/setobject.c index e32a7b65240094..c65b7d5d211159 100644 --- a/Objects/setobject.c +++ b/Objects/setobject.c @@ -1949,6 +1949,7 @@ set_reduce(PySetObject *so, PyObject *Py_UNUSED(ignored)) } if (dict == NULL) { dict = Py_None; + Py_INCREF(dict); } result = PyTuple_Pack(3, Py_TYPE(so), args, dict); done: diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c index 369bdc996a7bfa..22fb7c61c354f9 100644 --- a/Objects/sliceobject.c +++ b/Objects/sliceobject.c @@ -26,6 +26,7 @@ ellipsis_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) PyErr_SetString(PyExc_TypeError, "EllipsisType takes no arguments"); return NULL; } + Py_INCREF(Py_Ellipsis); return Py_Ellipsis; } @@ -89,8 +90,7 @@ PyTypeObject PyEllipsis_Type = { PyObject _Py_EllipsisObject = { _PyObject_EXTRA_INIT - _Py_IMMORTAL_REFCNT, - &PyEllipsis_Type + 1, &PyEllipsis_Type }; @@ -591,6 +591,7 @@ slice_richcompare(PyObject *v, PyObject *w, int op) res = Py_False; break; } + Py_INCREF(res); return res; } diff --git a/Objects/stringlib/unicode_format.h b/Objects/stringlib/unicode_format.h index dd9099313567db..a4eea7b91988b9 100644 --- a/Objects/stringlib/unicode_format.h +++ b/Objects/stringlib/unicode_format.h @@ -1043,6 +1043,7 @@ formatteriter_next(formatteriterobject *it) character */ if (conversion == '\0') { conversion_str = Py_None; + Py_INCREF(conversion_str); } else conversion_str = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 8778e3c971bf5a..78795150756130 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -237,7 +237,7 @@ _PyType_InitCache(PyInterpreterState *interp) entry->version = 0; // Set to None so _PyType_Lookup() can use Py_SETREF(), // rather than using slower Py_XSETREF(). - entry->name = Py_None; + entry->name = Py_NewRef(Py_None); entry->value = NULL; } } @@ -4633,6 +4633,7 @@ object_richcompare(PyObject *self, PyObject *other, int op) objects are compared, both get a chance at the comparison. See issue #1393. */ res = (self == other) ? Py_True : Py_NotImplemented; + Py_INCREF(res); break; case Py_NE: @@ -4640,6 +4641,7 @@ object_richcompare(PyObject *self, PyObject *other, int op) unless the latter returns NotImplemented. */ if (Py_TYPE(self)->tp_richcompare == NULL) { res = Py_NotImplemented; + Py_INCREF(res); break; } res = (*Py_TYPE(self)->tp_richcompare)(self, other, Py_EQ); @@ -4653,12 +4655,14 @@ object_richcompare(PyObject *self, PyObject *other, int op) res = Py_False; else res = Py_True; + Py_INCREF(res); } } break; default: res = Py_NotImplemented; + Py_INCREF(res); break; } @@ -4975,6 +4979,7 @@ _PyObject_GetState(PyObject *obj, int required) } if (_PyObject_IsInstanceDictEmpty(obj)) { state = Py_None; + Py_INCREF(state); } else { state = PyObject_GenericGetDict(obj, NULL); @@ -5197,6 +5202,7 @@ _PyObject_GetItemsIter(PyObject *obj, PyObject **listitems, if (!PyList_Check(obj)) { *listitems = Py_None; + Py_INCREF(*listitems); } else { *listitems = PyObject_GetIter(obj); @@ -5206,6 +5212,7 @@ _PyObject_GetItemsIter(PyObject *obj, PyObject **listitems, if (!PyDict_Check(obj)) { *dictitems = Py_None; + Py_INCREF(*dictitems); } else { PyObject *items = PyObject_CallMethodNoArgs(obj, &_Py_ID(items)); @@ -7205,6 +7212,7 @@ FUNCNAME(PyObject *self, PyObject *other) \ r = vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \ if (r != Py_NotImplemented) \ return r; \ + Py_DECREF(r); \ do_other = 0; \ } \ } \ @@ -7307,6 +7315,7 @@ slot_sq_contains(PyObject *self, PyObject *value) func = lookup_maybe_method(self, &_Py_ID(__contains__), &unbound); if (func == Py_None) { + Py_DECREF(func); PyErr_Format(PyExc_TypeError, "'%.200s' object is not a container", Py_TYPE(self)->tp_name); @@ -7508,6 +7517,7 @@ slot_tp_hash(PyObject *self) func = lookup_maybe_method(self, &_Py_ID(__hash__), &unbound); if (func == Py_None) { + Py_DECREF(func); func = NULL; } @@ -7702,6 +7712,7 @@ slot_tp_iter(PyObject *self) func = lookup_maybe_method(self, &_Py_ID(__iter__), &unbound); if (func == Py_None) { + Py_DECREF(func); PyErr_Format(PyExc_TypeError, "'%.200s' object is not iterable", Py_TYPE(self)->tp_name); diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index d1859d81863ae3..908ad514925999 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -8666,6 +8666,7 @@ charmapencode_output(Py_UCS4 c, PyObject *mapping, if (rep==NULL) return enc_EXCEPTION; else if (rep==Py_None) { + Py_DECREF(rep); return enc_FAILED; } else { if (PyLong_Check(rep)) { @@ -8744,6 +8745,7 @@ charmap_encoding_error( Py_DECREF(rep); break; } + Py_DECREF(rep); ++collendpos; } /* cache callback name lookup @@ -9077,6 +9079,7 @@ charmaptranslate_output(Py_UCS4 ch, PyObject *mapping, } if (item == Py_None) { + Py_DECREF(item); return 0; }