summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2022-11-16 09:39:47 (GMT)
committerGitHub <noreply@github.com>2022-11-16 09:39:47 (GMT)
commit3ed8803ef59f80e03c40b100b04c2e13f138ceed (patch)
treeec9ce18edd242485d4e3198d8e62924e1616c4ea
parentea88d34de27ba2b3acaeb03c7dc7829dff40ea5c (diff)
downloadcpython-3ed8803ef59f80e03c40b100b04c2e13f138ceed.zip
cpython-3ed8803ef59f80e03c40b100b04c2e13f138ceed.tar.gz
cpython-3ed8803ef59f80e03c40b100b04c2e13f138ceed.tar.bz2
gh-99300: Replace Py_INCREF() with Py_NewRef() (#99513)
Replace Py_INCREF() and Py_XINCREF() using a cast with Py_NewRef() and Py_XNewRef().
-rw-r--r--Modules/_asynciomodule.c6
-rw-r--r--Modules/_ctypes/_ctypes.c3
-rw-r--r--Modules/_ctypes/cfield.c3
-rw-r--r--Modules/_elementtree.c12
-rw-r--r--Modules/_testbuffer.c3
-rw-r--r--Modules/_testcapi/datetime.c6
-rw-r--r--Modules/mmapmodule.c3
-rw-r--r--Modules/selectmodule.c3
-rw-r--r--Modules/syslogmodule.c3
-rw-r--r--Objects/bytearrayobject.c12
-rw-r--r--Objects/funcobject.c3
-rw-r--r--Objects/setobject.c9
12 files changed, 22 insertions, 44 deletions
diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c
index a4f6d57..3edd8a9 100644
--- a/Modules/_asynciomodule.c
+++ b/Modules/_asynciomodule.c
@@ -1756,8 +1756,7 @@ future_new_iter(PyObject *fut)
}
}
- Py_INCREF(fut);
- it->future = (FutureObj*)fut;
+ it->future = (FutureObj*)Py_NewRef(fut);
PyObject_GC_Track(it);
return (PyObject*)it;
}
@@ -1821,8 +1820,7 @@ static PyObject *
TaskStepMethWrapper_get___self__(TaskStepMethWrapper *o, void *Py_UNUSED(ignored))
{
if (o->sw_task) {
- Py_INCREF(o->sw_task);
- return (PyObject*)o->sw_task;
+ return Py_NewRef(o->sw_task);
}
Py_RETURN_NONE;
}
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c
index 6ae2165..b9092d3 100644
--- a/Modules/_ctypes/_ctypes.c
+++ b/Modules/_ctypes/_ctypes.c
@@ -2937,8 +2937,7 @@ PyCData_FromBaseObj(PyObject *type, PyObject *base, Py_ssize_t index, char *adr)
assert(CDataObject_Check(base));
cmem->b_ptr = adr;
cmem->b_needsfree = 0;
- Py_INCREF(base);
- cmem->b_base = (CDataObject *)base;
+ cmem->b_base = (CDataObject *)Py_NewRef(base);
cmem->b_index = index;
} else { /* copy contents of adr */
if (-1 == PyCData_MallocBuffer(cmem, dict)) {
diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c
index fdd313f..791aeba 100644
--- a/Modules/_ctypes/cfield.c
+++ b/Modules/_ctypes/cfield.c
@@ -230,8 +230,7 @@ PyCField_get(CFieldObject *self, PyObject *inst, PyTypeObject *type)
{
CDataObject *src;
if (inst == NULL) {
- Py_INCREF(self);
- return (PyObject *)self;
+ return Py_NewRef(self);
}
if (!CDataObject_Check(inst)) {
PyErr_SetString(PyExc_TypeError,
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index f575d38..7b09779 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -933,8 +933,7 @@ _elementtree_Element___getstate___impl(ElementObject *self)
if (!children)
return NULL;
for (i = 0; i < PyList_GET_SIZE(children); i++) {
- PyObject *child = self->extra->children[i];
- Py_INCREF(child);
+ PyObject *child = Py_NewRef(self->extra->children[i]);
PyList_SET_ITEM(children, i, child);
}
@@ -1365,8 +1364,7 @@ _elementtree_Element_get_impl(ElementObject *self, PyObject *key,
/*[clinic end generated code: output=523c614142595d75 input=ee153bbf8cdb246e]*/
{
if (self->extra && self->extra->attrib) {
- PyObject *attrib = self->extra->attrib;
- Py_INCREF(attrib);
+ PyObject *attrib = Py_NewRef(self->extra->attrib);
PyObject *value = PyDict_GetItemWithError(attrib, key);
Py_XINCREF(value);
Py_DECREF(attrib);
@@ -1723,8 +1721,7 @@ element_subscr(PyObject* self_, PyObject* item)
for (cur = start, i = 0; i < slicelen;
cur += step, i++) {
- PyObject* item = self->extra->children[cur];
- Py_INCREF(item);
+ PyObject* item = Py_NewRef(self->extra->children[cur]);
PyList_SET_ITEM(list, i, item);
}
@@ -2761,8 +2758,7 @@ treebuilder_handle_end(TreeBuilderObject* self, PyObject* tag)
if (treebuilder_append_event(self, self->end_event_obj, self->last) < 0)
return NULL;
- Py_INCREF(self->last);
- return (PyObject*) self->last;
+ return Py_NewRef(self->last);
}
LOCAL(PyObject*)
diff --git a/Modules/_testbuffer.c b/Modules/_testbuffer.c
index bec20a1..63ed4dc 100644
--- a/Modules/_testbuffer.c
+++ b/Modules/_testbuffer.c
@@ -1787,8 +1787,7 @@ ndarray_subscript(NDArrayObject *self, PyObject *key)
return unpack_single(base->buf, base->format, base->itemsize);
}
else if (key == Py_Ellipsis) {
- Py_INCREF(self);
- return (PyObject *)self;
+ return Py_NewRef(self);
}
else {
PyErr_SetString(PyExc_TypeError, "invalid indexing of scalar");
diff --git a/Modules/_testcapi/datetime.c b/Modules/_testcapi/datetime.c
index 9fb6562..88f9929 100644
--- a/Modules/_testcapi/datetime.c
+++ b/Modules/_testcapi/datetime.c
@@ -137,11 +137,9 @@ get_timezone_utc_capi(PyObject *self, PyObject *args)
return NULL;
}
if (macro) {
- Py_INCREF(PyDateTime_TimeZone_UTC);
- return PyDateTime_TimeZone_UTC;
+ return Py_NewRef(PyDateTime_TimeZone_UTC);
}
- Py_INCREF(PyDateTimeAPI->TimeZone_UTC);
- return PyDateTimeAPI->TimeZone_UTC;
+ return Py_NewRef(PyDateTimeAPI->TimeZone_UTC);
}
static PyObject *
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
index fdce783..2311840 100644
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -746,8 +746,7 @@ mmap__enter__method(mmap_object *self, PyObject *args)
{
CHECK_VALID(NULL);
- Py_INCREF(self);
- return (PyObject *)self;
+ return Py_NewRef(self);
}
static PyObject *
diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c
index 4eea928..df4043d 100644
--- a/Modules/selectmodule.c
+++ b/Modules/selectmodule.c
@@ -1652,8 +1652,7 @@ select_epoll___enter___impl(pyEpoll_Object *self)
if (self->epfd < 0)
return pyepoll_err_closed();
- Py_INCREF(self);
- return (PyObject *)self;
+ return Py_NewRef(self);
}
/*[clinic input]
diff --git a/Modules/syslogmodule.c b/Modules/syslogmodule.c
index 5137d01..23833b7 100644
--- a/Modules/syslogmodule.c
+++ b/Modules/syslogmodule.c
@@ -205,8 +205,7 @@ syslog_syslog_impl(PyObject *module, int group_left_1, int priority,
/* Incref ident, because it can be decrefed if syslog.openlog() is
* called when the GIL is released.
*/
- PyObject *ident = S_ident_o;
- Py_XINCREF(ident);
+ PyObject *ident = Py_XNewRef(S_ident_o);
#ifdef __APPLE__
// gh-98178: On macOS, libc syslog() is not thread-safe
syslog(priority, "%s", message);
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index b2962fd..0ba6fb5 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -313,8 +313,7 @@ bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
}
memcpy(PyByteArray_AS_STRING(self) + size, vo.buf, vo.len);
PyBuffer_Release(&vo);
- Py_INCREF(self);
- return (PyObject *)self;
+ return Py_NewRef(self);
}
static PyObject *
@@ -340,8 +339,7 @@ bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
if (count < 0)
count = 0;
else if (count == 1) {
- Py_INCREF(self);
- return (PyObject*)self;
+ return Py_NewRef(self);
}
const Py_ssize_t mysize = Py_SIZE(self);
@@ -354,8 +352,7 @@ bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
char* buf = PyByteArray_AS_STRING(self);
_PyBytes_Repeat(buf, size, buf, mysize);
- Py_INCREF(self);
- return (PyObject *)self;
+ return Py_NewRef(self);
}
static PyObject *
@@ -2477,8 +2474,7 @@ bytearray_iter(PyObject *seq)
if (it == NULL)
return NULL;
it->it_index = 0;
- Py_INCREF(seq);
- it->it_seq = (PyByteArrayObject *)seq;
+ it->it_seq = (PyByteArrayObject *)Py_NewRef(seq);
_PyObject_GC_TRACK(it);
return (PyObject *)it;
}
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
index 7f5a71a..80117bf 100644
--- a/Objects/funcobject.c
+++ b/Objects/funcobject.c
@@ -45,8 +45,7 @@ PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject *qualname
PyThreadState *tstate = _PyThreadState_GET();
- PyCodeObject *code_obj = (PyCodeObject *)code;
- Py_INCREF(code_obj);
+ PyCodeObject *code_obj = (PyCodeObject *)Py_NewRef(code);
PyObject *name = code_obj->co_name;
assert(name != NULL);
diff --git a/Objects/setobject.c b/Objects/setobject.c
index 3c510b5..e064676 100644
--- a/Objects/setobject.c
+++ b/Objects/setobject.c
@@ -1601,8 +1601,7 @@ set_isub(PySetObject *so, PyObject *other)
Py_RETURN_NOTIMPLEMENTED;
if (set_difference_update_internal(so, other))
return NULL;
- Py_INCREF(so);
- return (PyObject *)so;
+ return Py_NewRef(so);
}
static PyObject *
@@ -1639,8 +1638,7 @@ set_symmetric_difference_update(PySetObject *so, PyObject *other)
}
if (PyAnySet_Check(other)) {
- Py_INCREF(other);
- otherset = (PySetObject *)other;
+ otherset = (PySetObject *)Py_NewRef(other);
} else {
otherset = (PySetObject *)make_new_set_basetype(Py_TYPE(so), other);
if (otherset == NULL)
@@ -1715,8 +1713,7 @@ set_ixor(PySetObject *so, PyObject *other)
if (result == NULL)
return NULL;
Py_DECREF(result);
- Py_INCREF(so);
- return (PyObject *)so;
+ return Py_NewRef(so);
}
static PyObject *