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

vstinner webhook-mailer at python.org
Thu Nov 10 10:28:12 EST 2022


https://github.com/python/cpython/commit/c0feb99187f449f844621b378273776d70a50f57
commit: c0feb99187f449f844621b378273776d70a50f57
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2022-11-10T16:27:32+01:00
summary:

gh-99300: Use Py_NewRef() in Objects/ directory (#99332)

Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Objects/ directory.

files:
M Objects/abstract.c
M Objects/boolobject.c
M Objects/bytesobject.c
M Objects/call.c
M Objects/cellobject.c
M Objects/classobject.c
M Objects/codeobject.c
M Objects/complexobject.c
M Objects/descrobject.c

diff --git a/Objects/abstract.c b/Objects/abstract.c
index 5d50491b2dd3..8aa3fc17c634 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -45,8 +45,7 @@ PyObject_Type(PyObject *o)
     }
 
     v = (PyObject *)Py_TYPE(o);
-    Py_INCREF(v);
-    return v;
+    return Py_NewRef(v);
 }
 
 Py_ssize_t
@@ -722,9 +721,7 @@ PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len,
         return -1;
     }
 
-    view->obj = obj;
-    if (obj)
-        Py_INCREF(obj);
+    view->obj = Py_XNewRef(obj);
     view->buf = buf;
     view->len = len;
     view->readonly = readonly;
@@ -776,8 +773,7 @@ PyObject_Format(PyObject *obj, PyObject *format_spec)
     /* Fast path for common types. */
     if (format_spec == NULL || PyUnicode_GET_LENGTH(format_spec) == 0) {
         if (PyUnicode_CheckExact(obj)) {
-            Py_INCREF(obj);
-            return obj;
+            return Py_NewRef(obj);
         }
         if (PyLong_CheckExact(obj)) {
             return PyObject_Str(obj);
@@ -1405,8 +1401,7 @@ _PyNumber_Index(PyObject *item)
     }
 
     if (PyLong_Check(item)) {
-        Py_INCREF(item);
-        return item;
+        return Py_NewRef(item);
     }
     if (!_PyIndex_Check(item)) {
         PyErr_Format(PyExc_TypeError,
@@ -1520,8 +1515,7 @@ PyNumber_Long(PyObject *o)
     }
 
     if (PyLong_CheckExact(o)) {
-        Py_INCREF(o);
-        return o;
+        return Py_NewRef(o);
     }
     m = Py_TYPE(o)->tp_as_number;
     if (m && m->nb_int) { /* This should include subclasses of int */
@@ -2045,8 +2039,7 @@ PySequence_Tuple(PyObject *v)
            a tuple *subclass* instance as-is, hence the restriction
            to exact tuples here.  In contrast, lists always make
            a copy, so there's no need for exactness below. */
-        Py_INCREF(v);
-        return v;
+        return Py_NewRef(v);
     }
     if (PyList_CheckExact(v))
         return PyList_AsTuple(v);
@@ -2144,8 +2137,7 @@ PySequence_Fast(PyObject *v, const char *m)
     }
 
     if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) {
-        Py_INCREF(v);
-        return v;
+        return Py_NewRef(v);
     }
 
     it = PyObject_GetIter(v);
diff --git a/Objects/boolobject.c b/Objects/boolobject.c
index 8a20e368d4a4..18666f88cbde 100644
--- a/Objects/boolobject.c
+++ b/Objects/boolobject.c
@@ -23,8 +23,7 @@ PyObject *PyBool_FromLong(long ok)
         result = Py_True;
     else
         result = Py_False;
-    Py_INCREF(result);
-    return result;
+    return Py_NewRef(result);
 }
 
 /* We define bool_new to always return either Py_True or Py_False */
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 80660881920f..91c89bbd9005 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -53,8 +53,7 @@ static inline PyObject* bytes_get_empty(void)
 // Return a strong reference to the empty bytes string singleton.
 static inline PyObject* bytes_new_empty(void)
 {
-    Py_INCREF(EMPTY);
-    return (PyObject *)EMPTY;
+    return Py_NewRef(EMPTY);
 }
 
 
@@ -126,8 +125,7 @@ PyBytes_FromStringAndSize(const char *str, Py_ssize_t size)
     }
     if (size == 1 && str != NULL) {
         op = CHARACTER(*str & 255);
-        Py_INCREF(op);
-        return (PyObject *)op;
+        return Py_NewRef(op);
     }
     if (size == 0) {
         return bytes_new_empty();
@@ -162,8 +160,7 @@ PyBytes_FromString(const char *str)
     }
     else if (size == 1) {
         op = CHARACTER(*str & 255);
-        Py_INCREF(op);
-        return (PyObject *)op;
+        return Py_NewRef(op);
     }
 
     /* Inline PyObject_NewVar */
@@ -527,14 +524,12 @@ format_obj(PyObject *v, const char **pbuf, Py_ssize_t *plen)
     if (PyBytes_Check(v)) {
         *pbuf = PyBytes_AS_STRING(v);
         *plen = PyBytes_GET_SIZE(v);
-        Py_INCREF(v);
-        return v;
+        return Py_NewRef(v);
     }
     if (PyByteArray_Check(v)) {
         *pbuf = PyByteArray_AS_STRING(v);
         *plen = PyByteArray_GET_SIZE(v);
-        Py_INCREF(v);
-        return v;
+        return Py_NewRef(v);
     }
     /* does it support __bytes__? */
     func = _PyObject_LookupSpecial(v, &_Py_ID(__bytes__));
@@ -1411,13 +1406,11 @@ bytes_concat(PyObject *a, PyObject *b)
 
     /* Optimize end cases */
     if (va.len == 0 && PyBytes_CheckExact(b)) {
-        result = b;
-        Py_INCREF(result);
+        result = Py_NewRef(b);
         goto done;
     }
     if (vb.len == 0 && PyBytes_CheckExact(a)) {
-        result = a;
-        Py_INCREF(result);
+        result = Py_NewRef(a);
         goto done;
     }
 
@@ -1458,8 +1451,7 @@ bytes_repeat(PyBytesObject *a, Py_ssize_t n)
     }
     size = Py_SIZE(a) * n;
     if (size == Py_SIZE(a) && PyBytes_CheckExact(a)) {
-        Py_INCREF(a);
-        return (PyObject *)a;
+        return Py_NewRef(a);
     }
     nbytes = (size_t)size;
     if (nbytes + PyBytesObject_SIZE <= nbytes) {
@@ -1625,8 +1617,7 @@ bytes_subscript(PyBytesObject* self, PyObject* item)
         else if (start == 0 && step == 1 &&
                  slicelength == PyBytes_GET_SIZE(self) &&
                  PyBytes_CheckExact(self)) {
-            Py_INCREF(self);
-            return (PyObject *)self;
+            return Py_NewRef(self);
         }
         else if (step == 1) {
             return PyBytes_FromStringAndSize(
@@ -1696,8 +1687,7 @@ bytes___bytes___impl(PyBytesObject *self)
 /*[clinic end generated code: output=63a306a9bc0caac5 input=34ec5ddba98bd6bb]*/
 {
     if (PyBytes_CheckExact(self)) {
-        Py_INCREF(self);
-        return (PyObject *)self;
+        return Py_NewRef(self);
     }
     else {
         return PyBytes_FromStringAndSize(self->ob_sval, Py_SIZE(self));
@@ -1922,8 +1912,7 @@ do_xstrip(PyBytesObject *self, int striptype, PyObject *sepobj)
     PyBuffer_Release(&vsep);
 
     if (i == 0 && j == len && PyBytes_CheckExact(self)) {
-        Py_INCREF(self);
-        return (PyObject*)self;
+        return Py_NewRef(self);
     }
     else
         return PyBytes_FromStringAndSize(s+i, j-i);
@@ -1952,8 +1941,7 @@ do_strip(PyBytesObject *self, int striptype)
     }
 
     if (i == 0 && j == len && PyBytes_CheckExact(self)) {
-        Py_INCREF(self);
-        return (PyObject*)self;
+        return Py_NewRef(self);
     }
     else
         return PyBytes_FromStringAndSize(s+i, j-i);
@@ -2152,8 +2140,7 @@ bytes_translate_impl(PyBytesObject *self, PyObject *table,
     }
     if (!changed && PyBytes_CheckExact(input_obj)) {
         Py_DECREF(result);
-        Py_INCREF(input_obj);
-        return input_obj;
+        return Py_NewRef(input_obj);
     }
     /* Fix the size of the resulting byte string */
     if (inlen > 0)
@@ -2245,8 +2232,7 @@ bytes_removeprefix_impl(PyBytesObject *self, Py_buffer *prefix)
     }
 
     if (PyBytes_CheckExact(self)) {
-        Py_INCREF(self);
-        return (PyObject *)self;
+        return Py_NewRef(self);
     }
 
     return PyBytes_FromStringAndSize(self_start, self_len);
@@ -2284,8 +2270,7 @@ bytes_removesuffix_impl(PyBytesObject *self, Py_buffer *suffix)
     }
 
     if (PyBytes_CheckExact(self)) {
-        Py_INCREF(self);
-        return (PyObject *)self;
+        return Py_NewRef(self);
     }
 
     return PyBytes_FromStringAndSize(self_start, self_len);
@@ -2844,8 +2829,7 @@ PyBytes_FromObject(PyObject *x)
     }
 
     if (PyBytes_CheckExact(x)) {
-        Py_INCREF(x);
-        return x;
+        return Py_NewRef(x);
     }
 
     /* Use the modern buffer interface */
@@ -3269,8 +3253,7 @@ bytes_iter(PyObject *seq)
     if (it == NULL)
         return NULL;
     it->it_index = 0;
-    Py_INCREF(seq);
-    it->it_seq = (PyBytesObject *)seq;
+    it->it_seq = (PyBytesObject *)Py_NewRef(seq);
     _PyObject_GC_TRACK(it);
     return (PyObject *)it;
 }
diff --git a/Objects/call.c b/Objects/call.c
index c2509db2a9a2..c4d31d0a27ec 100644
--- a/Objects/call.c
+++ b/Objects/call.c
@@ -1000,8 +1000,7 @@ _PyStack_UnpackDict(PyThreadState *tstate,
 
     /* Copy positional arguments */
     for (Py_ssize_t i = 0; i < nargs; i++) {
-        Py_INCREF(args[i]);
-        stack[i] = args[i];
+        stack[i] = Py_NewRef(args[i]);
     }
 
     PyObject **kwstack = stack + nargs;
@@ -1013,10 +1012,8 @@ _PyStack_UnpackDict(PyThreadState *tstate,
     unsigned long keys_are_strings = Py_TPFLAGS_UNICODE_SUBCLASS;
     while (PyDict_Next(kwargs, &pos, &key, &value)) {
         keys_are_strings &= Py_TYPE(key)->tp_flags;
-        Py_INCREF(key);
-        Py_INCREF(value);
-        PyTuple_SET_ITEM(kwnames, i, key);
-        kwstack[i] = value;
+        PyTuple_SET_ITEM(kwnames, i, Py_NewRef(key));
+        kwstack[i] = Py_NewRef(value);
         i++;
     }
 
diff --git a/Objects/cellobject.c b/Objects/cellobject.c
index 1ddf4c5d8b8b..f516707f6f80 100644
--- a/Objects/cellobject.c
+++ b/Objects/cellobject.c
@@ -11,8 +11,7 @@ PyCell_New(PyObject *obj)
     op = (PyCellObject *)PyObject_GC_New(PyCellObject, &PyCell_Type);
     if (op == NULL)
         return NULL;
-    op->ob_ref = obj;
-    Py_XINCREF(obj);
+    op->ob_ref = Py_XNewRef(obj);
 
     _PyObject_GC_TRACK(op);
     return (PyObject *)op;
@@ -68,8 +67,7 @@ PyCell_Set(PyObject *op, PyObject *value)
         return -1;
     }
     PyObject *old_value = PyCell_GET(op);
-    Py_XINCREF(value);
-    PyCell_SET(op, value);
+    PyCell_SET(op, Py_XNewRef(value));
     Py_XDECREF(old_value);
     return 0;
 }
@@ -135,15 +133,13 @@ cell_get_contents(PyCellObject *op, void *closure)
         PyErr_SetString(PyExc_ValueError, "Cell is empty");
         return NULL;
     }
-    Py_INCREF(op->ob_ref);
-    return op->ob_ref;
+    return Py_NewRef(op->ob_ref);
 }
 
 static int
 cell_set_contents(PyCellObject *op, PyObject *obj, void *Py_UNUSED(ignored))
 {
-    Py_XINCREF(obj);
-    Py_XSETREF(op->ob_ref, obj);
+    Py_XSETREF(op->ob_ref, Py_XNewRef(obj));
     return 0;
 }
 
diff --git a/Objects/classobject.c b/Objects/classobject.c
index b9708ba0e41a..eedf8f0e1e1a 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -113,10 +113,8 @@ PyMethod_New(PyObject *func, PyObject *self)
         return NULL;
     }
     im->im_weakreflist = NULL;
-    Py_INCREF(func);
-    im->im_func = func;
-    Py_INCREF(self);
-    im->im_self = self;
+    im->im_func = Py_NewRef(func);
+    im->im_self = Py_NewRef(self);
     im->vectorcall = method_vectorcall;
     _PyObject_GC_TRACK(im);
     return (PyObject *)im;
@@ -195,8 +193,7 @@ method_getattro(PyObject *obj, PyObject *name)
         if (f != NULL)
             return f(descr, obj, (PyObject *)Py_TYPE(obj));
         else {
-            Py_INCREF(descr);
-            return descr;
+            return Py_NewRef(descr);
         }
     }
 
@@ -267,8 +264,7 @@ method_richcompare(PyObject *self, PyObject *other, int op)
         res = eq ? Py_True : Py_False;
     else
         res = eq ? Py_False : Py_True;
-    Py_INCREF(res);
-    return res;
+    return Py_NewRef(res);
 }
 
 static PyObject *
@@ -359,8 +355,7 @@ PyInstanceMethod_New(PyObject *func) {
     method = PyObject_GC_New(PyInstanceMethodObject,
                              &PyInstanceMethod_Type);
     if (method == NULL) return NULL;
-    Py_INCREF(func);
-    method->func = func;
+    method->func = Py_NewRef(func);
     _PyObject_GC_TRACK(method);
     return (PyObject *)method;
 }
@@ -412,8 +407,7 @@ instancemethod_getattro(PyObject *self, PyObject *name)
         if (f != NULL)
             return f(descr, self, (PyObject *)Py_TYPE(self));
         else {
-            Py_INCREF(descr);
-            return descr;
+            return Py_NewRef(descr);
         }
     }
 
@@ -443,8 +437,7 @@ static PyObject *
 instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
     PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
     if (obj == NULL) {
-        Py_INCREF(func);
-        return func;
+        return Py_NewRef(func);
     }
     else
         return PyMethod_New(func, obj);
@@ -472,8 +465,7 @@ instancemethod_richcompare(PyObject *self, PyObject *other, int op)
         res = eq ? Py_True : Py_False;
     else
         res = eq ? Py_False : Py_True;
-    Py_INCREF(res);
-    return res;
+    return Py_NewRef(res);
 }
 
 static PyObject *
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index 3824fc0f5a4e..fc1db72977aa 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -175,8 +175,7 @@ void
 _Py_set_localsplus_info(int offset, PyObject *name, _PyLocals_Kind kind,
                         PyObject *names, PyObject *kinds)
 {
-    Py_INCREF(name);
-    PyTuple_SET_ITEM(names, offset, name);
+    PyTuple_SET_ITEM(names, offset, Py_NewRef(name));
     _PyLocals_SetKind(kinds, offset, kind);
 }
 
@@ -235,8 +234,7 @@ get_localsplus_names(PyCodeObject *co, _PyLocals_Kind kind, int num)
         }
         assert(index < num);
         PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, offset);
-        Py_INCREF(name);
-        PyTuple_SET_ITEM(names, index, name);
+        PyTuple_SET_ITEM(names, index, Py_NewRef(name));
         index += 1;
     }
     assert(index == num);
@@ -311,27 +309,19 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
     get_localsplus_counts(con->localsplusnames, con->localspluskinds,
                           &nlocals, &nplaincellvars, &ncellvars, &nfreevars);
 
-    Py_INCREF(con->filename);
-    co->co_filename = con->filename;
-    Py_INCREF(con->name);
-    co->co_name = con->name;
-    Py_INCREF(con->qualname);
-    co->co_qualname = con->qualname;
+    co->co_filename = Py_NewRef(con->filename);
+    co->co_name = Py_NewRef(con->name);
+    co->co_qualname = Py_NewRef(con->qualname);
     co->co_flags = con->flags;
 
     co->co_firstlineno = con->firstlineno;
-    Py_INCREF(con->linetable);
-    co->co_linetable = con->linetable;
+    co->co_linetable = Py_NewRef(con->linetable);
 
-    Py_INCREF(con->consts);
-    co->co_consts = con->consts;
-    Py_INCREF(con->names);
-    co->co_names = con->names;
+    co->co_consts = Py_NewRef(con->consts);
+    co->co_names = Py_NewRef(con->names);
 
-    Py_INCREF(con->localsplusnames);
-    co->co_localsplusnames = con->localsplusnames;
-    Py_INCREF(con->localspluskinds);
-    co->co_localspluskinds = con->localspluskinds;
+    co->co_localsplusnames = Py_NewRef(con->localsplusnames);
+    co->co_localspluskinds = Py_NewRef(con->localspluskinds);
 
     co->co_argcount = con->argcount;
     co->co_posonlyargcount = con->posonlyargcount;
@@ -339,8 +329,7 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
 
     co->co_stacksize = con->stacksize;
 
-    Py_INCREF(con->exceptiontable);
-    co->co_exceptiontable = con->exceptiontable;
+    co->co_exceptiontable = Py_NewRef(con->exceptiontable);
 
     /* derived values */
     co->co_nlocalsplus = nlocalsplus;
@@ -1144,8 +1133,7 @@ lineiter_next(lineiterator *li)
     start = PyLong_FromLong(bounds->ar_start);
     end = PyLong_FromLong(bounds->ar_end);
     if (bounds->ar_line < 0) {
-        Py_INCREF(Py_None);
-        line = Py_None;
+        line = Py_NewRef(Py_None);
     }
     else {
         line = PyLong_FromLong(bounds->ar_line);
@@ -1215,8 +1203,7 @@ new_linesiterator(PyCodeObject *code)
     if (li == NULL) {
         return NULL;
     }
-    Py_INCREF(code);
-    li->li_code = code;
+    li->li_code = (PyCodeObject*)Py_NewRef(code);
     _PyCode_InitAddressRange(code, &li->li_line);
     return li;
 }
@@ -1315,8 +1302,7 @@ code_positionsiterator(PyCodeObject* code, PyObject* Py_UNUSED(args))
     if (pi == NULL) {
         return NULL;
     }
-    Py_INCREF(code);
-    pi->pi_code = code;
+    pi->pi_code = (PyCodeObject*)Py_NewRef(code);
     _PyCode_InitAddressRange(code, &pi->pi_range);
     pi->pi_offset = pi->pi_range.ar_end;
     return (PyObject*)pi;
@@ -1777,8 +1763,7 @@ code_richcompare(PyObject *self, PyObject *other, int op)
         res = Py_False;
 
   done:
-    Py_INCREF(res);
-    return res;
+    return Py_NewRef(res);
 }
 
 static Py_hash_t
@@ -2030,8 +2015,7 @@ code__varname_from_oparg_impl(PyCodeObject *self, int oparg)
     if (name == NULL) {
         return NULL;
     }
-    Py_INCREF(name);
-    return name;
+    return Py_NewRef(name);
 }
 
 /* XXX code objects need to participate in GC? */
@@ -2106,8 +2090,7 @@ _PyCode_ConstantKey(PyObject *op)
     {
         /* Objects of these types are always different from object of other
          * type and from tuples. */
-        Py_INCREF(op);
-        key = op;
+        key = Py_NewRef(op);
     }
     else if (PyBool_Check(op) || PyBytes_CheckExact(op)) {
         /* Make booleans different from integers 0 and 1.
diff --git a/Objects/complexobject.c b/Objects/complexobject.c
index 9bd68d50c30a..aee03ddfb07a 100644
--- a/Objects/complexobject.c
+++ b/Objects/complexobject.c
@@ -449,8 +449,7 @@ to_complex(PyObject **pobj, Py_complex *pc)
         pc->real = PyFloat_AsDouble(obj);
         return 0;
     }
-    Py_INCREF(Py_NotImplemented);
-    *pobj = Py_NotImplemented;
+    *pobj = Py_NewRef(Py_NotImplemented);
     return -1;
 }
 
@@ -553,8 +552,7 @@ static PyObject *
 complex_pos(PyComplexObject *v)
 {
     if (PyComplex_CheckExact(v)) {
-        Py_INCREF(v);
-        return (PyObject *)v;
+        return Py_NewRef(v);
     }
     else
         return PyComplex_FromCComplex(v->cval);
@@ -631,8 +629,7 @@ complex_richcompare(PyObject *v, PyObject *w, int op)
     else
          res = Py_False;
 
-    Py_INCREF(res);
-    return res;
+    return Py_NewRef(res);
 
 Unimplemented:
     Py_RETURN_NOTIMPLEMENTED;
@@ -705,8 +702,7 @@ complex___complex___impl(PyComplexObject *self)
 /*[clinic end generated code: output=e6b35ba3d275dc9c input=3589ada9d27db854]*/
 {
     if (PyComplex_CheckExact(self)) {
-        Py_INCREF(self);
-        return (PyObject *)self;
+        return Py_NewRef(self);
     }
     else {
         return PyComplex_FromCComplex(self->cval);
@@ -917,8 +913,7 @@ complex_new_impl(PyTypeObject *type, PyObject *r, PyObject *i)
            to exact complexes here.  If either the input or the
            output is a complex subclass, it will be handled below
            as a non-orthogonal vector.  */
-        Py_INCREF(r);
-        return r;
+        return Py_NewRef(r);
     }
     if (PyUnicode_Check(r)) {
         if (i != NULL) {
diff --git a/Objects/descrobject.c b/Objects/descrobject.c
index a2974f91aaae..550bfb36df0f 100644
--- a/Objects/descrobject.c
+++ b/Objects/descrobject.c
@@ -622,8 +622,7 @@ descr_get_qualname(PyDescrObject *descr, void *Py_UNUSED(ignored))
 {
     if (descr->d_qualname == NULL)
         descr->d_qualname = calculate_qualname(descr);
-    Py_XINCREF(descr->d_qualname);
-    return descr->d_qualname;
+    return Py_XNewRef(descr->d_qualname);
 }
 
 static PyObject *
@@ -904,8 +903,7 @@ descr_new(PyTypeObject *descrtype, PyTypeObject *type, const char *name)
 
     descr = (PyDescrObject *)PyType_GenericAlloc(descrtype, 0);
     if (descr != NULL) {
-        Py_XINCREF(type);
-        descr->d_type = type;
+        descr->d_type = (PyTypeObject*)Py_XNewRef(type);
         descr->d_name = PyUnicode_InternFromString(name);
         if (descr->d_name == NULL) {
             Py_DECREF(descr);
@@ -1244,8 +1242,7 @@ mappingproxy_new_impl(PyTypeObject *type, PyObject *mapping)
     mappingproxy = PyObject_GC_New(mappingproxyobject, &PyDictProxy_Type);
     if (mappingproxy == NULL)
         return NULL;
-    Py_INCREF(mapping);
-    mappingproxy->mapping = mapping;
+    mappingproxy->mapping = Py_NewRef(mapping);
     _PyObject_GC_TRACK(mappingproxy);
     return (PyObject *)mappingproxy;
 }
@@ -1260,8 +1257,7 @@ PyDictProxy_New(PyObject *mapping)
 
     pp = PyObject_GC_New(mappingproxyobject, &PyDictProxy_Type);
     if (pp != NULL) {
-        Py_INCREF(mapping);
-        pp->mapping = mapping;
+        pp->mapping = Py_NewRef(mapping);
         _PyObject_GC_TRACK(pp);
     }
     return (PyObject *)pp;
@@ -1361,8 +1357,7 @@ wrapper_objclass(wrapperobject *wp, void *Py_UNUSED(ignored))
 {
     PyObject *c = (PyObject *)PyDescr_TYPE(wp->descr);
 
-    Py_INCREF(c);
-    return c;
+    return Py_NewRef(c);
 }
 
 static PyObject *
@@ -1466,10 +1461,8 @@ PyWrapper_New(PyObject *d, PyObject *self)
 
     wp = PyObject_GC_New(wrapperobject, &_PyMethodWrapper_Type);
     if (wp != NULL) {
-        Py_INCREF(descr);
-        wp->descr = descr;
-        Py_INCREF(self);
-        wp->self = self;
+        wp->descr = (PyWrapperDescrObject*)Py_NewRef(descr);
+        wp->self = Py_NewRef(self);
         _PyObject_GC_TRACK(wp);
     }
     return (PyObject *)wp;
@@ -1566,8 +1559,7 @@ property_set_name(PyObject *self, PyObject *args) {
     propertyobject *prop = (propertyobject *)self;
     PyObject *name = PyTuple_GET_ITEM(args, 1);
 
-    Py_XINCREF(name);
-    Py_XSETREF(prop->prop_name, name);
+    Py_XSETREF(prop->prop_name, Py_XNewRef(name));
 
     Py_RETURN_NONE;
 }
@@ -1599,8 +1591,7 @@ static PyObject *
 property_descr_get(PyObject *self, PyObject *obj, PyObject *type)
 {
     if (obj == NULL || obj == Py_None) {
-        Py_INCREF(self);
-        return self;
+        return Py_NewRef(self);
     }
 
     propertyobject *gs = (propertyobject *)self;
@@ -1722,8 +1713,7 @@ property_copy(PyObject *old, PyObject *get, PyObject *set, PyObject *del)
     if (new == NULL)
         return NULL;
 
-    Py_XINCREF(pold->prop_name);
-    Py_XSETREF(((propertyobject *) new)->prop_name, pold->prop_name);
+    Py_XSETREF(((propertyobject *) new)->prop_name, Py_XNewRef(pold->prop_name));
     return new;
 }
 
@@ -1776,13 +1766,9 @@ property_init_impl(propertyobject *self, PyObject *fget, PyObject *fset,
     if (fdel == Py_None)
         fdel = NULL;
 
-    Py_XINCREF(fget);
-    Py_XINCREF(fset);
-    Py_XINCREF(fdel);
-
-    Py_XSETREF(self->prop_get, fget);
-    Py_XSETREF(self->prop_set, fset);
-    Py_XSETREF(self->prop_del, fdel);
+    Py_XSETREF(self->prop_get, Py_XNewRef(fget));
+    Py_XSETREF(self->prop_set, Py_XNewRef(fset));
+    Py_XSETREF(self->prop_del, Py_XNewRef(fdel));
     Py_XSETREF(self->prop_doc, NULL);
     Py_XSETREF(self->prop_name, NULL);
 
@@ -1790,8 +1776,7 @@ property_init_impl(propertyobject *self, PyObject *fget, PyObject *fset,
     PyObject *prop_doc = NULL;
 
     if (doc != NULL && doc != Py_None) {
-        prop_doc = doc;
-        Py_XINCREF(prop_doc);
+        prop_doc = Py_XNewRef(doc);
     }
     /* if no docstring given and the getter has one, use that one */
     else if (fget != NULL) {
@@ -1820,8 +1805,7 @@ property_init_impl(propertyobject *self, PyObject *fget, PyObject *fset,
            class's dict. */
 
         if (prop_doc == NULL) {
-            prop_doc = Py_None;
-            Py_INCREF(prop_doc);
+            prop_doc = Py_NewRef(Py_None);
         }
         int err = PyObject_SetAttr(
                     (PyObject *)self, &_Py_ID(__doc__), prop_doc);



More information about the Python-checkins mailing list