GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Replace Py_INCREF()/Py_XINCREF() usage with Py_NewRef()/Py_XNewRef() · Issue #99300 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
Closed
Replace Py_INCREF()/Py_XINCREF() usage with Py_NewRef()/Py_XNewRef()#99300
Copy link
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@vstinner
vstinner
opened on Nov 9, 2022
Issue body actions

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.

  • PR: gh-99300: Use Py_NewRef() in Python/ directory #99302
  • PR: gh-99300: Use Py_NewRef() in Python/ directory #99317
  • PR: gh-99300: Use Py_NewRef() in Python/ceval.c #99318
  • PR: gh-99300: Use Py_NewRef() in Parser/ directory #99330
  • PR: gh-99300: Use Py_NewRef() in Objects/ directory #99332
  • PR: gh-99300: Use Py_NewRef() in Objects/dictobject.c #99333
  • PR: gh-99300: Use Py_NewRef() in Objects/ directory #99335
  • PR: gh-99300: Use Py_NewRef() in Objects/listobject.c #99336
  • PR: gh-99300: Use Py_NewRef() in Objects/ directory #99351
  • PR: gh-99300: Use Py_NewRef() in Objects/ directory #99354
  • PR: gh-99300: Use Py_NewRef() in Modules/_ctypes/ #99436
  • PR: gh-99300: Use Py_NewRef() in Modules/_elementtree.c #99438
  • PR: gh-99300: Use Py_NewRef() in Modules/itertoolsmodule.c #99439
  • PR: gh-99300: Use Py_NewRef() in Modules/ directory #99440
  • PR: gh-99300: Use Py_NewRef() in Modules/_asynciomodule.c #99441
  • PR: gh-99300: Use Py_NewRef() in Modules/_datetimemodule.c #99465
  • PR: gh-99300: Use Py_NewRef() in Modules/ directory #99466
  • PR: gh-99300: Use Py_NewRef() in Modules/ directory #99467
  • PR: gh-99300: Use Py_NewRef() in Modules/ directory #99468
  • PR: gh-99300: Use Py_NewRef() in Modules/ directory #99469
  • PR: gh-99300: Use Py_NewRef() in Modules/ directory #99473
  • PR: gh-99300: Use Py_NewRef() in PC/ directory #99479
  • PR: gh-99300: Use Py_NewRef() in Doc/ directory  #99480
  • PR: gh-99300: Use Py_NewRef() in Python/Python-ast.c #99499
  • PR: gh-99300: Replace Py_INCREF() with Py_NewRef() #99513
  • PR: gh-99300: Replace Py_INCREF() with Py_NewRef() #99530
  • PR: gh-99300: Replace Py_INCREF() with Py_NewRef() in _elementtree.c #99696
Reacted by Gregory P. Smith and Erlend E. Aasland
Reacted by Erlend E. Aasland

Activity

Next
  1. vstinner
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Nov 9, 2022
  2. vstinner
    added 6 commits that reference this issue on Nov 10, 2022

    gh-99300: Use Py_NewRef() in Python/ directory (#99302)

    d8f239d

    gh-99300: Use Py_NewRef() in Python/ directory (#99317)

    231d83b

    gh-99300: Use Py_NewRef() in Python/ceval.c (#99318)

    2e343fc

    gh-99300: Use Py_NewRef() in Parser/ directory (#99330)

    4ce2a20

    gh-99300: Use Py_NewRef() in Objects/dictobject.c (#99333)

    6dedf42

    gh-99300: Use Py_NewRef() in Objects/ directory (#99332)

    c0feb99
  3. gvanrossum
    added 2 commits that reference this issue on Nov 10, 2022

    pythongh-99300: Use Py_NewRef() in Objects/ directory (python#99332)

    4ee85e7

    pythongh-99300: Use Py_NewRef() in Objects/dictobject.c (python#99333)

    873da31
  4. vstinner
    added 4 commits that reference this issue on Nov 10, 2022

    gh-99300: Use Py_NewRef() in Objects/listobject.c (#99336)

    dbf8613

    gh-99300: Use Py_NewRef() in Objects/ directory (#99335)

    584e55b

    gh-99300: Use Py_NewRef() in Objects/ directory (#99351)

    1960eb0

    gh-99300: Use Py_NewRef() in Objects/ directory (#99354)

    3a1dde8
  5. ethanfurman
    added 2 commits that reference this issue on Nov 12, 2022

    pythongh-99300: Use Py_NewRef() in Python/ directory (python#99302)

    26b445f

    pythongh-99300: Use Py_NewRef() in Python/ directory (python#99317)

    6346b8b
  6. 16 remaining items

  7. vstinner
    added 7 commits that reference this issue on Nov 14, 2022

    gh-99300: Use Py_NewRef() in Modules/ directory (#99467)

    7e4dec0

    gh-99300: Use Py_NewRef() in Modules/_datetimemodule.c (#99465)

    f15a0fc

    gh-99300: Use Py_NewRef() in Modules/ directory (#99468)

    c340cbb

    gh-99300: Use Py_NewRef() in Modules/ directory (#99469)

    3e2f713

    gh-99300: Use Py_NewRef() in Modules/ directory (#99473)

    65dd745

    gh-99300: Use Py_NewRef() in Doc/ directory (#99480)

    1089bdc

    gh-99300: Use Py_NewRef() in PC/ directory (#99479)

    f5e326e
  8. erlend-aasland commented on Nov 14, 2022

    @erlend-aasland
    erlend-aasland
    on Nov 14, 2022
    Contributor
    More actions

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

  9. vstinner
    added a commit that references this issue on Nov 15, 2022

    gh-99300: Use Py_NewRef() in Python/Python-ast.c (#99499)

    f13f466
  10. CuriousLearner
    added a commit that references this issue on Nov 16, 2022

    Merge branch 'main' into fix-issue21914

    abd206e
  11. vstinner
    added 2 commits that reference this issue on Nov 16, 2022

    gh-99300: Replace Py_INCREF() with Py_NewRef() (#99513)

    3ed8803

    gh-99300: Replace Py_INCREF() with Py_NewRef() (#99530)

    8211cf5
  12. vstinner commented on Nov 21, 2022

    @vstinner
    vstinner
    on Nov 21, 2022
    MemberAuthor
    More actions

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

    Reacted by Erlend E. Aasland
  13. vstinner
    closed this as completedon Nov 21, 2022
  14. vstinner
    added a commit that references this issue on Nov 22, 2022

    gh-99300: Replace Py_INCREF() with Py_NewRef() in _elementtree.c (#99696

    f1a4a6a
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 errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.