Skip to content

Support the limited API? #183

Description

@ngoldbaum

#ifdef Py_LIMITED_API
# error "Py_LIMITED_API is not supported"
#endif
seems to indicate that adding shims for the limited API is currently out of scope for the project.

I'm curious if there's any interest in adding support here. I'd particularly like to avoid adding extensive shims to NumPy to work around issues like in numpy/numpy#32864.

Activity

  1. added 2 commits that reference this issue on Oct 4, 2026
    51310e1
    7aefdb2
  2. vstinner commented on Oct 4, 2026

    @vstinner
    Member

    I wrote #184 to add support for the limited C API.

  3. added a commit that references this issue on Oct 5, 2026
    cb1eadf
  4. vstinner commented on Oct 5, 2026

    @vstinner
    Member

    It's now implemented in the main branch.

  5. vstinner commented on Oct 7, 2026

    @vstinner
    Member

    @ngoldbaum: I just merged Py_SETREF()/Py_XSETREF() into Python 3.16: python/cpython@ec14f81. Do you expect that pythoncapi-compat provides new limited C API functions to older Python versions? It sounds somehow to go against limited C API, no? I expect Py_SETREF()/Py_XSETREF() to be only available in Python 3.16 limited C API, even if pythoncapi-compat. Well, I'm not sure. What do you think?

  6. ngoldbaum commented on Oct 7, 2026

    @ngoldbaum
    ContributorAuthor

    It sounds someone to go against limited C API, no?

    I think it goes with the intention of this repo, which allows people to adopt idioms that are added in future Python versions. It's a bad idea to use those idioms if it hasn't been added in a future version but once it has, it makes sense to use internal details to implement backward compatibility shims.

    Of course that may be difficult in practice for most things in the limited API because it will require new ABI surface. In this case, though, we can provide a shim. So I would say why not allow that?

    Without support here, NumPy would add a shim somewhere internally in NumPy that only NumPy uses and may be buggy. IMO it's better to centralize compat shims here as much as possible.

  7. vstinner commented on Oct 7, 2026

    @vstinner
    Member

    @ngoldbaum: Hum. Supporting the limited C API on old Python versions is tricky to get it right, because of the stable ABI. I suggest to work on a case by case basis to add new limited C API to old Python versions. Py_SETREF() / Py_XSETREF() is special since it doesn't call (new) Python functions in its implementation, but only memcpy() which comes from the C library. So for these two macros, I think that it's safe to add them to old Python versions.

    I created #187 to add Py_SETREF/Py_XSETREF to old limited C API versions.

    Three years ago, I created https://github.com/vstinner/deadparrot experimental project: a shared library providing old and new Python C API functions to support all Python versions. But I didn't go far, and no one adopted it. The pythoncapi-compat "header file" approach seems to fit well developers needs and it's convenient to use (just include the header file, you're done).

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions