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 — Add Py_SETREF() and Py_XSETREF() macros to the limited C API 3.12 · Issue #99574 · python/cpython

Add Py_SETREF() and Py_XSETREF() macros to the limited C API 3.12 #99574

New issue
Copy link
New issue
Copy link
Closed
Closed
Add Py_SETREF() and Py_XSETREF() macros to the limited C API 3.12#99574
Copy link
Labels
type-featureA feature request or enhancementA feature request or enhancement

Description

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

The Py_SETREF() and Py_XSETREF() macros were added to Python 3.6 (and 3.5.2) in 2016 with commit 57a01d3.

In 2017, Serhiy @serhiy-storchaka proposed to add these two macros to the limited C API: https://mail.python.org/archives/list/[email protected]/thread/HGBT2Y4VAZOOU5XI2RPSURDGFBMRJKPO/ But these macros were not added.

Py_CLEAR() is already part of the limited C API (since Python 3.2) and is very similar to Py_SETREF(). Py_CLEAR() rely on the C preprocessor to get a reference to a variable, so the variable can be set to NULL without having to pass a pointer to the variable. The usage is Py_CLEAR(var), rather than Py_CLEAR(&var). The Py_SETREF() macro does the same, but the value is passed by the caller, it's not always NULL.

Multiple projects use these macros. Code search in PyPI top 5000 projects using (Py_SETREF|Py_XSETREF) regex. 16 projects (177 matching lines in total):

  • bitarray-2.6.0
  • coverage-6.4.4
  • datatable-1.0.0
  • gnureadline-8.1.2
  • immutables-0.18
  • inflate64-0.3.0
  • iteration_utilities-0.11.0
  • mypy-0.971
  • numpy-1.23.2
  • pandas-1.4.4
  • pickle5-0.0.12
  • python-rapidjson-1.8
  • python-snappy-0.6.1
  • scipy-1.9.1
  • typed_ast-1.5.4
  • zstd-1.5.2.5

numpy and the pythoncapi-compat project (code) define these macros on Python older than 3.5.2.

I propose to add Py_SETREF() and Py_XSETREF() macros to the limited C API of Python 3.12.

By the way, I recently documented these two macros in Python 3.12 (commit c03e05c): https://docs.python.org/dev/c-api/refcounting.html#c.Py_SETREF

See also issue #98724 and its PR #99100 which discuss Py_CLEAR(), Py_SETREF() and Py_XSETREF() in length.


I would prefer exposing functions (taking a reference to a variable, &var) which can be used when C macros cannot be used, for example in Rust or other programming languages. But adding functions is a different which can be addressed separately.

Linked PRs

  • gh-99574: Add Py_SETREF() to the limited C API #99575

Activity

  1. vstinner
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Nov 18, 2022
  2. vstinner commented on Nov 18, 2022

    @vstinner
    vstinner
    on Nov 18, 2022
    MemberAuthor
    More actions

    Obviously, since Py_SETREF() and Py_XSETREF() are macros, they are not part of the stable ABI. I only propose to add them to the limited C API.

  3. serhiy-storchaka commented on Nov 21, 2022

    @serhiy-storchaka
    serhiy-storchaka
    on Nov 21, 2022
    Member
    More actions

    Since the previous attempt to add them failed, should not it be discussed in more public place?

  4. vstinner
    closed this as completedon Dec 13, 2022
  5. vstinner commented on Oct 5, 2026

    @vstinner
    vstinner
    on Oct 5, 2026
    MemberAuthor
    More actions

    Follow-up in 2026 (Python 3.16): issue gh-158842.

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-featureA feature request or enhancementA feature request or enhancement

    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.