Skip to content

Commit 65dd745

Browse files
authored
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.
1 parent 3e2f713 commit 65dd745

File tree

7 files changed

+54
-97
lines changed

7 files changed

+54
-97
lines changed

Modules/symtablemodule.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ _symtable_symtable_impl(PyObject *module, PyObject *source,
5656
if (st == NULL) {
5757
return NULL;
5858
}
59-
t = (PyObject *)st->st_top;
60-
Py_INCREF(t);
59+
t = Py_NewRef(st->st_top);
6160
_PySymtable_Free(st);
6261
return t;
6362
}

Modules/unicodedata.c

+10-20
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ unicodedata_UCD_decimal_impl(PyObject *self, int chr,
159159
return NULL;
160160
}
161161
else {
162-
Py_INCREF(default_value);
163-
return default_value;
162+
return Py_NewRef(default_value);
164163
}
165164
}
166165
return PyLong_FromLong(rc);
@@ -194,8 +193,7 @@ unicodedata_UCD_digit_impl(PyObject *self, int chr, PyObject *default_value)
194193
return NULL;
195194
}
196195
else {
197-
Py_INCREF(default_value);
198-
return default_value;
196+
return Py_NewRef(default_value);
199197
}
200198
}
201199
return PyLong_FromLong(rc);
@@ -246,8 +244,7 @@ unicodedata_UCD_numeric_impl(PyObject *self, int chr,
246244
return NULL;
247245
}
248246
else {
249-
Py_INCREF(default_value);
250-
return default_value;
247+
return Py_NewRef(default_value);
251248
}
252249
}
253250
return PyFloat_FromDouble(rc);
@@ -917,8 +914,7 @@ unicodedata_UCD_is_normalized_impl(PyObject *self, PyObject *form,
917914
result = (m == YES) ? Py_True : Py_False;
918915
}
919916

920-
Py_INCREF(result);
921-
return result;
917+
return Py_NewRef(result);
922918
}
923919

924920

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

950945
if (PyUnicode_CompareWithASCIIString(form, "NFC") == 0) {
951946
if (is_normalized_quickcheck(self, input,
952947
true, false, true) == YES) {
953-
Py_INCREF(input);
954-
return input;
948+
return Py_NewRef(input);
955949
}
956950
return nfc_nfkc(self, input, 0);
957951
}
958952
if (PyUnicode_CompareWithASCIIString(form, "NFKC") == 0) {
959953
if (is_normalized_quickcheck(self, input,
960954
true, true, true) == YES) {
961-
Py_INCREF(input);
962-
return input;
955+
return Py_NewRef(input);
963956
}
964957
return nfc_nfkc(self, input, 1);
965958
}
966959
if (PyUnicode_CompareWithASCIIString(form, "NFD") == 0) {
967960
if (is_normalized_quickcheck(self, input,
968961
false, false, true) == YES) {
969-
Py_INCREF(input);
970-
return input;
962+
return Py_NewRef(input);
971963
}
972964
return nfd_nfkd(self, input, 0);
973965
}
974966
if (PyUnicode_CompareWithASCIIString(form, "NFKD") == 0) {
975967
if (is_normalized_quickcheck(self, input,
976968
false, true, true) == YES) {
977-
Py_INCREF(input);
978-
return input;
969+
return Py_NewRef(input);
979970
}
980971
return nfd_nfkd(self, input, 1);
981972
}
@@ -1370,8 +1361,7 @@ unicodedata_UCD_name_impl(PyObject *self, int chr, PyObject *default_value)
13701361
return NULL;
13711362
}
13721363
else {
1373-
Py_INCREF(default_value);
1374-
return default_value;
1364+
return Py_NewRef(default_value);
13751365
}
13761366
}
13771367

Modules/xxlimited.c

+4-8
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ Xxo_getattro(XxoObject *self, PyObject *name)
155155
if (self->x_attr != NULL) {
156156
PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
157157
if (v != NULL) {
158-
Py_INCREF(v);
159-
return v;
158+
return Py_NewRef(v);
160159
}
161160
else if (PyErr_Occurred()) {
162161
return NULL;
@@ -210,18 +209,15 @@ Xxo_demo(XxoObject *self, PyTypeObject *defining_class,
210209

211210
/* Test if the argument is "str" */
212211
if (PyUnicode_Check(o)) {
213-
Py_INCREF(o);
214-
return o;
212+
return Py_NewRef(o);
215213
}
216214

217215
/* test if the argument is of the Xxo class */
218216
if (PyObject_TypeCheck(o, defining_class)) {
219-
Py_INCREF(o);
220-
return o;
217+
return Py_NewRef(o);
221218
}
222219

223-
Py_INCREF(Py_None);
224-
return Py_None;
220+
return Py_NewRef(Py_None);
225221
}
226222

227223
static PyMethodDef Xxo_methods[] = {

Modules/xxlimited_35.c

+4-8
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,9 @@ Xxo_demo(XxoObject *self, PyObject *args)
6464
return NULL;
6565
/* Test availability of fast type checks */
6666
if (o != NULL && PyUnicode_Check(o)) {
67-
Py_INCREF(o);
68-
return o;
67+
return Py_NewRef(o);
6968
}
70-
Py_INCREF(Py_None);
71-
return Py_None;
69+
return Py_NewRef(Py_None);
7270
}
7371

7472
static PyMethodDef Xxo_methods[] = {
@@ -83,8 +81,7 @@ Xxo_getattro(XxoObject *self, PyObject *name)
8381
if (self->x_attr != NULL) {
8482
PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
8583
if (v != NULL) {
86-
Py_INCREF(v);
87-
return v;
84+
return Py_NewRef(v);
8885
}
8986
else if (PyErr_Occurred()) {
9087
return NULL;
@@ -176,8 +173,7 @@ xx_roj(PyObject *self, PyObject *args)
176173
long b;
177174
if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
178175
return NULL;
179-
Py_INCREF(Py_None);
180-
return Py_None;
176+
return Py_NewRef(Py_None);
181177
}
182178

183179

Modules/xxmodule.c

+5-10
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ Xxo_demo(XxoObject *self, PyObject *args)
5252
{
5353
if (!PyArg_ParseTuple(args, ":demo"))
5454
return NULL;
55-
Py_INCREF(Py_None);
56-
return Py_None;
55+
return Py_NewRef(Py_None);
5756
}
5857

5958
static PyMethodDef Xxo_methods[] = {
@@ -68,8 +67,7 @@ Xxo_getattro(XxoObject *self, PyObject *name)
6867
if (self->x_attr != NULL) {
6968
PyObject *v = PyDict_GetItemWithError(self->x_attr, name);
7069
if (v != NULL) {
71-
Py_INCREF(v);
72-
return v;
70+
return Py_NewRef(v);
7371
}
7472
else if (PyErr_Occurred()) {
7573
return NULL;
@@ -195,8 +193,7 @@ xx_bug(PyObject *self, PyObject *args)
195193
printf("\n");
196194
/* Py_DECREF(item); */
197195

198-
Py_INCREF(Py_None);
199-
return Py_None;
196+
return Py_NewRef(Py_None);
200197
}
201198

202199
/* Test bad format character */
@@ -208,8 +205,7 @@ xx_roj(PyObject *self, PyObject *args)
208205
long b;
209206
if (!PyArg_ParseTuple(args, "O#:roj", &a, &b))
210207
return NULL;
211-
Py_INCREF(Py_None);
212-
return Py_None;
208+
return Py_NewRef(Py_None);
213209
}
214210

215211

@@ -266,8 +262,7 @@ static PyTypeObject Str_Type = {
266262
static PyObject *
267263
null_richcompare(PyObject *self, PyObject *other, int op)
268264
{
269-
Py_INCREF(Py_NotImplemented);
270-
return Py_NotImplemented;
265+
return Py_NewRef(Py_NotImplemented);
271266
}
272267

273268
static PyTypeObject Null_Type = {

Modules/xxsubtype.c

+7-14
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ spamlist_setstate(spamlistobject *self, PyObject *args)
3939
if (!PyArg_ParseTuple(args, "i:setstate", &state))
4040
return NULL;
4141
self->state = state;
42-
Py_INCREF(Py_None);
43-
return Py_None;
42+
return Py_NewRef(Py_None);
4443
}
4544

4645
static PyObject *
@@ -53,12 +52,9 @@ spamlist_specialmeth(PyObject *self, PyObject *args, PyObject *kw)
5352
self = Py_None;
5453
if (kw == NULL)
5554
kw = Py_None;
56-
Py_INCREF(self);
57-
PyTuple_SET_ITEM(result, 0, self);
58-
Py_INCREF(args);
59-
PyTuple_SET_ITEM(result, 1, args);
60-
Py_INCREF(kw);
61-
PyTuple_SET_ITEM(result, 2, kw);
55+
PyTuple_SET_ITEM(result, 0, Py_NewRef(self));
56+
PyTuple_SET_ITEM(result, 1, Py_NewRef(args));
57+
PyTuple_SET_ITEM(result, 2, Py_NewRef(kw));
6258
}
6359
return result;
6460
}
@@ -164,8 +160,7 @@ spamdict_setstate(spamdictobject *self, PyObject *args)
164160
if (!PyArg_ParseTuple(args, "i:setstate", &state))
165161
return NULL;
166162
self->state = state;
167-
Py_INCREF(Py_None);
168-
return Py_None;
163+
return Py_NewRef(Py_None);
169164
}
170165

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

282-
Py_INCREF(&spamlist_type);
283277
if (PyModule_AddObject(m, "spamlist",
284-
(PyObject *) &spamlist_type) < 0)
278+
Py_NewRef(&spamlist_type)) < 0)
285279
return -1;
286280

287-
Py_INCREF(&spamdict_type);
288281
if (PyModule_AddObject(m, "spamdict",
289-
(PyObject *) &spamdict_type) < 0)
282+
Py_NewRef(&spamdict_type)) < 0)
290283
return -1;
291284
return 0;
292285
}

0 commit comments

Comments
 (0)