Skip to content

Commit 1089bdc

Browse files
authored
gh-99300: Use Py_NewRef() in Doc/ directory (#99480)
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and Py_XNewRef() in test C files of the Doc/ directory. Replace PyModule_AddObject() with PyModule_AddObjectRef() to simplify reference counting.
1 parent 65dd745 commit 1089bdc

File tree

3 files changed

+15
-33
lines changed

3 files changed

+15
-33
lines changed

Diff for: Doc/includes/custom2.c

+3-7
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,12 @@ Custom_init(CustomObject *self, PyObject *args, PyObject *kwds)
5151

5252
if (first) {
5353
tmp = self->first;
54-
Py_INCREF(first);
55-
self->first = first;
54+
self->first = Py_NewRef(first);
5655
Py_XDECREF(tmp);
5756
}
5857
if (last) {
5958
tmp = self->last;
60-
Py_INCREF(last);
61-
self->last = last;
59+
self->last = Py_NewRef(last);
6260
Py_XDECREF(tmp);
6361
}
6462
return 0;
@@ -127,9 +125,7 @@ PyInit_custom2(void)
127125
if (m == NULL)
128126
return NULL;
129127

130-
Py_INCREF(&CustomType);
131-
if (PyModule_AddObject(m, "Custom", (PyObject *) &CustomType) < 0) {
132-
Py_DECREF(&CustomType);
128+
if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) {
133129
Py_DECREF(m);
134130
return NULL;
135131
}

Diff for: Doc/includes/custom3.c

+7-15
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,12 @@ Custom_init(CustomObject *self, PyObject *args, PyObject *kwds)
5151

5252
if (first) {
5353
tmp = self->first;
54-
Py_INCREF(first);
55-
self->first = first;
54+
self->first = Py_NewRef(first);
5655
Py_DECREF(tmp);
5756
}
5857
if (last) {
5958
tmp = self->last;
60-
Py_INCREF(last);
61-
self->last = last;
59+
self->last = Py_NewRef(last);
6260
Py_DECREF(tmp);
6361
}
6462
return 0;
@@ -73,8 +71,7 @@ static PyMemberDef Custom_members[] = {
7371
static PyObject *
7472
Custom_getfirst(CustomObject *self, void *closure)
7573
{
76-
Py_INCREF(self->first);
77-
return self->first;
74+
return Py_NewRef(self->first);
7875
}
7976

8077
static int
@@ -91,17 +88,15 @@ Custom_setfirst(CustomObject *self, PyObject *value, void *closure)
9188
return -1;
9289
}
9390
tmp = self->first;
94-
Py_INCREF(value);
95-
self->first = value;
91+
self->first = Py_NewRef(value);
9692
Py_DECREF(tmp);
9793
return 0;
9894
}
9995

10096
static PyObject *
10197
Custom_getlast(CustomObject *self, void *closure)
10298
{
103-
Py_INCREF(self->last);
104-
return self->last;
99+
return Py_NewRef(self->last);
105100
}
106101

107102
static int
@@ -118,8 +113,7 @@ Custom_setlast(CustomObject *self, PyObject *value, void *closure)
118113
return -1;
119114
}
120115
tmp = self->last;
121-
Py_INCREF(value);
122-
self->last = value;
116+
self->last = Py_NewRef(value);
123117
Py_DECREF(tmp);
124118
return 0;
125119
}
@@ -178,9 +172,7 @@ PyInit_custom3(void)
178172
if (m == NULL)
179173
return NULL;
180174

181-
Py_INCREF(&CustomType);
182-
if (PyModule_AddObject(m, "Custom", (PyObject *) &CustomType) < 0) {
183-
Py_DECREF(&CustomType);
175+
if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) {
184176
Py_DECREF(m);
185177
return NULL;
186178
}

Diff for: Doc/includes/custom4.c

+5-11
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,12 @@ Custom_init(CustomObject *self, PyObject *args, PyObject *kwds)
6767

6868
if (first) {
6969
tmp = self->first;
70-
Py_INCREF(first);
71-
self->first = first;
70+
self->first = Py_NewRef(first);
7271
Py_DECREF(tmp);
7372
}
7473
if (last) {
7574
tmp = self->last;
76-
Py_INCREF(last);
77-
self->last = last;
75+
self->last = Py_NewRef(last);
7876
Py_DECREF(tmp);
7977
}
8078
return 0;
@@ -89,8 +87,7 @@ static PyMemberDef Custom_members[] = {
8987
static PyObject *
9088
Custom_getfirst(CustomObject *self, void *closure)
9189
{
92-
Py_INCREF(self->first);
93-
return self->first;
90+
return Py_NewRef(self->first);
9491
}
9592

9693
static int
@@ -114,8 +111,7 @@ Custom_setfirst(CustomObject *self, PyObject *value, void *closure)
114111
static PyObject *
115112
Custom_getlast(CustomObject *self, void *closure)
116113
{
117-
Py_INCREF(self->last);
118-
return self->last;
114+
return Py_NewRef(self->last);
119115
}
120116

121117
static int
@@ -192,9 +188,7 @@ PyInit_custom4(void)
192188
if (m == NULL)
193189
return NULL;
194190

195-
Py_INCREF(&CustomType);
196-
if (PyModule_AddObject(m, "Custom", (PyObject *) &CustomType) < 0) {
197-
Py_DECREF(&CustomType);
191+
if (PyModule_AddObjectRef(m, "Custom", (PyObject *) &CustomType) < 0) {
198192
Py_DECREF(m);
199193
return NULL;
200194
}

0 commit comments

Comments
 (0)