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

vstinner webhook-mailer at python.org
Sun Nov 13 10:05:00 EST 2022


https://github.com/python/cpython/commit/50b0415127009119882e32377d25a4d191088a76
commit: 50b0415127009119882e32377d25a4d191088a76
branch: main
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2022-11-13T16:04:54+01:00
summary:

gh-99300: Use Py_NewRef() in Modules/_ctypes/ (#99436)

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

files:
M Modules/_ctypes/_ctypes.c
M Modules/_ctypes/callbacks.c
M Modules/_ctypes/callproc.c
M Modules/_ctypes/cfield.c
M Modules/_ctypes/stgdict.c

diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index f342b17abca6..6ae216541585 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -231,10 +231,8 @@ PyDict_SetItemProxy(PyObject *dict, PyObject *key, PyObject *item)
     remover = (DictRemoverObject *)obj;
     assert(remover->key == NULL);
     assert(remover->dict == NULL);
-    Py_INCREF(key);
-    remover->key = key;
-    Py_INCREF(dict);
-    remover->dict = dict;
+    remover->key = Py_NewRef(key);
+    remover->dict = Py_NewRef(dict);
 
     proxy = PyWeakref_NewProxy(item, obj);
     Py_DECREF(obj);
@@ -470,8 +468,7 @@ StructUnionType_paramfunc(CDataObject *self)
         struct_param->ptr = ptr;
     } else {
         ptr = self->b_ptr;
-        obj = (PyObject *)self;
-        Py_INCREF(obj);
+        obj = Py_NewRef(self);
     }
 
     parg = PyCArgObject_new();
@@ -798,8 +795,7 @@ CDataType_from_param(PyObject *type, PyObject *value)
     if (res == -1)
         return NULL;
     if (res) {
-        Py_INCREF(value);
-        return value;
+        return Py_NewRef(value);
     }
     if (PyCArg_CheckExact(value)) {
         PyCArgObject *p = (PyCArgObject *)value;
@@ -815,8 +811,7 @@ CDataType_from_param(PyObject *type, PyObject *value)
             if (res == -1)
                 return NULL;
             if (res) {
-                Py_INCREF(value);
-                return value;
+                return Py_NewRef(value);
             }
         }
         ob_name = (ob) ? Py_TYPE(ob)->tp_name : "???";
@@ -1050,8 +1045,7 @@ PyCPointerType_paramfunc(CDataObject *self)
 
     parg->tag = 'P';
     parg->pffi_type = &ffi_type_pointer;
-    Py_INCREF(self);
-    parg->obj = (PyObject *)self;
+    parg->obj = Py_NewRef(self);
     parg->value.p = *(void **)self->b_ptr;
     return parg;
 }
@@ -1168,8 +1162,7 @@ PyCPointerType_from_param(PyObject *type, PyObject *value)
 
     if (value == Py_None) {
         /* ConvParam will convert to a NULL pointer later */
-        Py_INCREF(value);
-        return value;
+        return Py_NewRef(value);
     }
 
     typedict = PyType_stgdict(type);
@@ -1203,8 +1196,7 @@ PyCPointerType_from_param(PyObject *type, PyObject *value)
             return NULL;
         }
         if (ret) {
-            Py_INCREF(value);
-            return value;
+            return Py_NewRef(value);
         }
     }
     return CDataType_from_param(type, value);
@@ -1448,8 +1440,7 @@ PyCArrayType_paramfunc(CDataObject *self)
     p->tag = 'P';
     p->pffi_type = &ffi_type_pointer;
     p->value.p = (char *)self->b_ptr;
-    Py_INCREF(self);
-    p->obj = (PyObject *)self;
+    p->obj = Py_NewRef(self);
     return p;
 }
 
@@ -1677,8 +1668,7 @@ c_wchar_p_from_param(PyObject *type, PyObject *value)
     if (res == -1)
         return NULL;
     if (res) {
-        Py_INCREF(value);
-        return value;
+        return Py_NewRef(value);
     }
     if (ArrayObject_Check(value) || PointerObject_Check(value)) {
         /* c_wchar array instance or pointer(c_wchar(...)) */
@@ -1687,8 +1677,7 @@ c_wchar_p_from_param(PyObject *type, PyObject *value)
         assert(dt); /* Cannot be NULL for pointer or array objects */
         dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL;
         if (dict && (dict->setfunc == _ctypes_get_fielddesc("u")->setfunc)) {
-            Py_INCREF(value);
-            return value;
+            return Py_NewRef(value);
         }
     }
     if (PyCArg_CheckExact(value)) {
@@ -1696,8 +1685,7 @@ c_wchar_p_from_param(PyObject *type, PyObject *value)
         PyCArgObject *a = (PyCArgObject *)value;
         StgDictObject *dict = PyObject_stgdict(a->obj);
         if (dict && (dict->setfunc == _ctypes_get_fielddesc("u")->setfunc)) {
-            Py_INCREF(value);
-            return value;
+            return Py_NewRef(value);
         }
     }
 
@@ -1743,8 +1731,7 @@ c_char_p_from_param(PyObject *type, PyObject *value)
     if (res == -1)
         return NULL;
     if (res) {
-        Py_INCREF(value);
-        return value;
+        return Py_NewRef(value);
     }
     if (ArrayObject_Check(value) || PointerObject_Check(value)) {
         /* c_char array instance or pointer(c_char(...)) */
@@ -1753,8 +1740,7 @@ c_char_p_from_param(PyObject *type, PyObject *value)
         assert(dt); /* Cannot be NULL for pointer or array objects */
         dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL;
         if (dict && (dict->setfunc == _ctypes_get_fielddesc("c")->setfunc)) {
-            Py_INCREF(value);
-            return value;
+            return Py_NewRef(value);
         }
     }
     if (PyCArg_CheckExact(value)) {
@@ -1762,8 +1748,7 @@ c_char_p_from_param(PyObject *type, PyObject *value)
         PyCArgObject *a = (PyCArgObject *)value;
         StgDictObject *dict = PyObject_stgdict(a->obj);
         if (dict && (dict->setfunc == _ctypes_get_fielddesc("c")->setfunc)) {
-            Py_INCREF(value);
-            return value;
+            return Py_NewRef(value);
         }
     }
 
@@ -1851,22 +1836,19 @@ c_void_p_from_param(PyObject *type, PyObject *value)
         return NULL;
     if (res) {
         /* c_void_p instances */
-        Py_INCREF(value);
-        return value;
+        return Py_NewRef(value);
     }
 /* ctypes array or pointer instance */
     if (ArrayObject_Check(value) || PointerObject_Check(value)) {
         /* Any array or pointer is accepted */
-        Py_INCREF(value);
-        return value;
+        return Py_NewRef(value);
     }
 /* byref(...) */
     if (PyCArg_CheckExact(value)) {
         /* byref(c_xxx()) */
         PyCArgObject *a = (PyCArgObject *)value;
         if (a->tag == 'P') {
-            Py_INCREF(value);
-            return value;
+            return Py_NewRef(value);
         }
     }
 /* function pointer */
@@ -1897,8 +1879,7 @@ c_void_p_from_param(PyObject *type, PyObject *value)
                 return NULL;
             parg->pffi_type = &ffi_type_pointer;
             parg->tag = 'Z';
-            Py_INCREF(value);
-            parg->obj = value;
+            parg->obj = Py_NewRef(value);
             /* Remember: b_ptr points to where the pointer is stored! */
             parg->value.p = *(void **)(((CDataObject *)value)->b_ptr);
             return (PyObject *)parg;
@@ -1983,8 +1964,7 @@ static PyObject *CreateSwappedType(PyTypeObject *type, PyObject *args, PyObject
     stgdict->setfunc = fmt->setfunc_swapped;
     stgdict->getfunc = fmt->getfunc_swapped;
 
-    Py_INCREF(proto);
-    stgdict->proto = proto;
+    stgdict->proto = Py_NewRef(proto);
 
     /* replace the class dict by our updated spam dict */
     if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) {
@@ -2019,8 +1999,7 @@ PyCSimpleType_paramfunc(CDataObject *self)
 
     parg->tag = fmt[0];
     parg->pffi_type = fd->pffi_type;
-    Py_INCREF(self);
-    parg->obj = (PyObject *)self;
+    parg->obj = Py_NewRef(self);
     memcpy(&parg->value, self->b_ptr, self->b_size);
     return parg;
 }
@@ -2225,8 +2204,7 @@ PyCSimpleType_from_param(PyObject *type, PyObject *value)
     if (res == -1)
         return NULL;
     if (res) {
-        Py_INCREF(value);
-        return value;
+        return Py_NewRef(value);
     }
 
     dict = PyType_stgdict(type);
@@ -2456,8 +2434,7 @@ make_funcptrtype_dict(StgDictObject *stgdict)
         converters = converters_from_argtypes(ob);
         if (!converters)
             return -1;
-        Py_INCREF(ob);
-        stgdict->argtypes = ob;
+        stgdict->argtypes = Py_NewRef(ob);
         stgdict->converters = converters;
     }
     else if (PyErr_Occurred()) {
@@ -2471,8 +2448,7 @@ make_funcptrtype_dict(StgDictObject *stgdict)
                 "_restype_ must be a type, a callable, or None");
             return -1;
         }
-        Py_INCREF(ob);
-        stgdict->restype = ob;
+        stgdict->restype = Py_NewRef(ob);
         if (_PyObject_LookupAttr(ob, &_Py_ID(_check_retval_),
                                    &stgdict->checker) < 0)
         {
@@ -2490,8 +2466,7 @@ make_funcptrtype_dict(StgDictObject *stgdict)
                 "_errcheck_ must be callable");
             return -1;
         }
-        Py_INCREF(ob);
-        stgdict->errcheck = ob;
+        stgdict->errcheck = Py_NewRef(ob);
     }
     else if (PyErr_Occurred()) {
         return -1;
@@ -2511,8 +2486,7 @@ PyCFuncPtrType_paramfunc(CDataObject *self)
 
     parg->tag = 'P';
     parg->pffi_type = &ffi_type_pointer;
-    Py_INCREF(self);
-    parg->obj = (PyObject *)self;
+    parg->obj = Py_NewRef(self);
     parg->value.p = *(void **)self->b_ptr;
     return parg;
 }
@@ -2624,8 +2598,7 @@ PyCData_GetContainer(CDataObject *self)
             if (self->b_objects == NULL)
                 return NULL;
         } else {
-            Py_INCREF(Py_None);
-            self->b_objects = Py_None;
+            self->b_objects = Py_NewRef(Py_None);
         }
     }
     return self;
@@ -2767,8 +2740,7 @@ static int PyCData_NewGetBuffer(PyObject *myself, Py_buffer *view, int flags)
     if (view == NULL) return 0;
 
     view->buf = self->b_ptr;
-    view->obj = myself;
-    Py_INCREF(myself);
+    view->obj = Py_NewRef(myself);
     view->len = self->b_size;
     view->readonly = 0;
     /* use default format character if not set */
@@ -2860,8 +2832,7 @@ PyCData_setstate(PyObject *myself, PyObject *args)
 static PyObject *
 PyCData_from_outparam(PyObject *self, PyObject *args)
 {
-    Py_INCREF(self);
-    return self;
+    return Py_NewRef(self);
 }
 
 static PyMethodDef PyCData_methods[] = {
@@ -3104,8 +3075,7 @@ _PyCData_set(CDataObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value,
         if (value == NULL)
             return NULL;
 
-        Py_INCREF(value);
-        return value;
+        return Py_NewRef(value);
     }
 
     if (PyCPointerTypeObject_Check(type)
@@ -3228,8 +3198,7 @@ static PyObject *
 PyCFuncPtr_get_errcheck(PyCFuncPtrObject *self, void *Py_UNUSED(ignored))
 {
     if (self->errcheck) {
-        Py_INCREF(self->errcheck);
-        return self->errcheck;
+        return Py_NewRef(self->errcheck);
     }
     Py_RETURN_NONE;
 }
@@ -3266,14 +3235,12 @@ PyCFuncPtr_get_restype(PyCFuncPtrObject *self, void *Py_UNUSED(ignored))
 {
     StgDictObject *dict;
     if (self->restype) {
-        Py_INCREF(self->restype);
-        return self->restype;
+        return Py_NewRef(self->restype);
     }
     dict = PyObject_stgdict((PyObject *)self);
     assert(dict); /* Cannot be NULL for PyCFuncPtrObject instances */
     if (dict->restype) {
-        Py_INCREF(dict->restype);
-        return dict->restype;
+        return Py_NewRef(dict->restype);
     } else {
         Py_RETURN_NONE;
     }
@@ -3303,14 +3270,12 @@ PyCFuncPtr_get_argtypes(PyCFuncPtrObject *self, void *Py_UNUSED(ignored))
 {
     StgDictObject *dict;
     if (self->argtypes) {
-        Py_INCREF(self->argtypes);
-        return self->argtypes;
+        return Py_NewRef(self->argtypes);
     }
     dict = PyObject_stgdict((PyObject *)self);
     assert(dict); /* Cannot be NULL for PyCFuncPtrObject instances */
     if (dict->argtypes) {
-        Py_INCREF(dict->argtypes);
-        return dict->argtypes;
+        return Py_NewRef(dict->argtypes);
     } else {
         Py_RETURN_NONE;
     }
@@ -3604,8 +3569,7 @@ PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds)
         return NULL;
     }
 
-    Py_XINCREF(paramflags);
-    self->paramflags = paramflags;
+    self->paramflags = Py_XNewRef(paramflags);
 
     *(void **)self->b_ptr = address;
     Py_INCREF(dll);
@@ -3615,8 +3579,7 @@ PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds)
         return NULL;
     }
 
-    Py_INCREF(self);
-    self->callable = (PyObject *)self;
+    self->callable = Py_NewRef(self);
     return (PyObject *)self;
 }
 
@@ -3641,8 +3604,7 @@ PyCFuncPtr_FromVtblIndex(PyTypeObject *type, PyObject *args, PyObject *kwds)
 
     self = (PyCFuncPtrObject *)GenericPyCData_new(type, args, kwds);
     self->index = index + 0x1000;
-    Py_XINCREF(paramflags);
-    self->paramflags = paramflags;
+    self->paramflags = Py_XNewRef(paramflags);
     if (iid_len == sizeof(GUID))
         self->iid = iid;
     return (PyObject *)self;
@@ -3740,8 +3702,7 @@ PyCFuncPtr_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
         return NULL;
     }
 
-    Py_INCREF(callable);
-    self->callable = callable;
+    self->callable = Py_NewRef(callable);
 
     self->thunk = thunk;
     *(void **)self->b_ptr = (void *)thunk->pcl_exec;
@@ -3789,23 +3750,20 @@ _get_arg(int *pindex, PyObject *name, PyObject *defval, PyObject *inargs, PyObje
     if (*pindex < PyTuple_GET_SIZE(inargs)) {
         v = PyTuple_GET_ITEM(inargs, *pindex);
         ++*pindex;
-        Py_INCREF(v);
-        return v;
+        return Py_NewRef(v);
     }
     if (kwds && name) {
         v = PyDict_GetItemWithError(kwds, name);
         if (v) {
             ++*pindex;
-            Py_INCREF(v);
-            return v;
+            return Py_NewRef(v);
         }
         else if (PyErr_Occurred()) {
             return NULL;
         }
     }
     if (defval) {
-        Py_INCREF(defval);
-        return defval;
+        return Py_NewRef(defval);
     }
     /* we can't currently emit a better error message */
     if (name)
@@ -3860,8 +3818,7 @@ _build_callargs(PyCFuncPtrObject *self, PyObject *argtypes,
         if (self->index)
             return PyTuple_GetSlice(inargs, 1, PyTuple_GET_SIZE(inargs));
 #endif
-        Py_INCREF(inargs);
-        return inargs;
+        return Py_NewRef(inargs);
     }
 
     len = PyTuple_GET_SIZE(argtypes);
@@ -4948,8 +4905,7 @@ static PyObject *
 Simple_from_outparm(PyObject *self, PyObject *args)
 {
     if (_ctypes_simple_instance((PyObject *)Py_TYPE(self))) {
-        Py_INCREF(self);
-        return self;
+        return Py_NewRef(self);
     }
     /* call stgdict->getfunc */
     return Simple_get_value((CDataObject *)self, NULL);
@@ -5583,8 +5539,7 @@ cast(void *ptr, PyObject *src, PyObject *ctype)
             if (obj->b_objects == NULL)
                 goto failed;
         }
-        Py_XINCREF(obj->b_objects);
-        result->b_objects = obj->b_objects;
+        result->b_objects = Py_XNewRef(obj->b_objects);
         if (result->b_objects && PyDict_CheckExact(result->b_objects)) {
             PyObject *index;
             int rc;
diff --git a/Modules/_ctypes/callbacks.c b/Modules/_ctypes/callbacks.c
index 88b1c1a8cea5..8df2f541808b 100644
--- a/Modules/_ctypes/callbacks.c
+++ b/Modules/_ctypes/callbacks.c
@@ -374,8 +374,7 @@ CThunkObject *_ctypes_alloc_callback(PyObject *callable,
     }
     p->atypes[i] = NULL;
 
-    Py_INCREF(restype);
-    p->restype = restype;
+    p->restype = Py_NewRef(restype);
     if (restype == Py_None) {
         p->setfunc = NULL;
         p->ffi_restype = &ffi_type_void;
@@ -446,10 +445,8 @@ CThunkObject *_ctypes_alloc_callback(PyObject *callable,
         goto error;
     }
 
-    Py_INCREF(converters);
-    p->converters = converters;
-    Py_INCREF(callable);
-    p->callable = callable;
+    p->converters = Py_NewRef(converters);
+    p->callable = Py_NewRef(callable);
     return p;
 
   error:
diff --git a/Modules/_ctypes/callproc.c b/Modules/_ctypes/callproc.c
index 136f114fc941..520a0f921bb8 100644
--- a/Modules/_ctypes/callproc.c
+++ b/Modules/_ctypes/callproc.c
@@ -671,8 +671,7 @@ static int ConvParam(PyObject *obj, Py_ssize_t index, struct argument *pa)
     if (PyCArg_CheckExact(obj)) {
         PyCArgObject *carg = (PyCArgObject *)obj;
         pa->ffi_type = carg->pffi_type;
-        Py_INCREF(obj);
-        pa->keep = obj;
+        pa->keep = Py_NewRef(obj);
         memcpy(&pa->value, &carg->value, sizeof(pa->value));
         return 0;
     }
@@ -702,8 +701,7 @@ static int ConvParam(PyObject *obj, Py_ssize_t index, struct argument *pa)
     if (PyBytes_Check(obj)) {
         pa->ffi_type = &ffi_type_pointer;
         pa->value.p = PyBytes_AsString(obj);
-        Py_INCREF(obj);
-        pa->keep = obj;
+        pa->keep = Py_NewRef(obj);
         return 0;
     }
 
@@ -1733,8 +1731,7 @@ byref(PyObject *self, PyObject *args)
 
     parg->tag = 'P';
     parg->pffi_type = &ffi_type_pointer;
-    Py_INCREF(obj);
-    parg->obj = obj;
+    parg->obj = Py_NewRef(obj);
     parg->value.p = (char *)((CDataObject *)obj)->b_ptr + offset;
     return (PyObject *)parg;
 }
@@ -1774,8 +1771,7 @@ My_PyObj_FromPtr(PyObject *self, PyObject *args)
     if (PySys_Audit("ctypes.PyObj_FromPtr", "(O)", ob) < 0) {
         return NULL;
     }
-    Py_INCREF(ob);
-    return ob;
+    return Py_NewRef(ob);
 }
 
 static PyObject *
@@ -1888,8 +1884,7 @@ POINTER(PyObject *self, PyObject *cls)
 
     result = PyDict_GetItemWithError(_ctypes_ptrtype_cache, cls);
     if (result) {
-        Py_INCREF(result);
-        return result;
+        return Py_NewRef(result);
     }
     else if (PyErr_Occurred()) {
         return NULL;
@@ -1928,8 +1923,7 @@ POINTER(PyObject *self, PyObject *cls)
         PyMem_Free(buf);
         if (result == NULL)
             return result;
-        Py_INCREF(cls);
-        key = cls;
+        key = Py_NewRef(cls);
     } else {
         PyErr_SetString(PyExc_TypeError, "must be a ctypes type");
         return NULL;
diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c
index 13ed8b7eda65..fdd313f67506 100644
--- a/Modules/_ctypes/cfield.c
+++ b/Modules/_ctypes/cfield.c
@@ -144,8 +144,7 @@ PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
     self->getfunc = getfunc;
     self->index = index;
 
-    Py_INCREF(proto);
-    self->proto = proto;
+    self->proto = Py_NewRef(proto);
 
     switch (fieldtype) {
     case NEW_BITFIELD:
@@ -1097,8 +1096,7 @@ O_get(void *ptr, Py_ssize_t size)
                             "PyObject is NULL");
         return NULL;
     }
-    Py_INCREF(ob);
-    return ob;
+    return Py_NewRef(ob);
 }
 
 static PyObject *
@@ -1106,8 +1104,7 @@ O_set(void *ptr, PyObject *value, Py_ssize_t size)
 {
     /* Hm, does the memory block need it's own refcount or not? */
     *(PyObject **)ptr = value;
-    Py_INCREF(value);
-    return value;
+    return Py_NewRef(value);
 }
 
 
@@ -1233,8 +1230,7 @@ U_set(void *ptr, PyObject *value, Py_ssize_t length)
         return NULL;
     }
 
-    Py_INCREF(value);
-    return value;
+    return Py_NewRef(value);
 }
 
 
@@ -1292,13 +1288,11 @@ z_set(void *ptr, PyObject *value, Py_ssize_t size)
 {
     if (value == Py_None) {
         *(char **)ptr = NULL;
-        Py_INCREF(value);
-        return value;
+        return Py_NewRef(value);
     }
     if (PyBytes_Check(value)) {
         *(const char **)ptr = PyBytes_AsString(value);
-        Py_INCREF(value);
-        return value;
+        return Py_NewRef(value);
     } else if (PyLong_Check(value)) {
 #if SIZEOF_VOID_P == SIZEOF_LONG_LONG
         *(char **)ptr = (char *)PyLong_AsUnsignedLongLongMask(value);
@@ -1334,8 +1328,7 @@ Z_set(void *ptr, PyObject *value, Py_ssize_t size)
 
     if (value == Py_None) {
         *(wchar_t **)ptr = NULL;
-        Py_INCREF(value);
-        return value;
+        return Py_NewRef(value);
     }
     if (PyLong_Check(value)) {
 #if SIZEOF_VOID_P == SIZEOF_LONG_LONG
diff --git a/Modules/_ctypes/stgdict.c b/Modules/_ctypes/stgdict.c
index 975916dcd968..1c69a290ee22 100644
--- a/Modules/_ctypes/stgdict.c
+++ b/Modules/_ctypes/stgdict.c
@@ -267,8 +267,7 @@ MakeFields(PyObject *type, CFieldObject *descr,
         new_descr->size = fdescr->size;
         new_descr->offset = fdescr->offset + offset;
         new_descr->index = fdescr->index + index;
-        new_descr->proto = fdescr->proto;
-        Py_XINCREF(new_descr->proto);
+        new_descr->proto = Py_XNewRef(fdescr->proto);
         new_descr->getfunc = fdescr->getfunc;
         new_descr->setfunc = fdescr->setfunc;
 



More information about the Python-checkins mailing list