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

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 16:21:40 +01:00 committed by GitHub
parent 3e2f7135e6
commit 65dd745f1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 54 additions and 97 deletions

View File

@ -56,8 +56,7 @@ _symtable_symtable_impl(PyObject *module, PyObject *source,
if (st == NULL) { if (st == NULL) {
return NULL; return NULL;
} }
t = (PyObject *)st->st_top; t = Py_NewRef(st->st_top);
Py_INCREF(t);
_PySymtable_Free(st); _PySymtable_Free(st);
return t; return t;
} }

View File

@ -159,8 +159,7 @@ unicodedata_UCD_decimal_impl(PyObject *self, int chr,
return NULL; return NULL;
} }
else { else {
Py_INCREF(default_value); return Py_NewRef(default_value);
return default_value;
} }
} }
return PyLong_FromLong(rc); return PyLong_FromLong(rc);
@ -194,8 +193,7 @@ unicodedata_UCD_digit_impl(PyObject *self, int chr, PyObject *default_value)
return NULL; return NULL;
} }
else { else {
Py_INCREF(default_value); return Py_NewRef(default_value);
return default_value;
} }
} }
return PyLong_FromLong(rc); return PyLong_FromLong(rc);
@ -246,8 +244,7 @@ unicodedata_UCD_numeric_impl(PyObject *self, int chr,
return NULL; return NULL;
} }
else { else {
Py_INCREF(default_value); return Py_NewRef(default_value);
return default_value;
} }
} }
return PyFloat_FromDouble(rc); return PyFloat_FromDouble(rc);
@ -917,8 +914,7 @@ unicodedata_UCD_is_normalized_impl(PyObject *self, PyObject *form,
result = (m == YES) ? Py_True : Py_False; result = (m == YES) ? Py_True : Py_False;
} }
Py_INCREF(result); return Py_NewRef(result);
return result;
} }
@ -943,39 +939,34 @@ unicodedata_UCD_normalize_impl(PyObject *self, PyObject *form,
if (PyUnicode_GET_LENGTH(input) == 0) { if (PyUnicode_GET_LENGTH(input) == 0) {
/* Special case empty input strings, since resizing /* Special case empty input strings, since resizing
them later would cause internal errors. */ them later would cause internal errors. */
Py_INCREF(input); return Py_NewRef(input);
return input;
} }
if (PyUnicode_CompareWithASCIIString(form, "NFC") == 0) { if (PyUnicode_CompareWithASCIIString(form, "NFC") == 0) {
if (is_normalized_quickcheck(self, input, if (is_normalized_quickcheck(self, input,
true, false, true) == YES) { true, false, true) == YES) {
Py_INCREF(input); return Py_NewRef(input);
return input;
} }
return nfc_nfkc(self, input, 0); return nfc_nfkc(self, input, 0);
} }
if (PyUnicode_CompareWithASCIIString(form, "NFKC") == 0) { if (PyUnicode_CompareWithASCIIString(form, "NFKC") == 0) {
if (is_normalized_quickcheck(self, input, if (is_normalized_quickcheck(self, input,
true, true, true) == YES) { true, true, true) == YES) {
Py_INCREF(input); return Py_NewRef(input);
return input;
} }
return nfc_nfkc(self, input, 1); return nfc_nfkc(self, input, 1);
} }
if (PyUnicode_CompareWithASCIIString(form, "NFD") == 0) { if (PyUnicode_CompareWithASCIIString(form, "NFD") == 0) {
if (is_normalized_quickcheck(self, input, if (is_normalized_quickcheck(self, input,
false, false, true) == YES) { false, false, true) == YES) {
Py_INCREF(input); return Py_NewRef(input);
return input;
} }
return nfd_nfkd(self, input, 0); return nfd_nfkd(self, input, 0);
} }
if (PyUnicode_CompareWithASCIIString(form, "NFKD") == 0) { if (PyUnicode_CompareWithASCIIString(form, "NFKD") == 0) {
if (is_normalized_quickcheck(self, input, if (is_normalized_quickcheck(self, input,
false, true, true) == YES) { false, true, true) == YES) {
Py_INCREF(input); return Py_NewRef(input);
return input;
} }
return nfd_nfkd(self, input, 1); return nfd_nfkd(self, input, 1);
} }
@ -1370,8 +1361,7 @@ unicodedata_UCD_name_impl(PyObject *self, int chr, PyObject *default_value)
return NULL; return NULL;
} }
else { else {
Py_INCREF(default_value); return Py_NewRef(default_value);
return default_value;
} }
} }

View File

@ -155,8 +155,7 @@ Xxo_getattro(XxoObject *self, PyObject *name)
if (self->x_attr != NULL) { if (self->x_attr != NULL) {
PyObject *v = PyDict_GetItemWithError(self->x_attr, name); PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
if (v != NULL) { if (v != NULL) {
Py_INCREF(v); return Py_NewRef(v);
return v;
} }
else if (PyErr_Occurred()) { else if (PyErr_Occurred()) {
return NULL; return NULL;
@ -210,18 +209,15 @@ Xxo_demo(XxoObject *self, PyTypeObject *defining_class,
/* Test if the argument is "str" */ /* Test if the argument is "str" */
if (PyUnicode_Check(o)) { if (PyUnicode_Check(o)) {
Py_INCREF(o); return Py_NewRef(o);
return o;
} }
/* test if the argument is of the Xxo class */ /* test if the argument is of the Xxo class */
if (PyObject_TypeCheck(o, defining_class)) { if (PyObject_TypeCheck(o, defining_class)) {
Py_INCREF(o); return Py_NewRef(o);
return o;
} }
Py_INCREF(Py_None); return Py_NewRef(Py_None);
return Py_None;
} }
static PyMethodDef Xxo_methods[] = { static PyMethodDef Xxo_methods[] = {

View File

@ -64,11 +64,9 @@ Xxo_demo(XxoObject *self, PyObject *args)
return NULL; return NULL;
/* Test availability of fast type checks */ /* Test availability of fast type checks */
if (o != NULL && PyUnicode_Check(o)) { if (o != NULL && PyUnicode_Check(o)) {
Py_INCREF(o); return Py_NewRef(o);
return o;
} }
Py_INCREF(Py_None); return Py_NewRef(Py_None);
return Py_None;
} }
static PyMethodDef Xxo_methods[] = { static PyMethodDef Xxo_methods[] = {
@ -83,8 +81,7 @@ Xxo_getattro(XxoObject *self, PyObject *name)
if (self->x_attr != NULL) { if (self->x_attr != NULL) {
PyObject *v = PyDict_GetItemWithError(self->x_attr, name); PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
if (v != NULL) { if (v != NULL) {
Py_INCREF(v); return Py_NewRef(v);
return v;
} }
else if (PyErr_Occurred()) { else if (PyErr_Occurred()) {
return NULL; return NULL;
@ -176,8 +173,7 @@ xx_roj(PyObject *self, PyObject *args)
long b; long b;
if (!PyArg_ParseTuple(args, "O#:roj", &a, &b)) if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
return NULL; return NULL;
Py_INCREF(Py_None); return Py_NewRef(Py_None);
return Py_None;
} }

View File

@ -52,8 +52,7 @@ Xxo_demo(XxoObject *self, PyObject *args)
{ {
if (!PyArg_ParseTuple(args, ":demo")) if (!PyArg_ParseTuple(args, ":demo"))
return NULL; return NULL;
Py_INCREF(Py_None); return Py_NewRef(Py_None);
return Py_None;
} }
static PyMethodDef Xxo_methods[] = { static PyMethodDef Xxo_methods[] = {
@ -68,8 +67,7 @@ Xxo_getattro(XxoObject *self, PyObject *name)
if (self->x_attr != NULL) { if (self->x_attr != NULL) {
PyObject *v = PyDict_GetItemWithError(self->x_attr, name); PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
if (v != NULL) { if (v != NULL) {
Py_INCREF(v); return Py_NewRef(v);
return v;
} }
else if (PyErr_Occurred()) { else if (PyErr_Occurred()) {
return NULL; return NULL;
@ -195,8 +193,7 @@ xx_bug(PyObject *self, PyObject *args)
printf("\n"); printf("\n");
/* Py_DECREF(item); */ /* Py_DECREF(item); */
Py_INCREF(Py_None); return Py_NewRef(Py_None);
return Py_None;
} }
/* Test bad format character */ /* Test bad format character */
@ -208,8 +205,7 @@ xx_roj(PyObject *self, PyObject *args)
long b; long b;
if (!PyArg_ParseTuple(args, "O#:roj", &a, &b)) if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
return NULL; return NULL;
Py_INCREF(Py_None); return Py_NewRef(Py_None);
return Py_None;
} }
@ -266,8 +262,7 @@ static PyTypeObject Str_Type = {
static PyObject * static PyObject *
null_richcompare(PyObject *self, PyObject *other, int op) null_richcompare(PyObject *self, PyObject *other, int op)
{ {
Py_INCREF(Py_NotImplemented); return Py_NewRef(Py_NotImplemented);
return Py_NotImplemented;
} }
static PyTypeObject Null_Type = { static PyTypeObject Null_Type = {

View File

@ -39,8 +39,7 @@ spamlist_setstate(spamlistobject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "i:setstate", &state)) if (!PyArg_ParseTuple(args, "i:setstate", &state))
return NULL; return NULL;
self->state = state; self->state = state;
Py_INCREF(Py_None); return Py_NewRef(Py_None);
return Py_None;
} }
static PyObject * static PyObject *
@ -53,12 +52,9 @@ spamlist_specialmeth(PyObject *self, PyObject *args, PyObject *kw)
self = Py_None; self = Py_None;
if (kw == NULL) if (kw == NULL)
kw = Py_None; kw = Py_None;
Py_INCREF(self); PyTuple_SET_ITEM(result, 0, Py_NewRef(self));
PyTuple_SET_ITEM(result, 0, self); PyTuple_SET_ITEM(result, 1, Py_NewRef(args));
Py_INCREF(args); PyTuple_SET_ITEM(result, 2, Py_NewRef(kw));
PyTuple_SET_ITEM(result, 1, args);
Py_INCREF(kw);
PyTuple_SET_ITEM(result, 2, kw);
} }
return result; return result;
} }
@ -164,8 +160,7 @@ spamdict_setstate(spamdictobject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "i:setstate", &state)) if (!PyArg_ParseTuple(args, "i:setstate", &state))
return NULL; return NULL;
self->state = state; self->state = state;
Py_INCREF(Py_None); return Py_NewRef(Py_None);
return Py_None;
} }
static PyMethodDef spamdict_methods[] = { static PyMethodDef spamdict_methods[] = {
@ -279,14 +274,12 @@ xxsubtype_exec(PyObject* m)
if (PyType_Ready(&spamdict_type) < 0) if (PyType_Ready(&spamdict_type) < 0)
return -1; return -1;
Py_INCREF(&spamlist_type);
if (PyModule_AddObject(m, "spamlist", if (PyModule_AddObject(m, "spamlist",
(PyObject *) &spamlist_type) < 0) Py_NewRef(&spamlist_type)) < 0)
return -1; return -1;
Py_INCREF(&spamdict_type);
if (PyModule_AddObject(m, "spamdict", if (PyModule_AddObject(m, "spamdict",
(PyObject *) &spamdict_type) < 0) Py_NewRef(&spamdict_type)) < 0)
return -1; return -1;
return 0; return 0;
} }

View File

@ -671,8 +671,7 @@ zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
self->zst.next_in = NULL; self->zst.next_in = NULL;
self->zst.avail_in = 0; self->zst.avail_in = 0;
if (zdict != NULL) { if (zdict != NULL) {
Py_INCREF(zdict); self->zdict = Py_NewRef(zdict);
self->zdict = zdict;
} }
int err = inflateInit2(&self->zst, wbits); int err = inflateInit2(&self->zst, wbits);
switch (err) { switch (err) {
@ -1089,12 +1088,9 @@ zlib_Compress_copy_impl(compobject *self, PyTypeObject *cls)
zlib_error(state, self->zst, err, "while copying compression object"); zlib_error(state, self->zst, err, "while copying compression object");
goto error; goto error;
} }
Py_INCREF(self->unused_data); Py_XSETREF(return_value->unused_data, Py_NewRef(self->unused_data));
Py_XSETREF(return_value->unused_data, self->unused_data); Py_XSETREF(return_value->unconsumed_tail, Py_NewRef(self->unconsumed_tail));
Py_INCREF(self->unconsumed_tail); Py_XSETREF(return_value->zdict, Py_XNewRef(self->zdict));
Py_XSETREF(return_value->unconsumed_tail, self->unconsumed_tail);
Py_XINCREF(self->zdict);
Py_XSETREF(return_value->zdict, self->zdict);
return_value->eof = self->eof; return_value->eof = self->eof;
/* Mark it as being initialized */ /* Mark it as being initialized */
@ -1177,12 +1173,9 @@ zlib_Decompress_copy_impl(compobject *self, PyTypeObject *cls)
goto error; goto error;
} }
Py_INCREF(self->unused_data); Py_XSETREF(return_value->unused_data, Py_NewRef(self->unused_data));
Py_XSETREF(return_value->unused_data, self->unused_data); Py_XSETREF(return_value->unconsumed_tail, Py_NewRef(self->unconsumed_tail));
Py_INCREF(self->unconsumed_tail); Py_XSETREF(return_value->zdict, Py_XNewRef(self->zdict));
Py_XSETREF(return_value->unconsumed_tail, self->unconsumed_tail);
Py_XINCREF(self->zdict);
Py_XSETREF(return_value->zdict, self->zdict);
return_value->eof = self->eof; return_value->eof = self->eof;
/* Mark it as being initialized */ /* Mark it as being initialized */
@ -1733,10 +1726,7 @@ ZlibDecompressor__new__(PyTypeObject *cls,
self->avail_in_real = 0; self->avail_in_real = 0;
self->input_buffer = NULL; self->input_buffer = NULL;
self->input_buffer_size = 0; self->input_buffer_size = 0;
if (zdict != NULL) { self->zdict = Py_XNewRef(zdict);
Py_INCREF(zdict);
}
self->zdict = zdict;
self->zst.opaque = NULL; self->zst.opaque = NULL;
self->zst.zalloc = PyZlib_Malloc; self->zst.zalloc = PyZlib_Malloc;
self->zst.zfree = PyZlib_Free; self->zst.zfree = PyZlib_Free;
@ -2042,14 +2032,12 @@ zlib_exec(PyObject *mod)
return -1; return -1;
} }
Py_INCREF(state->ZlibError); if (PyModule_AddObject(mod, "error", Py_NewRef(state->ZlibError)) < 0) {
if (PyModule_AddObject(mod, "error", state->ZlibError) < 0) {
Py_DECREF(state->ZlibError); Py_DECREF(state->ZlibError);
return -1; return -1;
} }
Py_INCREF(state->ZlibDecompressorType);
if (PyModule_AddObject(mod, "_ZlibDecompressor", if (PyModule_AddObject(mod, "_ZlibDecompressor",
(PyObject *)state->ZlibDecompressorType) < 0) { Py_NewRef(state->ZlibDecompressorType)) < 0) {
Py_DECREF(state->ZlibDecompressorType); Py_DECREF(state->ZlibDecompressorType);
return -1; return -1;
} }