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

Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Objects/ directory.
This commit is contained in:
Victor Stinner 2022-11-10 22:22:02 +01:00 committed by GitHub
parent 2f4af2d99c
commit 584e55bd34
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 120 additions and 244 deletions

View file

@ -389,8 +389,7 @@ reversed_new_impl(PyTypeObject *type, PyObject *seq)
return NULL;
ro->index = n-1;
Py_INCREF(seq);
ro->seq = seq;
ro->seq = Py_NewRef(seq);
return (PyObject *)ro;
}

View file

@ -53,8 +53,7 @@ BaseException_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
self->suppress_context = 0;
if (args) {
self->args = args;
Py_INCREF(args);
self->args = Py_NewRef(args);
return (PyObject *)self;
}
@ -73,9 +72,7 @@ BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
return -1;
Py_INCREF(args);
Py_XSETREF(self->args, args);
Py_XSETREF(self->args, Py_NewRef(args));
return 0;
}
@ -185,8 +182,7 @@ BaseException_with_traceback(PyObject *self, PyObject *tb) {
if (PyException_SetTraceback(self, tb))
return NULL;
Py_INCREF(self);
return self;
return Py_NewRef(self);
}
PyDoc_STRVAR(with_traceback_doc,
@ -258,8 +254,7 @@ BaseException_get_args(PyBaseExceptionObject *self, void *Py_UNUSED(ignored))
if (self->args == NULL) {
Py_RETURN_NONE;
}
Py_INCREF(self->args);
return self->args;
return Py_NewRef(self->args);
}
static int
@ -283,8 +278,7 @@ BaseException_get_tb(PyBaseExceptionObject *self, void *Py_UNUSED(ignored))
if (self->traceback == NULL) {
Py_RETURN_NONE;
}
Py_INCREF(self->traceback);
return self->traceback;
return Py_NewRef(self->traceback);
}
static int
@ -300,8 +294,7 @@ BaseException_set_tb(PyBaseExceptionObject *self, PyObject *tb, void *Py_UNUSED(
return -1;
}
Py_INCREF(tb);
Py_XSETREF(self->traceback, tb);
Py_XSETREF(self->traceback, Py_NewRef(tb));
return 0;
}
@ -380,8 +373,7 @@ PyObject *
PyException_GetTraceback(PyObject *self)
{
PyBaseExceptionObject *base_self = _PyBaseExceptionObject_cast(self);
Py_XINCREF(base_self->traceback);
return base_self->traceback;
return Py_XNewRef(base_self->traceback);
}
@ -395,8 +387,7 @@ PyObject *
PyException_GetCause(PyObject *self)
{
PyObject *cause = _PyBaseExceptionObject_cast(self)->cause;
Py_XINCREF(cause);
return cause;
return Py_XNewRef(cause);
}
/* Steals a reference to cause */
@ -412,8 +403,7 @@ PyObject *
PyException_GetContext(PyObject *self)
{
PyObject *context = _PyBaseExceptionObject_cast(self)->context;
Py_XINCREF(context);
return context;
return Py_XNewRef(context);
}
/* Steals a reference to context */
@ -579,8 +569,7 @@ StopIteration_init(PyStopIterationObject *self, PyObject *args, PyObject *kwds)
value = PyTuple_GET_ITEM(args, 0);
else
value = Py_None;
Py_INCREF(value);
self->value = value;
self->value = Py_NewRef(value);
return 0;
}
@ -633,12 +622,10 @@ SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
if (size == 0)
return 0;
if (size == 1) {
Py_INCREF(PyTuple_GET_ITEM(args, 0));
Py_XSETREF(self->code, PyTuple_GET_ITEM(args, 0));
Py_XSETREF(self->code, Py_NewRef(PyTuple_GET_ITEM(args, 0)));
}
else { /* size > 1 */
Py_INCREF(args);
Py_XSETREF(self->code, args);
Py_XSETREF(self->code, Py_NewRef(args));
}
return 0;
}
@ -1493,18 +1480,12 @@ ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds)
}
Py_DECREF(empty_tuple);
Py_XINCREF(name);
Py_XSETREF(self->name, name);
Py_XINCREF(path);
Py_XSETREF(self->path, path);
Py_XINCREF(name_from);
Py_XSETREF(self->name_from, name_from);
Py_XSETREF(self->name, Py_XNewRef(name));
Py_XSETREF(self->path, Py_XNewRef(path));
Py_XSETREF(self->name_from, Py_XNewRef(name_from));
if (PyTuple_GET_SIZE(args) == 1) {
msg = PyTuple_GET_ITEM(args, 0);
Py_INCREF(msg);
msg = Py_NewRef(PyTuple_GET_ITEM(args, 0));
}
Py_XSETREF(self->msg, msg);
@ -1543,8 +1524,7 @@ static PyObject *
ImportError_str(PyImportErrorObject *self)
{
if (self->msg && PyUnicode_CheckExact(self->msg)) {
Py_INCREF(self->msg);
return self->msg;
return Py_NewRef(self->msg);
}
else {
return BaseException_str((PyBaseExceptionObject *)self);
@ -1574,8 +1554,7 @@ ImportError_getstate(PyImportErrorObject *self)
return dict;
}
else if (dict) {
Py_INCREF(dict);
return dict;
return Py_NewRef(dict);
}
else {
Py_RETURN_NONE;
@ -1702,8 +1681,7 @@ oserror_parse_args(PyObject **p_args,
PyTuple_SET_ITEM(newargs, 0, *myerrno);
for (i = 1; i < nargs; i++) {
PyObject *val = PyTuple_GET_ITEM(args, i);
Py_INCREF(val);
PyTuple_SET_ITEM(newargs, i, val);
PyTuple_SET_ITEM(newargs, i, Py_NewRef(val));
}
Py_DECREF(args);
args = *p_args = newargs;
@ -1738,12 +1716,10 @@ oserror_init(PyOSErrorObject *self, PyObject **p_args,
return -1;
}
else {
Py_INCREF(filename);
self->filename = filename;
self->filename = Py_NewRef(filename);
if (filename2 && filename2 != Py_None) {
Py_INCREF(filename2);
self->filename2 = filename2;
self->filename2 = Py_NewRef(filename2);
}
if (nargs >= 2 && nargs <= 5) {
@ -1758,15 +1734,10 @@ oserror_init(PyOSErrorObject *self, PyObject **p_args,
}
}
}
Py_XINCREF(myerrno);
self->myerrno = myerrno;
Py_XINCREF(strerror);
self->strerror = strerror;
self->myerrno = Py_XNewRef(myerrno);
self->strerror = Py_XNewRef(strerror);
#ifdef MS_WINDOWS
Py_XINCREF(winerror);
self->winerror = winerror;
self->winerror = Py_XNewRef(winerror);
#endif
/* Steals the reference to args */
@ -1992,7 +1963,7 @@ static PyObject *
OSError_reduce(PyOSErrorObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject *args = self->args;
PyObject *res = NULL, *tmp;
PyObject *res = NULL;
/* self->args is only the first two real arguments if there was a
* file name given to OSError. */
@ -2002,16 +1973,9 @@ OSError_reduce(PyOSErrorObject *self, PyObject *Py_UNUSED(ignored))
if (!args)
return NULL;
tmp = PyTuple_GET_ITEM(self->args, 0);
Py_INCREF(tmp);
PyTuple_SET_ITEM(args, 0, tmp);
tmp = PyTuple_GET_ITEM(self->args, 1);
Py_INCREF(tmp);
PyTuple_SET_ITEM(args, 1, tmp);
Py_INCREF(self->filename);
PyTuple_SET_ITEM(args, 2, self->filename);
PyTuple_SET_ITEM(args, 0, Py_NewRef(PyTuple_GET_ITEM(self->args, 0)));
PyTuple_SET_ITEM(args, 1, Py_NewRef(PyTuple_GET_ITEM(self->args, 1)));
PyTuple_SET_ITEM(args, 2, Py_NewRef(self->filename));
if (self->filename2) {
/*
@ -2019,12 +1983,10 @@ OSError_reduce(PyOSErrorObject *self, PyObject *Py_UNUSED(ignored))
* So, to recreate filename2, we need to pass in
* winerror as well.
*/
Py_INCREF(Py_None);
PyTuple_SET_ITEM(args, 3, Py_None);
PyTuple_SET_ITEM(args, 3, Py_NewRef(Py_None));
/* filename2 */
Py_INCREF(self->filename2);
PyTuple_SET_ITEM(args, 4, self->filename2);
PyTuple_SET_ITEM(args, 4, Py_NewRef(self->filename2));
}
} else
Py_INCREF(args);
@ -2185,8 +2147,7 @@ NameError_init(PyNameErrorObject *self, PyObject *args, PyObject *kwds)
}
Py_DECREF(empty_tuple);
Py_XINCREF(name);
Py_XSETREF(self->name, name);
Py_XSETREF(self->name, Py_XNewRef(name));
return 0;
}
@ -2260,11 +2221,8 @@ AttributeError_init(PyAttributeErrorObject *self, PyObject *args, PyObject *kwds
}
Py_DECREF(empty_tuple);
Py_XINCREF(name);
Py_XSETREF(self->name, name);
Py_XINCREF(obj);
Py_XSETREF(self->obj, obj);
Py_XSETREF(self->name, Py_XNewRef(name));
Py_XSETREF(self->obj, Py_XNewRef(obj));
return 0;
}
@ -2322,8 +2280,7 @@ SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
return -1;
if (lenargs >= 1) {
Py_INCREF(PyTuple_GET_ITEM(args, 0));
Py_XSETREF(self->msg, PyTuple_GET_ITEM(args, 0));
Py_XSETREF(self->msg, Py_NewRef(PyTuple_GET_ITEM(args, 0)));
}
if (lenargs == 2) {
info = PyTuple_GET_ITEM(args, 1);
@ -2419,8 +2376,7 @@ my_basename(PyObject *name)
return PyUnicode_Substring(name, offset, size);
}
else {
Py_INCREF(name);
return name;
return Py_NewRef(name);
}
}
@ -2572,8 +2528,7 @@ get_string(PyObject *attr, const char *name)
PyErr_Format(PyExc_TypeError, "%.200s attribute must be bytes", name);
return NULL;
}
Py_INCREF(attr);
return attr;
return Py_NewRef(attr);
}
static PyObject *
@ -2589,8 +2544,7 @@ get_unicode(PyObject *attr, const char *name)
"%.200s attribute must be unicode", name);
return NULL;
}
Py_INCREF(attr);
return attr;
return Py_NewRef(attr);
}
static int

View file

@ -371,8 +371,7 @@ convert_to_double(PyObject **v, double *dbl)
}
}
else {
Py_INCREF(Py_NotImplemented);
*v = Py_NotImplemented;
*v = Py_NewRef(Py_NotImplemented);
return -1;
}
return 0;
@ -904,8 +903,7 @@ float_is_integer_impl(PyObject *self)
PyExc_ValueError);
return NULL;
}
Py_INCREF(o);
return o;
return Py_NewRef(o);
}
/*[clinic input]
@ -1124,11 +1122,12 @@ float___round___impl(PyObject *self, PyObject *o_ndigits)
static PyObject *
float_float(PyObject *v)
{
if (PyFloat_CheckExact(v))
Py_INCREF(v);
else
v = PyFloat_FromDouble(((PyFloatObject *)v)->ob_fval);
return v;
if (PyFloat_CheckExact(v)) {
return Py_NewRef(v);
}
else {
return PyFloat_FromDouble(((PyFloatObject *)v)->ob_fval);
}
}
/*[clinic input]

View file

@ -28,8 +28,7 @@ frame_getlocals(PyFrameObject *f, void *closure)
if (PyFrame_FastToLocalsWithError(f) < 0)
return NULL;
PyObject *locals = f->f_frame->f_locals;
Py_INCREF(locals);
return locals;
return Py_NewRef(locals);
}
int
@ -73,8 +72,7 @@ frame_getglobals(PyFrameObject *f, void *closure)
if (globals == NULL) {
globals = Py_None;
}
Py_INCREF(globals);
return globals;
return Py_NewRef(globals);
}
static PyObject *
@ -84,8 +82,7 @@ frame_getbuiltins(PyFrameObject *f, void *closure)
if (builtins == NULL) {
builtins = Py_None;
}
Py_INCREF(builtins);
return builtins;
return Py_NewRef(builtins);
}
static PyObject *
@ -823,13 +820,9 @@ static PyObject *
frame_gettrace(PyFrameObject *f, void *closure)
{
PyObject* trace = f->f_trace;
if (trace == NULL)
trace = Py_None;
Py_INCREF(trace);
return trace;
return Py_NewRef(trace);
}
static int
@ -838,9 +831,7 @@ frame_settrace(PyFrameObject *f, PyObject* v, void *closure)
if (v == Py_None) {
v = NULL;
}
Py_XINCREF(v);
Py_XSETREF(f->f_trace, v);
Py_XSETREF(f->f_trace, Py_XNewRef(v));
return 0;
}
@ -1028,11 +1019,9 @@ PyTypeObject PyFrame_Type = {
static void
init_frame(_PyInterpreterFrame *frame, PyFunctionObject *func, PyObject *locals)
{
/* _PyFrame_InitializeSpecials consumes reference to func */
Py_INCREF(func);
Py_XINCREF(locals);
PyCodeObject *code = (PyCodeObject *)func->func_code;
_PyFrame_InitializeSpecials(frame, func, locals, code);
_PyFrame_InitializeSpecials(frame, (PyFunctionObject*)Py_NewRef(func),
Py_XNewRef(locals), code);
for (Py_ssize_t i = 0; i < code->co_nlocalsplus; i++) {
frame->localsplus[i] = NULL;
}
@ -1146,8 +1135,7 @@ _PyFrame_FastToLocalsWithError(_PyInterpreterFrame *frame) {
int offset = co->co_nlocals + co->co_nplaincellvars;
for (int i = 0; i < co->co_nfreevars; ++i) {
PyObject *o = PyTuple_GET_ITEM(closure, i);
Py_INCREF(o);
frame->localsplus[offset + i] = o;
frame->localsplus[offset + i] = Py_NewRef(o);
}
// COPY_FREE_VARS doesn't have inline CACHEs, either:
frame->prev_instr = _PyCode_CODE(frame->f_code);
@ -1295,8 +1283,7 @@ _PyFrame_LocalsToFast(_PyInterpreterFrame *frame, int clear)
if (cell != NULL) {
oldvalue = PyCell_GET(cell);
if (value != oldvalue) {
Py_XINCREF(value);
PyCell_SET(cell, value);
PyCell_SET(cell, Py_XNewRef(value));
Py_XDECREF(oldvalue);
}
}
@ -1311,8 +1298,7 @@ _PyFrame_LocalsToFast(_PyInterpreterFrame *frame, int clear)
}
value = Py_NewRef(Py_None);
}
Py_INCREF(value);
Py_XSETREF(fast[i], value);
Py_XSETREF(fast[i], Py_NewRef(value));
}
Py_XDECREF(value);
}
@ -1345,8 +1331,7 @@ PyFrame_GetCode(PyFrameObject *frame)
assert(!_PyFrame_IsIncomplete(frame->f_frame));
PyCodeObject *code = frame->f_frame->f_code;
assert(code != NULL);
Py_INCREF(code);
return code;
return (PyCodeObject*)Py_NewRef(code);
}
@ -1365,8 +1350,7 @@ PyFrame_GetBack(PyFrameObject *frame)
back = _PyFrame_GetFrameObject(prev);
}
}
Py_XINCREF(back);
return back;
return (PyFrameObject*)Py_XNewRef(back);
}
PyObject*

View file

@ -17,22 +17,15 @@ _PyFunction_FromConstructor(PyFrameConstructor *constr)
if (op == NULL) {
return NULL;
}
Py_INCREF(constr->fc_globals);
op->func_globals = constr->fc_globals;
Py_INCREF(constr->fc_builtins);
op->func_builtins = constr->fc_builtins;
Py_INCREF(constr->fc_name);
op->func_name = constr->fc_name;
Py_INCREF(constr->fc_qualname);
op->func_qualname = constr->fc_qualname;
Py_INCREF(constr->fc_code);
op->func_code = constr->fc_code;
op->func_globals = Py_NewRef(constr->fc_globals);
op->func_builtins = Py_NewRef(constr->fc_builtins);
op->func_name = Py_NewRef(constr->fc_name);
op->func_qualname = Py_NewRef(constr->fc_qualname);
op->func_code = Py_NewRef(constr->fc_code);
op->func_defaults = NULL;
op->func_kwdefaults = NULL;
Py_XINCREF(constr->fc_closure);
op->func_closure = constr->fc_closure;
Py_INCREF(Py_None);
op->func_doc = Py_None;
op->func_closure = Py_XNewRef(constr->fc_closure);
op->func_doc = Py_NewRef(Py_None);
op->func_dict = NULL;
op->func_weakreflist = NULL;
op->func_module = NULL;
@ -367,8 +360,7 @@ func_get_code(PyFunctionObject *op, void *Py_UNUSED(ignored))
return NULL;
}
Py_INCREF(op->func_code);
return op->func_code;
return Py_NewRef(op->func_code);
}
static int
@ -402,16 +394,14 @@ func_set_code(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
return -1;
}
op->func_version = 0;
Py_INCREF(value);
Py_XSETREF(op->func_code, value);
Py_XSETREF(op->func_code, Py_NewRef(value));
return 0;
}
static PyObject *
func_get_name(PyFunctionObject *op, void *Py_UNUSED(ignored))
{
Py_INCREF(op->func_name);
return op->func_name;
return Py_NewRef(op->func_name);
}
static int
@ -424,16 +414,14 @@ func_set_name(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored))
"__name__ must be set to a string object");
return -1;
}
Py_INCREF(value);
Py_XSETREF(op->func_name, value);
Py_XSETREF(op->func_name, Py_NewRef(value));
return 0;
}
static PyObject *
func_get_qualname(PyFunctionObject *op, void *Py_UNUSED(ignored))
{
Py_INCREF(op->func_qualname);
return op->func_qualname;
return Py_NewRef(op->func_qualname);
}
static int
@ -446,8 +434,7 @@ func_set_qualname(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored
"__qualname__ must be set to a string object");
return -1;
}
Py_INCREF(value);
Py_XSETREF(op->func_qualname, value);
Py_XSETREF(op->func_qualname, Py_NewRef(value));
return 0;
}
@ -460,8 +447,7 @@ func_get_defaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
if (op->func_defaults == NULL) {
Py_RETURN_NONE;
}
Py_INCREF(op->func_defaults);
return op->func_defaults;
return Py_NewRef(op->func_defaults);
}
static int
@ -487,8 +473,7 @@ func_set_defaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignored
}
op->func_version = 0;
Py_XINCREF(value);
Py_XSETREF(op->func_defaults, value);
Py_XSETREF(op->func_defaults, Py_XNewRef(value));
return 0;
}
@ -502,8 +487,7 @@ func_get_kwdefaults(PyFunctionObject *op, void *Py_UNUSED(ignored))
if (op->func_kwdefaults == NULL) {
Py_RETURN_NONE;
}
Py_INCREF(op->func_kwdefaults);
return op->func_kwdefaults;
return Py_NewRef(op->func_kwdefaults);
}
static int
@ -529,8 +513,7 @@ func_set_kwdefaults(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(ignor
}
op->func_version = 0;
Py_XINCREF(value);
Py_XSETREF(op->func_kwdefaults, value);
Py_XSETREF(op->func_kwdefaults, Py_XNewRef(value));
return 0;
}
@ -563,8 +546,7 @@ func_set_annotations(PyFunctionObject *op, PyObject *value, void *Py_UNUSED(igno
return -1;
}
op->func_version = 0;
Py_XINCREF(value);
Py_XSETREF(op->func_annotations, value);
Py_XSETREF(op->func_annotations, Py_XNewRef(value));
return 0;
}
@ -674,16 +656,13 @@ func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
return NULL;
}
if (name != Py_None) {
Py_INCREF(name);
Py_SETREF(newfunc->func_name, name);
Py_SETREF(newfunc->func_name, Py_NewRef(name));
}
if (defaults != Py_None) {
Py_INCREF(defaults);
newfunc->func_defaults = defaults;
newfunc->func_defaults = Py_NewRef(defaults);
}
if (closure != Py_None) {
Py_INCREF(closure);
newfunc->func_closure = closure;
newfunc->func_closure = Py_NewRef(closure);
}
return (PyObject *)newfunc;
@ -757,8 +736,7 @@ static PyObject *
func_descr_get(PyObject *func, PyObject *obj, PyObject *type)
{
if (obj == Py_None || obj == NULL) {
Py_INCREF(func);
return func;
return Py_NewRef(func);
}
return PyMethod_New(func, obj);
}
@ -927,8 +905,7 @@ cm_init(PyObject *self, PyObject *args, PyObject *kwds)
return -1;
if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable))
return -1;
Py_INCREF(callable);
Py_XSETREF(cm->cm_callable, callable);
Py_XSETREF(cm->cm_callable, Py_NewRef(callable));
if (functools_wraps((PyObject *)cm, cm->cm_callable) < 0) {
return -1;
@ -1038,8 +1015,7 @@ PyClassMethod_New(PyObject *callable)
classmethod *cm = (classmethod *)
PyType_GenericAlloc(&PyClassMethod_Type, 0);
if (cm != NULL) {
Py_INCREF(callable);
cm->cm_callable = callable;
cm->cm_callable = Py_NewRef(callable);
}
return (PyObject *)cm;
}
@ -1104,8 +1080,7 @@ sm_descr_get(PyObject *self, PyObject *obj, PyObject *type)
"uninitialized staticmethod object");
return NULL;
}
Py_INCREF(sm->sm_callable);
return sm->sm_callable;
return Py_NewRef(sm->sm_callable);
}
static int
@ -1118,8 +1093,7 @@ sm_init(PyObject *self, PyObject *args, PyObject *kwds)
return -1;
if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable))
return -1;
Py_INCREF(callable);
Py_XSETREF(sm->sm_callable, callable);
Py_XSETREF(sm->sm_callable, Py_NewRef(callable));
if (functools_wraps((PyObject *)sm, sm->sm_callable) < 0) {
return -1;
@ -1234,8 +1208,7 @@ PyStaticMethod_New(PyObject *callable)
staticmethod *sm = (staticmethod *)
PyType_GenericAlloc(&PyStaticMethod_Type, 0);
if (sm != NULL) {
Py_INCREF(callable);
sm->sm_callable = callable;
sm->sm_callable = Py_NewRef(callable);
}
return (PyObject *)sm;
}

View file

@ -183,8 +183,7 @@ static int
tuple_add(PyObject *self, Py_ssize_t len, PyObject *item)
{
if (tuple_index(self, len, item) < 0) {
Py_INCREF(item);
PyTuple_SET_ITEM(self, len, item);
PyTuple_SET_ITEM(self, len, Py_NewRef(item));
return 1;
}
return 0;
@ -201,8 +200,7 @@ tuple_extend(PyObject **dst, Py_ssize_t dstindex,
assert(dstindex + count <= PyTuple_GET_SIZE(*dst));
for (Py_ssize_t i = 0; i < count; ++i) {
PyObject *item = src[i];
Py_INCREF(item);
PyTuple_SET_ITEM(*dst, dstindex + i, item);
PyTuple_SET_ITEM(*dst, dstindex + i, Py_NewRef(item));
}
return dstindex + count;
}
@ -304,8 +302,7 @@ subs_tvars(PyObject *obj, PyObject *params,
continue;
}
}
Py_INCREF(arg);
PyTuple_SET_ITEM(subargs, j, arg);
PyTuple_SET_ITEM(subargs, j, Py_NewRef(arg));
j++;
}
assert(j == PyTuple_GET_SIZE(subargs));
@ -347,8 +344,7 @@ _unpacked_tuple_args(PyObject *arg)
((gaobject *)arg)->origin == (PyObject *)&PyTuple_Type)
{
result = ((gaobject *)arg)->args;
Py_INCREF(result);
return result;
return Py_NewRef(result);
}
if (_PyObject_LookupAttr(arg, &_Py_ID(__typing_unpacked_tuple_args__), &result) > 0) {
@ -459,8 +455,7 @@ _Py_subs_parameters(PyObject *self, PyObject *args, PyObject *parameters, PyObje
for (Py_ssize_t iarg = 0, jarg = 0; iarg < nargs; iarg++) {
PyObject *arg = PyTuple_GET_ITEM(args, iarg);
if (PyType_Check(arg)) {
Py_INCREF(arg);
PyTuple_SET_ITEM(newargs, jarg, arg);
PyTuple_SET_ITEM(newargs, jarg, Py_NewRef(arg));
jarg++;
continue;
}
@ -767,8 +762,7 @@ ga_parameters(PyObject *self, void *unused)
return NULL;
}
}
Py_INCREF(alias->parameters);
return alias->parameters;
return Py_NewRef(alias->parameters);
}
static PyObject *
@ -776,8 +770,7 @@ ga_unpacked_tuple_args(PyObject *self, void *unused)
{
gaobject *alias = (gaobject *)self;
if (alias->starred && alias->origin == (PyObject *)&PyTuple_Type) {
Py_INCREF(alias->args);
return alias->args;
return Py_NewRef(alias->args);
}
Py_RETURN_NONE;
}
@ -803,8 +796,7 @@ setup_ga(gaobject *alias, PyObject *origin, PyObject *args) {
Py_INCREF(args);
}
Py_INCREF(origin);
alias->origin = origin;
alias->origin = Py_NewRef(origin);
alias->args = args;
alias->parameters = NULL;
alias->weakreflist = NULL;

View file

@ -194,8 +194,7 @@ gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult,
else if (arg && !exc) {
/* `gen` is an exhausted generator:
only return value if called from send(). */
*presult = Py_None;
Py_INCREF(*presult);
*presult = Py_NewRef(Py_None);
return PYGEN_RETURN;
}
return PYGEN_ERROR;
@ -204,8 +203,7 @@ gen_send_ex2(PyGenObject *gen, PyObject *arg, PyObject **presult,
assert(gen->gi_frame_state < FRAME_EXECUTING);
/* Push arg onto the frame's value stack */
result = arg ? arg : Py_None;
Py_INCREF(result);
_PyFrame_StackPush(frame, result);
_PyFrame_StackPush(frame, Py_NewRef(result));
_PyErr_StackItem *prev_exc_info = tstate->exc_info;
gen->gi_exc_state.previous_item = prev_exc_info;
@ -625,8 +623,7 @@ _PyGen_FetchStopIterationValue(PyObject **pvalue)
if (ev) {
/* exception will usually be normalised already */
if (PyObject_TypeCheck(ev, (PyTypeObject *) et)) {
value = ((PyStopIterationObject *)ev)->value;
Py_INCREF(value);
value = Py_NewRef(((PyStopIterationObject *)ev)->value);
Py_DECREF(ev);
} else if (et == PyExc_StopIteration && !PyTuple_Check(ev)) {
/* Avoid normalisation and take ev as value.
@ -645,8 +642,7 @@ _PyGen_FetchStopIterationValue(PyObject **pvalue)
PyErr_Restore(et, ev, tb);
return -1;
}
value = ((PyStopIterationObject *)ev)->value;
Py_INCREF(value);
value = Py_NewRef(((PyStopIterationObject *)ev)->value);
Py_DECREF(ev);
}
}
@ -656,8 +652,7 @@ _PyGen_FetchStopIterationValue(PyObject **pvalue)
return -1;
}
if (value == NULL) {
value = Py_None;
Py_INCREF(value);
value = Py_NewRef(Py_None);
}
*pvalue = value;
return 0;
@ -673,8 +668,7 @@ gen_repr(PyGenObject *gen)
static PyObject *
gen_get_name(PyGenObject *op, void *Py_UNUSED(ignored))
{
Py_INCREF(op->gi_name);
return op->gi_name;
return Py_NewRef(op->gi_name);
}
static int
@ -687,16 +681,14 @@ gen_set_name(PyGenObject *op, PyObject *value, void *Py_UNUSED(ignored))
"__name__ must be set to a string object");
return -1;
}
Py_INCREF(value);
Py_XSETREF(op->gi_name, value);
Py_XSETREF(op->gi_name, Py_NewRef(value));
return 0;
}
static PyObject *
gen_get_qualname(PyGenObject *op, void *Py_UNUSED(ignored))
{
Py_INCREF(op->gi_qualname);
return op->gi_qualname;
return Py_NewRef(op->gi_qualname);
}
static int
@ -709,8 +701,7 @@ gen_set_qualname(PyGenObject *op, PyObject *value, void *Py_UNUSED(ignored))
"__qualname__ must be set to a string object");
return -1;
}
Py_INCREF(value);
Py_XSETREF(op->gi_qualname, value);
Py_XSETREF(op->gi_qualname, Py_NewRef(value));
return 0;
}
@ -868,8 +859,7 @@ make_gen(PyTypeObject *type, PyFunctionObject *func)
return NULL;
}
gen->gi_frame_state = FRAME_CLEARED;
gen->gi_code = (PyCodeObject *)func->func_code;
Py_INCREF(gen->gi_code);
gen->gi_code = (PyCodeObject *)Py_NewRef(func->func_code);
gen->gi_weakreflist = NULL;
gen->gi_exc_state.exc_value = NULL;
gen->gi_exc_state.previous_item = NULL;
@ -955,15 +945,13 @@ gen_new_with_qualname(PyTypeObject *type, PyFrameObject *f,
gen->gi_exc_state.exc_value = NULL;
gen->gi_exc_state.previous_item = NULL;
if (name != NULL)
gen->gi_name = name;
gen->gi_name = Py_NewRef(name);
else
gen->gi_name = gen->gi_code->co_name;
Py_INCREF(gen->gi_name);
gen->gi_name = Py_NewRef(gen->gi_code->co_name);
if (qualname != NULL)
gen->gi_qualname = qualname;
gen->gi_qualname = Py_NewRef(qualname);
else
gen->gi_qualname = gen->gi_code->co_qualname;
Py_INCREF(gen->gi_qualname);
gen->gi_qualname = Py_NewRef(gen->gi_code->co_qualname);
_PyObject_GC_TRACK(gen);
return (PyObject *)gen;
}
@ -1015,8 +1003,7 @@ _PyCoro_GetAwaitableIter(PyObject *o)
if (PyCoro_CheckExact(o) || gen_is_coroutine(o)) {
/* 'o' is a coroutine. */
Py_INCREF(o);
return o;
return Py_NewRef(o);
}
ot = Py_TYPE(o);
@ -1063,8 +1050,7 @@ coro_await(PyCoroObject *coro)
if (cw == NULL) {
return NULL;
}
Py_INCREF(coro);
cw->cw_coroutine = coro;
cw->cw_coroutine = (PyCoroObject*)Py_NewRef(coro);
_PyObject_GC_TRACK(cw);
return (PyObject *)cw;
}
@ -1434,8 +1420,7 @@ async_gen_init_hooks(PyAsyncGenObject *o)
finalizer = tstate->async_gen_finalizer;
if (finalizer) {
Py_INCREF(finalizer);
o->ag_origin_or_finalizer = finalizer;
o->ag_origin_or_finalizer = Py_NewRef(finalizer);
}
firstiter = tstate->async_gen_firstiter;
@ -1897,11 +1882,9 @@ async_gen_asend_new(PyAsyncGenObject *gen, PyObject *sendval)
}
}
Py_INCREF(gen);
o->ags_gen = gen;
o->ags_gen = (PyAsyncGenObject*)Py_NewRef(gen);
Py_XINCREF(sendval);
o->ags_sendval = sendval;
o->ags_sendval = Py_XNewRef(sendval);
o->ags_state = AWAITABLE_STATE_INIT;
@ -2017,8 +2000,7 @@ _PyAsyncGenValueWrapperNew(PyObject *val)
return NULL;
}
}
o->agw_val = val;
Py_INCREF(val);
o->agw_val = Py_NewRef(val);
_PyObject_GC_TRACK((PyObject*)o);
return (PyObject*)o;
}
@ -2301,11 +2283,9 @@ async_gen_athrow_new(PyAsyncGenObject *gen, PyObject *args)
if (o == NULL) {
return NULL;
}
o->agt_gen = gen;
o->agt_args = args;
o->agt_gen = (PyAsyncGenObject*)Py_NewRef(gen);
o->agt_args = Py_XNewRef(args);
o->agt_state = AWAITABLE_STATE_INIT;
Py_INCREF(gen);
Py_XINCREF(args);
_PyObject_GC_TRACK((PyObject*)o);
return (PyObject*)o;
}

View file

@ -23,8 +23,7 @@ PySeqIter_New(PyObject *seq)
if (it == NULL)
return NULL;
it->it_index = 0;
Py_INCREF(seq);
it->it_seq = seq;
it->it_seq = Py_NewRef(seq);
_PyObject_GC_TRACK(it);
return (PyObject *)it;
}
@ -183,10 +182,8 @@ PyCallIter_New(PyObject *callable, PyObject *sentinel)
it = PyObject_GC_New(calliterobject, &PyCallIter_Type);
if (it == NULL)
return NULL;
Py_INCREF(callable);
it->it_callable = callable;
Py_INCREF(sentinel);
it->it_sentinel = sentinel;
it->it_callable = Py_NewRef(callable);
it->it_sentinel = Py_NewRef(sentinel);
_PyObject_GC_TRACK(it);
return (PyObject *)it;
}
@ -496,10 +493,8 @@ PyAnextAwaitable_New(PyObject *awaitable, PyObject *default_value)
if (anext == NULL) {
return NULL;
}
Py_INCREF(awaitable);
anext->wrapped = awaitable;
Py_INCREF(default_value);
anext->default_value = default_value;
anext->wrapped = Py_NewRef(awaitable);
anext->default_value = Py_NewRef(default_value);
_PyObject_GC_TRACK(anext);
return (PyObject *)anext;
}