Skip to content

Replace Py_INCREF()/Py_XINCREF() usage with Py_NewRef()/Py_XNewRef() #99300

@vstinner

Description

@vstinner

I added Py_NewRef() and Py_XNewRef() to Python 3.10 C API. IMO using them make to code easier to read and make the code looks "more correct". Examples:

(A) Assign + INCREF:

-                result = Py_False;
-                Py_INCREF(result);
+                result = Py_NewRef(Py_False);

(B) INCREF + assign:

-        Py_INCREF(last);
-        self->last = last;
+        self->last = Py_NewRef(last);

(C) INCREF + return:

-    Py_XINCREF(result);
-    return result;
+    return Py_XNewRef(result);

While technically, Py_INCREF() and Py_XINCREF() modify the object in-place (increment their reference counter), for me Py_NewRef() makes me sense: it creates "a new reference".

The example (A) is weird: it assigns a variable to something, and only later creates a new reference. For me, the syntax with Py_NewRef() makes more sense.

Examples (B) and (C) are shorter with Py_NewRef(), and again, IMO makes more sense and are more readable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy