]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-99300: Use Py_NewRef() in Python/ directory (#99302)
authorVictor Stinner <vstinner@python.org>
Thu, 10 Nov 2022 08:03:39 +0000 (09:03 +0100)
committerGitHub <noreply@github.com>
Thu, 10 Nov 2022 08:03:39 +0000 (09:03 +0100)
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Python/ directory.

14 files changed:
Python/_warnings.c
Python/ast_opt.c
Python/codecs.c
Python/errors.c
Python/getargs.c
Python/initconfig.c
Python/marshal.c
Python/modsupport.c
Python/pylifecycle.c
Python/pystate.c
Python/pythonrun.c
Python/suggestions.c
Python/symtable.c
Python/thread.c

index b46fbdca9db38c62a10fe5e5ecba577b5d11a3c2..d703e1e6d843a5c5e2f2618e2e94cf06476fc2ac 100644 (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);
     }
index 426c5341b56fed5c1efd08e1b45ed0d2376d299e..1a0b2a05b1c7133b96f4c8861497c1604536bf6a 100644 (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;
 }
index 33965f885f7064ed5577d17226ad6c7c43366901..64addf00d526ed9f6b437979f2d4e2f25062cb5f 100644 (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. */
index fc3e4689209456cd0da232feb53b7c6b5f8c537f..7d2dd9b8c18b80b60b435714cf125a203776310c 100644 (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)
 
 
index f0b84b8338ddc383df01cbb2a8bb86715cdac171..703462242a0585938bcf0951e129a8150999b669 100644 (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 {
index 4b784290b014d2a7a2fb3252fe83ff8de3408452..67f6777d3b1d9e8bf697916af939c501680b3b1e 100644 (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) \
index 2690f55766c83bf1851a523ab91267e96f22014a..d6cc04d6f2abd3d3aa1bc39dc782236ec1677529 100644 (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:
index 8655daa1fc5e0efe4b7edf41aacad8daea5606f3..ee4bf14e63e148e9d1963abfc0367dad043f91eb 100644 (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) {
index f3b64fe6d7288094a46278e4d71ec57b5f3add03..51192bd5bb9b88063f4ef1f2dd9a0758bad21bef 100644 (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) {
index df0f4e2a166f6f806895e53024fa0b778162c421..b5fd8c2b7ae297601df8d794dcae84aa7dfa81b1 100644 (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
index a0005b32fcf146befaafe5f1f642f48671138ffe..1c7ac4af63c6be5f893c35f7889bb950e605ea8a 100644 (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");
index eee61b26049164d141b74f3cba9048f63ede8fb6..ad645c7d96fa570a54f3df91d950c2ae8b8e4d96 100644 (file)
@@ -173,8 +173,7 @@ calculate_suggestions(PyObject *dir,
             suggestion_distance = current_distance;
         }
     }
-    Py_XINCREF(suggestion);
-    return suggestion;
+    return Py_XNewRef(suggestion);
 }
 
 static PyObject *
index ea195bc155f1266b4bb0540bc30ca31238f48a8c..9bb7ffa92ce090e4bd48e096c28ce6d1a689c8ac 100644 (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));
index 8c8a4e81895eb689c75b6b665d6ecfafca000a84..3c1e78ed1bca839d87657678866b73c58bb23c7f 100644 (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;