gh-99300: Use Py_NewRef() in Python/ directory (#99317)

Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Python/ directory.

Update Parser/asdl_c.py to regenerate Python/Python-ast.c.
This commit is contained in:
Victor Stinner 2022-11-10 11:23:36 +01:00 committed by GitHub
parent d8f239d86e
commit 231d83b724
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 169 additions and 327 deletions

View File

@ -995,10 +995,11 @@ static PyObject* ast2obj_list(struct ast_state *state, asdl_seq *seq, PyObject*
static PyObject* ast2obj_object(struct ast_state *Py_UNUSED(state), void *o) static PyObject* ast2obj_object(struct ast_state *Py_UNUSED(state), void *o)
{ {
if (!o) PyObject *op = (PyObject*)o;
o = Py_None; if (!op) {
Py_INCREF((PyObject*)o); op = Py_None;
return (PyObject*)o; }
return Py_NewRef(op);
} }
#define ast2obj_constant ast2obj_object #define ast2obj_constant ast2obj_object
#define ast2obj_identifier ast2obj_object #define ast2obj_identifier ast2obj_object
@ -1032,8 +1033,7 @@ static int obj2ast_constant(struct ast_state *Py_UNUSED(state), PyObject* obj, P
*out = NULL; *out = NULL;
return -1; return -1;
} }
Py_INCREF(obj); *out = Py_NewRef(obj);
*out = obj;
return 0; return 0;
} }
@ -1301,8 +1301,7 @@ class ObjVisitor(PickleVisitor):
self.emit("switch(o) {", 1) self.emit("switch(o) {", 1)
for t in sum.types: for t in sum.types:
self.emit("case %s:" % t.name, 2) self.emit("case %s:" % t.name, 2)
self.emit("Py_INCREF(state->%s_singleton);" % t.name, 3) self.emit("return Py_NewRef(state->%s_singleton);" % t.name, 3)
self.emit("return state->%s_singleton;" % t.name, 3)
self.emit("}", 1) self.emit("}", 1)
self.emit("Py_UNREACHABLE();", 1); self.emit("Py_UNREACHABLE();", 1);
self.emit("}", 0) self.emit("}", 0)

108
Python/Python-ast.c generated
View File

@ -993,10 +993,11 @@ static PyObject* ast2obj_list(struct ast_state *state, asdl_seq *seq, PyObject*
static PyObject* ast2obj_object(struct ast_state *Py_UNUSED(state), void *o) static PyObject* ast2obj_object(struct ast_state *Py_UNUSED(state), void *o)
{ {
if (!o) PyObject *op = (PyObject*)o;
o = Py_None; if (!op) {
Py_INCREF((PyObject*)o); op = Py_None;
return (PyObject*)o; }
return Py_NewRef(op);
} }
#define ast2obj_constant ast2obj_object #define ast2obj_constant ast2obj_object
#define ast2obj_identifier ast2obj_object #define ast2obj_identifier ast2obj_object
@ -1030,8 +1031,7 @@ static int obj2ast_constant(struct ast_state *Py_UNUSED(state), PyObject* obj, P
*out = NULL; *out = NULL;
return -1; return -1;
} }
Py_INCREF(obj); *out = Py_NewRef(obj);
*out = obj;
return 0; return 0;
} }
@ -4732,14 +4732,11 @@ PyObject* ast2obj_expr_context(struct ast_state *state, expr_context_ty o)
{ {
switch(o) { switch(o) {
case Load: case Load:
Py_INCREF(state->Load_singleton); return Py_NewRef(state->Load_singleton);
return state->Load_singleton;
case Store: case Store:
Py_INCREF(state->Store_singleton); return Py_NewRef(state->Store_singleton);
return state->Store_singleton;
case Del: case Del:
Py_INCREF(state->Del_singleton); return Py_NewRef(state->Del_singleton);
return state->Del_singleton;
} }
Py_UNREACHABLE(); Py_UNREACHABLE();
} }
@ -4747,11 +4744,9 @@ PyObject* ast2obj_boolop(struct ast_state *state, boolop_ty o)
{ {
switch(o) { switch(o) {
case And: case And:
Py_INCREF(state->And_singleton); return Py_NewRef(state->And_singleton);
return state->And_singleton;
case Or: case Or:
Py_INCREF(state->Or_singleton); return Py_NewRef(state->Or_singleton);
return state->Or_singleton;
} }
Py_UNREACHABLE(); Py_UNREACHABLE();
} }
@ -4759,44 +4754,31 @@ PyObject* ast2obj_operator(struct ast_state *state, operator_ty o)
{ {
switch(o) { switch(o) {
case Add: case Add:
Py_INCREF(state->Add_singleton); return Py_NewRef(state->Add_singleton);
return state->Add_singleton;
case Sub: case Sub:
Py_INCREF(state->Sub_singleton); return Py_NewRef(state->Sub_singleton);
return state->Sub_singleton;
case Mult: case Mult:
Py_INCREF(state->Mult_singleton); return Py_NewRef(state->Mult_singleton);
return state->Mult_singleton;
case MatMult: case MatMult:
Py_INCREF(state->MatMult_singleton); return Py_NewRef(state->MatMult_singleton);
return state->MatMult_singleton;
case Div: case Div:
Py_INCREF(state->Div_singleton); return Py_NewRef(state->Div_singleton);
return state->Div_singleton;
case Mod: case Mod:
Py_INCREF(state->Mod_singleton); return Py_NewRef(state->Mod_singleton);
return state->Mod_singleton;
case Pow: case Pow:
Py_INCREF(state->Pow_singleton); return Py_NewRef(state->Pow_singleton);
return state->Pow_singleton;
case LShift: case LShift:
Py_INCREF(state->LShift_singleton); return Py_NewRef(state->LShift_singleton);
return state->LShift_singleton;
case RShift: case RShift:
Py_INCREF(state->RShift_singleton); return Py_NewRef(state->RShift_singleton);
return state->RShift_singleton;
case BitOr: case BitOr:
Py_INCREF(state->BitOr_singleton); return Py_NewRef(state->BitOr_singleton);
return state->BitOr_singleton;
case BitXor: case BitXor:
Py_INCREF(state->BitXor_singleton); return Py_NewRef(state->BitXor_singleton);
return state->BitXor_singleton;
case BitAnd: case BitAnd:
Py_INCREF(state->BitAnd_singleton); return Py_NewRef(state->BitAnd_singleton);
return state->BitAnd_singleton;
case FloorDiv: case FloorDiv:
Py_INCREF(state->FloorDiv_singleton); return Py_NewRef(state->FloorDiv_singleton);
return state->FloorDiv_singleton;
} }
Py_UNREACHABLE(); Py_UNREACHABLE();
} }
@ -4804,17 +4786,13 @@ PyObject* ast2obj_unaryop(struct ast_state *state, unaryop_ty o)
{ {
switch(o) { switch(o) {
case Invert: case Invert:
Py_INCREF(state->Invert_singleton); return Py_NewRef(state->Invert_singleton);
return state->Invert_singleton;
case Not: case Not:
Py_INCREF(state->Not_singleton); return Py_NewRef(state->Not_singleton);
return state->Not_singleton;
case UAdd: case UAdd:
Py_INCREF(state->UAdd_singleton); return Py_NewRef(state->UAdd_singleton);
return state->UAdd_singleton;
case USub: case USub:
Py_INCREF(state->USub_singleton); return Py_NewRef(state->USub_singleton);
return state->USub_singleton;
} }
Py_UNREACHABLE(); Py_UNREACHABLE();
} }
@ -4822,35 +4800,25 @@ PyObject* ast2obj_cmpop(struct ast_state *state, cmpop_ty o)
{ {
switch(o) { switch(o) {
case Eq: case Eq:
Py_INCREF(state->Eq_singleton); return Py_NewRef(state->Eq_singleton);
return state->Eq_singleton;
case NotEq: case NotEq:
Py_INCREF(state->NotEq_singleton); return Py_NewRef(state->NotEq_singleton);
return state->NotEq_singleton;
case Lt: case Lt:
Py_INCREF(state->Lt_singleton); return Py_NewRef(state->Lt_singleton);
return state->Lt_singleton;
case LtE: case LtE:
Py_INCREF(state->LtE_singleton); return Py_NewRef(state->LtE_singleton);
return state->LtE_singleton;
case Gt: case Gt:
Py_INCREF(state->Gt_singleton); return Py_NewRef(state->Gt_singleton);
return state->Gt_singleton;
case GtE: case GtE:
Py_INCREF(state->GtE_singleton); return Py_NewRef(state->GtE_singleton);
return state->GtE_singleton;
case Is: case Is:
Py_INCREF(state->Is_singleton); return Py_NewRef(state->Is_singleton);
return state->Is_singleton;
case IsNot: case IsNot:
Py_INCREF(state->IsNot_singleton); return Py_NewRef(state->IsNot_singleton);
return state->IsNot_singleton;
case In: case In:
Py_INCREF(state->In_singleton); return Py_NewRef(state->In_singleton);
return state->In_singleton;
case NotIn: case NotIn:
Py_INCREF(state->NotIn_singleton); return Py_NewRef(state->NotIn_singleton);
return state->NotIn_singleton;
} }
Py_UNREACHABLE(); Py_UNREACHABLE();
} }

View File

@ -63,8 +63,7 @@ update_bases(PyObject *bases, PyObject *const *args, Py_ssize_t nargs)
} }
for (j = 0; j < i; j++) { for (j = 0; j < i; j++) {
base = args[j]; base = args[j];
PyList_SET_ITEM(new_bases, j, base); PyList_SET_ITEM(new_bases, j, Py_NewRef(base));
Py_INCREF(base);
} }
} }
j = PyList_GET_SIZE(new_bases); j = PyList_GET_SIZE(new_bases);
@ -170,8 +169,7 @@ builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
} }
if (winner != meta) { if (winner != meta) {
Py_DECREF(meta); Py_DECREF(meta);
meta = winner; meta = Py_NewRef(winner);
Py_INCREF(meta);
} }
} }
/* else: meta is not a class, so we cannot do the metaclass /* else: meta is not a class, so we cannot do the metaclass
@ -804,8 +802,7 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
goto error; goto error;
if (is_ast) { if (is_ast) {
if (flags & PyCF_ONLY_AST) { if (flags & PyCF_ONLY_AST) {
Py_INCREF(source); result = Py_NewRef(source);
result = source;
} }
else { else {
PyArena *arena; PyArena *arena;
@ -1128,8 +1125,7 @@ builtin_getattr(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
if (nargs > 2) { if (nargs > 2) {
if (_PyObject_LookupAttr(v, name, &result) == 0) { if (_PyObject_LookupAttr(v, name, &result) == 0) {
PyObject *dflt = args[2]; PyObject *dflt = args[2];
Py_INCREF(dflt); return Py_NewRef(dflt);
return dflt;
} }
} }
else { else {
@ -1162,8 +1158,7 @@ builtin_globals_impl(PyObject *module)
PyObject *d; PyObject *d;
d = PyEval_GetGlobals(); d = PyEval_GetGlobals();
Py_XINCREF(d); return Py_XNewRef(d);
return d;
} }
@ -1390,12 +1385,10 @@ map_reduce(mapobject *lz, PyObject *Py_UNUSED(ignored))
Py_ssize_t i; Py_ssize_t i;
if (args == NULL) if (args == NULL)
return NULL; return NULL;
Py_INCREF(lz->func); PyTuple_SET_ITEM(args, 0, Py_NewRef(lz->func));
PyTuple_SET_ITEM(args, 0, lz->func);
for (i = 0; i<numargs; i++){ for (i = 0; i<numargs; i++){
PyObject *it = PyTuple_GET_ITEM(lz->iters, i); PyObject *it = PyTuple_GET_ITEM(lz->iters, i);
Py_INCREF(it); PyTuple_SET_ITEM(args, i+1, Py_NewRef(it));
PyTuple_SET_ITEM(args, i+1, it);
} }
return Py_BuildValue("ON", Py_TYPE(lz), args); return Py_BuildValue("ON", Py_TYPE(lz), args);
@ -1486,8 +1479,7 @@ builtin_next(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
return NULL; return NULL;
PyErr_Clear(); PyErr_Clear();
} }
Py_INCREF(def); return Py_NewRef(def);
return def;
} else if (PyErr_Occurred()) { } else if (PyErr_Occurred()) {
return NULL; return NULL;
} else { } else {
@ -1723,8 +1715,7 @@ builtin_locals_impl(PyObject *module)
PyObject *d; PyObject *d;
d = PyEval_GetLocals(); d = PyEval_GetLocals();
Py_XINCREF(d); return Py_XNewRef(d);
return d;
} }
@ -1785,8 +1776,7 @@ min_max(PyObject *args, PyObject *kwds, int op)
} }
/* no key function; the value is the item */ /* no key function; the value is the item */
else { else {
val = item; val = Py_NewRef(item);
Py_INCREF(val);
} }
/* maximum value and item are unset; set them */ /* maximum value and item are unset; set them */
@ -1816,8 +1806,7 @@ min_max(PyObject *args, PyObject *kwds, int op)
if (maxval == NULL) { if (maxval == NULL) {
assert(maxitem == NULL); assert(maxitem == NULL);
if (defaultval != NULL) { if (defaultval != NULL) {
Py_INCREF(defaultval); maxitem = Py_NewRef(defaultval);
maxitem = defaultval;
} else { } else {
PyErr_Format(PyExc_ValueError, PyErr_Format(PyExc_ValueError,
"%s() arg is an empty sequence", name); "%s() arg is an empty sequence", name);
@ -2737,8 +2726,7 @@ zip_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL; return NULL;
} }
for (i=0 ; i < tuplesize ; i++) { for (i=0 ; i < tuplesize ; i++) {
Py_INCREF(Py_None); PyTuple_SET_ITEM(result, i, Py_NewRef(Py_None));
PyTuple_SET_ITEM(result, i, Py_None);
} }
/* create zipobject structure */ /* create zipobject structure */

View File

@ -530,8 +530,7 @@ _Py_Mangle(PyObject *privateobj, PyObject *ident)
if (privateobj == NULL || !PyUnicode_Check(privateobj) || if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
PyUnicode_READ_CHAR(ident, 0) != '_' || PyUnicode_READ_CHAR(ident, 0) != '_' ||
PyUnicode_READ_CHAR(ident, 1) != '_') { PyUnicode_READ_CHAR(ident, 1) != '_') {
Py_INCREF(ident); return Py_NewRef(ident);
return ident;
} }
nlen = PyUnicode_GET_LENGTH(ident); nlen = PyUnicode_GET_LENGTH(ident);
plen = PyUnicode_GET_LENGTH(privateobj); plen = PyUnicode_GET_LENGTH(privateobj);
@ -547,16 +546,14 @@ _Py_Mangle(PyObject *privateobj, PyObject *ident)
if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' && if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
PyUnicode_READ_CHAR(ident, nlen-2) == '_') || PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) { PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
Py_INCREF(ident); return Py_NewRef(ident); /* Don't mangle __whatever__ */
return ident; /* Don't mangle __whatever__ */
} }
/* Strip leading underscores from class name */ /* Strip leading underscores from class name */
ipriv = 0; ipriv = 0;
while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_') while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
ipriv++; ipriv++;
if (ipriv == plen) { if (ipriv == plen) {
Py_INCREF(ident); return Py_NewRef(ident); /* Don't mangle if class is just underscores */
return ident; /* Don't mangle if class is just underscores */
} }
plen -= ipriv; plen -= ipriv;
@ -616,8 +613,7 @@ _PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
int merged; int merged;
if (!compiler_init(&c)) if (!compiler_init(&c))
return NULL; return NULL;
Py_INCREF(filename); c.c_filename = Py_NewRef(filename);
c.c_filename = filename;
c.c_arena = arena; c.c_arena = arena;
if (!_PyFuture_FromAST(mod, filename, &c.c_future)) { if (!_PyFuture_FromAST(mod, filename, &c.c_future)) {
goto finally; goto finally;
@ -859,8 +855,7 @@ compiler_set_qualname(struct compiler *c)
return 0; return 0;
} }
else { else {
Py_INCREF(parent->u_qualname); base = Py_NewRef(parent->u_qualname);
base = parent->u_qualname;
} }
} }
} }
@ -876,8 +871,7 @@ compiler_set_qualname(struct compiler *c)
return 0; return 0;
} }
else { else {
Py_INCREF(u->u_name); name = Py_NewRef(u->u_name);
name = u->u_name;
} }
u->u_qualname = name; u->u_qualname = name;
@ -1381,8 +1375,7 @@ merge_consts_recursive(PyObject *const_cache, PyObject *o)
// None and Ellipsis are singleton, and key is the singleton. // None and Ellipsis are singleton, and key is the singleton.
// No need to merge object and key. // No need to merge object and key.
if (o == Py_None || o == Py_Ellipsis) { if (o == Py_None || o == Py_Ellipsis) {
Py_INCREF(o); return Py_NewRef(o);
return o;
} }
PyObject *key = _PyCode_ConstantKey(o); PyObject *key = _PyCode_ConstantKey(o);
@ -1421,8 +1414,7 @@ merge_consts_recursive(PyObject *const_cache, PyObject *o)
v = u; v = u;
} }
if (v != item) { if (v != item) {
Py_INCREF(v); PyTuple_SET_ITEM(o, i, Py_NewRef(v));
PyTuple_SET_ITEM(o, i, v);
Py_DECREF(item); Py_DECREF(item);
} }
@ -1708,8 +1700,7 @@ compiler_enter_scope(struct compiler *c, identifier name,
compiler_unit_free(u); compiler_unit_free(u);
return 0; return 0;
} }
Py_INCREF(name); u->u_name = Py_NewRef(name);
u->u_name = name;
u->u_varnames = list2dict(u->u_ste->ste_varnames); u->u_varnames = list2dict(u->u_ste->ste_varnames);
u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
if (!u->u_varnames || !u->u_cellvars) { if (!u->u_varnames || !u->u_cellvars) {
@ -1760,8 +1751,7 @@ compiler_enter_scope(struct compiler *c, identifier name,
return 0; return 0;
} }
Py_DECREF(capsule); Py_DECREF(capsule);
u->u_private = c->u->u_private; u->u_private = Py_XNewRef(c->u->u_private);
Py_XINCREF(u->u_private);
} }
c->u = u; c->u = u;
@ -2671,8 +2661,7 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async)
} }
} }
co = assemble(c, 1); co = assemble(c, 1);
qualname = c->u->u_qualname; qualname = Py_NewRef(c->u->u_qualname);
Py_INCREF(qualname);
compiler_exit_scope(c); compiler_exit_scope(c);
if (co == NULL) { if (co == NULL) {
Py_XDECREF(qualname); Py_XDECREF(qualname);
@ -3053,8 +3042,7 @@ compiler_lambda(struct compiler *c, expr_ty e)
ADDOP_IN_SCOPE(c, loc, RETURN_VALUE); ADDOP_IN_SCOPE(c, loc, RETURN_VALUE);
co = assemble(c, 1); co = assemble(c, 1);
} }
qualname = c->u->u_qualname; qualname = Py_NewRef(c->u->u_qualname);
Py_INCREF(qualname);
compiler_exit_scope(c); compiler_exit_scope(c);
if (co == NULL) { if (co == NULL) {
Py_DECREF(qualname); Py_DECREF(qualname);
@ -3925,8 +3913,7 @@ compiler_from_import(struct compiler *c, stmt_ty s)
/* build up the names */ /* build up the names */
for (Py_ssize_t i = 0; i < n; i++) { for (Py_ssize_t i = 0; i < n; i++) {
alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Py_INCREF(alias->name); PyTuple_SET_ITEM(names, i, Py_NewRef(alias->name));
PyTuple_SET_ITEM(names, i, alias->name);
} }
if (location_is_after(LOC(s), c->c_future.ff_location) && if (location_is_after(LOC(s), c->c_future.ff_location) &&
@ -4348,8 +4335,7 @@ starunpack_helper(struct compiler *c, location loc,
PyObject *val; PyObject *val;
for (Py_ssize_t i = 0; i < n; i++) { for (Py_ssize_t i = 0; i < n; i++) {
val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value; val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
Py_INCREF(val); PyTuple_SET_ITEM(folded, i, Py_NewRef(val));
PyTuple_SET_ITEM(folded, i, val);
} }
if (tuple && !pushed) { if (tuple && !pushed) {
ADDOP_LOAD_CONST_NEW(c, loc, folded); ADDOP_LOAD_CONST_NEW(c, loc, folded);
@ -4530,8 +4516,7 @@ compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end
} }
for (i = begin; i < end; i++) { for (i = begin; i < end; i++) {
key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value; key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
Py_INCREF(key); PyTuple_SET_ITEM(keys, i - begin, Py_NewRef(key));
PyTuple_SET_ITEM(keys, i - begin, key);
} }
ADDOP_LOAD_CONST_NEW(c, loc, keys); ADDOP_LOAD_CONST_NEW(c, loc, keys);
ADDOP_I(c, loc, BUILD_CONST_KEY_MAP, n); ADDOP_I(c, loc, BUILD_CONST_KEY_MAP, n);
@ -5014,8 +4999,7 @@ compiler_subkwargs(struct compiler *c, location loc,
} }
for (i = begin; i < end; i++) { for (i = begin; i < end; i++) {
key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
Py_INCREF(key); PyTuple_SET_ITEM(keys, i - begin, Py_NewRef(key));
PyTuple_SET_ITEM(keys, i - begin, key);
} }
ADDOP_LOAD_CONST_NEW(c, loc, keys); ADDOP_LOAD_CONST_NEW(c, loc, keys);
ADDOP_I(c, loc, BUILD_CONST_KEY_MAP, n); ADDOP_I(c, loc, BUILD_CONST_KEY_MAP, n);
@ -5053,8 +5037,7 @@ compiler_call_simple_kw_helper(struct compiler *c, location loc,
} }
for (int i = 0; i < nkwelts; i++) { for (int i = 0; i < nkwelts; i++) {
keyword_ty kw = asdl_seq_GET(keywords, i); keyword_ty kw = asdl_seq_GET(keywords, i);
Py_INCREF(kw->arg); PyTuple_SET_ITEM(names, i, Py_NewRef(kw->arg));
PyTuple_SET_ITEM(names, i, kw->arg);
} }
Py_ssize_t arg = compiler_add_const(c, names); Py_ssize_t arg = compiler_add_const(c, names);
if (arg < 0) { if (arg < 0) {
@ -5490,8 +5473,7 @@ compiler_comprehension(struct compiler *c, expr_ty e, int type,
} }
co = assemble(c, 1); co = assemble(c, 1);
qualname = c->u->u_qualname; qualname = Py_NewRef(c->u->u_qualname);
Py_INCREF(qualname);
compiler_exit_scope(c); compiler_exit_scope(c);
if (is_top_level_await && is_async_generator){ if (is_top_level_await && is_async_generator){
c->u->u_ste->ste_coroutine = 1; c->u->u_ste->ste_coroutine = 1;
@ -6170,8 +6152,7 @@ compiler_error(struct compiler *c, location loc,
} }
PyObject *loc_obj = PyErr_ProgramTextObject(c->c_filename, loc.lineno); PyObject *loc_obj = PyErr_ProgramTextObject(c->c_filename, loc.lineno);
if (loc_obj == NULL) { if (loc_obj == NULL) {
Py_INCREF(Py_None); loc_obj = Py_NewRef(Py_None);
loc_obj = Py_None;
} }
PyObject *args = Py_BuildValue("O(OiiOii)", msg, c->c_filename, PyObject *args = Py_BuildValue("O(OiiOii)", msg, c->c_filename,
loc.lineno, loc.col_offset + 1, loc_obj, loc.lineno, loc.col_offset + 1, loc_obj,
@ -6605,8 +6586,7 @@ compiler_pattern_class(struct compiler *c, location *ploc,
Py_ssize_t i; Py_ssize_t i;
for (i = 0; i < nattrs; i++) { for (i = 0; i < nattrs; i++) {
PyObject *name = asdl_seq_GET(kwd_attrs, i); PyObject *name = asdl_seq_GET(kwd_attrs, i);
Py_INCREF(name); PyTuple_SET_ITEM(attr_names, i, Py_NewRef(name));
PyTuple_SET_ITEM(attr_names, i, name);
} }
ADDOP_LOAD_CONST_NEW(c, *ploc, attr_names); ADDOP_LOAD_CONST_NEW(c, *ploc, attr_names);
ADDOP_I(c, *ploc, MATCH_CLASS, nargs); ADDOP_I(c, *ploc, MATCH_CLASS, nargs);
@ -6815,8 +6795,7 @@ compiler_pattern_or(struct compiler *c, location *ploc,
// for the others (they can't bind a different set of names, and // for the others (they can't bind a different set of names, and
// might need to be reordered): // might need to be reordered):
assert(control == NULL); assert(control == NULL);
control = pc->stores; control = Py_NewRef(pc->stores);
Py_INCREF(control);
} }
else if (nstores != PyList_GET_SIZE(control)) { else if (nstores != PyList_GET_SIZE(control)) {
goto diff; goto diff;
@ -8208,10 +8187,9 @@ dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
return NULL; return NULL;
while (PyDict_Next(dict, &pos, &k, &v)) { while (PyDict_Next(dict, &pos, &k, &v)) {
i = PyLong_AS_LONG(v); i = PyLong_AS_LONG(v);
Py_INCREF(k);
assert((i - offset) < size); assert((i - offset) < size);
assert((i - offset) >= 0); assert((i - offset) >= 0);
PyTuple_SET_ITEM(tuple, i - offset, k); PyTuple_SET_ITEM(tuple, i - offset, Py_NewRef(k));
} }
return tuple; return tuple;
} }
@ -8233,10 +8211,9 @@ consts_dict_keys_inorder(PyObject *dict)
if (PyTuple_CheckExact(k)) { if (PyTuple_CheckExact(k)) {
k = PyTuple_GET_ITEM(k, 1); k = PyTuple_GET_ITEM(k, 1);
} }
Py_INCREF(k);
assert(i < size); assert(i < size);
assert(i >= 0); assert(i >= 0);
PyList_SET_ITEM(consts, i, k); PyList_SET_ITEM(consts, i, Py_NewRef(k));
} }
return consts; return consts;
} }
@ -8936,8 +8913,7 @@ get_const_value(int opcode, int oparg, PyObject *co_consts)
"Internal error: failed to get value of a constant"); "Internal error: failed to get value of a constant");
return NULL; return NULL;
} }
Py_INCREF(constant); return Py_NewRef(constant);
return constant;
} }
/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n /* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
@ -9978,6 +9954,5 @@ PyObject *
PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts), PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj)) PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
{ {
Py_INCREF(code); return Py_NewRef(code);
return code;
} }

View File

@ -124,8 +124,7 @@ _PyContext_Enter(PyThreadState *ts, PyObject *octx)
ctx->ctx_prev = (PyContext *)ts->context; /* borrow */ ctx->ctx_prev = (PyContext *)ts->context; /* borrow */
ctx->ctx_entered = 1; ctx->ctx_entered = 1;
Py_INCREF(ctx); ts->context = Py_NewRef(ctx);
ts->context = (PyObject *)ctx;
ts->context_ver++; ts->context_ver++;
return 0; return 0;
@ -400,8 +399,7 @@ context_new_from_vars(PyHamtObject *vars)
return NULL; return NULL;
} }
Py_INCREF(vars); ctx->ctx_vars = (PyHamtObject*)Py_NewRef(vars);
ctx->ctx_vars = vars;
_PyObject_GC_TRACK(ctx); _PyObject_GC_TRACK(ctx);
return ctx; return ctx;
@ -546,8 +544,7 @@ context_tp_subscript(PyContext *self, PyObject *key)
PyErr_SetObject(PyExc_KeyError, key); PyErr_SetObject(PyExc_KeyError, key);
return NULL; return NULL;
} }
Py_INCREF(val); return Py_NewRef(val);
return val;
} }
static int static int
@ -588,11 +585,9 @@ _contextvars_Context_get_impl(PyContext *self, PyObject *key,
return NULL; return NULL;
} }
if (found == 0) { if (found == 0) {
Py_INCREF(default_value); return Py_NewRef(default_value);
return default_value;
} }
Py_INCREF(val); return Py_NewRef(val);
return val;
} }
@ -831,11 +826,9 @@ contextvar_new(PyObject *name, PyObject *def)
return NULL; return NULL;
} }
Py_INCREF(name); var->var_name = Py_NewRef(name);
var->var_name = name;
Py_XINCREF(def); var->var_default = Py_XNewRef(def);
var->var_default = def;
var->var_cached = NULL; var->var_cached = NULL;
var->var_cached_tsid = 0; var->var_cached_tsid = 0;
@ -1176,8 +1169,7 @@ error:
static PyObject * static PyObject *
token_get_var(PyContextToken *self, void *Py_UNUSED(ignored)) token_get_var(PyContextToken *self, void *Py_UNUSED(ignored))
{ {
Py_INCREF(self->tok_var); return Py_NewRef(self->tok_var);;
return (PyObject *)self->tok_var;
} }
static PyObject * static PyObject *
@ -1187,8 +1179,7 @@ token_get_old_value(PyContextToken *self, void *Py_UNUSED(ignored))
return get_token_missing(); return get_token_missing();
} }
Py_INCREF(self->tok_oldval); return Py_NewRef(self->tok_oldval);
return self->tok_oldval;
} }
static PyGetSetDef PyContextTokenType_getsetlist[] = { static PyGetSetDef PyContextTokenType_getsetlist[] = {
@ -1228,14 +1219,11 @@ token_new(PyContext *ctx, PyContextVar *var, PyObject *val)
return NULL; return NULL;
} }
Py_INCREF(ctx); tok->tok_ctx = (PyContext*)Py_NewRef(ctx);
tok->tok_ctx = ctx;
Py_INCREF(var); tok->tok_var = (PyContextVar*)Py_NewRef(var);
tok->tok_var = var;
Py_XINCREF(val); tok->tok_oldval = Py_XNewRef(val);
tok->tok_oldval = val;
tok->tok_used = 0; tok->tok_used = 0;
@ -1276,8 +1264,7 @@ static PyObject *
get_token_missing(void) get_token_missing(void)
{ {
if (_token_missing != NULL) { if (_token_missing != NULL) {
Py_INCREF(_token_missing); return Py_NewRef(_token_missing);
return _token_missing;
} }
_token_missing = (PyObject *)PyObject_New( _token_missing = (PyObject *)PyObject_New(
@ -1286,8 +1273,7 @@ get_token_missing(void)
return NULL; return NULL;
} }
Py_INCREF(_token_missing); return Py_NewRef(_token_missing);
return _token_missing;
} }

View File

@ -525,8 +525,7 @@ hamt_node_bitmap_new(Py_ssize_t size)
assert(size % 2 == 0); assert(size % 2 == 0);
if (size == 0 && _empty_bitmap_node != NULL) { if (size == 0 && _empty_bitmap_node != NULL) {
Py_INCREF(_empty_bitmap_node); return (PyHamtNode *)Py_NewRef(_empty_bitmap_node);
return (PyHamtNode *)_empty_bitmap_node;
} }
/* No freelist; allocate a new bitmap node */ /* No freelist; allocate a new bitmap node */
@ -550,8 +549,7 @@ hamt_node_bitmap_new(Py_ssize_t size)
/* Since bitmap nodes are immutable, we can cache the instance /* Since bitmap nodes are immutable, we can cache the instance
for size=0 and reuse it whenever we need an empty bitmap node. for size=0 and reuse it whenever we need an empty bitmap node.
*/ */
_empty_bitmap_node = node; _empty_bitmap_node = (PyHamtNode_Bitmap*)Py_NewRef(node);
Py_INCREF(_empty_bitmap_node);
} }
return (PyHamtNode *)node; return (PyHamtNode *)node;
@ -577,8 +575,7 @@ hamt_node_bitmap_clone(PyHamtNode_Bitmap *node)
} }
for (i = 0; i < Py_SIZE(node); i++) { for (i = 0; i < Py_SIZE(node); i++) {
Py_XINCREF(node->b_array[i]); clone->b_array[i] = Py_XNewRef(node->b_array[i]);
clone->b_array[i] = node->b_array[i];
} }
clone->b_bitmap = node->b_bitmap; clone->b_bitmap = node->b_bitmap;
@ -603,14 +600,12 @@ hamt_node_bitmap_clone_without(PyHamtNode_Bitmap *o, uint32_t bit)
uint32_t i; uint32_t i;
for (i = 0; i < key_idx; i++) { for (i = 0; i < key_idx; i++) {
Py_XINCREF(o->b_array[i]); new->b_array[i] = Py_XNewRef(o->b_array[i]);
new->b_array[i] = o->b_array[i];
} }
assert(Py_SIZE(o) >= 0 && Py_SIZE(o) <= 32); assert(Py_SIZE(o) >= 0 && Py_SIZE(o) <= 32);
for (i = val_idx + 1; i < (uint32_t)Py_SIZE(o); i++) { for (i = val_idx + 1; i < (uint32_t)Py_SIZE(o); i++) {
Py_XINCREF(o->b_array[i]); new->b_array[i - 2] = Py_XNewRef(o->b_array[i]);
new->b_array[i - 2] = o->b_array[i];
} }
new->b_bitmap = o->b_bitmap & ~bit; new->b_bitmap = o->b_bitmap & ~bit;
@ -643,15 +638,11 @@ hamt_node_new_bitmap_or_collision(uint32_t shift,
return NULL; return NULL;
} }
Py_INCREF(key1); n->c_array[0] = Py_NewRef(key1);
n->c_array[0] = key1; n->c_array[1] = Py_NewRef(val1);
Py_INCREF(val1);
n->c_array[1] = val1;
Py_INCREF(key2); n->c_array[2] = Py_NewRef(key2);
n->c_array[2] = key2; n->c_array[3] = Py_NewRef(val2);
Py_INCREF(val2);
n->c_array[3] = val2;
return (PyHamtNode *)n; return (PyHamtNode *)n;
} }
@ -736,8 +727,7 @@ hamt_node_bitmap_assoc(PyHamtNode_Bitmap *self,
if (val_or_node == (PyObject *)sub_node) { if (val_or_node == (PyObject *)sub_node) {
Py_DECREF(sub_node); Py_DECREF(sub_node);
Py_INCREF(self); return (PyHamtNode *)Py_NewRef(self);
return (PyHamtNode *)self;
} }
PyHamtNode_Bitmap *ret = hamt_node_bitmap_clone(self); PyHamtNode_Bitmap *ret = hamt_node_bitmap_clone(self);
@ -759,8 +749,7 @@ hamt_node_bitmap_assoc(PyHamtNode_Bitmap *self,
if (comp_err == 1) { /* key == key_or_null */ if (comp_err == 1) { /* key == key_or_null */
if (val == val_or_node) { if (val == val_or_node) {
/* we already have the same key/val pair; return self. */ /* we already have the same key/val pair; return self. */
Py_INCREF(self); return (PyHamtNode *)Py_NewRef(self);
return (PyHamtNode *)self;
} }
/* We're setting a new value for the key we had before. /* We're setting a new value for the key we had before.
@ -769,8 +758,7 @@ hamt_node_bitmap_assoc(PyHamtNode_Bitmap *self,
if (ret == NULL) { if (ret == NULL) {
return NULL; return NULL;
} }
Py_INCREF(val); Py_SETREF(ret->b_array[val_idx], Py_NewRef(val));
Py_SETREF(ret->b_array[val_idx], val);
return (PyHamtNode *)ret; return (PyHamtNode *)ret;
} }
@ -923,22 +911,18 @@ hamt_node_bitmap_assoc(PyHamtNode_Bitmap *self,
/* Copy all keys/values that will be before the new key/value /* Copy all keys/values that will be before the new key/value
we are adding. */ we are adding. */
for (i = 0; i < key_idx; i++) { for (i = 0; i < key_idx; i++) {
Py_XINCREF(self->b_array[i]); new_node->b_array[i] = Py_XNewRef(self->b_array[i]);
new_node->b_array[i] = self->b_array[i];
} }
/* Set the new key/value to the new Bitmap node. */ /* Set the new key/value to the new Bitmap node. */
Py_INCREF(key); new_node->b_array[key_idx] = Py_NewRef(key);
new_node->b_array[key_idx] = key; new_node->b_array[val_idx] = Py_NewRef(val);
Py_INCREF(val);
new_node->b_array[val_idx] = val;
/* Copy all keys/values that will be after the new key/value /* Copy all keys/values that will be after the new key/value
we are adding. */ we are adding. */
assert(Py_SIZE(self) >= 0 && Py_SIZE(self) <= 32); assert(Py_SIZE(self) >= 0 && Py_SIZE(self) <= 32);
for (i = key_idx; i < (uint32_t)Py_SIZE(self); i++) { for (i = key_idx; i < (uint32_t)Py_SIZE(self); i++) {
Py_XINCREF(self->b_array[i]); new_node->b_array[i + 2] = Py_XNewRef(self->b_array[i]);
new_node->b_array[i + 2] = self->b_array[i];
} }
new_node->b_bitmap = self->b_bitmap | bit; new_node->b_bitmap = self->b_bitmap | bit;
@ -1019,10 +1003,8 @@ hamt_node_bitmap_without(PyHamtNode_Bitmap *self,
PyObject *key = sub_tree->b_array[0]; PyObject *key = sub_tree->b_array[0];
PyObject *val = sub_tree->b_array[1]; PyObject *val = sub_tree->b_array[1];
Py_INCREF(key); Py_XSETREF(clone->b_array[key_idx], Py_NewRef(key));
Py_XSETREF(clone->b_array[key_idx], key); Py_SETREF(clone->b_array[val_idx], Py_NewRef(val));
Py_INCREF(val);
Py_SETREF(clone->b_array[val_idx], val);
Py_DECREF(sub_tree); Py_DECREF(sub_tree);
@ -1343,14 +1325,11 @@ hamt_node_collision_assoc(PyHamtNode_Collision *self,
} }
for (i = 0; i < Py_SIZE(self); i++) { for (i = 0; i < Py_SIZE(self); i++) {
Py_INCREF(self->c_array[i]); new_node->c_array[i] = Py_NewRef(self->c_array[i]);
new_node->c_array[i] = self->c_array[i];
} }
Py_INCREF(key); new_node->c_array[i] = Py_NewRef(key);
new_node->c_array[i] = key; new_node->c_array[i + 1] = Py_NewRef(val);
Py_INCREF(val);
new_node->c_array[i + 1] = val;
*added_leaf = 1; *added_leaf = 1;
return (PyHamtNode *)new_node; return (PyHamtNode *)new_node;
@ -1364,8 +1343,7 @@ hamt_node_collision_assoc(PyHamtNode_Collision *self,
if (self->c_array[val_idx] == val) { if (self->c_array[val_idx] == val) {
/* We're setting a key/value pair that's already set. */ /* We're setting a key/value pair that's already set. */
Py_INCREF(self); return (PyHamtNode *)Py_NewRef(self);
return (PyHamtNode *)self;
} }
/* We need to replace old value for the key /* We need to replace old value for the key
@ -1378,14 +1356,12 @@ hamt_node_collision_assoc(PyHamtNode_Collision *self,
/* Copy all elements of the old node to the new one. */ /* Copy all elements of the old node to the new one. */
for (i = 0; i < Py_SIZE(self); i++) { for (i = 0; i < Py_SIZE(self); i++) {
Py_INCREF(self->c_array[i]); new_node->c_array[i] = Py_NewRef(self->c_array[i]);
new_node->c_array[i] = self->c_array[i];
} }
/* Replace the old value with the new value for the our key. */ /* Replace the old value with the new value for the our key. */
Py_DECREF(new_node->c_array[val_idx]); Py_DECREF(new_node->c_array[val_idx]);
Py_INCREF(val); new_node->c_array[val_idx] = Py_NewRef(val);
new_node->c_array[val_idx] = val;
return (PyHamtNode *)new_node; return (PyHamtNode *)new_node;
@ -1410,8 +1386,7 @@ hamt_node_collision_assoc(PyHamtNode_Collision *self,
return NULL; return NULL;
} }
new_node->b_bitmap = hamt_bitpos(self->c_hash, shift); new_node->b_bitmap = hamt_bitpos(self->c_hash, shift);
Py_INCREF(self); new_node->b_array[1] = Py_NewRef(self);
new_node->b_array[1] = (PyObject*) self;
assoc_res = hamt_node_bitmap_assoc( assoc_res = hamt_node_bitmap_assoc(
new_node, shift, hash, key, val, added_leaf); new_node, shift, hash, key, val, added_leaf);
@ -1473,17 +1448,13 @@ hamt_node_collision_without(PyHamtNode_Collision *self,
} }
if (key_idx == 0) { if (key_idx == 0) {
Py_INCREF(self->c_array[2]); node->b_array[0] = Py_NewRef(self->c_array[2]);
node->b_array[0] = self->c_array[2]; node->b_array[1] = Py_NewRef(self->c_array[3]);
Py_INCREF(self->c_array[3]);
node->b_array[1] = self->c_array[3];
} }
else { else {
assert(key_idx == 2); assert(key_idx == 2);
Py_INCREF(self->c_array[0]); node->b_array[0] = Py_NewRef(self->c_array[0]);
node->b_array[0] = self->c_array[0]; node->b_array[1] = Py_NewRef(self->c_array[1]);
Py_INCREF(self->c_array[1]);
node->b_array[1] = self->c_array[1];
} }
node->b_bitmap = hamt_bitpos(hash, shift); node->b_bitmap = hamt_bitpos(hash, shift);
@ -1504,12 +1475,10 @@ hamt_node_collision_without(PyHamtNode_Collision *self,
/* Copy all other keys from `self` to `new` */ /* Copy all other keys from `self` to `new` */
Py_ssize_t i; Py_ssize_t i;
for (i = 0; i < key_idx; i++) { for (i = 0; i < key_idx; i++) {
Py_INCREF(self->c_array[i]); new->c_array[i] = Py_NewRef(self->c_array[i]);
new->c_array[i] = self->c_array[i];
} }
for (i = key_idx + 2; i < Py_SIZE(self); i++) { for (i = key_idx + 2; i < Py_SIZE(self); i++) {
Py_INCREF(self->c_array[i]); new->c_array[i - 2] = Py_NewRef(self->c_array[i]);
new->c_array[i - 2] = self->c_array[i];
} }
*new_node = (PyHamtNode*)new; *new_node = (PyHamtNode*)new;
@ -1661,8 +1630,7 @@ hamt_node_array_clone(PyHamtNode_Array *node)
/* Copy all elements from the current Array node to the new one. */ /* Copy all elements from the current Array node to the new one. */
for (i = 0; i < HAMT_ARRAY_NODE_SIZE; i++) { for (i = 0; i < HAMT_ARRAY_NODE_SIZE; i++) {
Py_XINCREF(node->a_array[i]); clone->a_array[i] = (PyHamtNode*)Py_XNewRef(node->a_array[i]);
clone->a_array[i] = node->a_array[i];
} }
VALIDATE_ARRAY_NODE(clone) VALIDATE_ARRAY_NODE(clone)
@ -1719,8 +1687,7 @@ hamt_node_array_assoc(PyHamtNode_Array *self,
/* Copy all elements from the current Array node to the /* Copy all elements from the current Array node to the
new one. */ new one. */
for (i = 0; i < HAMT_ARRAY_NODE_SIZE; i++) { for (i = 0; i < HAMT_ARRAY_NODE_SIZE; i++) {
Py_XINCREF(self->a_array[i]); new_node->a_array[i] = (PyHamtNode*)Py_XNewRef(self->a_array[i]);
new_node->a_array[i] = self->a_array[i];
} }
assert(new_node->a_array[idx] == NULL); assert(new_node->a_array[idx] == NULL);
@ -1868,15 +1835,12 @@ hamt_node_array_without(PyHamtNode_Array *self,
PyObject *key = child->b_array[0]; PyObject *key = child->b_array[0];
PyObject *val = child->b_array[1]; PyObject *val = child->b_array[1];
Py_INCREF(key); new->b_array[new_i] = Py_NewRef(key);
new->b_array[new_i] = key; new->b_array[new_i + 1] = Py_NewRef(val);
Py_INCREF(val);
new->b_array[new_i + 1] = val;
} }
else { else {
new->b_array[new_i] = NULL; new->b_array[new_i] = NULL;
Py_INCREF(node); new->b_array[new_i + 1] = Py_NewRef(node);
new->b_array[new_i + 1] = (PyObject*)node;
} }
} }
else { else {
@ -1894,8 +1858,7 @@ hamt_node_array_without(PyHamtNode_Array *self,
/* Just copy the node into our new Bitmap */ /* Just copy the node into our new Bitmap */
new->b_array[new_i] = NULL; new->b_array[new_i] = NULL;
Py_INCREF(node); new->b_array[new_i + 1] = Py_NewRef(node);
new->b_array[new_i + 1] = (PyObject*)node;
} }
new_i += 2; new_i += 2;
@ -2311,8 +2274,7 @@ _PyHamt_Assoc(PyHamtObject *o, PyObject *key, PyObject *val)
if (new_root == o->h_root) { if (new_root == o->h_root) {
Py_DECREF(new_root); Py_DECREF(new_root);
Py_INCREF(o); return (PyHamtObject*)Py_NewRef(o);
return o;
} }
new_o = hamt_alloc(); new_o = hamt_alloc();
@ -2348,8 +2310,7 @@ _PyHamt_Without(PyHamtObject *o, PyObject *key)
case W_EMPTY: case W_EMPTY:
return _PyHamt_New(); return _PyHamt_New();
case W_NOT_FOUND: case W_NOT_FOUND:
Py_INCREF(o); return (PyHamtObject*)Py_NewRef(o);
return o;
case W_NEWNODE: { case W_NEWNODE: {
assert(new_root != NULL); assert(new_root != NULL);
@ -2476,8 +2437,7 @@ _PyHamt_New(void)
if (_empty_hamt != NULL) { if (_empty_hamt != NULL) {
/* HAMT is an immutable object so we can easily cache an /* HAMT is an immutable object so we can easily cache an
empty instance. */ empty instance. */
Py_INCREF(_empty_hamt); return (PyHamtObject*)Py_NewRef(_empty_hamt);
return _empty_hamt;
} }
PyHamtObject *o = hamt_alloc(); PyHamtObject *o = hamt_alloc();
@ -2494,8 +2454,7 @@ _PyHamt_New(void)
o->h_count = 0; o->h_count = 0;
if (_empty_hamt == NULL) { if (_empty_hamt == NULL) {
Py_INCREF(o); _empty_hamt = (PyHamtObject*)Py_NewRef(o);
_empty_hamt = o;
} }
return o; return o;
@ -2591,8 +2550,7 @@ hamt_baseiter_new(PyTypeObject *type, binaryfunc yield, PyHamtObject *o)
return NULL; return NULL;
} }
Py_INCREF(o); it->hi_obj = (PyHamtObject*)Py_NewRef(o);
it->hi_obj = o;
it->hi_yield = yield; it->hi_yield = yield;
hamt_iterator_init(&it->hi_iter, o->h_root); hamt_iterator_init(&it->hi_iter, o->h_root);
@ -2648,8 +2606,7 @@ PyTypeObject _PyHamtKeys_Type = {
static PyObject * static PyObject *
hamt_iter_yield_keys(PyObject *key, PyObject *val) hamt_iter_yield_keys(PyObject *key, PyObject *val)
{ {
Py_INCREF(key); return Py_NewRef(key);
return key;
} }
PyObject * PyObject *
@ -2672,8 +2629,7 @@ PyTypeObject _PyHamtValues_Type = {
static PyObject * static PyObject *
hamt_iter_yield_values(PyObject *key, PyObject *val) hamt_iter_yield_values(PyObject *key, PyObject *val)
{ {
Py_INCREF(val); return Py_NewRef(val);
return val;
} }
PyObject * PyObject *
@ -2766,8 +2722,7 @@ hamt_tp_subscript(PyHamtObject *self, PyObject *key)
case F_ERROR: case F_ERROR:
return NULL; return NULL;
case F_FOUND: case F_FOUND:
Py_INCREF(val); return Py_NewRef(val);
return val;
case F_NOT_FOUND: case F_NOT_FOUND:
PyErr_SetObject(PyExc_KeyError, key); PyErr_SetObject(PyExc_KeyError, key);
return NULL; return NULL;
@ -2817,14 +2772,12 @@ hamt_py_get(PyHamtObject *self, PyObject *args)
case F_ERROR: case F_ERROR:
return NULL; return NULL;
case F_FOUND: case F_FOUND:
Py_INCREF(val); return Py_NewRef(val);
return val;
case F_NOT_FOUND: case F_NOT_FOUND:
if (def == NULL) { if (def == NULL) {
Py_RETURN_NONE; Py_RETURN_NONE;
} }
Py_INCREF(def); return Py_NewRef(def);
return def;
default: default:
Py_UNREACHABLE(); Py_UNREACHABLE();
} }

View File

@ -810,8 +810,7 @@ update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
if (PyUnicode_Compare(co->co_filename, oldname)) if (PyUnicode_Compare(co->co_filename, oldname))
return; return;
Py_INCREF(newname); Py_XSETREF(co->co_filename, Py_NewRef(newname));
Py_XSETREF(co->co_filename, newname);
constants = co->co_consts; constants = co->co_consts;
n = PyTuple_GET_SIZE(constants); n = PyTuple_GET_SIZE(constants);
@ -905,8 +904,7 @@ get_path_importer(PyThreadState *tstate, PyObject *path_importer_cache,
importer = PyDict_GetItemWithError(path_importer_cache, p); importer = PyDict_GetItemWithError(path_importer_cache, p);
if (importer != NULL || _PyErr_Occurred(tstate)) { if (importer != NULL || _PyErr_Occurred(tstate)) {
Py_XINCREF(importer); return Py_XNewRef(importer);
return importer;
} }
/* set path_importer_cache[p] to None to avoid recursion */ /* set path_importer_cache[p] to None to avoid recursion */
@ -1403,8 +1401,7 @@ PyImport_ImportFrozenModuleObject(PyObject *name)
} }
} }
else { else {
Py_INCREF(Py_None); origname = Py_NewRef(Py_None);
origname = Py_None;
} }
err = PyDict_SetItemString(d, "__origname__", origname); err = PyDict_SetItemString(d, "__origname__", origname);
Py_DECREF(origname); Py_DECREF(origname);
@ -1516,8 +1513,7 @@ remove_importlib_frames(PyThreadState *tstate)
if (in_importlib && if (in_importlib &&
(always_trim || (always_trim ||
_PyUnicode_EqualToASCIIString(code->co_name, remove_frames))) { _PyUnicode_EqualToASCIIString(code->co_name, remove_frames))) {
Py_XINCREF(next); Py_XSETREF(*outer_link, Py_XNewRef(next));
Py_XSETREF(*outer_link, next);
prev_link = outer_link; prev_link = outer_link;
} }
else { else {
@ -1813,8 +1809,7 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
_PyErr_SetString(tstate, PyExc_ValueError, "Empty module name"); _PyErr_SetString(tstate, PyExc_ValueError, "Empty module name");
goto error; goto error;
} }
abs_name = name; abs_name = Py_NewRef(name);
Py_INCREF(abs_name);
} }
mod = import_get_module(tstate, abs_name); mod = import_get_module(tstate, abs_name);
@ -1853,8 +1848,7 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
if (dot == -1) { if (dot == -1) {
/* No dot in module name, simple exit */ /* No dot in module name, simple exit */
final_mod = mod; final_mod = Py_NewRef(mod);
Py_INCREF(mod);
goto error; goto error;
} }
@ -1889,8 +1883,7 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
} }
} }
else { else {
final_mod = mod; final_mod = Py_NewRef(mod);
Py_INCREF(mod);
} }
} }
else { else {
@ -1905,8 +1898,7 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
mod, fromlist, interp->import_func, NULL); mod, fromlist, interp->import_func, NULL);
} }
else { else {
final_mod = mod; final_mod = Py_NewRef(mod);
Py_INCREF(mod);
} }
} }

View File

@ -49,8 +49,7 @@ PyMember_GetOne(const char *obj_addr, PyMemberDef *l)
break; break;
case T_STRING: case T_STRING:
if (*(char**)addr == NULL) { if (*(char**)addr == NULL) {
Py_INCREF(Py_None); v = Py_NewRef(Py_None);
v = Py_None;
} }
else else
v = PyUnicode_FromString(*(char**)addr); v = PyUnicode_FromString(*(char**)addr);
@ -85,8 +84,7 @@ PyMember_GetOne(const char *obj_addr, PyMemberDef *l)
v = PyLong_FromUnsignedLongLong(*(unsigned long long *)addr); v = PyLong_FromUnsignedLongLong(*(unsigned long long *)addr);
break; break;
case T_NONE: case T_NONE:
v = Py_None; v = Py_NewRef(Py_None);
Py_INCREF(v);
break; break;
default: default:
PyErr_SetString(PyExc_SystemError, "bad memberdescr type"); PyErr_SetString(PyExc_SystemError, "bad memberdescr type");
@ -247,9 +245,8 @@ PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
break; break;
case T_OBJECT: case T_OBJECT:
case T_OBJECT_EX: case T_OBJECT_EX:
Py_XINCREF(v);
oldv = *(PyObject **)addr; oldv = *(PyObject **)addr;
*(PyObject **)addr = v; *(PyObject **)addr = Py_XNewRef(v);
Py_XDECREF(oldv); Py_XDECREF(oldv);
break; break;
case T_CHAR: { case T_CHAR: {

View File

@ -838,8 +838,7 @@ sys_getdefaultencoding_impl(PyObject *module)
{ {
_Py_DECLARE_STR(utf_8, "utf-8"); _Py_DECLARE_STR(utf_8, "utf-8");
PyObject *ret = &_Py_STR(utf_8); PyObject *ret = &_Py_STR(utf_8);
Py_INCREF(ret); return Py_NewRef(ret);
return ret;
} }
/*[clinic input] /*[clinic input]
@ -1068,8 +1067,7 @@ sys_gettrace_impl(PyObject *module)
if (temp == NULL) if (temp == NULL)
temp = Py_None; temp = Py_None;
Py_INCREF(temp); return Py_NewRef(temp);
return temp;
} }
static PyObject * static PyObject *
@ -1142,8 +1140,7 @@ sys_getprofile_impl(PyObject *module)
if (temp == NULL) if (temp == NULL)
temp = Py_None; temp = Py_None;
Py_INCREF(temp); return Py_NewRef(temp);
return temp;
} }
@ -1368,11 +1365,8 @@ sys_get_asyncgen_hooks_impl(PyObject *module)
finalizer = Py_None; finalizer = Py_None;
} }
Py_INCREF(firstiter); PyStructSequence_SET_ITEM(res, 0, Py_NewRef(firstiter));
PyStructSequence_SET_ITEM(res, 0, firstiter); PyStructSequence_SET_ITEM(res, 1, Py_NewRef(finalizer));
Py_INCREF(finalizer);
PyStructSequence_SET_ITEM(res, 1, finalizer);
return res; return res;
} }
@ -1805,8 +1799,7 @@ sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds)
/* Has a default value been given */ /* Has a default value been given */
if (dflt != NULL && _PyErr_ExceptionMatches(tstate, PyExc_TypeError)) { if (dflt != NULL && _PyErr_ExceptionMatches(tstate, PyExc_TypeError)) {
_PyErr_Clear(tstate); _PyErr_Clear(tstate);
Py_INCREF(dflt); return Py_NewRef(dflt);
return dflt;
} }
else else
return NULL; return NULL;
@ -2572,8 +2565,7 @@ _PySys_AddXOptionWithError(const wchar_t *s)
const wchar_t *name_end = wcschr(s, L'='); const wchar_t *name_end = wcschr(s, L'=');
if (!name_end) { if (!name_end) {
name = PyUnicode_FromWideChar(s, -1); name = PyUnicode_FromWideChar(s, -1);
value = Py_True; value = Py_NewRef(Py_True);
Py_INCREF(value);
} }
else { else {
name = PyUnicode_FromWideChar(s, name_end - s); name = PyUnicode_FromWideChar(s, name_end - s);
@ -3028,8 +3020,7 @@ make_emscripten_info(void)
} }
PyStructSequence_SET_ITEM(emscripten_info, pos++, oua); PyStructSequence_SET_ITEM(emscripten_info, pos++, oua);
} else { } else {
Py_INCREF(Py_None); PyStructSequence_SET_ITEM(emscripten_info, pos++, Py_NewRef(Py_None));
PyStructSequence_SET_ITEM(emscripten_info, pos++, Py_None);
} }
#define SetBoolItem(flag) \ #define SetBoolItem(flag) \
@ -3226,8 +3217,7 @@ sys_add_xoption(PyObject *opts, const wchar_t *s)
const wchar_t *name_end = wcschr(s, L'='); const wchar_t *name_end = wcschr(s, L'=');
if (!name_end) { if (!name_end) {
name = PyUnicode_FromWideChar(s, -1); name = PyUnicode_FromWideChar(s, -1);
value = Py_True; value = Py_NewRef(Py_True);
Py_INCREF(value);
} }
else { else {
name = PyUnicode_FromWideChar(s, name_end - s); name = PyUnicode_FromWideChar(s, name_end - s);
@ -3404,8 +3394,7 @@ _PySys_Create(PyThreadState *tstate, PyObject **sysmod_p)
if (sysdict == NULL) { if (sysdict == NULL) {
goto error; goto error;
} }
Py_INCREF(sysdict); interp->sysdict = Py_NewRef(sysdict);
interp->sysdict = sysdict;
if (PyDict_SetItemString(sysdict, "modules", interp->modules) < 0) { if (PyDict_SetItemString(sysdict, "modules", interp->modules) < 0) {
goto error; goto error;

View File

@ -52,10 +52,8 @@ tb_create_raw(PyTracebackObject *next, PyFrameObject *frame, int lasti,
} }
tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type); tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type);
if (tb != NULL) { if (tb != NULL) {
Py_XINCREF(next); tb->tb_next = (PyTracebackObject*)Py_XNewRef(next);
tb->tb_next = next; tb->tb_frame = (PyFrameObject*)Py_XNewRef(frame);
Py_XINCREF(frame);
tb->tb_frame = frame;
tb->tb_lasti = lasti; tb->tb_lasti = lasti;
tb->tb_lineno = lineno; tb->tb_lineno = lineno;
PyObject_GC_Track(tb); PyObject_GC_Track(tb);
@ -106,8 +104,7 @@ tb_next_get(PyTracebackObject *self, void *Py_UNUSED(_))
if (!ret) { if (!ret) {
ret = Py_None; ret = Py_None;
} }
Py_INCREF(ret); return Py_NewRef(ret);
return ret;
} }
static int static int
@ -140,8 +137,7 @@ tb_next_set(PyTracebackObject *self, PyObject *new_next, void *Py_UNUSED(_))
} }
PyObject *old_next = (PyObject*)self->tb_next; PyObject *old_next = (PyObject*)self->tb_next;
Py_XINCREF(new_next); self->tb_next = (PyTracebackObject *)Py_XNewRef(new_next);
self->tb_next = (PyTracebackObject *)new_next;
Py_XDECREF(old_next); Py_XDECREF(old_next);
return 0; return 0;
@ -522,8 +518,7 @@ display_source_line_with_margin(PyObject *f, PyObject *filename, int lineno, int
} }
if (line) { if (line) {
Py_INCREF(lineobj); *line = Py_NewRef(lineobj);
*line = lineobj;
} }
/* remove the indentation of the line */ /* remove the indentation of the line */