Skip to content

Commit 7e4dec0

Browse files
authored
gh-99300: Use Py_NewRef() in Modules/ directory (#99467)
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in test C files of the Modules/ directory.
1 parent 3817607 commit 7e4dec0

File tree

10 files changed

+75
-144
lines changed

10 files changed

+75
-144
lines changed

Modules/_io/_iomodule.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,7 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,
212212
is_number = PyNumber_Check(file);
213213

214214
if (is_number) {
215-
path_or_fd = file;
216-
Py_INCREF(path_or_fd);
215+
path_or_fd = Py_NewRef(file);
217216
} else {
218217
path_or_fd = PyOS_FSPath(file);
219218
if (path_or_fd == NULL) {
@@ -489,8 +488,7 @@ _io_text_encoding_impl(PyObject *module, PyObject *encoding, int stacklevel)
489488
encoding = &_Py_ID(locale);
490489
}
491490
}
492-
Py_INCREF(encoding);
493-
return encoding;
491+
return Py_NewRef(encoding);
494492
}
495493

496494

@@ -697,9 +695,8 @@ PyInit__io(void)
697695
"UnsupportedOperation", PyExc_OSError, PyExc_ValueError);
698696
if (state->unsupported_operation == NULL)
699697
goto fail;
700-
Py_INCREF(state->unsupported_operation);
701698
if (PyModule_AddObject(m, "UnsupportedOperation",
702-
state->unsupported_operation) < 0)
699+
Py_NewRef(state->unsupported_operation)) < 0)
703700
goto fail;
704701

705702
/* BlockingIOError, for compatibility */

Modules/_io/bufferedio.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,7 @@ buffered_close(buffered *self, PyObject *args)
481481
if (r < 0)
482482
goto end;
483483
if (r > 0) {
484-
res = Py_None;
485-
Py_INCREF(res);
484+
res = Py_NewRef(Py_None);
486485
goto end;
487486
}
488487

@@ -1007,8 +1006,7 @@ _buffered_readinto_generic(buffered *self, Py_buffer *buffer, char readinto1)
10071006
break;
10081007
if (n < 0) {
10091008
if (n == -2) {
1010-
Py_INCREF(Py_None);
1011-
res = Py_None;
1009+
res = Py_NewRef(Py_None);
10121010
}
10131011
goto end;
10141012
}
@@ -1422,8 +1420,7 @@ _io_BufferedReader___init___impl(buffered *self, PyObject *raw,
14221420
if (_PyIOBase_check_readable(raw, Py_True) == NULL)
14231421
return -1;
14241422

1425-
Py_INCREF(raw);
1426-
Py_XSETREF(self->raw, raw);
1423+
Py_XSETREF(self->raw, Py_NewRef(raw));
14271424
self->buffer_size = buffer_size;
14281425
self->readable = 1;
14291426
self->writable = 0;

Modules/_io/bytesio.c

+6-12
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,7 @@ _io_BytesIO_getbuffer_impl(bytesio *self)
324324
buf = (bytesiobuf *) type->tp_alloc(type, 0);
325325
if (buf == NULL)
326326
return NULL;
327-
Py_INCREF(self);
328-
buf->source = self;
327+
buf->source = (bytesio*)Py_NewRef(self);
329328
view = PyMemoryView_FromObject((PyObject *) buf);
330329
Py_DECREF(buf);
331330
return view;
@@ -356,8 +355,7 @@ _io_BytesIO_getvalue_impl(bytesio *self)
356355
return NULL;
357356
}
358357
}
359-
Py_INCREF(self->buf);
360-
return self->buf;
358+
return Py_NewRef(self->buf);
361359
}
362360

363361
/*[clinic input]
@@ -401,8 +399,7 @@ read_bytes(bytesio *self, Py_ssize_t size)
401399
self->pos == 0 && size == PyBytes_GET_SIZE(self->buf) &&
402400
self->exports == 0) {
403401
self->pos += size;
404-
Py_INCREF(self->buf);
405-
return self->buf;
402+
return Py_NewRef(self->buf);
406403
}
407404

408405
output = PyBytes_AS_STRING(self->buf) + self->pos;
@@ -791,8 +788,7 @@ bytesio_getstate(bytesio *self, PyObject *Py_UNUSED(ignored))
791788
if (initvalue == NULL)
792789
return NULL;
793790
if (self->dict == NULL) {
794-
Py_INCREF(Py_None);
795-
dict = Py_None;
791+
dict = Py_NewRef(Py_None);
796792
}
797793
else {
798794
dict = PyDict_Copy(self->dict);
@@ -875,8 +871,7 @@ bytesio_setstate(bytesio *self, PyObject *state)
875871
return NULL;
876872
}
877873
else {
878-
Py_INCREF(dict);
879-
self->dict = dict;
874+
self->dict = Py_NewRef(dict);
880875
}
881876
}
882877

@@ -943,8 +938,7 @@ _io_BytesIO___init___impl(bytesio *self, PyObject *initvalue)
943938
}
944939
if (initvalue && initvalue != Py_None) {
945940
if (PyBytes_CheckExact(initvalue)) {
946-
Py_INCREF(initvalue);
947-
Py_XSETREF(self->buf, initvalue);
941+
Py_XSETREF(self->buf, Py_NewRef(initvalue));
948942
self->string_size = PyBytes_GET_SIZE(initvalue);
949943
}
950944
else {

Modules/_io/iobase.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,7 @@ iobase_enter(PyObject *self, PyObject *args)
464464
if (iobase_check_closed(self))
465465
return NULL;
466466

467-
Py_INCREF(self);
468-
return self;
467+
return Py_NewRef(self);
469468
}
470469

471470
static PyObject *
@@ -642,8 +641,7 @@ iobase_iter(PyObject *self)
642641
if (iobase_check_closed(self))
643642
return NULL;
644643

645-
Py_INCREF(self);
646-
return self;
644+
return Py_NewRef(self);
647645
}
648646

649647
static PyObject *

Modules/_io/stringio.c

+4-8
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,7 @@ write_str(stringio *self, PyObject *obj)
188188
self->decoder, obj, 1 /* always final */);
189189
}
190190
else {
191-
decoded = obj;
192-
Py_INCREF(decoded);
191+
decoded = Py_NewRef(obj);
193192
}
194193
if (self->writenl) {
195194
PyObject *translated = PyUnicode_Replace(
@@ -710,8 +709,7 @@ _io_StringIO___init___impl(stringio *self, PyObject *value,
710709
is pointless for StringIO)
711710
*/
712711
if (newline != NULL && newline[0] == '\r') {
713-
self->writenl = self->readnl;
714-
Py_INCREF(self->writenl);
712+
self->writenl = Py_NewRef(self->readnl);
715713
}
716714

717715
if (self->readuniversal) {
@@ -823,8 +821,7 @@ stringio_getstate(stringio *self, PyObject *Py_UNUSED(ignored))
823821
if (initvalue == NULL)
824822
return NULL;
825823
if (self->dict == NULL) {
826-
Py_INCREF(Py_None);
827-
dict = Py_None;
824+
dict = Py_NewRef(Py_None);
828825
}
829826
else {
830827
dict = PyDict_Copy(self->dict);
@@ -934,8 +931,7 @@ stringio_setstate(stringio *self, PyObject *state)
934931
return NULL;
935932
}
936933
else {
937-
Py_INCREF(dict);
938-
self->dict = dict;
934+
self->dict = Py_NewRef(dict);
939935
}
940936
}
941937

Modules/_io/textio.c

+12-23
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,14 @@ _io_IncrementalNewlineDecoder___init___impl(nldecoder_object *self,
231231
PyObject *errors)
232232
/*[clinic end generated code: output=fbd04d443e764ec2 input=89db6b19c6b126bf]*/
233233
{
234-
self->decoder = decoder;
235-
Py_INCREF(decoder);
234+
self->decoder = Py_NewRef(decoder);
236235

237236
if (errors == NULL) {
238-
self->errors = &_Py_ID(strict);
237+
self->errors = Py_NewRef(&_Py_ID(strict));
239238
}
240239
else {
241-
self->errors = errors;
240+
self->errors = Py_NewRef(errors);
242241
}
243-
Py_INCREF(self->errors);
244242

245243
self->translate = translate ? 1 : 0;
246244
self->seennl = 0;
@@ -301,8 +299,7 @@ _PyIncrementalNewlineDecoder_decode(PyObject *myself,
301299
&_Py_ID(decode), input, final ? Py_True : Py_False, NULL);
302300
}
303301
else {
304-
output = input;
305-
Py_INCREF(output);
302+
output = Py_NewRef(input);
306303
}
307304

308305
if (check_decoded(output) < 0)
@@ -1148,17 +1145,15 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
11481145
* of the partially constructed object (like self->encoding)
11491146
*/
11501147

1151-
Py_INCREF(errors);
1152-
self->errors = errors;
1148+
self->errors = Py_NewRef(errors);
11531149
self->chunk_size = 8192;
11541150
self->line_buffering = line_buffering;
11551151
self->write_through = write_through;
11561152
if (set_newline(self, newline) < 0) {
11571153
goto error;
11581154
}
11591155

1160-
self->buffer = buffer;
1161-
Py_INCREF(buffer);
1156+
self->buffer = Py_NewRef(buffer);
11621157

11631158
/* Build the decoder object */
11641159
if (_textiowrapper_set_decoder(self, codec_info, PyUnicode_AsUTF8(errors)) != 0)
@@ -1284,9 +1279,8 @@ textiowrapper_change_encoding(textio *self, PyObject *encoding,
12841279
}
12851280
Py_DECREF(codec_info);
12861281

1287-
Py_INCREF(errors);
12881282
Py_SETREF(self->encoding, encoding);
1289-
Py_SETREF(self->errors, errors);
1283+
Py_SETREF(self->errors, Py_NewRef(errors));
12901284

12911285
return _textiowrapper_fix_encoder_state(self);
12921286
}
@@ -1502,8 +1496,7 @@ _textiowrapper_writeflush(textio *self)
15021496
PyObject *b;
15031497

15041498
if (PyBytes_Check(pending)) {
1505-
b = pending;
1506-
Py_INCREF(b);
1499+
b = Py_NewRef(pending);
15071500
}
15081501
else if (PyUnicode_Check(pending)) {
15091502
assert(PyUnicode_IS_ASCII(pending));
@@ -1618,8 +1611,7 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
16181611
// See bpo-43260
16191612
PyUnicode_GET_LENGTH(text) <= self->chunk_size &&
16201613
is_asciicompat_encoding(self->encodefunc)) {
1621-
b = text;
1622-
Py_INCREF(b);
1614+
b = Py_NewRef(text);
16231615
}
16241616
else {
16251617
b = (*self->encodefunc)((PyObject *) self, text);
@@ -1741,8 +1733,7 @@ textiowrapper_get_decoded_chars(textio *self, Py_ssize_t n)
17411733
return NULL;
17421734
}
17431735
else {
1744-
chars = self->decoded_chars;
1745-
Py_INCREF(chars);
1736+
chars = Py_NewRef(self->decoded_chars);
17461737
}
17471738

17481739
self->decoded_chars_used += n;
@@ -2139,10 +2130,9 @@ _textiowrapper_readline(textio *self, Py_ssize_t limit)
21392130
}
21402131

21412132
if (remaining == NULL) {
2142-
line = self->decoded_chars;
2133+
line = Py_NewRef(self->decoded_chars);
21432134
start = self->decoded_chars_used;
21442135
offset_to_buffer = 0;
2145-
Py_INCREF(line);
21462136
}
21472137
else {
21482138
assert(self->decoded_chars_used == 0);
@@ -3115,8 +3105,7 @@ static PyObject *
31153105
textiowrapper_errors_get(textio *self, void *context)
31163106
{
31173107
CHECK_INITIALIZED(self);
3118-
Py_INCREF(self->errors);
3119-
return self->errors;
3108+
return Py_NewRef(self->errors);
31203109
}
31213110

31223111
static PyObject *

Modules/_json.c

+8-14
Original file line numberDiff line numberDiff line change
@@ -1243,16 +1243,17 @@ encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
12431243
if (s == NULL)
12441244
return NULL;
12451245

1246-
s->markers = markers;
1247-
s->defaultfn = defaultfn;
1248-
s->encoder = encoder;
1249-
s->indent = indent;
1250-
s->key_separator = key_separator;
1251-
s->item_separator = item_separator;
1246+
s->markers = Py_NewRef(markers);
1247+
s->defaultfn = Py_NewRef(defaultfn);
1248+
s->encoder = Py_NewRef(encoder);
1249+
s->indent = Py_NewRef(indent);
1250+
s->key_separator = Py_NewRef(key_separator);
1251+
s->item_separator = Py_NewRef(item_separator);
12521252
s->sort_keys = sort_keys;
12531253
s->skipkeys = skipkeys;
12541254
s->allow_nan = allow_nan;
12551255
s->fast_encode = NULL;
1256+
12561257
if (PyCFunction_Check(s->encoder)) {
12571258
PyCFunction f = PyCFunction_GetFunction(s->encoder);
12581259
if (f == (PyCFunction)py_encode_basestring_ascii ||
@@ -1261,12 +1262,6 @@ encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
12611262
}
12621263
}
12631264

1264-
Py_INCREF(s->markers);
1265-
Py_INCREF(s->defaultfn);
1266-
Py_INCREF(s->encoder);
1267-
Py_INCREF(s->indent);
1268-
Py_INCREF(s->key_separator);
1269-
Py_INCREF(s->item_separator);
12701265
return (PyObject *)s;
12711266
}
12721267

@@ -1480,8 +1475,7 @@ encoder_encode_key_value(PyEncoderObject *s, _PyUnicodeWriter *writer, bool *fir
14801475
PyObject *encoded;
14811476

14821477
if (PyUnicode_Check(key)) {
1483-
Py_INCREF(key);
1484-
keystr = key;
1478+
keystr = Py_NewRef(key);
14851479
}
14861480
else if (PyFloat_Check(key)) {
14871481
keystr = encoder_encode_float(s, key);

Modules/_lsprof.c

+4-8
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ normalizeUserObj(PyObject *obj)
128128
{
129129
PyCFunctionObject *fn;
130130
if (!PyCFunction_Check(obj)) {
131-
Py_INCREF(obj);
132-
return obj;
131+
return Py_NewRef(obj);
133132
}
134133
/* Replace built-in function objects with a descriptive string
135134
because of built-in methods -- keeping a reference to
@@ -142,8 +141,7 @@ normalizeUserObj(PyObject *obj)
142141
PyObject *modname = NULL;
143142
if (mod != NULL) {
144143
if (PyUnicode_Check(mod)) {
145-
modname = mod;
146-
Py_INCREF(modname);
144+
modname = Py_NewRef(mod);
147145
}
148146
else if (PyModule_Check(mod)) {
149147
modname = PyModule_GetNameObject(mod);
@@ -555,8 +553,7 @@ static int statsForEntry(rotating_node_t *node, void *arg)
555553
}
556554
}
557555
else {
558-
Py_INCREF(Py_None);
559-
collect->sublist = Py_None;
556+
collect->sublist = Py_NewRef(Py_None);
560557
}
561558

562559
info = PyObject_CallFunction((PyObject*) collect->state->stats_entry_type,
@@ -781,8 +778,7 @@ profiler_init(ProfilerObject *pObj, PyObject *args, PyObject *kw)
781778
if (setSubcalls(pObj, subcalls) < 0 || setBuiltins(pObj, builtins) < 0)
782779
return -1;
783780
pObj->externalTimerUnit = timeunit;
784-
Py_XINCREF(timer);
785-
Py_XSETREF(pObj->externalTimer, timer);
781+
Py_XSETREF(pObj->externalTimer, Py_XNewRef(timer));
786782
return 0;
787783
}
788784

0 commit comments

Comments
 (0)