Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 10 additions & 19 deletions Objects/memoryobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -682,8 +682,7 @@ mbuf_add_view(_PyManagedBufferObject *mbuf, const Py_buffer *src)
init_suboffsets(dest, src);
init_flags(mv);

mv->mbuf = mbuf;
Py_INCREF(mbuf);
mv->mbuf = (_PyManagedBufferObject*)Py_NewRef(mbuf);
mbuf->exports++;

return (PyObject *)mv;
Expand Down Expand Up @@ -713,8 +712,7 @@ mbuf_add_incomplete_view(_PyManagedBufferObject *mbuf, const Py_buffer *src,
dest = &mv->view;
init_shared_values(dest, src);

mv->mbuf = mbuf;
Py_INCREF(mbuf);
mv->mbuf = (_PyManagedBufferObject*)Py_NewRef(mbuf);
mbuf->exports++;

return (PyObject *)mv;
Expand Down Expand Up @@ -1102,8 +1100,7 @@ static PyObject *
memory_enter(PyObject *self, PyObject *args)
{
CHECK_RELEASED(self);
Py_INCREF(self);
return self;
return Py_NewRef(self);
}

static PyObject *
Expand Down Expand Up @@ -1515,8 +1512,7 @@ memory_getbuf(PyMemoryViewObject *self, Py_buffer *view, int flags)
}


view->obj = (PyObject *)self;
Py_INCREF(view->obj);
view->obj = Py_NewRef(self);
self->exports++;

return 0;
Expand Down Expand Up @@ -2047,10 +2043,9 @@ struct_unpack_single(const char *ptr, struct unpacker *x)
return NULL;

if (PyTuple_GET_SIZE(v) == 1) {
PyObject *tmp = PyTuple_GET_ITEM(v, 0);
Py_INCREF(tmp);
PyObject *res = Py_NewRef(PyTuple_GET_ITEM(v, 0));
Py_DECREF(v);
return tmp;
return res;
}

return v;
Expand Down Expand Up @@ -2496,8 +2491,7 @@ memory_subscript(PyMemoryViewObject *self, PyObject *key)
return unpack_single(self, view->buf, fmt);
}
else if (key == Py_Ellipsis) {
Py_INCREF(self);
return (PyObject *)self;
return Py_NewRef(self);
}
else {
PyErr_SetString(PyExc_TypeError,
Expand Down Expand Up @@ -2957,8 +2951,7 @@ memory_richcompare(PyObject *v, PyObject *w, int op)
unpacker_free(unpack_v);
unpacker_free(unpack_w);

Py_XINCREF(res);
return res;
return Py_XNewRef(res);
}

/**************************************************************************/
Expand Down Expand Up @@ -3052,8 +3045,7 @@ memory_obj_get(PyMemoryViewObject *self, void *Py_UNUSED(ignored))
if (view->obj == NULL) {
Py_RETURN_NONE;
}
Py_INCREF(view->obj);
return view->obj;
return Py_NewRef(view->obj);
}

static PyObject *
Expand Down Expand Up @@ -3281,8 +3273,7 @@ memory_iter(PyObject *seq)
it->it_fmt = fmt;
it->it_length = memory_length(obj);
it->it_index = 0;
Py_INCREF(seq);
it->it_seq = obj;
it->it_seq = (PyMemoryViewObject*)Py_NewRef(obj);
_PyObject_GC_TRACK(it);
return (PyObject *)it;
}
Expand Down
15 changes: 5 additions & 10 deletions Objects/methodobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ PyCMethod_New(PyMethodDef *ml, PyObject *self, PyObject *module, PyTypeObject *c
if (om == NULL) {
return NULL;
}
Py_INCREF(cls);
om->mm_class = cls;
om->mm_class = (PyTypeObject*)Py_NewRef(cls);
op = (PyCFunctionObject *)om;
} else {
if (cls) {
Expand All @@ -106,10 +105,8 @@ PyCMethod_New(PyMethodDef *ml, PyObject *self, PyObject *module, PyTypeObject *c

op->m_weakreflist = NULL;
op->m_ml = ml;
Py_XINCREF(self);
op->m_self = self;
Py_XINCREF(module);
op->m_module = module;
op->m_self = Py_XNewRef(self);
op->m_module = Py_XNewRef(module);
op->vectorcall = vectorcall;
_PyObject_GC_TRACK(op);
return (PyObject *)op;
Expand Down Expand Up @@ -260,8 +257,7 @@ meth_get__self__(PyCFunctionObject *m, void *closure)
self = PyCFunction_GET_SELF(m);
if (self == NULL)
self = Py_None;
Py_INCREF(self);
return self;
return Py_NewRef(self);
}

static PyGetSetDef meth_getsets [] = {
Expand Down Expand Up @@ -314,8 +310,7 @@ meth_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 Py_hash_t
Expand Down
9 changes: 3 additions & 6 deletions Objects/moduleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ module_init_dict(PyModuleObject *mod, PyObject *md_dict,
if (PyDict_SetItem(md_dict, &_Py_ID(__spec__), Py_None) != 0)
return -1;
if (PyUnicode_CheckExact(name)) {
Py_INCREF(name);
Py_XSETREF(mod->md_name, name);
Py_XSETREF(mod->md_name, Py_NewRef(name));
}

return 0;
Expand Down Expand Up @@ -506,8 +505,7 @@ PyModule_GetNameObject(PyObject *m)
}
return NULL;
}
Py_INCREF(name);
return name;
return Py_NewRef(name);
}

const char *
Expand Down Expand Up @@ -541,8 +539,7 @@ PyModule_GetFilenameObject(PyObject *m)
}
return NULL;
}
Py_INCREF(fileobj);
return fileobj;
return Py_NewRef(fileobj);
}

const char *
Expand Down
15 changes: 5 additions & 10 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,7 @@ PyObject_Str(PyObject *v)
if (PyUnicode_READY(v) < 0)
return NULL;
#endif
Py_INCREF(v);
return v;
return Py_NewRef(v);
}
if (Py_TYPE(v)->tp_str == NULL)
return PyObject_Repr(v);
Expand Down Expand Up @@ -532,8 +531,7 @@ PyObject_Bytes(PyObject *v)
return PyBytes_FromString("<NULL>");

if (PyBytes_CheckExact(v)) {
Py_INCREF(v);
return v;
return Py_NewRef(v);
}

func = _PyObject_LookupSpecial(v, &_Py_ID(__bytes__));
Expand Down Expand Up @@ -689,8 +687,7 @@ do_richcompare(PyThreadState *tstate, PyObject *v, PyObject *w, int op)
Py_TYPE(w)->tp_name);
return NULL;
}
Py_INCREF(res);
return res;
return Py_NewRef(res);
}

/* Perform a rich comparison with object result. This wraps do_richcompare()
Expand Down Expand Up @@ -1096,8 +1093,7 @@ _PyObject_GetDictPtr(PyObject *obj)
PyObject *
PyObject_SelfIter(PyObject *obj)
{
Py_INCREF(obj);
return obj;
return Py_NewRef(obj);
}

/* Helper used when the __next__ method is removed from a type:
Expand Down Expand Up @@ -1481,8 +1477,7 @@ PyObject_GenericSetDict(PyObject *obj, PyObject *value, void *context)
"not a '%.200s'", Py_TYPE(value)->tp_name);
return -1;
}
Py_INCREF(value);
Py_XSETREF(*dictptr, value);
Py_XSETREF(*dictptr, Py_NewRef(value));
return 0;
}

Expand Down
24 changes: 8 additions & 16 deletions Objects/odictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -889,8 +889,7 @@ odict_inplace_or(PyObject *self, PyObject *other)
if (mutablemapping_update_arg(self, other) < 0) {
return NULL;
}
Py_INCREF(self);
return self;
return Py_NewRef(self);
}

/* tp_as_number */
Expand Down Expand Up @@ -1007,8 +1006,7 @@ OrderedDict_setdefault_impl(PyODictObject *self, PyObject *key,
return NULL;
assert(_odict_find_node(self, key) == NULL);
if (PyODict_SetItem((PyObject *)self, key, default_value) >= 0) {
result = default_value;
Py_INCREF(result);
result = Py_NewRef(default_value);
}
}
else {
Expand All @@ -1024,8 +1022,7 @@ OrderedDict_setdefault_impl(PyODictObject *self, PyObject *key,
result = PyObject_GetItem((PyObject *)self, key);
}
else if (PyObject_SetItem((PyObject *)self, key, default_value) >= 0) {
result = default_value;
Py_INCREF(result);
result = Py_NewRef(default_value);
}
}

Expand Down Expand Up @@ -1055,8 +1052,7 @@ _odict_popkey_hash(PyObject *od, PyObject *key, PyObject *failobj,
else if (value == NULL && !PyErr_Occurred()) {
/* Apply the fallback value, if necessary. */
if (failobj) {
value = failobj;
Py_INCREF(failobj);
value = Py_NewRef(failobj);
}
else {
PyErr_SetObject(PyExc_KeyError, key);
Expand Down Expand Up @@ -1497,8 +1493,7 @@ odict_richcompare(PyObject *v, PyObject *w, int op)
return NULL;

res = (eq == (op == Py_EQ)) ? Py_True : Py_False;
Py_INCREF(res);
return res;
return Py_NewRef(res);
} else {
Py_RETURN_NOTIMPLEMENTED;
}
Expand Down Expand Up @@ -1714,8 +1709,7 @@ odictiter_nextkey(odictiterobject *di)
di->di_current = NULL;
}
else {
di->di_current = _odictnode_KEY(node);
Py_INCREF(di->di_current);
di->di_current = Py_NewRef(_odictnode_KEY(node));
}

return key;
Expand Down Expand Up @@ -1872,12 +1866,10 @@ odictiter_new(PyODictObject *od, int kind)

di->kind = kind;
node = reversed ? _odict_LAST(od) : _odict_FIRST(od);
di->di_current = node ? _odictnode_KEY(node) : NULL;
Py_XINCREF(di->di_current);
di->di_current = node ? Py_NewRef(_odictnode_KEY(node)) : NULL;
di->di_size = PyODict_SIZE(od);
di->di_state = od->od_state;
di->di_odict = od;
Py_INCREF(od);
di->di_odict = (PyODictObject*)Py_NewRef(od);

_PyObject_GC_TRACK(di);
return (PyObject *)di;
Expand Down
28 changes: 10 additions & 18 deletions Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,7 @@ set_merge(PySetObject *so, PyObject *otherset)
key = other_entry->key;
if (key != NULL) {
assert(so_entry->key == NULL);
Py_INCREF(key);
so_entry->key = key;
so_entry->key = Py_NewRef(key);
so_entry->hash = other_entry->hash;
}
}
Expand All @@ -607,8 +606,8 @@ set_merge(PySetObject *so, PyObject *otherset)
for (i = other->mask + 1; i > 0 ; i--, other_entry++) {
key = other_entry->key;
if (key != NULL && key != dummy) {
Py_INCREF(key);
set_insert_clean(newtable, newmask, key, other_entry->hash);
set_insert_clean(newtable, newmask, Py_NewRef(key),
other_entry->hash);
}
}
return 0;
Expand Down Expand Up @@ -820,8 +819,7 @@ static PyObject *setiter_iternext(setiterobject *si)
goto fail;
si->len--;
key = entry[i].key;
Py_INCREF(key);
return key;
return Py_NewRef(key);

fail:
si->si_set = NULL;
Expand Down Expand Up @@ -868,8 +866,7 @@ set_iter(PySetObject *so)
setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
if (si == NULL)
return NULL;
Py_INCREF(so);
si->si_set = so;
si->si_set = (PySetObject*)Py_NewRef(so);
si->si_used = so->used;
si->si_pos = 0;
si->len = so->used;
Expand Down Expand Up @@ -997,8 +994,7 @@ make_new_frozenset(PyTypeObject *type, PyObject *iterable)

if (iterable != NULL && PyFrozenSet_CheckExact(iterable)) {
/* frozenset(f) is idempotent */
Py_INCREF(iterable);
return iterable;
return Py_NewRef(iterable);
}
return make_new_set(type, iterable);
}
Expand Down Expand Up @@ -1100,8 +1096,7 @@ static PyObject *
frozenset_copy(PySetObject *so, PyObject *Py_UNUSED(ignored))
{
if (PyFrozenSet_CheckExact(so)) {
Py_INCREF(so);
return (PyObject *)so;
return Py_NewRef(so);
}
return set_copy(so, NULL);
}
Expand Down Expand Up @@ -1173,8 +1168,7 @@ set_ior(PySetObject *so, PyObject *other)

if (set_update_internal(so, other))
return NULL;
Py_INCREF(so);
return (PyObject *)so;
return Py_NewRef(so);
}

static PyObject *
Expand Down Expand Up @@ -1264,12 +1258,11 @@ static PyObject *
set_intersection_multi(PySetObject *so, PyObject *args)
{
Py_ssize_t i;
PyObject *result = (PyObject *)so;

if (PyTuple_GET_SIZE(args) == 0)
return set_copy(so, NULL);

Py_INCREF(so);
PyObject *result = Py_NewRef(so);
for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
PyObject *other = PyTuple_GET_ITEM(args, i);
PyObject *newresult = set_intersection((PySetObject *)result, other);
Expand Down Expand Up @@ -1336,8 +1329,7 @@ set_iand(PySetObject *so, PyObject *other)
if (result == NULL)
return NULL;
Py_DECREF(result);
Py_INCREF(so);
return (PyObject *)so;
return Py_NewRef(so);
}

static PyObject *
Expand Down
Loading