Skip to content

Commit 3e2f713

Browse files
authored
gh-99300: Use Py_NewRef() in Modules/ directory (#99469)
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in test C files of the Modules/ directory.
1 parent e3d4fed commit 3e2f713

10 files changed

+66
-130
lines changed

Modules/cjkcodecs/multibytecodec.c

+7-14
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@ codecctx_errors_get(MultibyteStatefulCodecContext *self, void *Py_UNUSED(ignored
141141
else if (self->errors == ERROR_REPLACE)
142142
errors = "replace";
143143
else {
144-
Py_INCREF(self->errors);
145-
return self->errors;
144+
return Py_NewRef(self->errors);
146145
}
147146

148147
return PyUnicode_FromString(errors);
@@ -341,8 +340,7 @@ multibytecodec_encerror(MultibyteCodec *codec,
341340
goto errorexit;
342341
}
343342
else {
344-
Py_INCREF(tobj);
345-
retstr = tobj;
343+
retstr = Py_NewRef(tobj);
346344
}
347345

348346
assert(PyBytes_Check(retstr));
@@ -786,11 +784,9 @@ encoder_encode_stateful(MultibyteStatefulEncoderContext *ctx,
786784
if (ctx->pending) {
787785
PyObject *inbuf_tmp;
788786

789-
Py_INCREF(ctx->pending);
790-
origpending = ctx->pending;
787+
origpending = Py_NewRef(ctx->pending);
791788

792-
Py_INCREF(ctx->pending);
793-
inbuf_tmp = ctx->pending;
789+
inbuf_tmp = Py_NewRef(ctx->pending);
794790
PyUnicode_Append(&inbuf_tmp, unistr);
795791
if (inbuf_tmp == NULL)
796792
goto errorexit;
@@ -800,8 +796,7 @@ encoder_encode_stateful(MultibyteStatefulEncoderContext *ctx,
800796
else {
801797
origpending = NULL;
802798

803-
Py_INCREF(unistr);
804-
inbuf = unistr;
799+
inbuf = Py_NewRef(unistr);
805800
}
806801
if (PyUnicode_READY(inbuf) < 0)
807802
goto errorexit;
@@ -1645,8 +1640,7 @@ mbstreamreader_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
16451640
}
16461641

16471642
self->codec = ((MultibyteCodecObject *)codec)->codec;
1648-
self->stream = stream;
1649-
Py_INCREF(stream);
1643+
self->stream = Py_NewRef(stream);
16501644
self->pendingsize = 0;
16511645
self->errors = internal_error_callback(errors);
16521646
if (self->errors == NULL)
@@ -1869,8 +1863,7 @@ mbstreamwriter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
18691863
}
18701864

18711865
self->codec = ((MultibyteCodecObject *)codec)->codec;
1872-
self->stream = stream;
1873-
Py_INCREF(stream);
1866+
self->stream = Py_NewRef(stream);
18741867
self->pending = NULL;
18751868
self->errors = internal_error_callback(errors);
18761869
if (self->errors == NULL)

Modules/gcmodule.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -1870,8 +1870,7 @@ gc_is_tracked(PyObject *module, PyObject *obj)
18701870
result = Py_True;
18711871
else
18721872
result = Py_False;
1873-
Py_INCREF(result);
1874-
return result;
1873+
return Py_NewRef(result);
18751874
}
18761875

18771876
/*[clinic input]

Modules/getpath.c

+10-19
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ getpath_isabs(PyObject *Py_UNUSED(self), PyObject *args)
125125
r = _Py_isabs(path) ? Py_True : Py_False;
126126
PyMem_Free((void *)path);
127127
}
128-
Py_XINCREF(r);
129-
return r;
128+
return Py_XNewRef(r);
130129
}
131130

132131

@@ -153,11 +152,10 @@ getpath_hassuffix(PyObject *Py_UNUSED(self), PyObject *args)
153152
wcscmp(&path[len - suffixLen], suffix) != 0
154153
#endif
155154
) {
156-
r = Py_False;
155+
r = Py_NewRef(Py_False);
157156
} else {
158-
r = Py_True;
157+
r = Py_NewRef(Py_True);
159158
}
160-
Py_INCREF(r);
161159
PyMem_Free((void *)suffix);
162160
}
163161
PyMem_Free((void *)path);
@@ -187,8 +185,7 @@ getpath_isdir(PyObject *Py_UNUSED(self), PyObject *args)
187185
#endif
188186
PyMem_Free((void *)path);
189187
}
190-
Py_XINCREF(r);
191-
return r;
188+
return Py_XNewRef(r);
192189
}
193190

194191

@@ -213,8 +210,7 @@ getpath_isfile(PyObject *Py_UNUSED(self), PyObject *args)
213210
#endif
214211
PyMem_Free((void *)path);
215212
}
216-
Py_XINCREF(r);
217-
return r;
213+
return Py_XNewRef(r);
218214
}
219215

220216

@@ -247,8 +243,7 @@ getpath_isxfile(PyObject *Py_UNUSED(self), PyObject *args)
247243
#endif
248244
PyMem_Free((void *)path);
249245
}
250-
Py_XINCREF(r);
251-
return r;
246+
return Py_XNewRef(r);
252247
}
253248

254249

@@ -488,8 +483,7 @@ getpath_realpath(PyObject *Py_UNUSED(self) , PyObject *args)
488483
goto done;
489484
}
490485
if (!S_ISLNK(st.st_mode)) {
491-
Py_INCREF(pathobj);
492-
r = pathobj;
486+
r = Py_NewRef(pathobj);
493487
goto done;
494488
}
495489
wchar_t resolved[MAXPATHLEN+1];
@@ -504,8 +498,7 @@ getpath_realpath(PyObject *Py_UNUSED(self) , PyObject *args)
504498
return r;
505499
#endif
506500

507-
Py_INCREF(pathobj);
508-
return pathobj;
501+
return Py_NewRef(pathobj);
509502
}
510503

511504

@@ -591,8 +584,7 @@ wchar_to_dict(PyObject *dict, const char *key, const wchar_t *s)
591584
return 0;
592585
}
593586
} else {
594-
u = Py_None;
595-
Py_INCREF(u);
587+
u = Py_NewRef(Py_None);
596588
}
597589
r = PyDict_SetItemString(dict, key, u) == 0;
598590
Py_DECREF(u);
@@ -617,8 +609,7 @@ decode_to_dict(PyObject *dict, const char *key, const char *s)
617609
return 0;
618610
}
619611
} else {
620-
u = Py_None;
621-
Py_INCREF(u);
612+
u = Py_NewRef(Py_None);
622613
}
623614
r = PyDict_SetItemString(dict, key, u) == 0;
624615
Py_DECREF(u);

Modules/mathmodule.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -2048,8 +2048,7 @@ factorial_odd_part(unsigned long n)
20482048
inner = PyLong_FromLong(1);
20492049
if (inner == NULL)
20502050
return NULL;
2051-
outer = inner;
2052-
Py_INCREF(outer);
2051+
outer = Py_NewRef(inner);
20532052

20542053
upper = 3;
20552054
for (i = _Py_bit_length(n) - 2; i >= 0; i--) {
@@ -3521,8 +3520,7 @@ perm_comb(PyObject *n, unsigned long long k, int iscomb)
35213520
return PyLong_FromLong(1);
35223521
}
35233522
if (k == 1) {
3524-
Py_INCREF(n);
3525-
return n;
3523+
return Py_NewRef(n);
35263524
}
35273525

35283526
/* P(n, k) = P(n, j) * P(n-j, k-j) */

Modules/ossaudiodev.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,7 @@ oss_close(oss_audio_t *self, PyObject *unused)
534534
static PyObject *
535535
oss_self(PyObject *self, PyObject *unused)
536536
{
537-
Py_INCREF(self);
538-
return self;
537+
return Py_NewRef(self);
539538
}
540539

541540
static PyObject *
@@ -1135,10 +1134,8 @@ PyInit_ossaudiodev(void)
11351134
NULL, NULL);
11361135
if (OSSAudioError) {
11371136
/* Each call to PyModule_AddObject decrefs it; compensate: */
1138-
Py_INCREF(OSSAudioError);
1139-
Py_INCREF(OSSAudioError);
1140-
PyModule_AddObject(m, "error", OSSAudioError);
1141-
PyModule_AddObject(m, "OSSAudioError", OSSAudioError);
1137+
PyModule_AddObject(m, "error", Py_NewRef(OSSAudioError));
1138+
PyModule_AddObject(m, "OSSAudioError", Py_NewRef(OSSAudioError));
11421139
}
11431140

11441141
/* Build 'control_labels' and 'control_names' lists and add them

Modules/overlapped.c

+4-8
Original file line numberDiff line numberDiff line change
@@ -912,8 +912,7 @@ _overlapped_Overlapped_getresult_impl(OverlappedObject *self, BOOL wait)
912912
_PyBytes_Resize(&self->allocated_buffer, transferred))
913913
return NULL;
914914

915-
Py_INCREF(self->allocated_buffer);
916-
return self->allocated_buffer;
915+
return Py_NewRef(self->allocated_buffer);
917916
case TYPE_READ_FROM:
918917
assert(PyBytes_CheckExact(self->read_from.allocated_buffer));
919918

@@ -940,14 +939,12 @@ _overlapped_Overlapped_getresult_impl(OverlappedObject *self, BOOL wait)
940939
}
941940

942941
// first item: message
943-
Py_INCREF(self->read_from.allocated_buffer);
944942
PyTuple_SET_ITEM(self->read_from.result, 0,
945-
self->read_from.allocated_buffer);
943+
Py_NewRef(self->read_from.allocated_buffer));
946944
// second item: address
947945
PyTuple_SET_ITEM(self->read_from.result, 1, addr);
948946

949-
Py_INCREF(self->read_from.result);
950-
return self->read_from.result;
947+
return Py_NewRef(self->read_from.result);
951948
case TYPE_READ_FROM_INTO:
952949
// unparse the address
953950
addr = unparse_address((SOCKADDR*)&self->read_from_into.address,
@@ -970,8 +967,7 @@ _overlapped_Overlapped_getresult_impl(OverlappedObject *self, BOOL wait)
970967
// second item: address
971968
PyTuple_SET_ITEM(self->read_from_into.result, 1, addr);
972969

973-
Py_INCREF(self->read_from_into.result);
974-
return self->read_from_into.result;
970+
return Py_NewRef(self->read_from_into.result);
975971
default:
976972
return PyLong_FromUnsignedLong((unsigned long) transferred);
977973
}

0 commit comments

Comments
 (0)