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) {
return NULL;
}
t = (PyObject *)st->st_top;
Py_INCREF(t);
t = Py_NewRef(st->st_top);
_PySymtable_Free(st);
return t;
}

View File

@ -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);
@ -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);
@ -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);
@ -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);
}
@ -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);
}
@ -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);
}
}

View File

@ -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;
@ -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[] = {

View File

@ -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[] = {
@ -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;
@ -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);
}

View File

@ -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[] = {
@ -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;
@ -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 */
@ -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);
}
@ -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 = {

View File

@ -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 *
@ -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;
}
@ -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[] = {
@ -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;
}

View File

@ -671,8 +671,7 @@ zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
self->zst.next_in = NULL;
self->zst.avail_in = 0;
if (zdict != NULL) {
Py_INCREF(zdict);
self->zdict = zdict;
self->zdict = Py_NewRef(zdict);
}
int err = inflateInit2(&self->zst, wbits);
switch (err) {
@ -1089,12 +1088,9 @@ zlib_Compress_copy_impl(compobject *self, PyTypeObject *cls)
zlib_error(state, self->zst, err, "while copying compression object");
goto error;
}
Py_INCREF(self->unused_data);
Py_XSETREF(return_value->unused_data, self->unused_data);
Py_INCREF(self->unconsumed_tail);
Py_XSETREF(return_value->unconsumed_tail, self->unconsumed_tail);
Py_XINCREF(self->zdict);
Py_XSETREF(return_value->zdict, self->zdict);
Py_XSETREF(return_value->unused_data, Py_NewRef(self->unused_data));
Py_XSETREF(return_value->unconsumed_tail, Py_NewRef(self->unconsumed_tail));
Py_XSETREF(return_value->zdict, Py_XNewRef(self->zdict));
return_value->eof = self->eof;
/* Mark it as being initialized */
@ -1177,12 +1173,9 @@ zlib_Decompress_copy_impl(compobject *self, PyTypeObject *cls)
goto error;
}
Py_INCREF(self->unused_data);
Py_XSETREF(return_value->unused_data, self->unused_data);
Py_INCREF(self->unconsumed_tail);
Py_XSETREF(return_value->unconsumed_tail, self->unconsumed_tail);
Py_XINCREF(self->zdict);
Py_XSETREF(return_value->zdict, self->zdict);
Py_XSETREF(return_value->unused_data, Py_NewRef(self->unused_data));
Py_XSETREF(return_value->unconsumed_tail, Py_NewRef(self->unconsumed_tail));
Py_XSETREF(return_value->zdict, Py_XNewRef(self->zdict));
return_value->eof = self->eof;
/* Mark it as being initialized */
@ -1440,11 +1433,11 @@ arrange_output_buffer_with_maximum(uint32_t *avail_out,
return length;
}
/* Decompress data of length self->avail_in_real in self->state.next_in. The
output buffer is allocated dynamically and returned. If the max_length is
of sufficiently low size, max_length is allocated immediately. At most
max_length bytes are returned, so some of the input may not be consumed.
self->state.next_in and self->avail_in_real are updated to reflect the
/* Decompress data of length self->avail_in_real in self->state.next_in. The
output buffer is allocated dynamically and returned. If the max_length is
of sufficiently low size, max_length is allocated immediately. At most
max_length bytes are returned, so some of the input may not be consumed.
self->state.next_in and self->avail_in_real are updated to reflect the
consumed input. */
static PyObject*
decompress_buf(ZlibDecompressor *self, Py_ssize_t max_length)
@ -1456,11 +1449,11 @@ decompress_buf(ZlibDecompressor *self, Py_ssize_t max_length)
Py_ssize_t hard_limit;
Py_ssize_t obuflen;
zlibstate *state = PyType_GetModuleState(Py_TYPE(self));
int err = Z_OK;
/* When sys.maxsize is passed as default use DEF_BUF_SIZE as start buffer.
In this particular case the data may not necessarily be very big, so
/* When sys.maxsize is passed as default use DEF_BUF_SIZE as start buffer.
In this particular case the data may not necessarily be very big, so
it is better to grow dynamically.*/
if ((max_length < 0) || max_length == PY_SSIZE_T_MAX) {
hard_limit = PY_SSIZE_T_MAX;
@ -1544,7 +1537,7 @@ success:
static PyObject *
decompress(ZlibDecompressor *self, uint8_t *data,
decompress(ZlibDecompressor *self, uint8_t *data,
size_t len, Py_ssize_t max_length)
{
bool input_buffer_in_use;
@ -1713,8 +1706,8 @@ PyDoc_STRVAR(ZlibDecompressor__new____doc__,
"\n");
static PyObject *
ZlibDecompressor__new__(PyTypeObject *cls,
PyObject *args,
ZlibDecompressor__new__(PyTypeObject *cls,
PyObject *args,
PyObject *kwargs)
{
static char *keywords[] = {"wbits", "zdict", NULL};
@ -1727,16 +1720,13 @@ ZlibDecompressor__new__(PyTypeObject *cls,
args, kwargs, format, keywords, &wbits, &zdict)) {
return NULL;
}
ZlibDecompressor *self = PyObject_New(ZlibDecompressor, cls);
ZlibDecompressor *self = PyObject_New(ZlibDecompressor, cls);
self->eof = 0;
self->needs_input = 1;
self->avail_in_real = 0;
self->input_buffer = NULL;
self->input_buffer_size = 0;
if (zdict != NULL) {
Py_INCREF(zdict);
}
self->zdict = zdict;
self->zdict = Py_XNewRef(zdict);
self->zst.opaque = NULL;
self->zst.zalloc = PyZlib_Malloc;
self->zst.zfree = PyZlib_Free;
@ -2042,14 +2032,12 @@ zlib_exec(PyObject *mod)
return -1;
}
Py_INCREF(state->ZlibError);
if (PyModule_AddObject(mod, "error", state->ZlibError) < 0) {
if (PyModule_AddObject(mod, "error", Py_NewRef(state->ZlibError)) < 0) {
Py_DECREF(state->ZlibError);
return -1;
}
Py_INCREF(state->ZlibDecompressorType);
if (PyModule_AddObject(mod, "_ZlibDecompressor",
(PyObject *)state->ZlibDecompressorType) < 0) {
if (PyModule_AddObject(mod, "_ZlibDecompressor",
Py_NewRef(state->ZlibDecompressorType)) < 0) {
Py_DECREF(state->ZlibDecompressorType);
return -1;
}