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

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:44:56 +01:00 committed by GitHub
parent 9a7e9f9921
commit c340cbb7f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 92 additions and 173 deletions

View file

@ -459,8 +459,7 @@ state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string,
state->start = (void*) ((char*) ptr + start * state->charsize);
state->end = (void*) ((char*) ptr + end * state->charsize);
Py_INCREF(string);
state->string = string;
state->string = Py_NewRef(string);
state->pos = start;
state->endpos = end;
@ -499,8 +498,7 @@ getslice(int isbytes, const void *ptr,
if (isbytes) {
if (PyBytes_CheckExact(string) &&
start == 0 && end == PyBytes_GET_SIZE(string)) {
Py_INCREF(string);
return string;
return Py_NewRef(string);
}
return PyBytes_FromStringAndSize(
(const char *)ptr + start, end - start);
@ -1089,8 +1087,7 @@ pattern_subx(_sremodulestate* module_state,
if (PyCallable_Check(ptemplate)) {
/* sub/subn takes either a function or a template */
filter = ptemplate;
Py_INCREF(filter);
filter = Py_NewRef(ptemplate);
filter_type = CALLABLE;
} else {
/* if not callable, check if it's a literal string */
@ -1109,8 +1106,7 @@ pattern_subx(_sremodulestate* module_state,
if (view.buf)
PyBuffer_Release(&view);
if (literal) {
filter = ptemplate;
Py_INCREF(filter);
filter = Py_NewRef(ptemplate);
filter_type = LITERAL;
} else {
/* not a literal; hand it over to the template compiler */
@ -1120,8 +1116,8 @@ pattern_subx(_sremodulestate* module_state,
assert(Py_TYPE(filter) == module_state->Template_Type);
if (Py_SIZE(filter) == 0) {
Py_INCREF(((TemplateObject *)filter)->literal);
Py_SETREF(filter, ((TemplateObject *)filter)->literal);
Py_SETREF(filter,
Py_NewRef(((TemplateObject *)filter)->literal));
filter_type = LITERAL;
}
else {
@ -1195,8 +1191,7 @@ pattern_subx(_sremodulestate* module_state,
goto error;
} else {
/* filter is literal string */
item = filter;
Py_INCREF(item);
item = Py_NewRef(filter);
}
/* add to list */
@ -1317,8 +1312,7 @@ static PyObject *
_sre_SRE_Pattern___copy___impl(PatternObject *self)
/*[clinic end generated code: output=85dedc2db1bd8694 input=a730a59d863bc9f5]*/
{
Py_INCREF(self);
return (PyObject *)self;
return Py_NewRef(self);
}
/*[clinic input]
@ -1333,8 +1327,7 @@ static PyObject *
_sre_SRE_Pattern___deepcopy__(PatternObject *self, PyObject *memo)
/*[clinic end generated code: output=2ad25679c1f1204a input=a465b1602f997bed]*/
{
Py_INCREF(self);
return (PyObject *)self;
return Py_NewRef(self);
}
static PyObject *
@ -1500,19 +1493,16 @@ _sre_compile_impl(PyObject *module, PyObject *pattern, int flags,
PyBuffer_Release(&view);
}
Py_INCREF(pattern);
self->pattern = pattern;
self->pattern = Py_NewRef(pattern);
self->flags = flags;
self->groups = groups;
if (PyDict_GET_SIZE(groupindex) > 0) {
Py_INCREF(groupindex);
self->groupindex = groupindex;
self->groupindex = Py_NewRef(groupindex);
if (PyTuple_GET_SIZE(indexgroup) > 0) {
Py_INCREF(indexgroup);
self->indexgroup = indexgroup;
self->indexgroup = Py_NewRef(indexgroup);
}
}
@ -1555,8 +1545,7 @@ _sre_template_impl(PyObject *module, PyObject *pattern, PyObject *template)
if (!self)
return NULL;
self->chunks = 1 + 2*n;
self->literal = PyList_GET_ITEM(template, 0);
Py_INCREF(self->literal);
self->literal = Py_NewRef(PyList_GET_ITEM(template, 0));
for (Py_ssize_t i = 0; i < n; i++) {
Py_ssize_t index = PyLong_AsSsize_t(PyList_GET_ITEM(template, 2*i+1));
if (index == -1 && PyErr_Occurred()) {
@ -1576,8 +1565,7 @@ _sre_template_impl(PyObject *module, PyObject *pattern, PyObject *template)
literal = NULL;
self->chunks--;
}
Py_XINCREF(literal);
self->items[i].literal = literal;
self->items[i].literal = Py_XNewRef(literal);
}
return (PyObject*) self;
@ -2128,8 +2116,7 @@ match_getslice_by_index(MatchObject* self, Py_ssize_t index, PyObject* def)
if (self->string == Py_None || self->mark[index] < 0) {
/* return default value if the string or group is undefined */
Py_INCREF(def);
return def;
return Py_NewRef(def);
}
ptr = getstring(self->string, &length, &isbytes, &charsize, &view);
@ -2448,8 +2435,7 @@ match_regs(MatchObject* self)
PyTuple_SET_ITEM(regs, index, item);
}
Py_INCREF(regs);
self->regs = regs;
self->regs = Py_NewRef(regs);
return regs;
}
@ -2463,8 +2449,7 @@ static PyObject *
_sre_SRE_Match___copy___impl(MatchObject *self)
/*[clinic end generated code: output=a779c5fc8b5b4eb4 input=3bb4d30b6baddb5b]*/
{
Py_INCREF(self);
return (PyObject *)self;
return Py_NewRef(self);
}
/*[clinic input]
@ -2479,8 +2464,7 @@ static PyObject *
_sre_SRE_Match___deepcopy__(MatchObject *self, PyObject *memo)
/*[clinic end generated code: output=ba7cb46d655e4ee2 input=779d12a31c2c325e]*/
{
Py_INCREF(self);
return (PyObject *)self;
return Py_NewRef(self);
}
PyDoc_STRVAR(match_doc,
@ -2509,8 +2493,7 @@ match_lastgroup_get(MatchObject *self, void *Py_UNUSED(ignored))
{
PyObject *result = PyTuple_GET_ITEM(self->pattern->indexgroup,
self->lastindex);
Py_INCREF(result);
return result;
return Py_NewRef(result);
}
Py_RETURN_NONE;
}
@ -2519,8 +2502,7 @@ static PyObject *
match_regs_get(MatchObject *self, void *Py_UNUSED(ignored))
{
if (self->regs) {
Py_INCREF(self->regs);
return self->regs;
return Py_NewRef(self->regs);
} else
return match_regs(self);
}
@ -2564,11 +2546,9 @@ pattern_new_match(_sremodulestate* module_state,
if (!match)
return NULL;
Py_INCREF(pattern);
match->pattern = pattern;
match->pattern = (PatternObject*)Py_NewRef(pattern);
Py_INCREF(state->string);
match->string = state->string;
match->string = Py_NewRef(state->string);
match->regs = NULL;
match->groups = pattern->groups+1;
@ -2788,8 +2768,7 @@ pattern_scanner(_sremodulestate *module_state,
return NULL;
}
Py_INCREF(self);
scanner->pattern = (PyObject*) self;
scanner->pattern = Py_NewRef(self);
PyObject_GC_Track(scanner);
return (PyObject*) scanner;
@ -2834,8 +2813,7 @@ static PyObject *
expand_template(TemplateObject *self, MatchObject *match)
{
if (Py_SIZE(self) == 0) {
Py_INCREF(self->literal);
return self->literal;
return Py_NewRef(self->literal);
}
PyObject *result = NULL;
@ -2855,8 +2833,7 @@ expand_template(TemplateObject *self, MatchObject *match)
out = &PyList_GET_ITEM(list, 0);
}
Py_INCREF(self->literal);
out[count++] = self->literal;
out[count++] = Py_NewRef(self->literal);
for (Py_ssize_t i = 0; i < Py_SIZE(self); i++) {
Py_ssize_t index = self->items[i].index;
if (index >= match->groups) {
@ -2868,15 +2845,13 @@ expand_template(TemplateObject *self, MatchObject *match)
goto cleanup;
}
if (item != Py_None) {
Py_INCREF(item);
out[count++] = item;
out[count++] = Py_NewRef(item);
}
Py_DECREF(item);
PyObject *literal = self->items[i].literal;
if (literal != NULL) {
Py_INCREF(literal);
out[count++] = literal;
out[count++] = Py_NewRef(literal);
}
}