diff options
author | Victor Stinner <vstinner@python.org> | 2022-11-16 17:34:24 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-16 17:34:24 (GMT) |
commit | 8211cf5d287acfd815b6a7f6471cdf83dcd2bb9b (patch) | |
tree | 510409237622aa30dfc6833602bdde97c578f2bb /Python | |
parent | 19c1462e8dca3319c8290e2edcce482bd18cb018 (diff) | |
download | cpython-8211cf5d287acfd815b6a7f6471cdf83dcd2bb9b.zip cpython-8211cf5d287acfd815b6a7f6471cdf83dcd2bb9b.tar.gz cpython-8211cf5d287acfd815b6a7f6471cdf83dcd2bb9b.tar.bz2 |
gh-99300: Replace Py_INCREF() with Py_NewRef() (#99530)
Replace Py_INCREF() and Py_XINCREF() using a cast with Py_NewRef()
and Py_XNewRef().
Diffstat (limited to 'Python')
-rw-r--r-- | Python/bltinmodule.c | 3 | ||||
-rw-r--r-- | Python/codecs.c | 9 | ||||
-rw-r--r-- | Python/compile.c | 6 | ||||
-rw-r--r-- | Python/errors.c | 23 | ||||
-rw-r--r-- | Python/hamt.c | 3 | ||||
-rw-r--r-- | Python/import.c | 3 | ||||
-rw-r--r-- | Python/marshal.c | 4 | ||||
-rw-r--r-- | Python/pythonrun.c | 3 | ||||
-rw-r--r-- | Python/structmember.c | 2 | ||||
-rw-r--r-- | Python/symtable.c | 6 |
10 files changed, 23 insertions, 39 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index b23f52e..119e21a 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -2402,8 +2402,7 @@ builtin_vars(PyObject *self, PyObject *args) if (!PyArg_UnpackTuple(args, "vars", 0, 1, &v)) return NULL; if (v == NULL) { - d = PyEval_GetLocals(); - Py_XINCREF(d); + d = Py_XNewRef(PyEval_GetLocals()); } else { if (_PyObject_LookupAttr(v, &_Py_ID(__dict__), &d) == 0) { diff --git a/Python/codecs.c b/Python/codecs.c index 64addf0..b2087b49 100644 --- a/Python/codecs.c +++ b/Python/codecs.c @@ -428,8 +428,7 @@ _PyCodec_EncodeInternal(PyObject *object, "encoder must return a tuple (object, integer)"); goto onError; } - v = PyTuple_GET_ITEM(result,0); - Py_INCREF(v); + v = Py_NewRef(PyTuple_GET_ITEM(result,0)); /* We don't check or use the second (integer) entry. */ Py_DECREF(args); @@ -473,8 +472,7 @@ _PyCodec_DecodeInternal(PyObject *object, "decoder must return a tuple (object,integer)"); goto onError; } - v = PyTuple_GET_ITEM(result,0); - Py_INCREF(v); + v = Py_NewRef(PyTuple_GET_ITEM(result,0)); /* We don't check or use the second (integer) entry. */ Py_DECREF(args); @@ -569,8 +567,7 @@ PyObject *codec_getitem_checked(const char *encoding, if (codec == NULL) return NULL; - v = PyTuple_GET_ITEM(codec, index); - Py_INCREF(v); + v = Py_NewRef(PyTuple_GET_ITEM(codec, index)); Py_DECREF(codec); return v; } diff --git a/Python/compile.c b/Python/compile.c index bd41ebc..9226bc2 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1459,8 +1459,7 @@ merge_consts_recursive(PyObject *const_cache, PyObject *o) } PyObject *u; if (PyTuple_CheckExact(k)) { - u = PyTuple_GET_ITEM(k, 1); - Py_INCREF(u); + u = Py_NewRef(PyTuple_GET_ITEM(k, 1)); Py_DECREF(k); } else { @@ -2732,8 +2731,7 @@ compiler_class(struct compiler *c, stmt_ty s) { location loc = LOCATION(firstlineno, firstlineno, 0, 0); /* use the class name for name mangling */ - Py_INCREF(s->v.ClassDef.name); - Py_XSETREF(c->u->u_private, s->v.ClassDef.name); + Py_XSETREF(c->u->u_private, Py_NewRef(s->v.ClassDef.name)); /* load (global) __name__ ... */ if (!compiler_nameop(c, loc, &_Py_ID(__name__), Load)) { compiler_exit_scope(c); diff --git a/Python/errors.c b/Python/errors.c index 7d2dd9b..d74ac34 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -178,8 +178,7 @@ _PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value) } if (value != NULL && PyExceptionInstance_Check(value)) tb = PyException_GetTraceback(value); - Py_XINCREF(exception); - _PyErr_Restore(tstate, exception, value, tb); + _PyErr_Restore(tstate, Py_XNewRef(exception), value, tb); } void @@ -489,13 +488,9 @@ _PyErr_GetExcInfo(PyThreadState *tstate, { _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate); - *p_type = get_exc_type(exc_info->exc_value); - *p_value = exc_info->exc_value; - *p_traceback = get_exc_traceback(exc_info->exc_value); - - Py_XINCREF(*p_type); - Py_XINCREF(*p_value); - Py_XINCREF(*p_traceback); + *p_type = Py_XNewRef(get_exc_type(exc_info->exc_value)); + *p_value = Py_XNewRef(exc_info->exc_value); + *p_traceback = Py_XNewRef(get_exc_traceback(exc_info->exc_value)); } PyObject* @@ -674,9 +669,9 @@ _PyErr_FormatVFromCause(PyThreadState *tstate, PyObject *exception, _PyErr_Fetch(tstate, &exc, &val2, &tb); _PyErr_NormalizeException(tstate, &exc, &val2, &tb); - Py_INCREF(val); - PyException_SetCause(val2, val); - PyException_SetContext(val2, val); + PyException_SetCause(val2, Py_NewRef(val)); + PyException_SetContext(val2, Py_NewRef(val)); + Py_DECREF(val); _PyErr_Restore(tstate, exc, val2, tb); return NULL; @@ -1165,9 +1160,7 @@ PyErr_NewException(const char *name, PyObject *base, PyObject *dict) goto failure; } if (PyTuple_Check(base)) { - bases = base; - /* INCREF as we create a new ref in the else branch */ - Py_INCREF(bases); + bases = Py_NewRef(base); } else { bases = PyTuple_Pack(1, base); if (bases == NULL) diff --git a/Python/hamt.c b/Python/hamt.c index 4e61a1f..c4e47eb 100644 --- a/Python/hamt.c +++ b/Python/hamt.c @@ -838,8 +838,7 @@ hamt_node_bitmap_assoc(PyHamtNode_Bitmap *self, if (self->b_array[j] == NULL) { new_node->a_array[i] = - (PyHamtNode *)self->b_array[j + 1]; - Py_INCREF(new_node->a_array[i]); + (PyHamtNode *)Py_NewRef(self->b_array[j + 1]); } else { int32_t rehash = hamt_hash(self->b_array[j]); diff --git a/Python/import.c b/Python/import.c index c12b3e0..ab42672 100644 --- a/Python/import.c +++ b/Python/import.c @@ -625,8 +625,7 @@ import_add_module(PyThreadState *tstate, PyObject *name) PyObject *m; if (PyDict_CheckExact(modules)) { - m = PyDict_GetItemWithError(modules, name); - Py_XINCREF(m); + m = Py_XNewRef(PyDict_GetItemWithError(modules, name)); } else { m = PyObject_GetItem(modules, name); diff --git a/Python/marshal.c b/Python/marshal.c index d6cc04d..86cf57d 100644 --- a/Python/marshal.c +++ b/Python/marshal.c @@ -326,8 +326,8 @@ w_ref(PyObject *v, char *flag, WFILE *p) goto err; } w = (int)s; - Py_INCREF(v); - if (_Py_hashtable_set(p->hashtable, v, (void *)(uintptr_t)w) < 0) { + if (_Py_hashtable_set(p->hashtable, Py_NewRef(v), + (void *)(uintptr_t)w) < 0) { Py_DECREF(v); goto err; } diff --git a/Python/pythonrun.c b/Python/pythonrun.c index eead707..7087222 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -515,8 +515,7 @@ parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename, if (v == Py_None) { Py_DECREF(v); _Py_DECLARE_STR(anon_string, "<string>"); - *filename = &_Py_STR(anon_string); - Py_INCREF(*filename); + *filename = Py_NewRef(&_Py_STR(anon_string)); } else { *filename = v; diff --git a/Python/structmember.c b/Python/structmember.c index b94f512..1b8be28 100644 --- a/Python/structmember.c +++ b/Python/structmember.c @@ -74,7 +74,7 @@ PyMember_GetOne(const char *obj_addr, PyMemberDef *l) PyErr_Format(PyExc_AttributeError, "'%.200s' object has no attribute '%s'", tp->tp_name, l->name); - } + } Py_XINCREF(v); break; case T_LONGLONG: diff --git a/Python/symtable.c b/Python/symtable.c index 9bb7ffa..fb2bb7d 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -373,17 +373,17 @@ PySymtable_Lookup(struct symtable *st, void *key) if (k == NULL) return NULL; v = PyDict_GetItemWithError(st->st_blocks, k); + Py_DECREF(k); + if (v) { assert(PySTEntry_Check(v)); - Py_INCREF(v); } else if (!PyErr_Occurred()) { PyErr_SetString(PyExc_KeyError, "unknown symbol table entry"); } - Py_DECREF(k); - return (PySTEntryObject *)v; + return (PySTEntryObject *)Py_XNewRef(v); } long |