From 40f41097de2bc8d589ab685c17cbbbca1b738e74 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 14 Nov 2022 16:49:26 +0100 Subject: [PATCH 1/2] gh-99300: Use Py_NewRef() in Doc/ directory Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in test C files of the Doc/ directory. --- Doc/includes/custom2.c | 6 ++---- Doc/includes/custom3.c | 18 ++++++------------ Doc/includes/custom4.c | 12 ++++-------- 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/Doc/includes/custom2.c b/Doc/includes/custom2.c index 2a3c59f8f04c3d..e289f3299d35ba 100644 --- a/Doc/includes/custom2.c +++ b/Doc/includes/custom2.c @@ -51,14 +51,12 @@ Custom_init(CustomObject *self, PyObject *args, PyObject *kwds) if (first) { tmp = self->first; - Py_INCREF(first); - self->first = first; + self->first = Py_NewRef(first); Py_XDECREF(tmp); } if (last) { tmp = self->last; - Py_INCREF(last); - self->last = last; + self->last = Py_NewRef(last); Py_XDECREF(tmp); } return 0; diff --git a/Doc/includes/custom3.c b/Doc/includes/custom3.c index 5a47530f0a6b0d..2f9741cd6f60ab 100644 --- a/Doc/includes/custom3.c +++ b/Doc/includes/custom3.c @@ -51,14 +51,12 @@ Custom_init(CustomObject *self, PyObject *args, PyObject *kwds) if (first) { tmp = self->first; - Py_INCREF(first); - self->first = first; + self->first = Py_NewRef(first); Py_DECREF(tmp); } if (last) { tmp = self->last; - Py_INCREF(last); - self->last = last; + self->last = Py_NewRef(last); Py_DECREF(tmp); } return 0; @@ -73,8 +71,7 @@ static PyMemberDef Custom_members[] = { static PyObject * Custom_getfirst(CustomObject *self, void *closure) { - Py_INCREF(self->first); - return self->first; + return Py_NewRef(self->first); } static int @@ -91,8 +88,7 @@ Custom_setfirst(CustomObject *self, PyObject *value, void *closure) return -1; } tmp = self->first; - Py_INCREF(value); - self->first = value; + self->first = Py_NewRef(value); Py_DECREF(tmp); return 0; } @@ -100,8 +96,7 @@ Custom_setfirst(CustomObject *self, PyObject *value, void *closure) static PyObject * Custom_getlast(CustomObject *self, void *closure) { - Py_INCREF(self->last); - return self->last; + return Py_NewRef(self->last); } static int @@ -118,8 +113,7 @@ Custom_setlast(CustomObject *self, PyObject *value, void *closure) return -1; } tmp = self->last; - Py_INCREF(value); - self->last = value; + self->last = Py_NewRef(value); Py_DECREF(tmp); return 0; } diff --git a/Doc/includes/custom4.c b/Doc/includes/custom4.c index c7ee55578488ed..6184b45fd39d05 100644 --- a/Doc/includes/custom4.c +++ b/Doc/includes/custom4.c @@ -67,14 +67,12 @@ Custom_init(CustomObject *self, PyObject *args, PyObject *kwds) if (first) { tmp = self->first; - Py_INCREF(first); - self->first = first; + self->first = Py_NewRef(first); Py_DECREF(tmp); } if (last) { tmp = self->last; - Py_INCREF(last); - self->last = last; + self->last = Py_NewRef(last); Py_DECREF(tmp); } return 0; @@ -89,8 +87,7 @@ static PyMemberDef Custom_members[] = { static PyObject * Custom_getfirst(CustomObject *self, void *closure) { - Py_INCREF(self->first); - return self->first; + return Py_NewRef(self->first); } static int @@ -114,8 +111,7 @@ Custom_setfirst(CustomObject *self, PyObject *value, void *closure) static PyObject * Custom_getlast(CustomObject *self, void *closure) { - Py_INCREF(self->last); - return self->last; + return Py_NewRef(self->last); } static int From cbdd8ed8587f1872aaf7501b7d750133584c8fc7 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 14 Nov 2022 18:08:34 +0100 Subject: [PATCH 2/2] Use PyModule_AddObjectRef() Replace PyModule_AddObject() with PyModule_AddObjectRef() to simplify reference counting. --- Doc/includes/custom2.c | 4 +--- Doc/includes/custom3.c | 4 +--- Doc/includes/custom4.c | 4 +--- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/Doc/includes/custom2.c b/Doc/includes/custom2.c index e289f3299d35ba..aee9e1bb7f2d74 100644 --- a/Doc/includes/custom2.c +++ b/Doc/includes/custom2.c @@ -125,9 +125,7 @@ PyInit_custom2(void) if (m == NULL) return NULL; - Py_INCREF(&CustomType); - if (PyModule_AddObject(m, "Custom", (PyObject *) &CustomType) < 0) { - Py_DECREF(&CustomType); + if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) { Py_DECREF(m); return NULL; } diff --git a/Doc/includes/custom3.c b/Doc/includes/custom3.c index 2f9741cd6f60ab..8d88bc24511829 100644 --- a/Doc/includes/custom3.c +++ b/Doc/includes/custom3.c @@ -172,9 +172,7 @@ PyInit_custom3(void) if (m == NULL) return NULL; - Py_INCREF(&CustomType); - if (PyModule_AddObject(m, "Custom", (PyObject *) &CustomType) < 0) { - Py_DECREF(&CustomType); + if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) { Py_DECREF(m); return NULL; } diff --git a/Doc/includes/custom4.c b/Doc/includes/custom4.c index 6184b45fd39d05..ad240ae6a8df7b 100644 --- a/Doc/includes/custom4.c +++ b/Doc/includes/custom4.c @@ -188,9 +188,7 @@ PyInit_custom4(void) if (m == NULL) return NULL; - Py_INCREF(&CustomType); - if (PyModule_AddObject(m, "Custom", (PyObject *) &CustomType) < 0) { - Py_DECREF(&CustomType); + if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) { Py_DECREF(m); return NULL; }