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 — [C API] Make Py_REFCNT() opaque in limited C API 3.14 · Issue #124127 · python/cpython

[C API] Make Py_REFCNT() opaque in limited C API 3.14 #124127

New issue
Copy link
New issue
Copy link
Closed
Closed
[C API] Make Py_REFCNT() opaque in limited C API 3.14#124127
Copy link
Labels
topic-C-API

Description

@vstinner
vstinner
opened on Sep 16, 2024
Issue body actions

In the limited C API 3.14 and newer, I propose to change Py_REFCNT() implementation to an opaque function call to hide implementation details. I made a similar change for Py_INCREF() and Py_DECREF() in Python 3.12.

The problem is that with Free Threading (PEP 703), the implementation of this functions becomes less trivial than just getting the object member PyObject.ob_refcnt:

static inline Py_ssize_t _Py_REFCNT(PyObject *ob) {
    uint32_t local = _Py_atomic_load_uint32_relaxed(&ob->ob_ref_local);
    if (local == _Py_IMMORTAL_REFCNT_LOCAL) {
        return _Py_IMMORTAL_REFCNT;
    }
    Py_ssize_t shared = _Py_atomic_load_ssize_relaxed(&ob->ob_ref_shared);
    return _Py_STATIC_CAST(Py_ssize_t, local) +
           Py_ARITHMETIC_RIGHT_SHIFT(Py_ssize_t, shared, _Py_REF_SHARED_SHIFT);
}

_Py_atomic_load_uint32_relaxed() and _Py_atomic_load_ssize_relaxed() must now be called. But I would prefer to not "leak" such implementation detail into the limited C API.

cc @colesbury @Fidget-Spinner @encukou

Linked PRs

  • gh-124127: Make Py_REFCNT() opaque in limited C API 3.14 #124128
Reacted by Petr Viktorin

Activity

  1. vstinner
    added
    topic-C-API
    on Sep 16, 2024
  2. vstinner
    added 4 commits that reference this issue on Sep 16, 2024

    pythongh-124127: Make Py_REFCNT() opaque in limited C API 3.14

    fcb803b

    pythongh-124127: Make Py_REFCNT() opaque in limited C API 3.14

    85d5f88

    pythongh-124127: Make Py_REFCNT() opaque in limited C API 3.14

    ce0e2bb

    pythongh-124127: Make Py_REFCNT() opaque in limited C API 3.14

    e4b3731
  3. vstinner commented on Sep 16, 2024

    @vstinner
    vstinner
    on Sep 16, 2024
    MemberAuthor
    More actions

    Previous issue about Py_TYPE(): #120600

  4. vstinner
    added a commit that references this issue on Sep 24, 2024

    gh-124127: Make Py_REFCNT() opaque in limited C API 3.14 (#124128)

    9d344fa
  5. vstinner commented on Sep 24, 2024

    @vstinner
    vstinner
    on Sep 24, 2024
    MemberAuthor
    More actions

    Implemented in 9d344fa.

  6. vstinner
    closed this as completedon Sep 24, 2024
  7. vstinner commented on Sep 24, 2024

    @vstinner
    vstinner
    on Sep 24, 2024
    MemberAuthor
    More actions

    Functions implemented as opaque function calls in limited C API 3.14:

    • Py_TYPE() (since 3.14)
    • Py_REFCNT() (since 3.14), Py_SET_REFCNT() (since 3.13)
    • Py_INCREF(), Py_XINCREF() (since 3.12)
    • Py_DECREF(), Py_XDECREF() (since 3.12)

    Py_SET_TYPE() is not implemented as an opaque function call since it should not be used in the limited C API, it's only useful with static types.

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

    topic-C-API

    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.