Skip to content
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

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

Open
vstinner opened this issue Nov 9, 2022 · 0 comments
Open

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

vstinner opened this issue Nov 9, 2022 · 0 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@vstinner
Copy link
Member

vstinner commented Nov 9, 2022

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.

@vstinner vstinner added the type-bug An unexpected behavior, bug, or error label Nov 9, 2022
vstinner added a commit to vstinner/cpython that referenced this issue Nov 9, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Python/ directory.
vstinner added a commit to vstinner/cpython that referenced this issue Nov 9, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Python/ directory.
vstinner added a commit to vstinner/cpython that referenced this issue Nov 9, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Python/ directory.
vstinner added a commit that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Python/ directory.
vstinner added a commit to vstinner/cpython that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Python/ directory.
vstinner added a commit to vstinner/cpython that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Python/ directory.
vstinner added a commit to vstinner/cpython that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Python/ directory.
vstinner added a commit to vstinner/cpython that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Python/ directory.

Update Parser/asdl_c.py to regenerate Python/Python-ast.c.
vstinner added a commit to vstinner/cpython that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in Python/ceval.c and related files.
vstinner added a commit to vstinner/cpython that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in Python/ceval.c and related files.
vstinner added a commit that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Python/ directory.

Update Parser/asdl_c.py to regenerate Python/Python-ast.c.
vstinner added a commit that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in Python/ceval.c and related files.
vstinner added a commit to vstinner/cpython that referenced this issue Nov 10, 2022
Replace Py_INCREF() with Py_NewRef() in C files of the Parser/
directory and in the PEG generator.
vstinner added a commit that referenced this issue Nov 10, 2022
Replace Py_INCREF() with Py_NewRef() in C files of the Parser/
directory and in the PEG generator.
vstinner added a commit to vstinner/cpython that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Objects/ directory.
vstinner added a commit to vstinner/cpython that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Objects/ directory.
vstinner added a commit to vstinner/cpython that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in Objects/dictobject.c.
vstinner added a commit to vstinner/cpython that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in Objects/dictobject.c.
vstinner added a commit to vstinner/cpython that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Objects/ directory.
vstinner added a commit to vstinner/cpython that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Objects/ directory.
vstinner added a commit to vstinner/cpython that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in Objects/listobject.c.
vstinner added a commit that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in Objects/dictobject.c.
vstinner added a commit that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Objects/ directory.
gvanrossum pushed a commit to gvanrossum/cpython that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Objects/ directory.
gvanrossum pushed a commit to gvanrossum/cpython that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in Objects/dictobject.c.
vstinner added a commit that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in Objects/listobject.c.
vstinner added a commit that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Objects/ directory.
vstinner added a commit to vstinner/cpython that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Objects/ directory.
vstinner added a commit to vstinner/cpython that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Objects/ directory.
vstinner added a commit to vstinner/cpython that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Objects/ directory.
vstinner added a commit to vstinner/cpython that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Objects/ directory.
vstinner added a commit to vstinner/cpython that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Objects/ directory.
vstinner added a commit that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Objects/ directory.
vstinner added a commit that referenced this issue Nov 10, 2022
Replace Py_INCREF() and Py_XINCREF() with Py_NewRef() and
Py_XNewRef() in C files of the Objects/ directory.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant