Skip to content

Commit

Permalink
gh-99300: Use Py_NewRef() in Modules/ directory (#99466)
Browse files Browse the repository at this point in the history
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in test C files of the Modules/ directory.
  • Loading branch information
vstinner authored Nov 14, 2022
1 parent db11568 commit 3817607
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 214 deletions.
18 changes: 6 additions & 12 deletions Modules/_abc.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,7 @@ _abc__abc_register_impl(PyObject *module, PyObject *self, PyObject *subclass)
}
int result = PyObject_IsSubclass(subclass, self);
if (result > 0) {
Py_INCREF(subclass);
return subclass; /* Already a subclass. */
return Py_NewRef(subclass); /* Already a subclass. */
}
if (result < 0) {
return NULL;
Expand Down Expand Up @@ -561,8 +560,7 @@ _abc__abc_register_impl(PyObject *module, PyObject *self, PyObject *subclass)
set_collection_flag_recursive((PyTypeObject *)subclass, collection_flag);
}
}
Py_INCREF(subclass);
return subclass;
return Py_NewRef(subclass);
}


Expand Down Expand Up @@ -598,8 +596,7 @@ _abc__abc_instancecheck_impl(PyObject *module, PyObject *self,
goto end;
}
if (incache > 0) {
result = Py_True;
Py_INCREF(result);
result = Py_NewRef(Py_True);
goto end;
}
subtype = (PyObject *)Py_TYPE(instance);
Expand All @@ -610,8 +607,7 @@ _abc__abc_instancecheck_impl(PyObject *module, PyObject *self,
goto end;
}
if (incache > 0) {
result = Py_False;
Py_INCREF(result);
result = Py_NewRef(Py_False);
goto end;
}
}
Expand Down Expand Up @@ -802,8 +798,7 @@ _abc__abc_subclasscheck_impl(PyObject *module, PyObject *self,
end:
Py_DECREF(impl);
Py_XDECREF(subclasses);
Py_XINCREF(result);
return result;
return Py_XNewRef(result);
}


Expand Down Expand Up @@ -842,8 +837,7 @@ subclasscheck_check_registry(_abc_data *impl, PyObject *subclass,
Py_ssize_t i = 0;

while (_PySet_NextEntry(impl->_abc_registry, &pos, &key, &hash)) {
Py_INCREF(key);
copy[i++] = key;
copy[i++] = Py_NewRef(key);
}
assert(i == registry_size);

Expand Down
60 changes: 20 additions & 40 deletions Modules/_collectionsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,7 @@ deque_append_internal(dequeobject *deque, PyObject *item, Py_ssize_t maxlen)
static PyObject *
deque_append(dequeobject *deque, PyObject *item)
{
Py_INCREF(item);
if (deque_append_internal(deque, item, deque->maxlen) < 0)
if (deque_append_internal(deque, Py_NewRef(item), deque->maxlen) < 0)
return NULL;
Py_RETURN_NONE;
}
Expand Down Expand Up @@ -336,8 +335,7 @@ deque_appendleft_internal(dequeobject *deque, PyObject *item, Py_ssize_t maxlen)
static PyObject *
deque_appendleft(dequeobject *deque, PyObject *item)
{
Py_INCREF(item);
if (deque_appendleft_internal(deque, item, deque->maxlen) < 0)
if (deque_appendleft_internal(deque, Py_NewRef(item), deque->maxlen) < 0)
return NULL;
Py_RETURN_NONE;
}
Expand Down Expand Up @@ -655,14 +653,12 @@ deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)

size = Py_SIZE(deque);
if (size == 0 || n == 1) {
Py_INCREF(deque);
return (PyObject *)deque;
return Py_NewRef(deque);
}

if (n <= 0) {
deque_clear(deque);
Py_INCREF(deque);
return (PyObject *)deque;
return Py_NewRef(deque);
}

if (size == 1) {
Expand Down Expand Up @@ -693,13 +689,11 @@ deque_inplace_repeat(dequeobject *deque, Py_ssize_t n)
i += m;
while (m--) {
deque->rightindex++;
Py_INCREF(item);
deque->rightblock->data[deque->rightindex] = item;
deque->rightblock->data[deque->rightindex] = Py_NewRef(item);
}
}
Py_SET_SIZE(deque, Py_SIZE(deque) + i);
Py_INCREF(deque);
return (PyObject *)deque;
return Py_NewRef(deque);
}

if ((size_t)size > PY_SSIZE_T_MAX / (size_t)n) {
Expand Down Expand Up @@ -972,8 +966,7 @@ deque_count(dequeobject *deque, PyObject *v)

while (--n >= 0) {
CHECK_NOT_END(b);
item = b->data[index];
Py_INCREF(item);
item = Py_NewRef(b->data[index]);
cmp = PyObject_RichCompareBool(item, v, Py_EQ);
Py_DECREF(item);
if (cmp < 0)
Expand Down Expand Up @@ -1011,8 +1004,7 @@ deque_contains(dequeobject *deque, PyObject *v)

while (--n >= 0) {
CHECK_NOT_END(b);
item = b->data[index];
Py_INCREF(item);
item = Py_NewRef(b->data[index]);
cmp = PyObject_RichCompareBool(item, v, Py_EQ);
Py_DECREF(item);
if (cmp) {
Expand Down Expand Up @@ -1201,8 +1193,7 @@ deque_item(dequeobject *deque, Py_ssize_t i)
}
}
item = b->data[i];
Py_INCREF(item);
return item;
return Py_NewRef(item);
}

static int
Expand Down Expand Up @@ -1231,8 +1222,7 @@ deque_remove(dequeobject *deque, PyObject *value)
int cmp, rv;

for (i = 0 ; i < n; i++) {
item = b->data[index];
Py_INCREF(item);
item = Py_NewRef(b->data[index]);
cmp = PyObject_RichCompareBool(item, value, Py_EQ);
Py_DECREF(item);
if (cmp < 0) {
Expand Down Expand Up @@ -1292,9 +1282,8 @@ deque_ass_item(dequeobject *deque, Py_ssize_t i, PyObject *v)
while (--n >= 0)
b = b->leftlink;
}
Py_INCREF(v);
old_value = b->data[i];
b->data[i] = v;
b->data[i] = Py_NewRef(v);
Py_DECREF(old_value);
return 0;
}
Expand Down Expand Up @@ -1686,8 +1675,7 @@ deque_iter(dequeobject *deque)
return NULL;
it->b = deque->leftblock;
it->index = deque->leftindex;
Py_INCREF(deque);
it->deque = deque;
it->deque = (dequeobject*)Py_NewRef(deque);
it->state = deque->state;
it->counter = Py_SIZE(deque);
PyObject_GC_Track(it);
Expand Down Expand Up @@ -1734,8 +1722,7 @@ dequeiter_next(dequeiterobject *it)
it->b = it->b->rightlink;
it->index = 0;
}
Py_INCREF(item);
return item;
return Py_NewRef(item);
}

static PyObject *
Expand Down Expand Up @@ -1844,8 +1831,7 @@ deque_reviter(dequeobject *deque, PyObject *Py_UNUSED(ignored))
return NULL;
it->b = deque->rightblock;
it->index = deque->rightindex;
Py_INCREF(deque);
it->deque = deque;
it->deque = (dequeobject*)Py_NewRef(deque);
it->state = deque->state;
it->counter = Py_SIZE(deque);
PyObject_GC_Track(it);
Expand Down Expand Up @@ -1876,8 +1862,7 @@ dequereviter_next(dequeiterobject *it)
it->b = it->b->leftlink;
it->index = BLOCKLEN - 1;
}
Py_INCREF(item);
return item;
return Py_NewRef(item);
}

static PyObject *
Expand Down Expand Up @@ -2203,8 +2188,7 @@ defdict_init(PyObject *self, PyObject *args, PyObject *kwds)
}
if (newargs == NULL)
return -1;
Py_XINCREF(newdefault);
dd->default_factory = newdefault;
dd->default_factory = Py_XNewRef(newdefault);
result = PyDict_Type.tp_init(self, newargs, kwds);
Py_DECREF(newargs);
Py_XDECREF(olddefault);
Expand Down Expand Up @@ -2414,8 +2398,7 @@ tuplegetter_new_impl(PyTypeObject *type, Py_ssize_t index, PyObject *doc)
return NULL;
}
self->index = index;
Py_INCREF(doc);
self->doc = doc;
self->doc = Py_NewRef(doc);
return (PyObject *)self;
}

Expand All @@ -2426,13 +2409,11 @@ tuplegetter_descr_get(PyObject *self, PyObject *obj, PyObject *type)
PyObject *result;

if (obj == NULL) {
Py_INCREF(self);
return self;
return Py_NewRef(self);
}
if (!PyTuple_Check(obj)) {
if (obj == Py_None) {
Py_INCREF(self);
return self;
return Py_NewRef(self);
}
PyErr_Format(PyExc_TypeError,
"descriptor for index '%zd' for tuple subclasses "
Expand All @@ -2448,8 +2429,7 @@ tuplegetter_descr_get(PyObject *self, PyObject *obj, PyObject *type)
}

result = PyTuple_GET_ITEM(obj, index);
Py_INCREF(result);
return result;
return Py_NewRef(result);
}

static int
Expand Down
9 changes: 3 additions & 6 deletions Modules/_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ get_char_or_None(Py_UCS4 c)
static PyObject *
Dialect_get_lineterminator(DialectObj *self, void *Py_UNUSED(ignored))
{
Py_XINCREF(self->lineterminator);
return self->lineterminator;
return Py_XNewRef(self->lineterminator);
}

static PyObject *
Expand Down Expand Up @@ -316,8 +315,7 @@ _set_str(const char *name, PyObject **target, PyObject *src, const char *dflt)
else {
if (PyUnicode_READY(src) == -1)
return -1;
Py_INCREF(src);
Py_XSETREF(*target, src);
Py_XSETREF(*target, Py_NewRef(src));
}
}
return 0;
Expand Down Expand Up @@ -514,8 +512,7 @@ dialect_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
goto err;
}

ret = (PyObject *)self;
Py_INCREF(self);
ret = Py_NewRef(self);
err:
Py_CLEAR(self);
Py_CLEAR(dialect);
Expand Down
27 changes: 9 additions & 18 deletions Modules/_curses_panel.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,7 @@ PyCursesPanel_New(_curses_panel_state *state, PANEL *pan,
Py_DECREF(po);
return NULL;
}
po->wo = wo;
Py_INCREF(wo);
po->wo = (PyCursesWindowObject*)Py_NewRef(wo);
return (PyObject *)po;
}

Expand Down Expand Up @@ -313,8 +312,7 @@ _curses_panel_panel_above_impl(PyCursesPanelObject *self)
"panel_above: can't find Panel Object");
return NULL;
}
Py_INCREF(po);
return (PyObject *)po;
return Py_NewRef(po);
}

/* panel_below(NULL) returns the top panel in the stack. To get
Expand Down Expand Up @@ -344,8 +342,7 @@ _curses_panel_panel_below_impl(PyCursesPanelObject *self)
"panel_below: can't find Panel Object");
return NULL;
}
Py_INCREF(po);
return (PyObject *)po;
return Py_NewRef(po);
}

/*[clinic input]
Expand Down Expand Up @@ -394,8 +391,7 @@ static PyObject *
_curses_panel_panel_window_impl(PyCursesPanelObject *self)
/*[clinic end generated code: output=5f05940d4106b4cb input=6067353d2c307901]*/
{
Py_INCREF(self->wo);
return (PyObject *)self->wo;
return Py_NewRef(self->wo);
}

/*[clinic input]
Expand Down Expand Up @@ -428,8 +424,7 @@ _curses_panel_panel_replace_impl(PyCursesPanelObject *self,
PyErr_SetString(state->PyCursesError, "replace_panel() returned ERR");
return NULL;
}
Py_INCREF(win);
Py_SETREF(po->wo, win);
Py_SETREF(po->wo, Py_NewRef(win));
Py_RETURN_NONE;
}

Expand Down Expand Up @@ -486,8 +481,7 @@ _curses_panel_panel_userptr_impl(PyCursesPanelObject *self,
return NULL;
}

Py_INCREF(obj);
return obj;
return Py_NewRef(obj);
}


Expand Down Expand Up @@ -555,8 +549,7 @@ _curses_panel_bottom_panel_impl(PyObject *module)
"panel_above: can't find Panel Object");
return NULL;
}
Py_INCREF(po);
return (PyObject *)po;
return Py_NewRef(po);
}

/*[clinic input]
Expand Down Expand Up @@ -614,8 +607,7 @@ _curses_panel_top_panel_impl(PyObject *module)
"panel_below: can't find Panel Object");
return NULL;
}
Py_INCREF(po);
return (PyObject *)po;
return Py_NewRef(po);
}

/*[clinic input]
Expand Down Expand Up @@ -670,8 +662,7 @@ _curses_panel_exec(PyObject *mod)
state->PyCursesError = PyErr_NewException(
"_curses_panel.error", NULL, NULL);

Py_INCREF(state->PyCursesError);
if (PyModule_AddObject(mod, "error", state->PyCursesError) < 0) {
if (PyModule_AddObject(mod, "error", Py_NewRef(state->PyCursesError)) < 0) {
Py_DECREF(state->PyCursesError);
return -1;
}
Expand Down
3 changes: 1 addition & 2 deletions Modules/_cursesmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,7 @@ PyCurses_ConvertToString(PyCursesWindowObject *win, PyObject *obj,
#endif
}
else if (PyBytes_Check(obj)) {
Py_INCREF(obj);
*bytes = obj;
*bytes = Py_NewRef(obj);
/* check for embedded null bytes */
if (PyBytes_AsStringAndSize(*bytes, &str, NULL) < 0) {
Py_DECREF(obj);
Expand Down
6 changes: 2 additions & 4 deletions Modules/_dbmmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,7 @@ _dbm_dbm_get_impl(dbmobject *self, PyTypeObject *cls, const char *key,
return PyBytes_FromStringAndSize(val.dptr, val.dsize);
}

Py_INCREF(default_value);
return default_value;
return Py_NewRef(default_value);
}

/*[clinic input]
Expand Down Expand Up @@ -419,8 +418,7 @@ _dbm_dbm_setdefault_impl(dbmobject *self, PyTypeObject *cls, const char *key,
static PyObject *
dbm__enter__(PyObject *self, PyObject *args)
{
Py_INCREF(self);
return self;
return Py_NewRef(self);
}

static PyObject *
Expand Down
Loading

0 comments on commit 3817607

Please sign in to comment.