[Python-checkins] gh-99300: Use Py_NewRef() in Modules/ directory (#99467)

vstinner webhook-mailer at python.org
Mon Nov 14 07:08:48 EST 2022


https://github.com/python/cpython/commit/7e4dec02ac86bcbe958f299f68ed25d543c23ef9
commit: 7e4dec02ac86bcbe958f299f68ed25d543c23ef9
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2022-11-14T13:08:43+01:00
summary:

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.

files:
M Modules/_io/_iomodule.c
M Modules/_io/bufferedio.c
M Modules/_io/bytesio.c
M Modules/_io/iobase.c
M Modules/_io/stringio.c
M Modules/_io/textio.c
M Modules/_json.c
M Modules/_lsprof.c
M Modules/_operator.c
M Modules/_pickle.c

diff --git a/Modules/_io/_iomodule.c b/Modules/_io/_iomodule.c
index a7b2e984310d..ccd40ab8bca4 100644
--- a/Modules/_io/_iomodule.c
+++ b/Modules/_io/_iomodule.c
@@ -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) {
@@ -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);
 }
 
 
@@ -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 */
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
index 4a4a1992dbbb..e2610931caae 100644
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -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;
     }
 
@@ -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;
         }
@@ -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;
diff --git a/Modules/_io/bytesio.c b/Modules/_io/bytesio.c
index 930ef7e29dbc..41be3497506d 100644
--- a/Modules/_io/bytesio.c
+++ b/Modules/_io/bytesio.c
@@ -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;
@@ -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]
@@ -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;
@@ -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);
@@ -875,8 +871,7 @@ bytesio_setstate(bytesio *self, PyObject *state)
                 return NULL;
         }
         else {
-            Py_INCREF(dict);
-            self->dict = dict;
+            self->dict = Py_NewRef(dict);
         }
     }
 
@@ -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 {
diff --git a/Modules/_io/iobase.c b/Modules/_io/iobase.c
index 6ae43a8b3bd6..7b9391ec54d7 100644
--- a/Modules/_io/iobase.c
+++ b/Modules/_io/iobase.c
@@ -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 *
@@ -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 *
diff --git a/Modules/_io/stringio.c b/Modules/_io/stringio.c
index 0f31c172499a..5c3bf353680e 100644
--- a/Modules/_io/stringio.c
+++ b/Modules/_io/stringio.c
@@ -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(
@@ -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) {
@@ -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);
@@ -934,8 +931,7 @@ stringio_setstate(stringio *self, PyObject *state)
                 return NULL;
         }
         else {
-            Py_INCREF(dict);
-            self->dict = dict;
+            self->dict = Py_NewRef(dict);
         }
     }
 
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c
index 3369694653fd..8b5d00fe4aaf 100644
--- a/Modules/_io/textio.c
+++ b/Modules/_io/textio.c
@@ -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;
@@ -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)
@@ -1148,8 +1145,7 @@ _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;
@@ -1157,8 +1153,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
         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)
@@ -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);
 }
@@ -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));
@@ -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);
@@ -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;
@@ -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);
@@ -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 *
diff --git a/Modules/_json.c b/Modules/_json.c
index 82f960fdc6c1..06f232f7d7de 100644
--- a/Modules/_json.c
+++ b/Modules/_json.c
@@ -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 ||
@@ -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;
 }
 
@@ -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);
diff --git a/Modules/_lsprof.c b/Modules/_lsprof.c
index a0e262bdac26..cb80c8d33932 100644
--- a/Modules/_lsprof.c
+++ b/Modules/_lsprof.c
@@ -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
@@ -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);
@@ -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,
@@ -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;
 }
 
diff --git a/Modules/_operator.c b/Modules/_operator.c
index 3005845ec932..4f2367150eef 100644
--- a/Modules/_operator.c
+++ b/Modules/_operator.c
@@ -722,8 +722,7 @@ _operator_is_not_impl(PyObject *module, PyObject *a, PyObject *b)
 {
     PyObject *result;
     result = (a != b) ? Py_True : Py_False;
-    Py_INCREF(result);
-    return result;
+    return Py_NewRef(result);
 }
 
 /* compare_digest **********************************************************/
@@ -1010,8 +1009,7 @@ itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
         return NULL;
     }
 
-    Py_INCREF(item);
-    ig->item = item;
+    ig->item = Py_NewRef(item);
     ig->nitems = nitems;
     ig->index = -1;
     if (PyLong_CheckExact(item)) {
@@ -1095,8 +1093,7 @@ itemgetter_call_impl(itemgetterobject *ig, PyObject *obj)
             && ig->index < PyTuple_GET_SIZE(obj))
         {
             result = PyTuple_GET_ITEM(obj, ig->index);
-            Py_INCREF(result);
-            return result;
+            return Py_NewRef(result);
         }
         return PyObject_GetItem(obj, ig->item);
     }
@@ -1440,8 +1437,7 @@ dotjoinattr(PyObject *attr, PyObject **attrsep)
         }
         return PyUnicode_Join(*attrsep, attr);
     } else {
-        Py_INCREF(attr);
-        return attr;
+        return Py_NewRef(attr);
     }
 }
 
@@ -1594,8 +1590,7 @@ methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
     PyUnicode_InternInPlace(&name);
     mc->name = name;
 
-    Py_XINCREF(kwds);
-    mc->kwds = kwds;
+    mc->kwds = Py_XNewRef(kwds);
 
     mc->args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
     if (mc->args == NULL) {
@@ -1740,12 +1735,10 @@ methodcaller_reduce(methodcallerobject *mc, PyObject *Py_UNUSED(ignored))
         newargs = PyTuple_New(1 + callargcount);
         if (newargs == NULL)
             return NULL;
-        Py_INCREF(mc->name);
-        PyTuple_SET_ITEM(newargs, 0, mc->name);
+        PyTuple_SET_ITEM(newargs, 0, Py_NewRef(mc->name));
         for (i = 0; i < callargcount; ++i) {
             PyObject *arg = PyTuple_GET_ITEM(mc->args, i);
-            Py_INCREF(arg);
-            PyTuple_SET_ITEM(newargs, i + 1, arg);
+            PyTuple_SET_ITEM(newargs, i + 1, Py_NewRef(arg));
         }
         return Py_BuildValue("ON", Py_TYPE(mc), newargs);
     }
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
index 51e0a2415921..db62094c5a5a 100644
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -386,10 +386,9 @@ init_method_ref(PyObject *self, PyObject *name,
 
     if (PyMethod_Check(func) && PyMethod_GET_SELF(func) == self) {
         /* Deconstruct a bound Python method */
-        func2 = PyMethod_GET_FUNCTION(func);
-        Py_INCREF(func2);
         *method_self = self; /* borrowed */
-        Py_XSETREF(*method_func, func2);
+        func2 = PyMethod_GET_FUNCTION(func);
+        Py_XSETREF(*method_func, Py_NewRef(func2));
         Py_DECREF(func);
         return 0;
     }
@@ -408,8 +407,7 @@ reconstruct_method(PyObject *func, PyObject *self)
         return PyMethod_New(func, self);
     }
     else {
-        Py_INCREF(func);
-        return func;
+        return Py_NewRef(func);
     }
 }
 
@@ -907,8 +905,7 @@ PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value)
         entry->me_value = value;
         return 0;
     }
-    Py_INCREF(key);
-    entry->me_key = key;
+    entry->me_key = Py_NewRef(key);
     entry->me_value = value;
     self->mt_used++;
 
@@ -1196,8 +1193,7 @@ _Pickler_SetBufferCallback(PicklerObject *self, PyObject *buffer_callback)
         return -1;
     }
 
-    Py_XINCREF(buffer_callback);
-    self->buffer_callback = buffer_callback;
+    self->buffer_callback = Py_XNewRef(buffer_callback);
     return 0;
 }
 
@@ -1543,9 +1539,8 @@ _Unpickler_MemoPut(UnpicklerObject *self, size_t idx, PyObject *value)
             return -1;
         assert(idx < self->memo_size);
     }
-    Py_INCREF(value);
     old_item = self->memo[idx];
-    self->memo[idx] = value;
+    self->memo[idx] = Py_NewRef(value);
     if (old_item != NULL) {
         Py_DECREF(old_item);
     }
@@ -1928,8 +1923,7 @@ whichmodule(PyObject *global, PyObject *dotted_path)
         i = 0;
         while (PyDict_Next(modules, &i, &module_name, &module)) {
             if (_checkmodule(module_name, module, global, dotted_path) == 0) {
-                Py_INCREF(module_name);
-                return module_name;
+                return Py_NewRef(module_name);
             }
             if (PyErr_Occurred()) {
                 return NULL;
@@ -1965,8 +1959,7 @@ whichmodule(PyObject *global, PyObject *dotted_path)
 
     /* If no module is found, use __main__. */
     module_name = &_Py_ID(__main__);
-    Py_INCREF(module_name);
-    return module_name;
+    return Py_NewRef(module_name);
 }
 
 /* fast_save_enter() and fast_save_leave() are guards against recursive
@@ -3557,10 +3550,8 @@ fix_imports(PyObject **module_name, PyObject **global_name)
 
         Py_CLEAR(*module_name);
         Py_CLEAR(*global_name);
-        Py_INCREF(fixed_module_name);
-        Py_INCREF(fixed_global_name);
-        *module_name = fixed_module_name;
-        *global_name = fixed_global_name;
+        *module_name = Py_NewRef(fixed_module_name);
+        *global_name = Py_NewRef(fixed_global_name);
         return 0;
     }
     else if (PyErr_Occurred()) {
@@ -3576,8 +3567,7 @@ fix_imports(PyObject **module_name, PyObject **global_name)
                          Py_TYPE(item)->tp_name);
             return -1;
         }
-        Py_INCREF(item);
-        Py_XSETREF(*module_name, item);
+        Py_XSETREF(*module_name, Py_NewRef(item));
     }
     else if (PyErr_Occurred()) {
         return -1;
@@ -3602,8 +3592,7 @@ save_global(PicklerObject *self, PyObject *obj, PyObject *name)
     const char global_op = GLOBAL;
 
     if (name) {
-        Py_INCREF(name);
-        global_name = name;
+        global_name = Py_NewRef(name);
     }
     else {
         if (_PyObject_LookupAttr(obj, &_Py_ID(__qualname__), &global_name) < 0)
@@ -3637,8 +3626,8 @@ save_global(PicklerObject *self, PyObject *obj, PyObject *name)
                      obj, module_name);
         goto error;
     }
-    lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1);
-    Py_INCREF(lastname);
+    lastname = Py_NewRef(PyList_GET_ITEM(dotted_path,
+                         PyList_GET_SIZE(dotted_path) - 1));
     cls = get_deep_attribute(module, dotted_path, &parent);
     Py_CLEAR(dotted_path);
     if (cls == NULL) {
@@ -3932,8 +3921,7 @@ get_class(PyObject *obj)
     PyObject *cls;
 
     if (_PyObject_LookupAttr(obj, &_Py_ID(__class__), &cls) == 0) {
-        cls = (PyObject *) Py_TYPE(obj);
-        Py_INCREF(cls);
+        cls = Py_NewRef(Py_TYPE(obj));
     }
     return cls;
 }
@@ -4084,12 +4072,10 @@ save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
                 return -1;
             }
             PyTuple_SET_ITEM(newargs, 0, cls_new);
-            Py_INCREF(cls);
-            PyTuple_SET_ITEM(newargs, 1, cls);
+            PyTuple_SET_ITEM(newargs, 1, Py_NewRef(cls));
             for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
                 PyObject *item = PyTuple_GET_ITEM(args, i);
-                Py_INCREF(item);
-                PyTuple_SET_ITEM(newargs, i + 2, item);
+                PyTuple_SET_ITEM(newargs, i + 2, Py_NewRef(item));
             }
 
             callable = PyObject_Call(st->partial, newargs, kwargs);
@@ -4405,8 +4391,7 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
         }
     }
     if (reduce_func != NULL) {
-        Py_INCREF(obj);
-        reduce_value = _Pickle_FastCall(reduce_func, obj);
+        reduce_value = _Pickle_FastCall(reduce_func, Py_NewRef(obj));
     }
     else if (PyType_IsSubtype(type, &PyType_Type)) {
         status = save_global(self, obj, NULL);
@@ -4869,8 +4854,7 @@ _pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self)
         return NULL;
     }
     PyTuple_SET_ITEM(dict_args, 0, contents);
-    Py_INCREF((PyObject *)&PyDict_Type);
-    PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
+    PyTuple_SET_ITEM(reduce_value, 0, Py_NewRef(&PyDict_Type));
     PyTuple_SET_ITEM(reduce_value, 1, dict_args);
     return reduce_value;
 }
@@ -4944,8 +4928,7 @@ PicklerMemoProxy_New(PicklerObject *pickler)
     self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType);
     if (self == NULL)
         return NULL;
-    Py_INCREF(pickler);
-    self->pickler = pickler;
+    self->pickler = (PicklerObject*)Py_NewRef(pickler);
     PyObject_GC_Track(self);
     return (PyObject *)self;
 }
@@ -5045,8 +5028,7 @@ Pickler_set_persid(PicklerObject *self, PyObject *value, void *Py_UNUSED(ignored
     }
 
     self->pers_func_self = NULL;
-    Py_INCREF(value);
-    Py_XSETREF(self->pers_func, value);
+    Py_XSETREF(self->pers_func, Py_NewRef(value));
 
     return 0;
 }
@@ -7370,8 +7352,7 @@ _pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self)
         return NULL;
     }
     PyTuple_SET_ITEM(constructor_args, 0, contents);
-    Py_INCREF((PyObject *)&PyDict_Type);
-    PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type);
+    PyTuple_SET_ITEM(reduce_value, 0, Py_NewRef(&PyDict_Type));
     PyTuple_SET_ITEM(reduce_value, 1, constructor_args);
     return reduce_value;
 }
@@ -7446,8 +7427,7 @@ UnpicklerMemoProxy_New(UnpicklerObject *unpickler)
                            &UnpicklerMemoProxyType);
     if (self == NULL)
         return NULL;
-    Py_INCREF(unpickler);
-    self->unpickler = unpickler;
+    self->unpickler = (UnpicklerObject*)Py_NewRef(unpickler);
     PyObject_GC_Track(self);
     return (PyObject *)self;
 }
@@ -7483,8 +7463,7 @@ Unpickler_set_memo(UnpicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored
             return -1;
 
         for (size_t i = 0; i < new_memo_size; i++) {
-            Py_XINCREF(unpickler->memo[i]);
-            new_memo[i] = unpickler->memo[i];
+            new_memo[i] = Py_XNewRef(unpickler->memo[i]);
         }
     }
     else if (PyDict_Check(obj)) {
@@ -7564,8 +7543,7 @@ Unpickler_set_persload(UnpicklerObject *self, PyObject *value, void *Py_UNUSED(i
     }
 
     self->pers_func_self = NULL;
-    Py_INCREF(value);
-    Py_XSETREF(self->pers_func, value);
+    Py_XSETREF(self->pers_func, Py_NewRef(value));
 
     return 0;
 }
@@ -7944,8 +7922,7 @@ PyInit__pickle(void)
 
     m = PyState_FindModule(&_picklemodule);
     if (m) {
-        Py_INCREF(m);
-        return m;
+        return Py_NewRef(m);
     }
 
     if (PyType_Ready(&Pdata_Type) < 0)



More information about the Python-checkins mailing list