Skip to content

Commit

Permalink
gh-99300: Use Py_NewRef() in Modules/ directory (#99467)
Browse files Browse the repository at this point in the history
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in test C files of the Modules/ directory.
  • Loading branch information
vstinner authored Nov 14, 2022
1 parent 3817607 commit 7e4dec0
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 144 deletions.
9 changes: 3 additions & 6 deletions Modules/_io/_iomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ _io_open_impl(PyObject *module, PyObject *file, const char *mode,
is_number = PyNumber_Check(file);

if (is_number) {
path_or_fd = file;
Py_INCREF(path_or_fd);
path_or_fd = Py_NewRef(file);
} else {
path_or_fd = PyOS_FSPath(file);
if (path_or_fd == NULL) {
Expand Down Expand Up @@ -489,8 +488,7 @@ _io_text_encoding_impl(PyObject *module, PyObject *encoding, int stacklevel)
encoding = &_Py_ID(locale);
}
}
Py_INCREF(encoding);
return encoding;
return Py_NewRef(encoding);
}


Expand Down Expand Up @@ -697,9 +695,8 @@ PyInit__io(void)
"UnsupportedOperation", PyExc_OSError, PyExc_ValueError);
if (state->unsupported_operation == NULL)
goto fail;
Py_INCREF(state->unsupported_operation);
if (PyModule_AddObject(m, "UnsupportedOperation",
state->unsupported_operation) < 0)
Py_NewRef(state->unsupported_operation)) < 0)
goto fail;

/* BlockingIOError, for compatibility */
Expand Down
9 changes: 3 additions & 6 deletions Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,7 @@ buffered_close(buffered *self, PyObject *args)
if (r < 0)
goto end;
if (r > 0) {
res = Py_None;
Py_INCREF(res);
res = Py_NewRef(Py_None);
goto end;
}

Expand Down Expand Up @@ -1007,8 +1006,7 @@ _buffered_readinto_generic(buffered *self, Py_buffer *buffer, char readinto1)
break;
if (n < 0) {
if (n == -2) {
Py_INCREF(Py_None);
res = Py_None;
res = Py_NewRef(Py_None);
}
goto end;
}
Expand Down Expand Up @@ -1422,8 +1420,7 @@ _io_BufferedReader___init___impl(buffered *self, PyObject *raw,
if (_PyIOBase_check_readable(raw, Py_True) == NULL)
return -1;

Py_INCREF(raw);
Py_XSETREF(self->raw, raw);
Py_XSETREF(self->raw, Py_NewRef(raw));
self->buffer_size = buffer_size;
self->readable = 1;
self->writable = 0;
Expand Down
18 changes: 6 additions & 12 deletions Modules/_io/bytesio.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,7 @@ _io_BytesIO_getbuffer_impl(bytesio *self)
buf = (bytesiobuf *) type->tp_alloc(type, 0);
if (buf == NULL)
return NULL;
Py_INCREF(self);
buf->source = self;
buf->source = (bytesio*)Py_NewRef(self);
view = PyMemoryView_FromObject((PyObject *) buf);
Py_DECREF(buf);
return view;
Expand Down Expand Up @@ -356,8 +355,7 @@ _io_BytesIO_getvalue_impl(bytesio *self)
return NULL;
}
}
Py_INCREF(self->buf);
return self->buf;
return Py_NewRef(self->buf);
}

/*[clinic input]
Expand Down Expand Up @@ -401,8 +399,7 @@ read_bytes(bytesio *self, Py_ssize_t size)
self->pos == 0 && size == PyBytes_GET_SIZE(self->buf) &&
self->exports == 0) {
self->pos += size;
Py_INCREF(self->buf);
return self->buf;
return Py_NewRef(self->buf);
}

output = PyBytes_AS_STRING(self->buf) + self->pos;
Expand Down Expand Up @@ -791,8 +788,7 @@ bytesio_getstate(bytesio *self, PyObject *Py_UNUSED(ignored))
if (initvalue == NULL)
return NULL;
if (self->dict == NULL) {
Py_INCREF(Py_None);
dict = Py_None;
dict = Py_NewRef(Py_None);
}
else {
dict = PyDict_Copy(self->dict);
Expand Down Expand Up @@ -875,8 +871,7 @@ bytesio_setstate(bytesio *self, PyObject *state)
return NULL;
}
else {
Py_INCREF(dict);
self->dict = dict;
self->dict = Py_NewRef(dict);
}
}

Expand Down Expand Up @@ -943,8 +938,7 @@ _io_BytesIO___init___impl(bytesio *self, PyObject *initvalue)
}
if (initvalue && initvalue != Py_None) {
if (PyBytes_CheckExact(initvalue)) {
Py_INCREF(initvalue);
Py_XSETREF(self->buf, initvalue);
Py_XSETREF(self->buf, Py_NewRef(initvalue));
self->string_size = PyBytes_GET_SIZE(initvalue);
}
else {
Expand Down
6 changes: 2 additions & 4 deletions Modules/_io/iobase.c
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,7 @@ iobase_enter(PyObject *self, PyObject *args)
if (iobase_check_closed(self))
return NULL;

Py_INCREF(self);
return self;
return Py_NewRef(self);
}

static PyObject *
Expand Down Expand Up @@ -642,8 +641,7 @@ iobase_iter(PyObject *self)
if (iobase_check_closed(self))
return NULL;

Py_INCREF(self);
return self;
return Py_NewRef(self);
}

static PyObject *
Expand Down
12 changes: 4 additions & 8 deletions Modules/_io/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ write_str(stringio *self, PyObject *obj)
self->decoder, obj, 1 /* always final */);
}
else {
decoded = obj;
Py_INCREF(decoded);
decoded = Py_NewRef(obj);
}
if (self->writenl) {
PyObject *translated = PyUnicode_Replace(
Expand Down Expand Up @@ -710,8 +709,7 @@ _io_StringIO___init___impl(stringio *self, PyObject *value,
is pointless for StringIO)
*/
if (newline != NULL && newline[0] == '\r') {
self->writenl = self->readnl;
Py_INCREF(self->writenl);
self->writenl = Py_NewRef(self->readnl);
}

if (self->readuniversal) {
Expand Down Expand Up @@ -823,8 +821,7 @@ stringio_getstate(stringio *self, PyObject *Py_UNUSED(ignored))
if (initvalue == NULL)
return NULL;
if (self->dict == NULL) {
Py_INCREF(Py_None);
dict = Py_None;
dict = Py_NewRef(Py_None);
}
else {
dict = PyDict_Copy(self->dict);
Expand Down Expand Up @@ -934,8 +931,7 @@ stringio_setstate(stringio *self, PyObject *state)
return NULL;
}
else {
Py_INCREF(dict);
self->dict = dict;
self->dict = Py_NewRef(dict);
}
}

Expand Down
35 changes: 12 additions & 23 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,14 @@ _io_IncrementalNewlineDecoder___init___impl(nldecoder_object *self,
PyObject *errors)
/*[clinic end generated code: output=fbd04d443e764ec2 input=89db6b19c6b126bf]*/
{
self->decoder = decoder;
Py_INCREF(decoder);
self->decoder = Py_NewRef(decoder);

if (errors == NULL) {
self->errors = &_Py_ID(strict);
self->errors = Py_NewRef(&_Py_ID(strict));
}
else {
self->errors = errors;
self->errors = Py_NewRef(errors);
}
Py_INCREF(self->errors);

self->translate = translate ? 1 : 0;
self->seennl = 0;
Expand Down Expand Up @@ -301,8 +299,7 @@ _PyIncrementalNewlineDecoder_decode(PyObject *myself,
&_Py_ID(decode), input, final ? Py_True : Py_False, NULL);
}
else {
output = input;
Py_INCREF(output);
output = Py_NewRef(input);
}

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

Py_INCREF(errors);
self->errors = errors;
self->errors = Py_NewRef(errors);
self->chunk_size = 8192;
self->line_buffering = line_buffering;
self->write_through = write_through;
if (set_newline(self, newline) < 0) {
goto error;
}

self->buffer = buffer;
Py_INCREF(buffer);
self->buffer = Py_NewRef(buffer);

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

Py_INCREF(errors);
Py_SETREF(self->encoding, encoding);
Py_SETREF(self->errors, errors);
Py_SETREF(self->errors, Py_NewRef(errors));

return _textiowrapper_fix_encoder_state(self);
}
Expand Down Expand Up @@ -1502,8 +1496,7 @@ _textiowrapper_writeflush(textio *self)
PyObject *b;

if (PyBytes_Check(pending)) {
b = pending;
Py_INCREF(b);
b = Py_NewRef(pending);
}
else if (PyUnicode_Check(pending)) {
assert(PyUnicode_IS_ASCII(pending));
Expand Down Expand Up @@ -1618,8 +1611,7 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
// See bpo-43260
PyUnicode_GET_LENGTH(text) <= self->chunk_size &&
is_asciicompat_encoding(self->encodefunc)) {
b = text;
Py_INCREF(b);
b = Py_NewRef(text);
}
else {
b = (*self->encodefunc)((PyObject *) self, text);
Expand Down Expand Up @@ -1741,8 +1733,7 @@ textiowrapper_get_decoded_chars(textio *self, Py_ssize_t n)
return NULL;
}
else {
chars = self->decoded_chars;
Py_INCREF(chars);
chars = Py_NewRef(self->decoded_chars);
}

self->decoded_chars_used += n;
Expand Down Expand Up @@ -2139,10 +2130,9 @@ _textiowrapper_readline(textio *self, Py_ssize_t limit)
}

if (remaining == NULL) {
line = self->decoded_chars;
line = Py_NewRef(self->decoded_chars);
start = self->decoded_chars_used;
offset_to_buffer = 0;
Py_INCREF(line);
}
else {
assert(self->decoded_chars_used == 0);
Expand Down Expand Up @@ -3115,8 +3105,7 @@ static PyObject *
textiowrapper_errors_get(textio *self, void *context)
{
CHECK_INITIALIZED(self);
Py_INCREF(self->errors);
return self->errors;
return Py_NewRef(self->errors);
}

static PyObject *
Expand Down
22 changes: 8 additions & 14 deletions Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -1243,16 +1243,17 @@ encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (s == NULL)
return NULL;

s->markers = markers;
s->defaultfn = defaultfn;
s->encoder = encoder;
s->indent = indent;
s->key_separator = key_separator;
s->item_separator = item_separator;
s->markers = Py_NewRef(markers);
s->defaultfn = Py_NewRef(defaultfn);
s->encoder = Py_NewRef(encoder);
s->indent = Py_NewRef(indent);
s->key_separator = Py_NewRef(key_separator);
s->item_separator = Py_NewRef(item_separator);
s->sort_keys = sort_keys;
s->skipkeys = skipkeys;
s->allow_nan = allow_nan;
s->fast_encode = NULL;

if (PyCFunction_Check(s->encoder)) {
PyCFunction f = PyCFunction_GetFunction(s->encoder);
if (f == (PyCFunction)py_encode_basestring_ascii ||
Expand All @@ -1261,12 +1262,6 @@ encoder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}
}

Py_INCREF(s->markers);
Py_INCREF(s->defaultfn);
Py_INCREF(s->encoder);
Py_INCREF(s->indent);
Py_INCREF(s->key_separator);
Py_INCREF(s->item_separator);
return (PyObject *)s;
}

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

if (PyUnicode_Check(key)) {
Py_INCREF(key);
keystr = key;
keystr = Py_NewRef(key);
}
else if (PyFloat_Check(key)) {
keystr = encoder_encode_float(s, key);
Expand Down
12 changes: 4 additions & 8 deletions Modules/_lsprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ normalizeUserObj(PyObject *obj)
{
PyCFunctionObject *fn;
if (!PyCFunction_Check(obj)) {
Py_INCREF(obj);
return obj;
return Py_NewRef(obj);
}
/* Replace built-in function objects with a descriptive string
because of built-in methods -- keeping a reference to
Expand All @@ -142,8 +141,7 @@ normalizeUserObj(PyObject *obj)
PyObject *modname = NULL;
if (mod != NULL) {
if (PyUnicode_Check(mod)) {
modname = mod;
Py_INCREF(modname);
modname = Py_NewRef(mod);
}
else if (PyModule_Check(mod)) {
modname = PyModule_GetNameObject(mod);
Expand Down Expand Up @@ -555,8 +553,7 @@ static int statsForEntry(rotating_node_t *node, void *arg)
}
}
else {
Py_INCREF(Py_None);
collect->sublist = Py_None;
collect->sublist = Py_NewRef(Py_None);
}

info = PyObject_CallFunction((PyObject*) collect->state->stats_entry_type,
Expand Down Expand Up @@ -781,8 +778,7 @@ profiler_init(ProfilerObject *pObj, PyObject *args, PyObject *kw)
if (setSubcalls(pObj, subcalls) < 0 || setBuiltins(pObj, builtins) < 0)
return -1;
pObj->externalTimerUnit = timeunit;
Py_XINCREF(timer);
Py_XSETREF(pObj->externalTimer, timer);
Py_XSETREF(pObj->externalTimer, Py_XNewRef(timer));
return 0;
}

Expand Down
Loading

0 comments on commit 7e4dec0

Please sign in to comment.