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

Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Python/ directory.
This commit is contained in:
Victor Stinner 2022-11-10 09:03:39 +01:00 committed by GitHub
parent f883b7f8ee
commit d8f239d86e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 41 additions and 80 deletions

View File

@ -382,8 +382,7 @@ get_filter(PyInterpreterState *interp, PyObject *category,
action = get_default_action(interp); action = get_default_action(interp);
if (action != NULL) { if (action != NULL) {
Py_INCREF(Py_None); *item = Py_NewRef(Py_None);
*item = Py_None;
return action; return action;
} }
@ -468,8 +467,7 @@ normalize_module(PyObject *filename)
module = PyUnicode_Substring(filename, 0, len-3); module = PyUnicode_Substring(filename, 0, len-3);
} }
else { else {
module = filename; module = Py_NewRef(filename);
Py_INCREF(module);
} }
return module; return module;
} }
@ -751,8 +749,7 @@ warn_explicit(PyThreadState *tstate, PyObject *category, PyObject *message,
goto cleanup; goto cleanup;
return_none: return_none:
result = Py_None; result = Py_NewRef(Py_None);
Py_INCREF(result);
cleanup: cleanup:
Py_XDECREF(item); Py_XDECREF(item);
@ -848,8 +845,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
} }
else { else {
globals = f->f_frame->f_globals; globals = f->f_frame->f_globals;
*filename = f->f_frame->f_code->co_filename; *filename = Py_NewRef(f->f_frame->f_code->co_filename);
Py_INCREF(*filename);
*lineno = PyFrame_GetLineNumber(f); *lineno = PyFrame_GetLineNumber(f);
Py_DECREF(f); Py_DECREF(f);
} }

View File

@ -533,8 +533,7 @@ make_const_tuple(asdl_expr_seq *elts)
for (int i = 0; i < asdl_seq_LEN(elts); i++) { for (int i = 0; i < asdl_seq_LEN(elts); i++) {
expr_ty e = (expr_ty)asdl_seq_GET(elts, i); expr_ty e = (expr_ty)asdl_seq_GET(elts, i);
PyObject *v = e->v.Constant.value; PyObject *v = e->v.Constant.value;
Py_INCREF(v); PyTuple_SET_ITEM(newval, i, Py_NewRef(v));
PyTuple_SET_ITEM(newval, i, v);
} }
return newval; return newval;
} }

View File

@ -235,8 +235,7 @@ PyObject *args_tuple(PyObject *object,
args = PyTuple_New(1 + (errors != NULL)); args = PyTuple_New(1 + (errors != NULL));
if (args == NULL) if (args == NULL)
return NULL; return NULL;
Py_INCREF(object); PyTuple_SET_ITEM(args, 0, Py_NewRef(object));
PyTuple_SET_ITEM(args,0,object);
if (errors) { if (errors) {
PyObject *v; PyObject *v;
@ -263,8 +262,7 @@ PyObject *codec_getitem(const char *encoding, int index)
return NULL; return NULL;
v = PyTuple_GET_ITEM(codecs, index); v = PyTuple_GET_ITEM(codecs, index);
Py_DECREF(codecs); Py_DECREF(codecs);
Py_INCREF(v); return Py_NewRef(v);
return v;
} }
/* Helper functions to create an incremental codec. */ /* Helper functions to create an incremental codec. */

View File

@ -326,8 +326,7 @@ _PyErr_NormalizeException(PyThreadState *tstate, PyObject **exc,
set to NULL. set to NULL.
*/ */
if (!value) { if (!value) {
value = Py_None; value = Py_NewRef(Py_None);
Py_INCREF(value);
} }
/* Normalize the exception so that if the type is a class, the /* Normalize the exception so that if the type is a class, the
@ -1287,8 +1286,7 @@ make_unraisable_hook_args(PyThreadState *tstate, PyObject *exc_type,
if (exc_type == NULL) { \ if (exc_type == NULL) { \
exc_type = Py_None; \ exc_type = Py_None; \
} \ } \
Py_INCREF(exc_type); \ PyStructSequence_SET_ITEM(args, pos++, Py_NewRef(exc_type)); \
PyStructSequence_SET_ITEM(args, pos++, exc_type); \
} while (0) } while (0)

View File

@ -1046,8 +1046,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
/* Encode object */ /* Encode object */
if (!recode_strings && if (!recode_strings &&
(PyBytes_Check(arg) || PyByteArray_Check(arg))) { (PyBytes_Check(arg) || PyByteArray_Check(arg))) {
s = arg; s = Py_NewRef(arg);
Py_INCREF(s);
if (PyBytes_Check(arg)) { if (PyBytes_Check(arg)) {
size = PyBytes_GET_SIZE(s); size = PyBytes_GET_SIZE(s);
ptr = PyBytes_AS_STRING(s); ptr = PyBytes_AS_STRING(s);
@ -2575,8 +2574,7 @@ _PyArg_UnpackKeywordsWithVararg(PyObject *const *args, Py_ssize_t nargs,
/* copy tuple args */ /* copy tuple args */
for (i = 0; i < nargs; i++) { for (i = 0; i < nargs; i++) {
if (i >= vararg) { if (i >= vararg) {
Py_INCREF(args[i]); PyTuple_SET_ITEM(buf[vararg], i - vararg, Py_NewRef(args[i]));
PyTuple_SET_ITEM(buf[vararg], i - vararg, args[i]);
continue; continue;
} }
else { else {

View File

@ -241,7 +241,7 @@ _Py_COMP_DIAG_IGNORE_DEPR_DECLS
#define FROM_STRING(STR) \ #define FROM_STRING(STR) \
((STR != NULL) ? \ ((STR != NULL) ? \
PyUnicode_FromString(STR) \ PyUnicode_FromString(STR) \
: (Py_INCREF(Py_None), Py_None)) : Py_NewRef(Py_None))
#define SET_ITEM_STR(VAR) \ #define SET_ITEM_STR(VAR) \
SET_ITEM(#VAR, FROM_STRING(VAR)) SET_ITEM(#VAR, FROM_STRING(VAR))
@ -1054,7 +1054,7 @@ _PyConfig_AsDict(const PyConfig *config)
#define FROM_WSTRING(STR) \ #define FROM_WSTRING(STR) \
((STR != NULL) ? \ ((STR != NULL) ? \
PyUnicode_FromWideChar(STR, -1) \ PyUnicode_FromWideChar(STR, -1) \
: (Py_INCREF(Py_None), Py_None)) : Py_NewRef(Py_None))
#define SET_ITEM_WSTR(ATTR) \ #define SET_ITEM_WSTR(ATTR) \
SET_ITEM(#ATTR, FROM_WSTRING(config->ATTR)) SET_ITEM(#ATTR, FROM_WSTRING(config->ATTR))
#define SET_ITEM_WSTRLIST(LIST) \ #define SET_ITEM_WSTRLIST(LIST) \

View File

@ -951,8 +951,7 @@ r_ref_insert(PyObject *o, Py_ssize_t idx, int flag, RFILE *p)
{ {
if (o != NULL && flag) { /* currently only FLAG_REF is defined */ if (o != NULL && flag) { /* currently only FLAG_REF is defined */
PyObject *tmp = PyList_GET_ITEM(p->refs, idx); PyObject *tmp = PyList_GET_ITEM(p->refs, idx);
Py_INCREF(o); PyList_SET_ITEM(p->refs, idx, Py_NewRef(o));
PyList_SET_ITEM(p->refs, idx, o);
Py_DECREF(tmp); Py_DECREF(tmp);
} }
return o; return o;
@ -1015,28 +1014,23 @@ r_object(RFILE *p)
break; break;
case TYPE_NONE: case TYPE_NONE:
Py_INCREF(Py_None); retval = Py_NewRef(Py_None);
retval = Py_None;
break; break;
case TYPE_STOPITER: case TYPE_STOPITER:
Py_INCREF(PyExc_StopIteration); retval = Py_NewRef(PyExc_StopIteration);
retval = PyExc_StopIteration;
break; break;
case TYPE_ELLIPSIS: case TYPE_ELLIPSIS:
Py_INCREF(Py_Ellipsis); retval = Py_NewRef(Py_Ellipsis);
retval = Py_Ellipsis;
break; break;
case TYPE_FALSE: case TYPE_FALSE:
Py_INCREF(Py_False); retval = Py_NewRef(Py_False);
retval = Py_False;
break; break;
case TYPE_TRUE: case TYPE_TRUE:
Py_INCREF(Py_True); retval = Py_NewRef(Py_True);
retval = Py_True;
break; break;
case TYPE_INT: case TYPE_INT:
@ -1486,8 +1480,7 @@ r_object(RFILE *p)
PyErr_SetString(PyExc_ValueError, "bad marshal data (invalid reference)"); PyErr_SetString(PyExc_ValueError, "bad marshal data (invalid reference)");
break; break;
} }
Py_INCREF(v); retval = Py_NewRef(v);
retval = v;
break; break;
default: default:

View File

@ -359,8 +359,7 @@ do_mkvalue(const char **p_format, va_list *p_va, int flags)
else else
n = -1; n = -1;
if (u == NULL) { if (u == NULL) {
v = Py_None; v = Py_NewRef(Py_None);
Py_INCREF(v);
} }
else { else {
if (n < 0) if (n < 0)
@ -410,8 +409,7 @@ do_mkvalue(const char **p_format, va_list *p_va, int flags)
else else
n = -1; n = -1;
if (str == NULL) { if (str == NULL) {
v = Py_None; v = Py_NewRef(Py_None);
Py_INCREF(v);
} }
else { else {
if (n < 0) { if (n < 0) {
@ -446,8 +444,7 @@ do_mkvalue(const char **p_format, va_list *p_va, int flags)
else else
n = -1; n = -1;
if (str == NULL) { if (str == NULL) {
v = Py_None; v = Py_NewRef(Py_None);
Py_INCREF(v);
} }
else { else {
if (n < 0) { if (n < 0) {

View File

@ -798,8 +798,7 @@ pycore_init_builtins(PyThreadState *tstate)
if (builtins_dict == NULL) { if (builtins_dict == NULL) {
goto error; goto error;
} }
Py_INCREF(builtins_dict); interp->builtins = Py_NewRef(builtins_dict);
interp->builtins = builtins_dict;
PyObject *isinstance = PyDict_GetItem(builtins_dict, &_Py_ID(isinstance)); PyObject *isinstance = PyDict_GetItem(builtins_dict, &_Py_ID(isinstance));
assert(isinstance); assert(isinstance);
@ -2289,8 +2288,7 @@ create_stdio(const PyConfig *config, PyObject* io,
goto error; goto error;
} }
else { else {
raw = buf; raw = Py_NewRef(buf);
Py_INCREF(raw);
} }
#ifdef MS_WINDOWS #ifdef MS_WINDOWS
@ -2552,8 +2550,7 @@ _Py_FatalError_PrintExc(PyThreadState *tstate)
_PyErr_NormalizeException(tstate, &exception, &v, &tb); _PyErr_NormalizeException(tstate, &exception, &v, &tb);
if (tb == NULL) { if (tb == NULL) {
tb = Py_None; tb = Py_NewRef(Py_None);
Py_INCREF(tb);
} }
PyException_SetTraceback(v, tb); PyException_SetTraceback(v, tb);
if (exception == NULL) { if (exception == NULL) {

View File

@ -944,9 +944,8 @@ _PyState_AddModule(PyThreadState *tstate, PyObject* module, PyModuleDef* def)
} }
} }
Py_INCREF(module);
return PyList_SetItem(interp->modules_by_index, return PyList_SetItem(interp->modules_by_index,
def->m_base.m_index, module); def->m_base.m_index, Py_NewRef(module));
} }
int int
@ -994,8 +993,7 @@ PyState_RemoveModule(PyModuleDef* def)
Py_FatalError("Module index out of bounds."); Py_FatalError("Module index out of bounds.");
} }
Py_INCREF(Py_None); return PyList_SetItem(interp->modules_by_index, index, Py_NewRef(Py_None));
return PyList_SetItem(interp->modules_by_index, index, Py_None);
} }
// Used by finalize_modules() // Used by finalize_modules()
@ -1299,8 +1297,7 @@ PyThreadState_GetFrame(PyThreadState *tstate)
if (frame == NULL) { if (frame == NULL) {
PyErr_Clear(); PyErr_Clear();
} }
Py_XINCREF(frame); return (PyFrameObject*)Py_XNewRef(frame);
return frame;
} }
@ -1346,8 +1343,7 @@ PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
* the decref. * the decref.
*/ */
PyObject *old_exc = tstate->async_exc; PyObject *old_exc = tstate->async_exc;
Py_XINCREF(exc); tstate->async_exc = Py_XNewRef(exc);
tstate->async_exc = exc;
HEAD_UNLOCK(runtime); HEAD_UNLOCK(runtime);
Py_XDECREF(old_exc); Py_XDECREF(old_exc);
@ -2027,8 +2023,7 @@ _bytes_shared(PyObject *obj, _PyCrossInterpreterData *data)
return -1; return -1;
} }
data->data = (void *)shared; data->data = (void *)shared;
Py_INCREF(obj); data->obj = Py_NewRef(obj); // Will be "released" (decref'ed) when data released.
data->obj = obj; // Will be "released" (decref'ed) when data released.
data->new_object = _new_bytes_object; data->new_object = _new_bytes_object;
data->free = PyMem_Free; data->free = PyMem_Free;
return 0; return 0;
@ -2055,8 +2050,7 @@ _str_shared(PyObject *obj, _PyCrossInterpreterData *data)
shared->buffer = PyUnicode_DATA(obj); shared->buffer = PyUnicode_DATA(obj);
shared->len = PyUnicode_GET_LENGTH(obj); shared->len = PyUnicode_GET_LENGTH(obj);
data->data = (void *)shared; data->data = (void *)shared;
Py_INCREF(obj); data->obj = Py_NewRef(obj); // Will be "released" (decref'ed) when data released.
data->obj = obj; // Will be "released" (decref'ed) when data released.
data->new_object = _new_str_object; data->new_object = _new_str_object;
data->free = PyMem_Free; data->free = PyMem_Free;
return 0; return 0;
@ -2093,8 +2087,7 @@ static PyObject *
_new_none_object(_PyCrossInterpreterData *data) _new_none_object(_PyCrossInterpreterData *data)
{ {
// XXX Singleton refcounts are problematic across interpreters... // XXX Singleton refcounts are problematic across interpreters...
Py_INCREF(Py_None); return Py_NewRef(Py_None);
return Py_None;
} }
static int static int

View File

@ -786,8 +786,7 @@ _PyErr_PrintEx(PyThreadState *tstate, int set_sys_last_vars)
_PyErr_NormalizeException(tstate, &exception, &v, &tb); _PyErr_NormalizeException(tstate, &exception, &v, &tb);
if (tb == NULL) { if (tb == NULL) {
tb = Py_None; tb = Py_NewRef(Py_None);
Py_INCREF(tb);
} }
PyException_SetTraceback(v, tb); PyException_SetTraceback(v, tb);
if (exception == NULL) { if (exception == NULL) {
@ -833,12 +832,10 @@ _PyErr_PrintEx(PyThreadState *tstate, int set_sys_last_vars)
to be NULL. However PyErr_Display() can't to be NULL. However PyErr_Display() can't
tolerate NULLs, so just be safe. */ tolerate NULLs, so just be safe. */
if (exception2 == NULL) { if (exception2 == NULL) {
exception2 = Py_None; exception2 = Py_NewRef(Py_None);
Py_INCREF(exception2);
} }
if (v2 == NULL) { if (v2 == NULL) {
v2 = Py_None; v2 = Py_NewRef(Py_None);
Py_INCREF(v2);
} }
fflush(stdout); fflush(stdout);
PySys_WriteStderr("Error in sys.excepthook:\n"); PySys_WriteStderr("Error in sys.excepthook:\n");

View File

@ -173,8 +173,7 @@ calculate_suggestions(PyObject *dir,
suggestion_distance = current_distance; suggestion_distance = current_distance;
} }
} }
Py_XINCREF(suggestion); return Py_XNewRef(suggestion);
return suggestion;
} }
static PyObject * static PyObject *

View File

@ -74,8 +74,7 @@ ste_new(struct symtable *st, identifier name, _Py_block_ty block,
ste->ste_table = st; ste->ste_table = st;
ste->ste_id = k; /* ste owns reference to k */ ste->ste_id = k; /* ste owns reference to k */
Py_INCREF(name); ste->ste_name = Py_NewRef(name);
ste->ste_name = name;
ste->ste_symbols = NULL; ste->ste_symbols = NULL;
ste->ste_varnames = NULL; ste->ste_varnames = NULL;
@ -286,8 +285,7 @@ _PySymtable_Build(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
_PySymtable_Free(st); _PySymtable_Free(st);
return NULL; return NULL;
} }
Py_INCREF(filename); st->st_filename = Py_NewRef(filename);
st->st_filename = filename;
st->st_future = future; st->st_future = future;
/* Setup recursion depth check counters */ /* Setup recursion depth check counters */
@ -1949,8 +1947,7 @@ symtable_visit_alias(struct symtable *st, alias_ty a)
return 0; return 0;
} }
else { else {
store_name = name; store_name = Py_NewRef(name);
Py_INCREF(store_name);
} }
if (!_PyUnicode_EqualToASCIIString(name, "*")) { if (!_PyUnicode_EqualToASCIIString(name, "*")) {
int r = symtable_add_def(st, store_name, DEF_IMPORT, LOCATION(a)); int r = symtable_add_def(st, store_name, DEF_IMPORT, LOCATION(a));

View File

@ -207,8 +207,7 @@ PyThread_GetInfo(void)
if (value == NULL) if (value == NULL)
#endif #endif
{ {
Py_INCREF(Py_None); value = Py_NewRef(Py_None);
value = Py_None;
} }
PyStructSequence_SET_ITEM(threadinfo, pos++, value); PyStructSequence_SET_ITEM(threadinfo, pos++, value);
return threadinfo; return threadinfo;