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);
if (action != NULL) {
Py_INCREF(Py_None);
*item = Py_None;
*item = Py_NewRef(Py_None);
return action;
}
@ -468,8 +467,7 @@ normalize_module(PyObject *filename)
module = PyUnicode_Substring(filename, 0, len-3);
}
else {
module = filename;
Py_INCREF(module);
module = Py_NewRef(filename);
}
return module;
}
@ -751,8 +749,7 @@ warn_explicit(PyThreadState *tstate, PyObject *category, PyObject *message,
goto cleanup;
return_none:
result = Py_None;
Py_INCREF(result);
result = Py_NewRef(Py_None);
cleanup:
Py_XDECREF(item);
@ -848,8 +845,7 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno,
}
else {
globals = f->f_frame->f_globals;
*filename = f->f_frame->f_code->co_filename;
Py_INCREF(*filename);
*filename = Py_NewRef(f->f_frame->f_code->co_filename);
*lineno = PyFrame_GetLineNumber(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++) {
expr_ty e = (expr_ty)asdl_seq_GET(elts, i);
PyObject *v = e->v.Constant.value;
Py_INCREF(v);
PyTuple_SET_ITEM(newval, i, v);
PyTuple_SET_ITEM(newval, i, Py_NewRef(v));
}
return newval;
}

View File

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

View File

@ -326,8 +326,7 @@ _PyErr_NormalizeException(PyThreadState *tstate, PyObject **exc,
set to NULL.
*/
if (!value) {
value = Py_None;
Py_INCREF(value);
value = Py_NewRef(Py_None);
}
/* 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) { \
exc_type = Py_None; \
} \
Py_INCREF(exc_type); \
PyStructSequence_SET_ITEM(args, pos++, exc_type); \
PyStructSequence_SET_ITEM(args, pos++, Py_NewRef(exc_type)); \
} while (0)

View File

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

View File

@ -241,7 +241,7 @@ _Py_COMP_DIAG_IGNORE_DEPR_DECLS
#define FROM_STRING(STR) \
((STR != NULL) ? \
PyUnicode_FromString(STR) \
: (Py_INCREF(Py_None), Py_None))
: Py_NewRef(Py_None))
#define SET_ITEM_STR(VAR) \
SET_ITEM(#VAR, FROM_STRING(VAR))
@ -1054,7 +1054,7 @@ _PyConfig_AsDict(const PyConfig *config)
#define FROM_WSTRING(STR) \
((STR != NULL) ? \
PyUnicode_FromWideChar(STR, -1) \
: (Py_INCREF(Py_None), Py_None))
: Py_NewRef(Py_None))
#define SET_ITEM_WSTR(ATTR) \
SET_ITEM(#ATTR, FROM_WSTRING(config->ATTR))
#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 */
PyObject *tmp = PyList_GET_ITEM(p->refs, idx);
Py_INCREF(o);
PyList_SET_ITEM(p->refs, idx, o);
PyList_SET_ITEM(p->refs, idx, Py_NewRef(o));
Py_DECREF(tmp);
}
return o;
@ -1015,28 +1014,23 @@ r_object(RFILE *p)
break;
case TYPE_NONE:
Py_INCREF(Py_None);
retval = Py_None;
retval = Py_NewRef(Py_None);
break;
case TYPE_STOPITER:
Py_INCREF(PyExc_StopIteration);
retval = PyExc_StopIteration;
retval = Py_NewRef(PyExc_StopIteration);
break;
case TYPE_ELLIPSIS:
Py_INCREF(Py_Ellipsis);
retval = Py_Ellipsis;
retval = Py_NewRef(Py_Ellipsis);
break;
case TYPE_FALSE:
Py_INCREF(Py_False);
retval = Py_False;
retval = Py_NewRef(Py_False);
break;
case TYPE_TRUE:
Py_INCREF(Py_True);
retval = Py_True;
retval = Py_NewRef(Py_True);
break;
case TYPE_INT:
@ -1486,8 +1480,7 @@ r_object(RFILE *p)
PyErr_SetString(PyExc_ValueError, "bad marshal data (invalid reference)");
break;
}
Py_INCREF(v);
retval = v;
retval = Py_NewRef(v);
break;
default:

View File

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

View File

@ -798,8 +798,7 @@ pycore_init_builtins(PyThreadState *tstate)
if (builtins_dict == NULL) {
goto error;
}
Py_INCREF(builtins_dict);
interp->builtins = builtins_dict;
interp->builtins = Py_NewRef(builtins_dict);
PyObject *isinstance = PyDict_GetItem(builtins_dict, &_Py_ID(isinstance));
assert(isinstance);
@ -2289,8 +2288,7 @@ create_stdio(const PyConfig *config, PyObject* io,
goto error;
}
else {
raw = buf;
Py_INCREF(raw);
raw = Py_NewRef(buf);
}
#ifdef MS_WINDOWS
@ -2552,8 +2550,7 @@ _Py_FatalError_PrintExc(PyThreadState *tstate)
_PyErr_NormalizeException(tstate, &exception, &v, &tb);
if (tb == NULL) {
tb = Py_None;
Py_INCREF(tb);
tb = Py_NewRef(Py_None);
}
PyException_SetTraceback(v, tb);
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,
def->m_base.m_index, module);
def->m_base.m_index, Py_NewRef(module));
}
int
@ -994,8 +993,7 @@ PyState_RemoveModule(PyModuleDef* def)
Py_FatalError("Module index out of bounds.");
}
Py_INCREF(Py_None);
return PyList_SetItem(interp->modules_by_index, index, Py_None);
return PyList_SetItem(interp->modules_by_index, index, Py_NewRef(Py_None));
}
// Used by finalize_modules()
@ -1299,8 +1297,7 @@ PyThreadState_GetFrame(PyThreadState *tstate)
if (frame == NULL) {
PyErr_Clear();
}
Py_XINCREF(frame);
return frame;
return (PyFrameObject*)Py_XNewRef(frame);
}
@ -1346,8 +1343,7 @@ PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
* the decref.
*/
PyObject *old_exc = tstate->async_exc;
Py_XINCREF(exc);
tstate->async_exc = exc;
tstate->async_exc = Py_XNewRef(exc);
HEAD_UNLOCK(runtime);
Py_XDECREF(old_exc);
@ -2027,8 +2023,7 @@ _bytes_shared(PyObject *obj, _PyCrossInterpreterData *data)
return -1;
}
data->data = (void *)shared;
Py_INCREF(obj);
data->obj = obj; // Will be "released" (decref'ed) when data released.
data->obj = Py_NewRef(obj); // Will be "released" (decref'ed) when data released.
data->new_object = _new_bytes_object;
data->free = PyMem_Free;
return 0;
@ -2055,8 +2050,7 @@ _str_shared(PyObject *obj, _PyCrossInterpreterData *data)
shared->buffer = PyUnicode_DATA(obj);
shared->len = PyUnicode_GET_LENGTH(obj);
data->data = (void *)shared;
Py_INCREF(obj);
data->obj = obj; // Will be "released" (decref'ed) when data released.
data->obj = Py_NewRef(obj); // Will be "released" (decref'ed) when data released.
data->new_object = _new_str_object;
data->free = PyMem_Free;
return 0;
@ -2093,8 +2087,7 @@ static PyObject *
_new_none_object(_PyCrossInterpreterData *data)
{
// XXX Singleton refcounts are problematic across interpreters...
Py_INCREF(Py_None);
return Py_None;
return Py_NewRef(Py_None);
}
static int

View File

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

View File

@ -173,8 +173,7 @@ calculate_suggestions(PyObject *dir,
suggestion_distance = current_distance;
}
}
Py_XINCREF(suggestion);
return suggestion;
return Py_XNewRef(suggestion);
}
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_id = k; /* ste owns reference to k */
Py_INCREF(name);
ste->ste_name = name;
ste->ste_name = Py_NewRef(name);
ste->ste_symbols = NULL;
ste->ste_varnames = NULL;
@ -286,8 +285,7 @@ _PySymtable_Build(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
_PySymtable_Free(st);
return NULL;
}
Py_INCREF(filename);
st->st_filename = filename;
st->st_filename = Py_NewRef(filename);
st->st_future = future;
/* Setup recursion depth check counters */
@ -1949,8 +1947,7 @@ symtable_visit_alias(struct symtable *st, alias_ty a)
return 0;
}
else {
store_name = name;
Py_INCREF(store_name);
store_name = Py_NewRef(name);
}
if (!_PyUnicode_EqualToASCIIString(name, "*")) {
int r = symtable_add_def(st, store_name, DEF_IMPORT, LOCATION(a));

View File

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