gh-99300: Use Py_NewRef() in Modules/ directory (#99466)

Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in test C files of the Modules/ directory.
This commit is contained in:
Victor Stinner 2022-11-14 13:08:15 +01:00 committed by GitHub
parent db115682bd
commit 3817607b8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 106 additions and 214 deletions

View File

@ -524,8 +524,7 @@ _abc__abc_register_impl(PyObject *module, PyObject *self, PyObject *subclass)
} }
int result = PyObject_IsSubclass(subclass, self); int result = PyObject_IsSubclass(subclass, self);
if (result > 0) { if (result > 0) {
Py_INCREF(subclass); return Py_NewRef(subclass); /* Already a subclass. */
return subclass; /* Already a subclass. */
} }
if (result < 0) { if (result < 0) {
return NULL; return NULL;
@ -561,8 +560,7 @@ _abc__abc_register_impl(PyObject *module, PyObject *self, PyObject *subclass)
set_collection_flag_recursive((PyTypeObject *)subclass, collection_flag); set_collection_flag_recursive((PyTypeObject *)subclass, collection_flag);
} }
} }
Py_INCREF(subclass); return Py_NewRef(subclass);
return subclass;
} }
@ -598,8 +596,7 @@ _abc__abc_instancecheck_impl(PyObject *module, PyObject *self,
goto end; goto end;
} }
if (incache > 0) { if (incache > 0) {
result = Py_True; result = Py_NewRef(Py_True);
Py_INCREF(result);
goto end; goto end;
} }
subtype = (PyObject *)Py_TYPE(instance); subtype = (PyObject *)Py_TYPE(instance);
@ -610,8 +607,7 @@ _abc__abc_instancecheck_impl(PyObject *module, PyObject *self,
goto end; goto end;
} }
if (incache > 0) { if (incache > 0) {
result = Py_False; result = Py_NewRef(Py_False);
Py_INCREF(result);
goto end; goto end;
} }
} }
@ -802,8 +798,7 @@ _abc__abc_subclasscheck_impl(PyObject *module, PyObject *self,
end: end:
Py_DECREF(impl); Py_DECREF(impl);
Py_XDECREF(subclasses); Py_XDECREF(subclasses);
Py_XINCREF(result); return Py_XNewRef(result);
return result;
} }
@ -842,8 +837,7 @@ subclasscheck_check_registry(_abc_data *impl, PyObject *subclass,
Py_ssize_t i = 0; Py_ssize_t i = 0;
while (_PySet_NextEntry(impl->_abc_registry, &pos, &key, &hash)) { while (_PySet_NextEntry(impl->_abc_registry, &pos, &key, &hash)) {
Py_INCREF(key); copy[i++] = Py_NewRef(key);
copy[i++] = key;
} }
assert(i == registry_size); assert(i == registry_size);

View File

@ -299,8 +299,7 @@ deque_append_internal(dequeobject *deque, PyObject *item, Py_ssize_t maxlen)
static PyObject * static PyObject *
deque_append(dequeobject *deque, PyObject *item) deque_append(dequeobject *deque, PyObject *item)
{ {
Py_INCREF(item); if (deque_append_internal(deque, Py_NewRef(item), deque->maxlen) < 0)
if (deque_append_internal(deque, item, deque->maxlen) < 0)
return NULL; return NULL;
Py_RETURN_NONE; Py_RETURN_NONE;
} }
@ -336,8 +335,7 @@ deque_appendleft_internal(dequeobject *deque, PyObject *item, Py_ssize_t maxlen)
static PyObject * static PyObject *
deque_appendleft(dequeobject *deque, PyObject *item) deque_appendleft(dequeobject *deque, PyObject *item)
{ {
Py_INCREF(item); if (deque_appendleft_internal(deque, Py_NewRef(item), deque->maxlen) < 0)
if (deque_appendleft_internal(deque, item, deque->maxlen) < 0)
return NULL; return NULL;
Py_RETURN_NONE; Py_RETURN_NONE;
} }
@ -655,14 +653,12 @@ deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
size = Py_SIZE(deque); size = Py_SIZE(deque);
if (size == 0 || n == 1) { if (size == 0 || n == 1) {
Py_INCREF(deque); return Py_NewRef(deque);
return (PyObject *)deque;
} }
if (n <= 0) { if (n <= 0) {
deque_clear(deque); deque_clear(deque);
Py_INCREF(deque); return Py_NewRef(deque);
return (PyObject *)deque;
} }
if (size == 1) { if (size == 1) {
@ -693,13 +689,11 @@ deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
i += m; i += m;
while (m--) { while (m--) {
deque->rightindex++; deque->rightindex++;
Py_INCREF(item); deque->rightblock->data[deque->rightindex] = Py_NewRef(item);
deque->rightblock->data[deque->rightindex] = item;
} }
} }
Py_SET_SIZE(deque, Py_SIZE(deque) + i); Py_SET_SIZE(deque, Py_SIZE(deque) + i);
Py_INCREF(deque); return Py_NewRef(deque);
return (PyObject *)deque;
} }
if ((size_t)size > PY_SSIZE_T_MAX / (size_t)n) { if ((size_t)size > PY_SSIZE_T_MAX / (size_t)n) {
@ -972,8 +966,7 @@ deque_count(dequeobject *deque, PyObject *v)
while (--n >= 0) { while (--n >= 0) {
CHECK_NOT_END(b); CHECK_NOT_END(b);
item = b->data[index]; item = Py_NewRef(b->data[index]);
Py_INCREF(item);
cmp = PyObject_RichCompareBool(item, v, Py_EQ); cmp = PyObject_RichCompareBool(item, v, Py_EQ);
Py_DECREF(item); Py_DECREF(item);
if (cmp < 0) if (cmp < 0)
@ -1011,8 +1004,7 @@ deque_contains(dequeobject *deque, PyObject *v)
while (--n >= 0) { while (--n >= 0) {
CHECK_NOT_END(b); CHECK_NOT_END(b);
item = b->data[index]; item = Py_NewRef(b->data[index]);
Py_INCREF(item);
cmp = PyObject_RichCompareBool(item, v, Py_EQ); cmp = PyObject_RichCompareBool(item, v, Py_EQ);
Py_DECREF(item); Py_DECREF(item);
if (cmp) { if (cmp) {
@ -1201,8 +1193,7 @@ deque_item(dequeobject *deque, Py_ssize_t i)
} }
} }
item = b->data[i]; item = b->data[i];
Py_INCREF(item); return Py_NewRef(item);
return item;
} }
static int static int
@ -1231,8 +1222,7 @@ deque_remove(dequeobject *deque, PyObject *value)
int cmp, rv; int cmp, rv;
for (i = 0 ; i < n; i++) { for (i = 0 ; i < n; i++) {
item = b->data[index]; item = Py_NewRef(b->data[index]);
Py_INCREF(item);
cmp = PyObject_RichCompareBool(item, value, Py_EQ); cmp = PyObject_RichCompareBool(item, value, Py_EQ);
Py_DECREF(item); Py_DECREF(item);
if (cmp < 0) { if (cmp < 0) {
@ -1292,9 +1282,8 @@ deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v)
while (--n >= 0) while (--n >= 0)
b = b->leftlink; b = b->leftlink;
} }
Py_INCREF(v);
old_value = b->data[i]; old_value = b->data[i];
b->data[i] = v; b->data[i] = Py_NewRef(v);
Py_DECREF(old_value); Py_DECREF(old_value);
return 0; return 0;
} }
@ -1686,8 +1675,7 @@ deque_iter(dequeobject *deque)
return NULL; return NULL;
it->b = deque->leftblock; it->b = deque->leftblock;
it->index = deque->leftindex; it->index = deque->leftindex;
Py_INCREF(deque); it->deque = (dequeobject*)Py_NewRef(deque);
it->deque = deque;
it->state = deque->state; it->state = deque->state;
it->counter = Py_SIZE(deque); it->counter = Py_SIZE(deque);
PyObject_GC_Track(it); PyObject_GC_Track(it);
@ -1734,8 +1722,7 @@ dequeiter_next(dequeiterobject *it)
it->b = it->b->rightlink; it->b = it->b->rightlink;
it->index = 0; it->index = 0;
} }
Py_INCREF(item); return Py_NewRef(item);
return item;
} }
static PyObject * static PyObject *
@ -1844,8 +1831,7 @@ deque_reviter(dequeobject *deque, PyObject *Py_UNUSED(ignored))
return NULL; return NULL;
it->b = deque->rightblock; it->b = deque->rightblock;
it->index = deque->rightindex; it->index = deque->rightindex;
Py_INCREF(deque); it->deque = (dequeobject*)Py_NewRef(deque);
it->deque = deque;
it->state = deque->state; it->state = deque->state;
it->counter = Py_SIZE(deque); it->counter = Py_SIZE(deque);
PyObject_GC_Track(it); PyObject_GC_Track(it);
@ -1876,8 +1862,7 @@ dequereviter_next(dequeiterobject *it)
it->b = it->b->leftlink; it->b = it->b->leftlink;
it->index = BLOCKLEN - 1; it->index = BLOCKLEN - 1;
} }
Py_INCREF(item); return Py_NewRef(item);
return item;
} }
static PyObject * static PyObject *
@ -2203,8 +2188,7 @@ defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
} }
if (newargs == NULL) if (newargs == NULL)
return -1; return -1;
Py_XINCREF(newdefault); dd->default_factory = Py_XNewRef(newdefault);
dd->default_factory = newdefault;
result = PyDict_Type.tp_init(self, newargs, kwds); result = PyDict_Type.tp_init(self, newargs, kwds);
Py_DECREF(newargs); Py_DECREF(newargs);
Py_XDECREF(olddefault); Py_XDECREF(olddefault);
@ -2414,8 +2398,7 @@ tuplegetter_new_impl(PyTypeObject *type, Py_ssize_t index, PyObject *doc)
return NULL; return NULL;
} }
self->index = index; self->index = index;
Py_INCREF(doc); self->doc = Py_NewRef(doc);
self->doc = doc;
return (PyObject *)self; return (PyObject *)self;
} }
@ -2426,13 +2409,11 @@ tuplegetter_descr_get(PyObject *self, PyObject *obj, PyObject *type)
PyObject *result; PyObject *result;
if (obj == NULL) { if (obj == NULL) {
Py_INCREF(self); return Py_NewRef(self);
return self;
} }
if (!PyTuple_Check(obj)) { if (!PyTuple_Check(obj)) {
if (obj == Py_None) { if (obj == Py_None) {
Py_INCREF(self); return Py_NewRef(self);
return self;
} }
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"descriptor for index '%zd' for tuple subclasses " "descriptor for index '%zd' for tuple subclasses "
@ -2448,8 +2429,7 @@ tuplegetter_descr_get(PyObject *self, PyObject *obj, PyObject *type)
} }
result = PyTuple_GET_ITEM(obj, index); result = PyTuple_GET_ITEM(obj, index);
Py_INCREF(result); return Py_NewRef(result);
return result;
} }
static int static int

View File

@ -176,8 +176,7 @@ get_char_or_None(Py_UCS4 c)
static PyObject * static PyObject *
Dialect_get_lineterminator(DialectObj *self, void *Py_UNUSED(ignored)) Dialect_get_lineterminator(DialectObj *self, void *Py_UNUSED(ignored))
{ {
Py_XINCREF(self->lineterminator); return Py_XNewRef(self->lineterminator);
return self->lineterminator;
} }
static PyObject * static PyObject *
@ -316,8 +315,7 @@ _set_str(const char *name, PyObject **target, PyObject *src, const char *dflt)
else { else {
if (PyUnicode_READY(src) == -1) if (PyUnicode_READY(src) == -1)
return -1; return -1;
Py_INCREF(src); Py_XSETREF(*target, Py_NewRef(src));
Py_XSETREF(*target, src);
} }
} }
return 0; return 0;
@ -514,8 +512,7 @@ dialect_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
goto err; goto err;
} }
ret = (PyObject *)self; ret = Py_NewRef(self);
Py_INCREF(self);
err: err:
Py_CLEAR(self); Py_CLEAR(self);
Py_CLEAR(dialect); Py_CLEAR(dialect);

View File

@ -261,8 +261,7 @@ PyCursesPanel_New(_curses_panel_state *state, PANEL *pan,
Py_DECREF(po); Py_DECREF(po);
return NULL; return NULL;
} }
po->wo = wo; po->wo = (PyCursesWindowObject*)Py_NewRef(wo);
Py_INCREF(wo);
return (PyObject *)po; return (PyObject *)po;
} }
@ -313,8 +312,7 @@ _curses_panel_panel_above_impl(PyCursesPanelObject *self)
"panel_above: can't find Panel Object"); "panel_above: can't find Panel Object");
return NULL; return NULL;
} }
Py_INCREF(po); return Py_NewRef(po);
return (PyObject *)po;
} }
/* panel_below(NULL) returns the top panel in the stack. To get /* panel_below(NULL) returns the top panel in the stack. To get
@ -344,8 +342,7 @@ _curses_panel_panel_below_impl(PyCursesPanelObject *self)
"panel_below: can't find Panel Object"); "panel_below: can't find Panel Object");
return NULL; return NULL;
} }
Py_INCREF(po); return Py_NewRef(po);
return (PyObject *)po;
} }
/*[clinic input] /*[clinic input]
@ -394,8 +391,7 @@ static PyObject *
_curses_panel_panel_window_impl(PyCursesPanelObject *self) _curses_panel_panel_window_impl(PyCursesPanelObject *self)
/*[clinic end generated code: output=5f05940d4106b4cb input=6067353d2c307901]*/ /*[clinic end generated code: output=5f05940d4106b4cb input=6067353d2c307901]*/
{ {
Py_INCREF(self->wo); return Py_NewRef(self->wo);
return (PyObject *)self->wo;
} }
/*[clinic input] /*[clinic input]
@ -428,8 +424,7 @@ _curses_panel_panel_replace_impl(PyCursesPanelObject *self,
PyErr_SetString(state->PyCursesError, "replace_panel() returned ERR"); PyErr_SetString(state->PyCursesError, "replace_panel() returned ERR");
return NULL; return NULL;
} }
Py_INCREF(win); Py_SETREF(po->wo, Py_NewRef(win));
Py_SETREF(po->wo, win);
Py_RETURN_NONE; Py_RETURN_NONE;
} }
@ -486,8 +481,7 @@ _curses_panel_panel_userptr_impl(PyCursesPanelObject *self,
return NULL; return NULL;
} }
Py_INCREF(obj); return Py_NewRef(obj);
return obj;
} }
@ -555,8 +549,7 @@ _curses_panel_bottom_panel_impl(PyObject *module)
"panel_above: can't find Panel Object"); "panel_above: can't find Panel Object");
return NULL; return NULL;
} }
Py_INCREF(po); return Py_NewRef(po);
return (PyObject *)po;
} }
/*[clinic input] /*[clinic input]
@ -614,8 +607,7 @@ _curses_panel_top_panel_impl(PyObject *module)
"panel_below: can't find Panel Object"); "panel_below: can't find Panel Object");
return NULL; return NULL;
} }
Py_INCREF(po); return Py_NewRef(po);
return (PyObject *)po;
} }
/*[clinic input] /*[clinic input]
@ -670,8 +662,7 @@ _curses_panel_exec(PyObject *mod)
state->PyCursesError = PyErr_NewException( state->PyCursesError = PyErr_NewException(
"_curses_panel.error", NULL, NULL); "_curses_panel.error", NULL, NULL);
Py_INCREF(state->PyCursesError); if (PyModule_AddObject(mod, "error", Py_NewRef(state->PyCursesError)) < 0) {
if (PyModule_AddObject(mod, "error", state->PyCursesError) < 0) {
Py_DECREF(state->PyCursesError); Py_DECREF(state->PyCursesError);
return -1; return -1;
} }

View File

@ -389,8 +389,7 @@ PyCurses_ConvertToString(PyCursesWindowObject *win, PyObject *obj,
#endif #endif
} }
else if (PyBytes_Check(obj)) { else if (PyBytes_Check(obj)) {
Py_INCREF(obj); *bytes = Py_NewRef(obj);
*bytes = obj;
/* check for embedded null bytes */ /* check for embedded null bytes */
if (PyBytes_AsStringAndSize(*bytes, &str, NULL) < 0) { if (PyBytes_AsStringAndSize(*bytes, &str, NULL) < 0) {
Py_DECREF(obj); Py_DECREF(obj);

View File

@ -358,8 +358,7 @@ _dbm_dbm_get_impl(dbmobject *self, PyTypeObject *cls, const char *key,
return PyBytes_FromStringAndSize(val.dptr, val.dsize); return PyBytes_FromStringAndSize(val.dptr, val.dsize);
} }
Py_INCREF(default_value); return Py_NewRef(default_value);
return default_value;
} }
/*[clinic input] /*[clinic input]
@ -419,8 +418,7 @@ _dbm_dbm_setdefault_impl(dbmobject *self, PyTypeObject *cls, const char *key,
static PyObject * static PyObject *
dbm__enter__(PyObject *self, PyObject *args) dbm__enter__(PyObject *self, PyObject *args)
{ {
Py_INCREF(self); return Py_NewRef(self);
return self;
} }
static PyObject * static PyObject *

View File

@ -116,15 +116,13 @@ static PyTypeObject PyDecContextManager_Type;
Py_LOCAL_INLINE(PyObject *) Py_LOCAL_INLINE(PyObject *)
incr_true(void) incr_true(void)
{ {
Py_INCREF(Py_True); return Py_NewRef(Py_True);
return Py_True;
} }
Py_LOCAL_INLINE(PyObject *) Py_LOCAL_INLINE(PyObject *)
incr_false(void) incr_false(void)
{ {
Py_INCREF(Py_False); return Py_NewRef(Py_False);
return Py_False;
} }
@ -655,8 +653,7 @@ signaldict_richcompare(PyObject *v, PyObject *w, int op)
} }
} }
Py_INCREF(res); return Py_NewRef(res);
return res;
} }
static PyObject * static PyObject *
@ -754,8 +751,7 @@ context_getround(PyObject *self, void *closure UNUSED)
{ {
int i = mpd_getround(CTX(self)); int i = mpd_getround(CTX(self));
Py_INCREF(round_map[i]); return Py_NewRef(round_map[i]);
return round_map[i];
} }
static PyObject * static PyObject *
@ -1122,13 +1118,11 @@ context_getattr(PyObject *self, PyObject *name)
if (PyUnicode_Check(name)) { if (PyUnicode_Check(name)) {
if (PyUnicode_CompareWithASCIIString(name, "traps") == 0) { if (PyUnicode_CompareWithASCIIString(name, "traps") == 0) {
retval = ((PyDecContextObject *)self)->traps; retval = ((PyDecContextObject *)self)->traps;
Py_INCREF(retval); return Py_NewRef(retval);
return retval;
} }
if (PyUnicode_CompareWithASCIIString(name, "flags") == 0) { if (PyUnicode_CompareWithASCIIString(name, "flags") == 0) {
retval = ((PyDecContextObject *)self)->flags; retval = ((PyDecContextObject *)self)->flags;
Py_INCREF(retval); return Py_NewRef(retval);
return retval;
} }
} }
@ -1602,8 +1596,7 @@ PyDec_GetCurrentContext(PyObject *self UNUSED, PyObject *args UNUSED)
return NULL; return NULL;
} }
Py_INCREF(context); return Py_NewRef(context);
return context;
} }
/* Set the thread local context to a new context, decrement old reference */ /* Set the thread local context to a new context, decrement old reference */
@ -1778,8 +1771,7 @@ ctxmanager_new(PyTypeObject *type UNUSED, PyObject *args, PyObject *kwds)
Py_DECREF(self); Py_DECREF(self);
return NULL; return NULL;
} }
self->global = global; self->global = Py_NewRef(global);
Py_INCREF(self->global);
int ret = context_setattrs( int ret = context_setattrs(
self->local, prec, rounding, self->local, prec, rounding,
@ -1814,8 +1806,7 @@ ctxmanager_set_local(PyDecContextManagerObject *self, PyObject *args UNUSED)
} }
Py_DECREF(ret); Py_DECREF(ret);
Py_INCREF(self->local); return Py_NewRef(self->local);
return self->local;
} }
static PyObject * static PyObject *
@ -2418,8 +2409,7 @@ PyDecType_FromDecimalExact(PyTypeObject *type, PyObject *v, PyObject *context)
uint32_t status = 0; uint32_t status = 0;
if (type == &PyDec_Type && PyDec_CheckExact(v)) { if (type == &PyDec_Type && PyDec_CheckExact(v)) {
Py_INCREF(v); return Py_NewRef(v);
return v;
} }
dec = PyDecType_New(type); dec = PyDecType_New(type);
@ -2440,8 +2430,7 @@ static PyObject *
sequence_as_tuple(PyObject *v, PyObject *ex, const char *mesg) sequence_as_tuple(PyObject *v, PyObject *ex, const char *mesg)
{ {
if (PyTuple_Check(v)) { if (PyTuple_Check(v)) {
Py_INCREF(v); return Py_NewRef(v);
return v;
} }
if (PyList_Check(v)) { if (PyList_Check(v)) {
return PyList_AsTuple(v); return PyList_AsTuple(v);
@ -2863,8 +2852,7 @@ convert_op(int type_err, PyObject **conv, PyObject *v, PyObject *context)
{ {
if (PyDec_Check(v)) { if (PyDec_Check(v)) {
*conv = v; *conv = Py_NewRef(v);
Py_INCREF(v);
return 1; return 1;
} }
if (PyLong_Check(v)) { if (PyLong_Check(v)) {
@ -2881,8 +2869,7 @@ convert_op(int type_err, PyObject **conv, PyObject *v, PyObject *context)
Py_TYPE(v)->tp_name); Py_TYPE(v)->tp_name);
} }
else { else {
Py_INCREF(Py_NotImplemented); *conv = Py_NewRef(Py_NotImplemented);
*conv = Py_NotImplemented;
} }
return 0; return 0;
} }
@ -3041,8 +3028,7 @@ convert_op_cmp(PyObject **vcmp, PyObject **wcmp, PyObject *v, PyObject *w,
*vcmp = v; *vcmp = v;
if (PyDec_Check(w)) { if (PyDec_Check(w)) {
Py_INCREF(w); *wcmp = Py_NewRef(w);
*wcmp = w;
} }
else if (PyLong_Check(w)) { else if (PyLong_Check(w)) {
*wcmp = PyDec_FromLongExact(w, context); *wcmp = PyDec_FromLongExact(w, context);
@ -3074,8 +3060,7 @@ convert_op_cmp(PyObject **vcmp, PyObject **wcmp, PyObject *v, PyObject *w,
} }
} }
else { else {
Py_INCREF(Py_NotImplemented); *wcmp = Py_NewRef(Py_NotImplemented);
*wcmp = Py_NotImplemented;
} }
} }
else { else {
@ -3093,8 +3078,7 @@ convert_op_cmp(PyObject **vcmp, PyObject **wcmp, PyObject *v, PyObject *w,
} }
} }
else { else {
Py_INCREF(Py_NotImplemented); *wcmp = Py_NewRef(Py_NotImplemented);
*wcmp = Py_NotImplemented;
} }
} }
@ -4329,15 +4313,13 @@ dec_mpd_adjexp(PyObject *self, PyObject *dummy UNUSED)
static PyObject * static PyObject *
dec_canonical(PyObject *self, PyObject *dummy UNUSED) dec_canonical(PyObject *self, PyObject *dummy UNUSED)
{ {
Py_INCREF(self); return Py_NewRef(self);
return self;
} }
static PyObject * static PyObject *
dec_conjugate(PyObject *self, PyObject *dummy UNUSED) dec_conjugate(PyObject *self, PyObject *dummy UNUSED)
{ {
Py_INCREF(self); return Py_NewRef(self);
return self;
} }
static PyObject * static PyObject *
@ -4654,8 +4636,7 @@ dec_complex(PyObject *self, PyObject *dummy UNUSED)
static PyObject * static PyObject *
dec_copy(PyObject *self, PyObject *dummy UNUSED) dec_copy(PyObject *self, PyObject *dummy UNUSED)
{ {
Py_INCREF(self); return Py_NewRef(self);
return self;
} }
/* __floor__ */ /* __floor__ */
@ -4838,8 +4819,7 @@ dec_trunc(PyObject *self, PyObject *dummy UNUSED)
static PyObject * static PyObject *
dec_real(PyObject *self, void *closure UNUSED) dec_real(PyObject *self, void *closure UNUSED)
{ {
Py_INCREF(self); return Py_NewRef(self);
return self;
} }
static PyObject * static PyObject *
@ -5384,8 +5364,7 @@ ctx_canonical(PyObject *context UNUSED, PyObject *v)
return NULL; return NULL;
} }
Py_INCREF(v); return Py_NewRef(v);
return v;
} }
static PyObject * static PyObject *
@ -5916,23 +5895,17 @@ PyInit__decimal(void)
/* Create the module */ /* Create the module */
ASSIGN_PTR(m, PyModule_Create(&_decimal_module)); ASSIGN_PTR(m, PyModule_Create(&_decimal_module));
/* Add types to the module */ /* Add types to the module */
Py_INCREF(&PyDec_Type); CHECK_INT(PyModule_AddObject(m, "Decimal", Py_NewRef(&PyDec_Type)));
CHECK_INT(PyModule_AddObject(m, "Decimal", (PyObject *)&PyDec_Type));
Py_INCREF(&PyDecContext_Type);
CHECK_INT(PyModule_AddObject(m, "Context", CHECK_INT(PyModule_AddObject(m, "Context",
(PyObject *)&PyDecContext_Type)); Py_NewRef(&PyDecContext_Type)));
Py_INCREF(DecimalTuple); CHECK_INT(PyModule_AddObject(m, "DecimalTuple", Py_NewRef(DecimalTuple)));
CHECK_INT(PyModule_AddObject(m, "DecimalTuple", (PyObject *)DecimalTuple));
/* Create top level exception */ /* Create top level exception */
ASSIGN_PTR(DecimalException, PyErr_NewException( ASSIGN_PTR(DecimalException, PyErr_NewException(
"decimal.DecimalException", "decimal.DecimalException",
PyExc_ArithmeticError, NULL)); PyExc_ArithmeticError, NULL));
Py_INCREF(DecimalException); CHECK_INT(PyModule_AddObject(m, "DecimalException", Py_NewRef(DecimalException)));
CHECK_INT(PyModule_AddObject(m, "DecimalException", DecimalException));
/* Create signal tuple */ /* Create signal tuple */
ASSIGN_PTR(SignalTuple, PyTuple_New(SIGNAL_MAP_LEN)); ASSIGN_PTR(SignalTuple, PyTuple_New(SIGNAL_MAP_LEN));
@ -5972,12 +5945,10 @@ PyInit__decimal(void)
Py_DECREF(base); Py_DECREF(base);
/* add to module */ /* add to module */
Py_INCREF(cm->ex); CHECK_INT(PyModule_AddObject(m, cm->name, Py_NewRef(cm->ex)));
CHECK_INT(PyModule_AddObject(m, cm->name, cm->ex));
/* add to signal tuple */ /* add to signal tuple */
Py_INCREF(cm->ex); PyTuple_SET_ITEM(SignalTuple, i, Py_NewRef(cm->ex));
PyTuple_SET_ITEM(SignalTuple, i, cm->ex);
} }
/* /*
@ -6003,45 +5974,38 @@ PyInit__decimal(void)
ASSIGN_PTR(cm->ex, PyErr_NewException(cm->fqname, base, NULL)); ASSIGN_PTR(cm->ex, PyErr_NewException(cm->fqname, base, NULL));
Py_DECREF(base); Py_DECREF(base);
Py_INCREF(cm->ex); CHECK_INT(PyModule_AddObject(m, cm->name, Py_NewRef(cm->ex)));
CHECK_INT(PyModule_AddObject(m, cm->name, cm->ex));
} }
/* Init default context template first */ /* Init default context template first */
ASSIGN_PTR(default_context_template, ASSIGN_PTR(default_context_template,
PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL)); PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL));
Py_INCREF(default_context_template);
CHECK_INT(PyModule_AddObject(m, "DefaultContext", CHECK_INT(PyModule_AddObject(m, "DefaultContext",
default_context_template)); Py_NewRef(default_context_template)));
#ifndef WITH_DECIMAL_CONTEXTVAR #ifndef WITH_DECIMAL_CONTEXTVAR
ASSIGN_PTR(tls_context_key, PyUnicode_FromString("___DECIMAL_CTX__")); ASSIGN_PTR(tls_context_key, PyUnicode_FromString("___DECIMAL_CTX__"));
Py_INCREF(Py_False); CHECK_INT(PyModule_AddObject(m, "HAVE_CONTEXTVAR", Py_NewRef(Py_False)));
CHECK_INT(PyModule_AddObject(m, "HAVE_CONTEXTVAR", Py_False));
#else #else
ASSIGN_PTR(current_context_var, PyContextVar_New("decimal_context", NULL)); ASSIGN_PTR(current_context_var, PyContextVar_New("decimal_context", NULL));
Py_INCREF(Py_True); CHECK_INT(PyModule_AddObject(m, "HAVE_CONTEXTVAR", Py_NewRef(Py_True)));
CHECK_INT(PyModule_AddObject(m, "HAVE_CONTEXTVAR", Py_True));
#endif #endif
Py_INCREF(Py_True); CHECK_INT(PyModule_AddObject(m, "HAVE_THREADS", Py_NewRef(Py_True)));
CHECK_INT(PyModule_AddObject(m, "HAVE_THREADS", Py_True));
/* Init basic context template */ /* Init basic context template */
ASSIGN_PTR(basic_context_template, ASSIGN_PTR(basic_context_template,
PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL)); PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL));
init_basic_context(basic_context_template); init_basic_context(basic_context_template);
Py_INCREF(basic_context_template);
CHECK_INT(PyModule_AddObject(m, "BasicContext", CHECK_INT(PyModule_AddObject(m, "BasicContext",
basic_context_template)); Py_NewRef(basic_context_template)));
/* Init extended context template */ /* Init extended context template */
ASSIGN_PTR(extended_context_template, ASSIGN_PTR(extended_context_template,
PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL)); PyObject_CallObject((PyObject *)&PyDecContext_Type, NULL));
init_extended_context(extended_context_template); init_extended_context(extended_context_template);
Py_INCREF(extended_context_template);
CHECK_INT(PyModule_AddObject(m, "ExtendedContext", CHECK_INT(PyModule_AddObject(m, "ExtendedContext",
extended_context_template)); Py_NewRef(extended_context_template)));
/* Init mpd_ssize_t constants */ /* Init mpd_ssize_t constants */
@ -6060,8 +6024,7 @@ PyInit__decimal(void)
/* Init string constants */ /* Init string constants */
for (i = 0; i < _PY_DEC_ROUND_GUARD; i++) { for (i = 0; i < _PY_DEC_ROUND_GUARD; i++) {
ASSIGN_PTR(round_map[i], PyUnicode_InternFromString(mpd_round_string[i])); ASSIGN_PTR(round_map[i], PyUnicode_InternFromString(mpd_round_string[i]));
Py_INCREF(round_map[i]); CHECK_INT(PyModule_AddObject(m, mpd_round_string[i], Py_NewRef(round_map[i])));
CHECK_INT(PyModule_AddObject(m, mpd_round_string[i], round_map[i]));
} }
/* Add specification version number */ /* Add specification version number */

View File

@ -105,8 +105,7 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
if (pto == NULL) if (pto == NULL)
return NULL; return NULL;
pto->fn = func; pto->fn = Py_NewRef(func);
Py_INCREF(func);
nargs = PyTuple_GetSlice(args, 1, PY_SSIZE_T_MAX); nargs = PyTuple_GetSlice(args, 1, PY_SSIZE_T_MAX);
if (nargs == NULL) { if (nargs == NULL) {
@ -131,8 +130,7 @@ partial_new(PyTypeObject *type, PyObject *args, PyObject *kw)
pto->kw = PyDict_New(); pto->kw = PyDict_New();
} }
else if (Py_REFCNT(kw) == 1) { else if (Py_REFCNT(kw) == 1) {
Py_INCREF(kw); pto->kw = Py_NewRef(kw);
pto->kw = kw;
} }
else { else {
pto->kw = PyDict_Copy(kw); pto->kw = PyDict_Copy(kw);
@ -302,8 +300,7 @@ partial_call(partialobject *pto, PyObject *args, PyObject *kwargs)
PyObject *kwargs2; PyObject *kwargs2;
if (PyDict_GET_SIZE(pto->kw) == 0) { if (PyDict_GET_SIZE(pto->kw) == 0) {
/* kwargs can be NULL */ /* kwargs can be NULL */
kwargs2 = kwargs; kwargs2 = Py_XNewRef(kwargs);
Py_XINCREF(kwargs2);
} }
else { else {
/* bpo-27840, bpo-29318: dictionary of keyword parameters must be /* bpo-27840, bpo-29318: dictionary of keyword parameters must be
@ -463,8 +460,7 @@ partial_setstate(partialobject *pto, PyObject *state)
else else
Py_INCREF(dict); Py_INCREF(dict);
Py_INCREF(fn); Py_SETREF(pto->fn, Py_NewRef(fn));
Py_SETREF(pto->fn, fn);
Py_SETREF(pto->args, fnargs); Py_SETREF(pto->args, fnargs);
Py_SETREF(pto->kw, kw); Py_SETREF(pto->kw, kw);
Py_XSETREF(pto->dict, dict); Py_XSETREF(pto->dict, dict);
@ -588,10 +584,8 @@ keyobject_call(keyobject *ko, PyObject *args, PyObject *kwds)
if (result == NULL) { if (result == NULL) {
return NULL; return NULL;
} }
Py_INCREF(ko->cmp); result->cmp = Py_NewRef(ko->cmp);
result->cmp = ko->cmp; result->object = Py_NewRef(object);
Py_INCREF(object);
result->object = object;
PyObject_GC_Track(result); PyObject_GC_Track(result);
return (PyObject *)result; return (PyObject *)result;
} }
@ -654,8 +648,7 @@ _functools_cmp_to_key_impl(PyObject *module, PyObject *mycmp)
object = PyObject_GC_New(keyobject, state->keyobject_type); object = PyObject_GC_New(keyobject, state->keyobject_type);
if (!object) if (!object)
return NULL; return NULL;
Py_INCREF(mycmp); object->cmp = Py_NewRef(mycmp);
object->cmp = mycmp;
object->object = NULL; object->object = NULL;
PyObject_GC_Track(object); PyObject_GC_Track(object);
return (PyObject *)object; return (PyObject *)object;
@ -837,12 +830,10 @@ lru_cache_make_key(PyObject *kwd_mark, PyObject *args,
if (PyUnicode_CheckExact(key) || PyLong_CheckExact(key)) { if (PyUnicode_CheckExact(key) || PyLong_CheckExact(key)) {
/* For common scalar keys, save space by /* For common scalar keys, save space by
dropping the enclosing args tuple */ dropping the enclosing args tuple */
Py_INCREF(key); return Py_NewRef(key);
return key;
} }
} }
Py_INCREF(args); return Py_NewRef(args);
return args;
} }
key_size = PyTuple_GET_SIZE(args); key_size = PyTuple_GET_SIZE(args);
@ -858,31 +849,25 @@ lru_cache_make_key(PyObject *kwd_mark, PyObject *args,
key_pos = 0; key_pos = 0;
for (pos = 0; pos < PyTuple_GET_SIZE(args); ++pos) { for (pos = 0; pos < PyTuple_GET_SIZE(args); ++pos) {
PyObject *item = PyTuple_GET_ITEM(args, pos); PyObject *item = PyTuple_GET_ITEM(args, pos);
Py_INCREF(item); PyTuple_SET_ITEM(key, key_pos++, Py_NewRef(item));
PyTuple_SET_ITEM(key, key_pos++, item);
} }
if (kwds_size) { if (kwds_size) {
Py_INCREF(kwd_mark); PyTuple_SET_ITEM(key, key_pos++, Py_NewRef(kwd_mark));
PyTuple_SET_ITEM(key, key_pos++, kwd_mark);
for (pos = 0; PyDict_Next(kwds, &pos, &keyword, &value);) { for (pos = 0; PyDict_Next(kwds, &pos, &keyword, &value);) {
Py_INCREF(keyword); PyTuple_SET_ITEM(key, key_pos++, Py_NewRef(keyword));
PyTuple_SET_ITEM(key, key_pos++, keyword); PyTuple_SET_ITEM(key, key_pos++, Py_NewRef(value));
Py_INCREF(value);
PyTuple_SET_ITEM(key, key_pos++, value);
} }
assert(key_pos == PyTuple_GET_SIZE(args) + kwds_size * 2 + 1); assert(key_pos == PyTuple_GET_SIZE(args) + kwds_size * 2 + 1);
} }
if (typed) { if (typed) {
for (pos = 0; pos < PyTuple_GET_SIZE(args); ++pos) { for (pos = 0; pos < PyTuple_GET_SIZE(args); ++pos) {
PyObject *item = (PyObject *)Py_TYPE(PyTuple_GET_ITEM(args, pos)); PyObject *item = (PyObject *)Py_TYPE(PyTuple_GET_ITEM(args, pos));
Py_INCREF(item); PyTuple_SET_ITEM(key, key_pos++, Py_NewRef(item));
PyTuple_SET_ITEM(key, key_pos++, item);
} }
if (kwds_size) { if (kwds_size) {
for (pos = 0; PyDict_Next(kwds, &pos, &keyword, &value);) { for (pos = 0; PyDict_Next(kwds, &pos, &keyword, &value);) {
PyObject *item = (PyObject *)Py_TYPE(value); PyObject *item = (PyObject *)Py_TYPE(value);
Py_INCREF(item); PyTuple_SET_ITEM(key, key_pos++, Py_NewRef(item));
PyTuple_SET_ITEM(key, key_pos++, item);
} }
} }
} }
@ -1084,8 +1069,7 @@ bounded_lru_cache_wrapper(lru_cache_object *self, PyObject *args, PyObject *kwds
return NULL; return NULL;
} }
lru_cache_append_link(self, link); lru_cache_append_link(self, link);
Py_INCREF(result); /* for return */ return Py_NewRef(result);
return result;
} }
/* Since the cache is full, we need to evict an old key and add /* Since the cache is full, we need to evict an old key and add
a new key. Rather than free the old link and allocate a new a new key. Rather than free the old link and allocate a new
@ -1230,16 +1214,12 @@ lru_cache_new(PyTypeObject *type, PyObject *args, PyObject *kw)
obj->wrapper = wrapper; obj->wrapper = wrapper;
obj->typed = typed; obj->typed = typed;
obj->cache = cachedict; obj->cache = cachedict;
Py_INCREF(func); obj->func = Py_NewRef(func);
obj->func = func;
obj->misses = obj->hits = 0; obj->misses = obj->hits = 0;
obj->maxsize = maxsize; obj->maxsize = maxsize;
Py_INCREF(state->kwd_mark); obj->kwd_mark = Py_NewRef(state->kwd_mark);
obj->kwd_mark = state->kwd_mark; obj->lru_list_elem_type = (PyTypeObject*)Py_NewRef(state->lru_list_elem_type);
Py_INCREF(state->lru_list_elem_type); obj->cache_info_type = Py_NewRef(cache_info_type);
obj->lru_list_elem_type = state->lru_list_elem_type;
Py_INCREF(cache_info_type);
obj->cache_info_type = cache_info_type;
obj->dict = NULL; obj->dict = NULL;
obj->weakreflist = NULL; obj->weakreflist = NULL;
return (PyObject *)obj; return (PyObject *)obj;
@ -1306,8 +1286,7 @@ static PyObject *
lru_cache_descr_get(PyObject *self, PyObject *obj, PyObject *type) lru_cache_descr_get(PyObject *self, PyObject *obj, PyObject *type)
{ {
if (obj == Py_None || obj == NULL) { if (obj == Py_None || obj == NULL) {
Py_INCREF(self); return Py_NewRef(self);
return self;
} }
return PyMethod_New(self, obj); return PyMethod_New(self, obj);
} }
@ -1360,15 +1339,13 @@ lru_cache_reduce(PyObject *self, PyObject *unused)
static PyObject * static PyObject *
lru_cache_copy(PyObject *self, PyObject *unused) lru_cache_copy(PyObject *self, PyObject *unused)
{ {
Py_INCREF(self); return Py_NewRef(self);
return self;
} }
static PyObject * static PyObject *
lru_cache_deepcopy(PyObject *self, PyObject *unused) lru_cache_deepcopy(PyObject *self, PyObject *unused)
{ {
Py_INCREF(self); return Py_NewRef(self);
return self;
} }
static int static int

View File

@ -256,8 +256,7 @@ _gdbm_gdbm_get_impl(gdbmobject *self, PyObject *key, PyObject *default_value)
res = gdbm_subscript(self, key); res = gdbm_subscript(self, key);
if (res == NULL && PyErr_ExceptionMatches(PyExc_KeyError)) { if (res == NULL && PyErr_ExceptionMatches(PyExc_KeyError)) {
PyErr_Clear(); PyErr_Clear();
Py_INCREF(default_value); return Py_NewRef(default_value);
return default_value;
} }
return res; return res;
} }
@ -566,8 +565,7 @@ _gdbm_gdbm_sync_impl(gdbmobject *self, PyTypeObject *cls)
static PyObject * static PyObject *
gdbm__enter__(PyObject *self, PyObject *args) gdbm__enter__(PyObject *self, PyObject *args)
{ {
Py_INCREF(self); return Py_NewRef(self);
return self;
} }
static PyObject * static PyObject *

View File

@ -197,8 +197,7 @@ heapreplace_internal(PyObject *heap, PyObject *item, int siftup_func(PyListObjec
} }
returnitem = PyList_GET_ITEM(heap, 0); returnitem = PyList_GET_ITEM(heap, 0);
Py_INCREF(item); PyList_SET_ITEM(heap, 0, Py_NewRef(item));
PyList_SET_ITEM(heap, 0, item);
if (siftup_func((PyListObject *)heap, 0)) { if (siftup_func((PyListObject *)heap, 0)) {
Py_DECREF(returnitem); Py_DECREF(returnitem);
return NULL; return NULL;
@ -253,8 +252,7 @@ _heapq_heappushpop_impl(PyObject *module, PyObject *heap, PyObject *item)
int cmp; int cmp;
if (PyList_GET_SIZE(heap) == 0) { if (PyList_GET_SIZE(heap) == 0) {
Py_INCREF(item); return Py_NewRef(item);
return item;
} }
PyObject* top = PyList_GET_ITEM(heap, 0); PyObject* top = PyList_GET_ITEM(heap, 0);
@ -264,8 +262,7 @@ _heapq_heappushpop_impl(PyObject *module, PyObject *heap, PyObject *item)
if (cmp < 0) if (cmp < 0)
return NULL; return NULL;
if (cmp == 0) { if (cmp == 0) {
Py_INCREF(item); return Py_NewRef(item);
return item;
} }
if (PyList_GET_SIZE(heap) == 0) { if (PyList_GET_SIZE(heap) == 0) {
@ -274,8 +271,7 @@ _heapq_heappushpop_impl(PyObject *module, PyObject *heap, PyObject *item)
} }
returnitem = PyList_GET_ITEM(heap, 0); returnitem = PyList_GET_ITEM(heap, 0);
Py_INCREF(item); PyList_SET_ITEM(heap, 0, Py_NewRef(item));
PyList_SET_ITEM(heap, 0, item);
if (siftup((PyListObject *)heap, 0)) { if (siftup((PyListObject *)heap, 0)) {
Py_DECREF(returnitem); Py_DECREF(returnitem);
return NULL; return NULL;
@ -410,8 +406,7 @@ siftdown_max(PyListObject *heap, Py_ssize_t startpos, Py_ssize_t pos)
newitem = arr[pos]; newitem = arr[pos];
while (pos > startpos) { while (pos > startpos) {
parentpos = (pos - 1) >> 1; parentpos = (pos - 1) >> 1;
parent = arr[parentpos]; parent = Py_NewRef(arr[parentpos]);
Py_INCREF(parent);
Py_INCREF(newitem); Py_INCREF(newitem);
cmp = PyObject_RichCompareBool(parent, newitem, Py_LT); cmp = PyObject_RichCompareBool(parent, newitem, Py_LT);
Py_DECREF(parent); Py_DECREF(parent);