Skip to content

Commit

Permalink
gh-99300: Use Py_NewRef() in Modules/ directory (#99473)
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 committed Nov 14, 2022
1 parent 3e2f713 commit 65dd745
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 97 deletions.
3 changes: 1 addition & 2 deletions Modules/symtablemodule.c
Expand Up @@ -56,8 +56,7 @@ _symtable_symtable_impl(PyObject *module, PyObject *source,
if (st == NULL) {
return NULL;
}
t = (PyObject *)st->st_top;
Py_INCREF(t);
t = Py_NewRef(st->st_top);
_PySymtable_Free(st);
return t;
}
Expand Down
30 changes: 10 additions & 20 deletions Modules/unicodedata.c
Expand Up @@ -159,8 +159,7 @@ unicodedata_UCD_decimal_impl(PyObject *self, int chr,
return NULL;
}
else {
Py_INCREF(default_value);
return default_value;
return Py_NewRef(default_value);
}
}
return PyLong_FromLong(rc);
Expand Down Expand Up @@ -194,8 +193,7 @@ unicodedata_UCD_digit_impl(PyObject *self, int chr, PyObject *default_value)
return NULL;
}
else {
Py_INCREF(default_value);
return default_value;
return Py_NewRef(default_value);
}
}
return PyLong_FromLong(rc);
Expand Down Expand Up @@ -246,8 +244,7 @@ unicodedata_UCD_numeric_impl(PyObject *self, int chr,
return NULL;
}
else {
Py_INCREF(default_value);
return default_value;
return Py_NewRef(default_value);
}
}
return PyFloat_FromDouble(rc);
Expand Down Expand Up @@ -917,8 +914,7 @@ unicodedata_UCD_is_normalized_impl(PyObject *self, PyObject *form,
result = (m == YES) ? Py_True : Py_False;
}

Py_INCREF(result);
return result;
return Py_NewRef(result);
}


Expand All @@ -943,39 +939,34 @@ unicodedata_UCD_normalize_impl(PyObject *self, PyObject *form,
if (PyUnicode_GET_LENGTH(input) == 0) {
/* Special case empty input strings, since resizing
them later would cause internal errors. */
Py_INCREF(input);
return input;
return Py_NewRef(input);
}

if (PyUnicode_CompareWithASCIIString(form, "NFC") == 0) {
if (is_normalized_quickcheck(self, input,
true, false, true) == YES) {
Py_INCREF(input);
return input;
return Py_NewRef(input);
}
return nfc_nfkc(self, input, 0);
}
if (PyUnicode_CompareWithASCIIString(form, "NFKC") == 0) {
if (is_normalized_quickcheck(self, input,
true, true, true) == YES) {
Py_INCREF(input);
return input;
return Py_NewRef(input);
}
return nfc_nfkc(self, input, 1);
}
if (PyUnicode_CompareWithASCIIString(form, "NFD") == 0) {
if (is_normalized_quickcheck(self, input,
false, false, true) == YES) {
Py_INCREF(input);
return input;
return Py_NewRef(input);
}
return nfd_nfkd(self, input, 0);
}
if (PyUnicode_CompareWithASCIIString(form, "NFKD") == 0) {
if (is_normalized_quickcheck(self, input,
false, true, true) == YES) {
Py_INCREF(input);
return input;
return Py_NewRef(input);
}
return nfd_nfkd(self, input, 1);
}
Expand Down Expand Up @@ -1370,8 +1361,7 @@ unicodedata_UCD_name_impl(PyObject *self, int chr, PyObject *default_value)
return NULL;
}
else {
Py_INCREF(default_value);
return default_value;
return Py_NewRef(default_value);
}
}

Expand Down
12 changes: 4 additions & 8 deletions Modules/xxlimited.c
Expand Up @@ -155,8 +155,7 @@ Xxo_getattro(XxoObject *self, PyObject *name)
if (self->x_attr != NULL) {
PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
if (v != NULL) {
Py_INCREF(v);
return v;
return Py_NewRef(v);
}
else if (PyErr_Occurred()) {
return NULL;
Expand Down Expand Up @@ -210,18 +209,15 @@ Xxo_demo(XxoObject *self, PyTypeObject *defining_class,

/* Test if the argument is "str" */
if (PyUnicode_Check(o)) {
Py_INCREF(o);
return o;
return Py_NewRef(o);
}

/* test if the argument is of the Xxo class */
if (PyObject_TypeCheck(o, defining_class)) {
Py_INCREF(o);
return o;
return Py_NewRef(o);
}

Py_INCREF(Py_None);
return Py_None;
return Py_NewRef(Py_None);
}

static PyMethodDef Xxo_methods[] = {
Expand Down
12 changes: 4 additions & 8 deletions Modules/xxlimited_35.c
Expand Up @@ -64,11 +64,9 @@ Xxo_demo(XxoObject *self, PyObject *args)
return NULL;
/* Test availability of fast type checks */
if (o != NULL && PyUnicode_Check(o)) {
Py_INCREF(o);
return o;
return Py_NewRef(o);
}
Py_INCREF(Py_None);
return Py_None;
return Py_NewRef(Py_None);
}

static PyMethodDef Xxo_methods[] = {
Expand All @@ -83,8 +81,7 @@ Xxo_getattro(XxoObject *self, PyObject *name)
if (self->x_attr != NULL) {
PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
if (v != NULL) {
Py_INCREF(v);
return v;
return Py_NewRef(v);
}
else if (PyErr_Occurred()) {
return NULL;
Expand Down Expand Up @@ -176,8 +173,7 @@ xx_roj(PyObject *self, PyObject *args)
long b;
if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
return NULL;
Py_INCREF(Py_None);
return Py_None;
return Py_NewRef(Py_None);
}


Expand Down
15 changes: 5 additions & 10 deletions Modules/xxmodule.c
Expand Up @@ -52,8 +52,7 @@ Xxo_demo(XxoObject *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, ":demo"))
return NULL;
Py_INCREF(Py_None);
return Py_None;
return Py_NewRef(Py_None);
}

static PyMethodDef Xxo_methods[] = {
Expand All @@ -68,8 +67,7 @@ Xxo_getattro(XxoObject *self, PyObject *name)
if (self->x_attr != NULL) {
PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
if (v != NULL) {
Py_INCREF(v);
return v;
return Py_NewRef(v);
}
else if (PyErr_Occurred()) {
return NULL;
Expand Down Expand Up @@ -195,8 +193,7 @@ xx_bug(PyObject *self, PyObject *args)
printf("\n");
/* Py_DECREF(item); */

Py_INCREF(Py_None);
return Py_None;
return Py_NewRef(Py_None);
}

/* Test bad format character */
Expand All @@ -208,8 +205,7 @@ xx_roj(PyObject *self, PyObject *args)
long b;
if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
return NULL;
Py_INCREF(Py_None);
return Py_None;
return Py_NewRef(Py_None);
}


Expand Down Expand Up @@ -266,8 +262,7 @@ static PyTypeObject Str_Type = {
static PyObject *
null_richcompare(PyObject *self, PyObject *other, int op)
{
Py_INCREF(Py_NotImplemented);
return Py_NotImplemented;
return Py_NewRef(Py_NotImplemented);
}

static PyTypeObject Null_Type = {
Expand Down
21 changes: 7 additions & 14 deletions Modules/xxsubtype.c
Expand Up @@ -39,8 +39,7 @@ spamlist_setstate(spamlistobject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "i:setstate", &state))
return NULL;
self->state = state;
Py_INCREF(Py_None);
return Py_None;
return Py_NewRef(Py_None);
}

static PyObject *
Expand All @@ -53,12 +52,9 @@ spamlist_specialmeth(PyObject *self, PyObject *args, PyObject *kw)
self = Py_None;
if (kw == NULL)
kw = Py_None;
Py_INCREF(self);
PyTuple_SET_ITEM(result, 0, self);
Py_INCREF(args);
PyTuple_SET_ITEM(result, 1, args);
Py_INCREF(kw);
PyTuple_SET_ITEM(result, 2, kw);
PyTuple_SET_ITEM(result, 0, Py_NewRef(self));
PyTuple_SET_ITEM(result, 1, Py_NewRef(args));
PyTuple_SET_ITEM(result, 2, Py_NewRef(kw));
}
return result;
}
Expand Down Expand Up @@ -164,8 +160,7 @@ spamdict_setstate(spamdictobject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "i:setstate", &state))
return NULL;
self->state = state;
Py_INCREF(Py_None);
return Py_None;
return Py_NewRef(Py_None);
}

static PyMethodDef spamdict_methods[] = {
Expand Down Expand Up @@ -279,14 +274,12 @@ xxsubtype_exec(PyObject* m)
if (PyType_Ready(&spamdict_type) < 0)
return -1;

Py_INCREF(&spamlist_type);
if (PyModule_AddObject(m, "spamlist",
(PyObject *) &spamlist_type) < 0)
Py_NewRef(&spamlist_type)) < 0)
return -1;

Py_INCREF(&spamdict_type);
if (PyModule_AddObject(m, "spamdict",
(PyObject *) &spamdict_type) < 0)
Py_NewRef(&spamdict_type)) < 0)
return -1;
return 0;
}
Expand Down

0 comments on commit 65dd745

Please sign in to comment.