Skip to content

Commit

Permalink
Revert "Remove unused refcounts in singletons within CPython/Objects"
Browse files Browse the repository at this point in the history
This reverts commit 5af0167.
  • Loading branch information
eduardo-elizondo committed Mar 9, 2022
1 parent 02cf9af commit 25fd52a
Show file tree
Hide file tree
Showing 26 changed files with 96 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -885,20 +886,23 @@ 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);
assert(_Py_CheckSlotResult(v, op_name, x != NULL));
if (x != Py_NotImplemented) {
return x;
}
Py_DECREF(x); /* can't do it */
}
if (slotw) {
PyObject *x = slotw(v, w);
assert(_Py_CheckSlotResult(w, op_name, x != NULL));
if (x != Py_NotImplemented) {
return x;
}
Py_DECREF(x); /* can't do it */
}
Py_RETURN_NOTIMPLEMENTED;
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -989,20 +995,23 @@ ternary_op(PyObject *v,
if (x != Py_NotImplemented) {
return x;
}
Py_DECREF(x); /* can't do it */
slotw = NULL;
}
x = slotv(v, w, z);
assert(_Py_CheckSlotResult(v, op_name, x != NULL));
if (x != Py_NotImplemented) {
return x;
}
Py_DECREF(x); /* can't do it */
}
if (slotw) {
PyObject *x = slotw(v, w, z);
assert(_Py_CheckSlotResult(w, op_name, x != NULL));
if (x != Py_NotImplemented) {
return x;
}
Py_DECREF(x); /* can't do it */
}

PyNumberMethods *mz = Py_TYPE(z)->tp_as_number;
Expand All @@ -1017,6 +1026,7 @@ ternary_op(PyObject *v,
if (x != Py_NotImplemented) {
return x;
}
Py_DECREF(x); /* can't do it */
}
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions Objects/boolobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ PyObject *PyBool_FromLong(long ok)
result = Py_True;
else
result = Py_False;
Py_INCREF(result);
return result;
}

Expand Down
1 change: 1 addition & 0 deletions Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2119,6 +2119,7 @@ _common_reduce(PyByteArrayObject *self, int proto)
}
if (dict == NULL) {
dict = Py_None;
Py_INCREF(dict);
}

buf = PyByteArray_AS_STRING(self);
Expand Down
1 change: 1 addition & 0 deletions Objects/classobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -1457,6 +1458,7 @@ code_richcompare(PyObject *self, PyObject *other, int op)
res = Py_False;

done:
Py_INCREF(res);
return res;
}

Expand Down
2 changes: 2 additions & 0 deletions Objects/complexobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -630,6 +631,7 @@ complex_richcompare(PyObject *v, PyObject *w, int op)
else
res = Py_False;

Py_INCREF(res);
return res;

Unimplemented:
Expand Down
3 changes: 3 additions & 0 deletions Objects/descrobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3219,6 +3219,7 @@ dict_richcompare(PyObject *v, PyObject *w, int op)
}
else
res = Py_NotImplemented;
Py_INCREF(res);
return res;
}

Expand Down Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions Objects/enumobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 9 additions & 10 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 */
Expand Down
2 changes: 2 additions & 0 deletions Objects/floatobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ convert_to_double(PyObject **v, double *dbl)
}
}
else {
Py_INCREF(Py_NotImplemented);
*v = Py_NotImplemented;
return -1;
}
Expand Down Expand Up @@ -881,6 +882,7 @@ float_is_integer_impl(PyObject *self)
PyExc_ValueError);
return NULL;
}
Py_INCREF(o);
return o;
}

Expand Down
8 changes: 4 additions & 4 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down
4 changes: 3 additions & 1 deletion Objects/funcobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions Objects/memoryobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Loading

0 comments on commit 25fd52a

Please sign in to comment.