Skip to content

Commit

Permalink
pythongh-99300: Use Py_NewRef() in Objects/ directory (python#99332)
Browse files Browse the repository at this point in the history
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Objects/ directory.
  • Loading branch information
vstinner authored and gvanrossum committed Nov 10, 2022
1 parent a8d608d commit 4ee85e7
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 156 deletions.
22 changes: 7 additions & 15 deletions Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ PyObject_Type(PyObject *o)
}

v = (PyObject *)Py_TYPE(o);
Py_INCREF(v);
return v;
return Py_NewRef(v);
}

Py_ssize_t
Expand Down Expand Up @@ -722,9 +721,7 @@ PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len,
return -1;
}

view->obj = obj;
if (obj)
Py_INCREF(obj);
view->obj = Py_XNewRef(obj);
view->buf = buf;
view->len = len;
view->readonly = readonly;
Expand Down Expand Up @@ -776,8 +773,7 @@ PyObject_Format(PyObject *obj, PyObject *format_spec)
/* Fast path for common types. */
if (format_spec == NULL || PyUnicode_GET_LENGTH(format_spec) == 0) {
if (PyUnicode_CheckExact(obj)) {
Py_INCREF(obj);
return obj;
return Py_NewRef(obj);
}
if (PyLong_CheckExact(obj)) {
return PyObject_Str(obj);
Expand Down Expand Up @@ -1405,8 +1401,7 @@ _PyNumber_Index(PyObject *item)
}

if (PyLong_Check(item)) {
Py_INCREF(item);
return item;
return Py_NewRef(item);
}
if (!_PyIndex_Check(item)) {
PyErr_Format(PyExc_TypeError,
Expand Down Expand Up @@ -1520,8 +1515,7 @@ PyNumber_Long(PyObject *o)
}

if (PyLong_CheckExact(o)) {
Py_INCREF(o);
return o;
return Py_NewRef(o);
}
m = Py_TYPE(o)->tp_as_number;
if (m && m->nb_int) { /* This should include subclasses of int */
Expand Down Expand Up @@ -2045,8 +2039,7 @@ PySequence_Tuple(PyObject *v)
a tuple *subclass* instance as-is, hence the restriction
to exact tuples here. In contrast, lists always make
a copy, so there's no need for exactness below. */
Py_INCREF(v);
return v;
return Py_NewRef(v);
}
if (PyList_CheckExact(v))
return PyList_AsTuple(v);
Expand Down Expand Up @@ -2144,8 +2137,7 @@ PySequence_Fast(PyObject *v, const char *m)
}

if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) {
Py_INCREF(v);
return v;
return Py_NewRef(v);
}

it = PyObject_GetIter(v);
Expand Down
3 changes: 1 addition & 2 deletions Objects/boolobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ PyObject *PyBool_FromLong(long ok)
result = Py_True;
else
result = Py_False;
Py_INCREF(result);
return result;
return Py_NewRef(result);
}

/* We define bool_new to always return either Py_True or Py_False */
Expand Down
51 changes: 17 additions & 34 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ static inline PyObject* bytes_get_empty(void)
// Return a strong reference to the empty bytes string singleton.
static inline PyObject* bytes_new_empty(void)
{
Py_INCREF(EMPTY);
return (PyObject *)EMPTY;
return Py_NewRef(EMPTY);
}


Expand Down Expand Up @@ -126,8 +125,7 @@ PyBytes_FromStringAndSize(const char *str, Py_ssize_t size)
}
if (size == 1 && str != NULL) {
op = CHARACTER(*str & 255);
Py_INCREF(op);
return (PyObject *)op;
return Py_NewRef(op);
}
if (size == 0) {
return bytes_new_empty();
Expand Down Expand Up @@ -162,8 +160,7 @@ PyBytes_FromString(const char *str)
}
else if (size == 1) {
op = CHARACTER(*str & 255);
Py_INCREF(op);
return (PyObject *)op;
return Py_NewRef(op);
}

/* Inline PyObject_NewVar */
Expand Down Expand Up @@ -527,14 +524,12 @@ format_obj(PyObject *v, const char **pbuf, Py_ssize_t *plen)
if (PyBytes_Check(v)) {
*pbuf = PyBytes_AS_STRING(v);
*plen = PyBytes_GET_SIZE(v);
Py_INCREF(v);
return v;
return Py_NewRef(v);
}
if (PyByteArray_Check(v)) {
*pbuf = PyByteArray_AS_STRING(v);
*plen = PyByteArray_GET_SIZE(v);
Py_INCREF(v);
return v;
return Py_NewRef(v);
}
/* does it support __bytes__? */
func = _PyObject_LookupSpecial(v, &_Py_ID(__bytes__));
Expand Down Expand Up @@ -1411,13 +1406,11 @@ bytes_concat(PyObject *a, PyObject *b)

/* Optimize end cases */
if (va.len == 0 && PyBytes_CheckExact(b)) {
result = b;
Py_INCREF(result);
result = Py_NewRef(b);
goto done;
}
if (vb.len == 0 && PyBytes_CheckExact(a)) {
result = a;
Py_INCREF(result);
result = Py_NewRef(a);
goto done;
}

Expand Down Expand Up @@ -1458,8 +1451,7 @@ bytes_repeat(PyBytesObject *a, Py_ssize_t n)
}
size = Py_SIZE(a) * n;
if (size == Py_SIZE(a) && PyBytes_CheckExact(a)) {
Py_INCREF(a);
return (PyObject *)a;
return Py_NewRef(a);
}
nbytes = (size_t)size;
if (nbytes + PyBytesObject_SIZE <= nbytes) {
Expand Down Expand Up @@ -1625,8 +1617,7 @@ bytes_subscript(PyBytesObject* self, PyObject* item)
else if (start == 0 && step == 1 &&
slicelength == PyBytes_GET_SIZE(self) &&
PyBytes_CheckExact(self)) {
Py_INCREF(self);
return (PyObject *)self;
return Py_NewRef(self);
}
else if (step == 1) {
return PyBytes_FromStringAndSize(
Expand Down Expand Up @@ -1696,8 +1687,7 @@ bytes___bytes___impl(PyBytesObject *self)
/*[clinic end generated code: output=63a306a9bc0caac5 input=34ec5ddba98bd6bb]*/
{
if (PyBytes_CheckExact(self)) {
Py_INCREF(self);
return (PyObject *)self;
return Py_NewRef(self);
}
else {
return PyBytes_FromStringAndSize(self->ob_sval, Py_SIZE(self));
Expand Down Expand Up @@ -1922,8 +1912,7 @@ do_xstrip(PyBytesObject *self, int striptype, PyObject *sepobj)
PyBuffer_Release(&vsep);

if (i == 0 && j == len && PyBytes_CheckExact(self)) {
Py_INCREF(self);
return (PyObject*)self;
return Py_NewRef(self);
}
else
return PyBytes_FromStringAndSize(s+i, j-i);
Expand Down Expand Up @@ -1952,8 +1941,7 @@ do_strip(PyBytesObject *self, int striptype)
}

if (i == 0 && j == len && PyBytes_CheckExact(self)) {
Py_INCREF(self);
return (PyObject*)self;
return Py_NewRef(self);
}
else
return PyBytes_FromStringAndSize(s+i, j-i);
Expand Down Expand Up @@ -2152,8 +2140,7 @@ bytes_translate_impl(PyBytesObject *self, PyObject *table,
}
if (!changed && PyBytes_CheckExact(input_obj)) {
Py_DECREF(result);
Py_INCREF(input_obj);
return input_obj;
return Py_NewRef(input_obj);
}
/* Fix the size of the resulting byte string */
if (inlen > 0)
Expand Down Expand Up @@ -2245,8 +2232,7 @@ bytes_removeprefix_impl(PyBytesObject *self, Py_buffer *prefix)
}

if (PyBytes_CheckExact(self)) {
Py_INCREF(self);
return (PyObject *)self;
return Py_NewRef(self);
}

return PyBytes_FromStringAndSize(self_start, self_len);
Expand Down Expand Up @@ -2284,8 +2270,7 @@ bytes_removesuffix_impl(PyBytesObject *self, Py_buffer *suffix)
}

if (PyBytes_CheckExact(self)) {
Py_INCREF(self);
return (PyObject *)self;
return Py_NewRef(self);
}

return PyBytes_FromStringAndSize(self_start, self_len);
Expand Down Expand Up @@ -2844,8 +2829,7 @@ PyBytes_FromObject(PyObject *x)
}

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

/* Use the modern buffer interface */
Expand Down Expand Up @@ -3269,8 +3253,7 @@ bytes_iter(PyObject *seq)
if (it == NULL)
return NULL;
it->it_index = 0;
Py_INCREF(seq);
it->it_seq = (PyBytesObject *)seq;
it->it_seq = (PyBytesObject *)Py_NewRef(seq);
_PyObject_GC_TRACK(it);
return (PyObject *)it;
}
Expand Down
9 changes: 3 additions & 6 deletions Objects/call.c
Original file line number Diff line number Diff line change
Expand Up @@ -1000,8 +1000,7 @@ _PyStack_UnpackDict(PyThreadState *tstate,

/* Copy positional arguments */
for (Py_ssize_t i = 0; i < nargs; i++) {
Py_INCREF(args[i]);
stack[i] = args[i];
stack[i] = Py_NewRef(args[i]);
}

PyObject **kwstack = stack + nargs;
Expand All @@ -1013,10 +1012,8 @@ _PyStack_UnpackDict(PyThreadState *tstate,
unsigned long keys_are_strings = Py_TPFLAGS_UNICODE_SUBCLASS;
while (PyDict_Next(kwargs, &pos, &key, &value)) {
keys_are_strings &= Py_TYPE(key)->tp_flags;
Py_INCREF(key);
Py_INCREF(value);
PyTuple_SET_ITEM(kwnames, i, key);
kwstack[i] = value;
PyTuple_SET_ITEM(kwnames, i, Py_NewRef(key));
kwstack[i] = Py_NewRef(value);
i++;
}

Expand Down
12 changes: 4 additions & 8 deletions Objects/cellobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ PyCell_New(PyObject *obj)
op = (PyCellObject *)PyObject_GC_New(PyCellObject, &PyCell_Type);
if (op == NULL)
return NULL;
op->ob_ref = obj;
Py_XINCREF(obj);
op->ob_ref = Py_XNewRef(obj);

_PyObject_GC_TRACK(op);
return (PyObject *)op;
Expand Down Expand Up @@ -68,8 +67,7 @@ PyCell_Set(PyObject *op, PyObject *value)
return -1;
}
PyObject *old_value = PyCell_GET(op);
Py_XINCREF(value);
PyCell_SET(op, value);
PyCell_SET(op, Py_XNewRef(value));
Py_XDECREF(old_value);
return 0;
}
Expand Down Expand Up @@ -135,15 +133,13 @@ cell_get_contents(PyCellObject *op, void *closure)
PyErr_SetString(PyExc_ValueError, "Cell is empty");
return NULL;
}
Py_INCREF(op->ob_ref);
return op->ob_ref;
return Py_NewRef(op->ob_ref);
}

static int
cell_set_contents(PyCellObject *op, PyObject *obj, void *Py_UNUSED(ignored))
{
Py_XINCREF(obj);
Py_XSETREF(op->ob_ref, obj);
Py_XSETREF(op->ob_ref, Py_XNewRef(obj));
return 0;
}

Expand Down
24 changes: 8 additions & 16 deletions Objects/classobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,8 @@ PyMethod_New(PyObject *func, PyObject *self)
return NULL;
}
im->im_weakreflist = NULL;
Py_INCREF(func);
im->im_func = func;
Py_INCREF(self);
im->im_self = self;
im->im_func = Py_NewRef(func);
im->im_self = Py_NewRef(self);
im->vectorcall = method_vectorcall;
_PyObject_GC_TRACK(im);
return (PyObject *)im;
Expand Down Expand Up @@ -195,8 +193,7 @@ method_getattro(PyObject *obj, PyObject *name)
if (f != NULL)
return f(descr, obj, (PyObject *)Py_TYPE(obj));
else {
Py_INCREF(descr);
return descr;
return Py_NewRef(descr);
}
}

Expand Down Expand Up @@ -267,8 +264,7 @@ method_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 PyObject *
Expand Down Expand Up @@ -359,8 +355,7 @@ PyInstanceMethod_New(PyObject *func) {
method = PyObject_GC_New(PyInstanceMethodObject,
&PyInstanceMethod_Type);
if (method == NULL) return NULL;
Py_INCREF(func);
method->func = func;
method->func = Py_NewRef(func);
_PyObject_GC_TRACK(method);
return (PyObject *)method;
}
Expand Down Expand Up @@ -412,8 +407,7 @@ instancemethod_getattro(PyObject *self, PyObject *name)
if (f != NULL)
return f(descr, self, (PyObject *)Py_TYPE(self));
else {
Py_INCREF(descr);
return descr;
return Py_NewRef(descr);
}
}

Expand Down Expand Up @@ -443,8 +437,7 @@ static PyObject *
instancemethod_descr_get(PyObject *descr, PyObject *obj, PyObject *type) {
PyObject *func = PyInstanceMethod_GET_FUNCTION(descr);
if (obj == NULL) {
Py_INCREF(func);
return func;
return Py_NewRef(func);
}
else
return PyMethod_New(func, obj);
Expand Down Expand Up @@ -472,8 +465,7 @@ instancemethod_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 PyObject *
Expand Down
Loading

0 comments on commit 4ee85e7

Please sign in to comment.