Visitar URL original
Replace Py_INCREF()/Py_XINCREF() usage with Py_NewRef()/Py_XNewRef() · Issue #99300 · python/cpython · GitHub
Skip to content

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

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.

Activity

  1. added 6 commits that reference this issue on Nov 10, 2022
  2. added 2 commits that reference this issue on Nov 10, 2022
  3. added 4 commits that reference this issue on Nov 10, 2022
  4. added 2 commits that reference this issue on Nov 12, 2022
  5. 16 remaining items

  6. added 7 commits that reference this issue on Nov 14, 2022
  7. erlend-aasland commented on Nov 14, 2022

    @erlend-aasland
    Contributor

    For the record, see gh-23170 for the PR that converted sqlite3 to Py_NewRef/Py_XNewRef.

  8. added a commit that references this issue on Nov 15, 2022
  9. added 2 commits that reference this issue on Nov 16, 2022
  10. vstinner commented on Nov 21, 2022

    @vstinner
    MemberAuthor

    26 commits later, I have converted all "INCREF + assign" and "INCREF + return" to NewRef. I am now closing the issue.

  11. added a commit that references this issue on Nov 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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