Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions Python/_warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down
3 changes: 1 addition & 2 deletions Python/ast_opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 2 additions & 4 deletions Python/codecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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. */
Expand Down
6 changes: 2 additions & 4 deletions Python/errors.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)


Expand Down
6 changes: 2 additions & 4 deletions Python/getargs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions Python/initconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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) \
Expand Down
21 changes: 7 additions & 14 deletions Python/marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 3 additions & 6 deletions Python/modsupport.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 3 additions & 6 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 7 additions & 14 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -936,9 +936,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
Expand Down Expand Up @@ -986,8 +985,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()
Expand Down Expand Up @@ -1291,8 +1289,7 @@ PyThreadState_GetFrame(PyThreadState *tstate)
if (frame == NULL) {
PyErr_Clear();
}
Py_XINCREF(frame);
return frame;
return (PyFrameObject*)Py_XNewRef(frame);
}


Expand Down Expand Up @@ -1338,8 +1335,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);
Expand Down Expand Up @@ -2019,8 +2015,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;
Expand All @@ -2047,8 +2042,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;
Expand Down Expand Up @@ -2085,8 +2079,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
Expand Down
9 changes: 3 additions & 6 deletions Python/pythonrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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");
Expand Down
3 changes: 1 addition & 2 deletions Python/suggestions.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ calculate_suggestions(PyObject *dir,
suggestion_distance = current_distance;
}
}
Py_XINCREF(suggestion);
return suggestion;
return Py_XNewRef(suggestion);
}

static PyObject *
Expand Down
9 changes: 3 additions & 6 deletions Python/symtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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));
Expand Down
3 changes: 1 addition & 2 deletions Python/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down