diff options
author | Victor Stinner <vstinner@python.org> | 2022-11-10 08:03:39 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-10 08:03:39 (GMT) |
commit | d8f239d86eb70c31aa4c4ac46a1d0a27bdb14b20 (patch) | |
tree | 7055b2d966bf41e636751177cea0248b56072e92 /Python | |
parent | f883b7f8ee3209b52863fc662343c8cd81abdc59 (diff) | |
download | cpython-d8f239d86eb70c31aa4c4ac46a1d0a27bdb14b20.zip cpython-d8f239d86eb70c31aa4c4ac46a1d0a27bdb14b20.tar.gz cpython-d8f239d86eb70c31aa4c4ac46a1d0a27bdb14b20.tar.bz2 |
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.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/_warnings.c | 12 | ||||
-rw-r--r-- | Python/ast_opt.c | 3 | ||||
-rw-r--r-- | Python/codecs.c | 6 | ||||
-rw-r--r-- | Python/errors.c | 6 | ||||
-rw-r--r-- | Python/getargs.c | 6 | ||||
-rw-r--r-- | Python/initconfig.c | 4 | ||||
-rw-r--r-- | Python/marshal.c | 21 | ||||
-rw-r--r-- | Python/modsupport.c | 9 | ||||
-rw-r--r-- | Python/pylifecycle.c | 9 | ||||
-rw-r--r-- | Python/pystate.c | 21 | ||||
-rw-r--r-- | Python/pythonrun.c | 9 | ||||
-rw-r--r-- | Python/suggestions.c | 3 | ||||
-rw-r--r-- | Python/symtable.c | 9 | ||||
-rw-r--r-- | Python/thread.c | 3 |
14 files changed, 41 insertions, 80 deletions
diff --git a/Python/_warnings.c b/Python/_warnings.c index b46fbdc..d703e1e 100644 --- a/Python/_warnings.c +++ b/Python/_warnings.c @@ -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); } diff --git a/Python/ast_opt.c b/Python/ast_opt.c index 426c534..1a0b2a0 100644 --- a/Python/ast_opt.c +++ b/Python/ast_opt.c @@ -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; } diff --git a/Python/codecs.c b/Python/codecs.c index 33965f8..64addf0 100644 --- a/Python/codecs.c +++ b/Python/codecs.c @@ -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. */ diff --git a/Python/errors.c b/Python/errors.c index fc3e468..7d2dd9b 100644 --- a/Python/errors.c +++ b/Python/errors.c @@ -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) diff --git a/Python/getargs.c b/Python/getargs.c index f0b84b8..7034622 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -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 { diff --git a/Python/initconfig.c b/Python/initconfig.c index 4b78429..67f6777 100644 --- a/Python/initconfig.c +++ b/Python/initconfig.c @@ -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) \ diff --git a/Python/marshal.c b/Python/marshal.c index 2690f55..d6cc04d 100644 --- a/Python/marshal.c +++ b/Python/marshal.c @@ -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: diff --git a/Python/modsupport.c b/Python/modsupport.c index 8655daa..ee4bf14 100644 --- a/Python/modsupport.c +++ b/Python/modsupport.c @@ -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) { diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index f3b64fe..51192bd 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -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) { diff --git a/Python/pystate.c b/Python/pystate.c index df0f4e2..b5fd8c2 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -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 diff --git a/Python/pythonrun.c b/Python/pythonrun.c index a0005b3..1c7ac4a 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -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"); diff --git a/Python/suggestions.c b/Python/suggestions.c index eee61b2..ad645c7 100644 --- a/Python/suggestions.c +++ b/Python/suggestions.c @@ -173,8 +173,7 @@ calculate_suggestions(PyObject *dir, suggestion_distance = current_distance; } } - Py_XINCREF(suggestion); - return suggestion; + return Py_XNewRef(suggestion); } static PyObject * diff --git a/Python/symtable.c b/Python/symtable.c index ea195bc..9bb7ffa 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -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)); diff --git a/Python/thread.c b/Python/thread.c index 8c8a4e8..3c1e78e 100644 --- a/Python/thread.c +++ b/Python/thread.c @@ -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; |