Skip to content

Commit c340cbb

Browse files
authoredNov 14, 2022
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.
1 parent 9a7e9f9 commit c340cbb

10 files changed

+92
-173
lines changed
 

‎Modules/_sre/sre.c

+27-52
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,7 @@ state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string,
459459
state->start = (void*) ((char*) ptr + start * state->charsize);
460460
state->end = (void*) ((char*) ptr + end * state->charsize);
461461

462-
Py_INCREF(string);
463-
state->string = string;
462+
state->string = Py_NewRef(string);
464463
state->pos = start;
465464
state->endpos = end;
466465

@@ -499,8 +498,7 @@ getslice(int isbytes, const void *ptr,
499498
if (isbytes) {
500499
if (PyBytes_CheckExact(string) &&
501500
start == 0 && end == PyBytes_GET_SIZE(string)) {
502-
Py_INCREF(string);
503-
return string;
501+
return Py_NewRef(string);
504502
}
505503
return PyBytes_FromStringAndSize(
506504
(const char *)ptr + start, end - start);
@@ -1089,8 +1087,7 @@ pattern_subx(_sremodulestate* module_state,
10891087

10901088
if (PyCallable_Check(ptemplate)) {
10911089
/* sub/subn takes either a function or a template */
1092-
filter = ptemplate;
1093-
Py_INCREF(filter);
1090+
filter = Py_NewRef(ptemplate);
10941091
filter_type = CALLABLE;
10951092
} else {
10961093
/* if not callable, check if it's a literal string */
@@ -1109,8 +1106,7 @@ pattern_subx(_sremodulestate* module_state,
11091106
if (view.buf)
11101107
PyBuffer_Release(&view);
11111108
if (literal) {
1112-
filter = ptemplate;
1113-
Py_INCREF(filter);
1109+
filter = Py_NewRef(ptemplate);
11141110
filter_type = LITERAL;
11151111
} else {
11161112
/* not a literal; hand it over to the template compiler */
@@ -1120,8 +1116,8 @@ pattern_subx(_sremodulestate* module_state,
11201116

11211117
assert(Py_TYPE(filter) == module_state->Template_Type);
11221118
if (Py_SIZE(filter) == 0) {
1123-
Py_INCREF(((TemplateObject *)filter)->literal);
1124-
Py_SETREF(filter, ((TemplateObject *)filter)->literal);
1119+
Py_SETREF(filter,
1120+
Py_NewRef(((TemplateObject *)filter)->literal));
11251121
filter_type = LITERAL;
11261122
}
11271123
else {
@@ -1195,8 +1191,7 @@ pattern_subx(_sremodulestate* module_state,
11951191
goto error;
11961192
} else {
11971193
/* filter is literal string */
1198-
item = filter;
1199-
Py_INCREF(item);
1194+
item = Py_NewRef(filter);
12001195
}
12011196

12021197
/* add to list */
@@ -1317,8 +1312,7 @@ static PyObject *
13171312
_sre_SRE_Pattern___copy___impl(PatternObject *self)
13181313
/*[clinic end generated code: output=85dedc2db1bd8694 input=a730a59d863bc9f5]*/
13191314
{
1320-
Py_INCREF(self);
1321-
return (PyObject *)self;
1315+
return Py_NewRef(self);
13221316
}
13231317

13241318
/*[clinic input]
@@ -1333,8 +1327,7 @@ static PyObject *
13331327
_sre_SRE_Pattern___deepcopy__(PatternObject *self, PyObject *memo)
13341328
/*[clinic end generated code: output=2ad25679c1f1204a input=a465b1602f997bed]*/
13351329
{
1336-
Py_INCREF(self);
1337-
return (PyObject *)self;
1330+
return Py_NewRef(self);
13381331
}
13391332

13401333
static PyObject *
@@ -1500,19 +1493,16 @@ _sre_compile_impl(PyObject *module, PyObject *pattern, int flags,
15001493
PyBuffer_Release(&view);
15011494
}
15021495

1503-
Py_INCREF(pattern);
1504-
self->pattern = pattern;
1496+
self->pattern = Py_NewRef(pattern);
15051497

15061498
self->flags = flags;
15071499

15081500
self->groups = groups;
15091501

15101502
if (PyDict_GET_SIZE(groupindex) > 0) {
1511-
Py_INCREF(groupindex);
1512-
self->groupindex = groupindex;
1503+
self->groupindex = Py_NewRef(groupindex);
15131504
if (PyTuple_GET_SIZE(indexgroup) > 0) {
1514-
Py_INCREF(indexgroup);
1515-
self->indexgroup = indexgroup;
1505+
self->indexgroup = Py_NewRef(indexgroup);
15161506
}
15171507
}
15181508

@@ -1555,8 +1545,7 @@ _sre_template_impl(PyObject *module, PyObject *pattern, PyObject *template)
15551545
if (!self)
15561546
return NULL;
15571547
self->chunks = 1 + 2*n;
1558-
self->literal = PyList_GET_ITEM(template, 0);
1559-
Py_INCREF(self->literal);
1548+
self->literal = Py_NewRef(PyList_GET_ITEM(template, 0));
15601549
for (Py_ssize_t i = 0; i < n; i++) {
15611550
Py_ssize_t index = PyLong_AsSsize_t(PyList_GET_ITEM(template, 2*i+1));
15621551
if (index == -1 && PyErr_Occurred()) {
@@ -1576,8 +1565,7 @@ _sre_template_impl(PyObject *module, PyObject *pattern, PyObject *template)
15761565
literal = NULL;
15771566
self->chunks--;
15781567
}
1579-
Py_XINCREF(literal);
1580-
self->items[i].literal = literal;
1568+
self->items[i].literal = Py_XNewRef(literal);
15811569
}
15821570
return (PyObject*) self;
15831571

@@ -2128,8 +2116,7 @@ match_getslice_by_index(MatchObject* self, Py_ssize_t index, PyObject* def)
21282116

21292117
if (self->string == Py_None || self->mark[index] < 0) {
21302118
/* return default value if the string or group is undefined */
2131-
Py_INCREF(def);
2132-
return def;
2119+
return Py_NewRef(def);
21332120
}
21342121

21352122
ptr = getstring(self->string, &length, &isbytes, &charsize, &view);
@@ -2448,8 +2435,7 @@ match_regs(MatchObject* self)
24482435
PyTuple_SET_ITEM(regs, index, item);
24492436
}
24502437

2451-
Py_INCREF(regs);
2452-
self->regs = regs;
2438+
self->regs = Py_NewRef(regs);
24532439

24542440
return regs;
24552441
}
@@ -2463,8 +2449,7 @@ static PyObject *
24632449
_sre_SRE_Match___copy___impl(MatchObject *self)
24642450
/*[clinic end generated code: output=a779c5fc8b5b4eb4 input=3bb4d30b6baddb5b]*/
24652451
{
2466-
Py_INCREF(self);
2467-
return (PyObject *)self;
2452+
return Py_NewRef(self);
24682453
}
24692454

24702455
/*[clinic input]
@@ -2479,8 +2464,7 @@ static PyObject *
24792464
_sre_SRE_Match___deepcopy__(MatchObject *self, PyObject *memo)
24802465
/*[clinic end generated code: output=ba7cb46d655e4ee2 input=779d12a31c2c325e]*/
24812466
{
2482-
Py_INCREF(self);
2483-
return (PyObject *)self;
2467+
return Py_NewRef(self);
24842468
}
24852469

24862470
PyDoc_STRVAR(match_doc,
@@ -2509,8 +2493,7 @@ match_lastgroup_get(MatchObject *self, void *Py_UNUSED(ignored))
25092493
{
25102494
PyObject *result = PyTuple_GET_ITEM(self->pattern->indexgroup,
25112495
self->lastindex);
2512-
Py_INCREF(result);
2513-
return result;
2496+
return Py_NewRef(result);
25142497
}
25152498
Py_RETURN_NONE;
25162499
}
@@ -2519,8 +2502,7 @@ static PyObject *
25192502
match_regs_get(MatchObject *self, void *Py_UNUSED(ignored))
25202503
{
25212504
if (self->regs) {
2522-
Py_INCREF(self->regs);
2523-
return self->regs;
2505+
return Py_NewRef(self->regs);
25242506
} else
25252507
return match_regs(self);
25262508
}
@@ -2564,11 +2546,9 @@ pattern_new_match(_sremodulestate* module_state,
25642546
if (!match)
25652547
return NULL;
25662548

2567-
Py_INCREF(pattern);
2568-
match->pattern = pattern;
2549+
match->pattern = (PatternObject*)Py_NewRef(pattern);
25692550

2570-
Py_INCREF(state->string);
2571-
match->string = state->string;
2551+
match->string = Py_NewRef(state->string);
25722552

25732553
match->regs = NULL;
25742554
match->groups = pattern->groups+1;
@@ -2788,8 +2768,7 @@ pattern_scanner(_sremodulestate *module_state,
27882768
return NULL;
27892769
}
27902770

2791-
Py_INCREF(self);
2792-
scanner->pattern = (PyObject*) self;
2771+
scanner->pattern = Py_NewRef(self);
27932772

27942773
PyObject_GC_Track(scanner);
27952774
return (PyObject*) scanner;
@@ -2834,8 +2813,7 @@ static PyObject *
28342813
expand_template(TemplateObject *self, MatchObject *match)
28352814
{
28362815
if (Py_SIZE(self) == 0) {
2837-
Py_INCREF(self->literal);
2838-
return self->literal;
2816+
return Py_NewRef(self->literal);
28392817
}
28402818

28412819
PyObject *result = NULL;
@@ -2855,8 +2833,7 @@ expand_template(TemplateObject *self, MatchObject *match)
28552833
out = &PyList_GET_ITEM(list, 0);
28562834
}
28572835

2858-
Py_INCREF(self->literal);
2859-
out[count++] = self->literal;
2836+
out[count++] = Py_NewRef(self->literal);
28602837
for (Py_ssize_t i = 0; i < Py_SIZE(self); i++) {
28612838
Py_ssize_t index = self->items[i].index;
28622839
if (index >= match->groups) {
@@ -2868,15 +2845,13 @@ expand_template(TemplateObject *self, MatchObject *match)
28682845
goto cleanup;
28692846
}
28702847
if (item != Py_None) {
2871-
Py_INCREF(item);
2872-
out[count++] = item;
2848+
out[count++] = Py_NewRef(item);
28732849
}
28742850
Py_DECREF(item);
28752851

28762852
PyObject *literal = self->items[i].literal;
28772853
if (literal != NULL) {
2878-
Py_INCREF(literal);
2879-
out[count++] = literal;
2854+
out[count++] = Py_NewRef(literal);
28802855
}
28812856
}
28822857

0 commit comments

Comments
 (0)