Skip to content

Add support for the limited C API (Py_LIMITED_API) - #184

Merged
vstinner merged 18 commits into
python:mainfrom
vstinner:limited
Oct 5, 2026
Merged

vstinner merged 18 commits into
python:mainfrom
vstinner:limited

Conversation

@vstinner

@vstinner vstinner commented Oct 4, 2026

Copy link
Copy Markdown
Member

Examples of API not available in the limited C API:

  • PyBytesWriter
  • PyConfig_Get(), PyConfig_GetInt().
  • PyDict_ContainsString().
  • PyFrame_GetBack(), PyFrame_GetVar()
  • PyLongWriter_Create() and PyLong_Export()
  • PyObject_VisitManagedDict(), PyObject_ClearManagedDict()
  • PyThreadState_GetUnchecked()
  • PyTime_t API.
  • PyUnicodeWriter
  • PyUnstable_SetImmortal().
  • Py_SETREF(), Py_XSETREF().

Implementation notes:

  • PyUnicode_EqualToUTF8AndSize() has no ASCII fast path.
  • PyLong_GetSign(), PyLong_IsPositive(), PyLong_IsNegative() and PyLong_IsZero() are implemented with PyObject_RichCompareBool().
  • PyBytes_Join() calls PyObject_CallMethod(sep, "join", iterable).
  • Add PYTHONCAPI_COMPAT_EXPECT_TYPE() macro to raise TypeError. There are 3 implementations depending on the API and Python version.

test_pythoncapi_compat.py now reuses the setup module to list tests.

Closes #183

Examples of API not available in the limited C API:

* PyBytesWriter
* PyConfig_Get(), PyConfig_GetInt().
* PyDict_ContainsString().
* PyFrame_GetBack(), PyFrame_GetVar()
* PyLongWriter_Create() and PyLong_Export()
* PyObject_VisitManagedDict(), PyObject_ClearManagedDict()
* PyThreadState_GetUnchecked()
* PyTime_t API.
* PyUnicodeWriter
* PyUnstable_SetImmortal().
* Py_SETREF(), Py_XSETREF().

Implementation notes:

* PyUnicode_EqualToUTF8AndSize() has no ASCII fast path.
* PyLong_GetSign(), PyLong_IsPositive(), PyLong_IsNegative() and
  PyLong_IsZero() are implemented with PyObject_RichCompareBool().
* PyBytes_Join() calls PyObject_CallMethod(sep, "join", iterable).
* Add PYTHONCAPI_COMPAT_EXPECT_TYPE() macro to raise TypeError.
  There are 3 implementations depending on the API and Python
  version.

test_pythoncapi_compat.py now reuses the setup module to list tests.

Closes python#183
PyObject_GC_IsTracked() and PyObject_GC_IsFinalized() are only added
to limited C API 3.10 on Windows.
Tests fails on Windows with Python 3.15:
"unresolved external symbol PyInit_test_pythoncapi_compat_cext_c11".
@vstinner
vstinner marked this pull request as ready for review October 4, 2026 07:22
@vstinner vstinner mentioned this pull request Oct 4, 2026
@vstinner

vstinner commented Oct 4, 2026

Copy link
Copy Markdown
Member Author

@ngoldbaum: Here is a large change to add support for the limited C API in pythoncapi-compat. Do you think that you would be able to test it in numpy? Like create a draft numpy PR updating pythoncapi-compat and check if the CI pass?

@ngoldbaum

Copy link
Copy Markdown
Contributor

ping @prathamhole14 can you take on Victor's request above?

@vstinner

vstinner commented Oct 4, 2026

Copy link
Copy Markdown
Member Author

Note: this PR doesn't add Py_SETREF / Py_XSETREF to the limited C API. My plan is first to add these macros the limited C API in Python 3.16: capi-workgroup/decisions#110. Then merge this PR. And finally, add these macros to pythoncapi-compat.

@prathamhole14

Copy link
Copy Markdown

ping @prathamhole14 can you take on Victor's request above?

Yes, here is the PR to test numpy/numpy#32871

@vstinner

vstinner commented Oct 4, 2026

Copy link
Copy Markdown
Member Author

Yes, here is the PR to test numpy/numpy#32871

The CI says "92 successful checks". So it means that there is no big red warning from numpy side at least? (I would just like a confirmation :-))

@vstinner
vstinner merged commit cb1eadf into python:main Oct 5, 2026
44 checks passed
@vstinner
vstinner deleted the limited branch October 5, 2026 13:32
@vstinner

vstinner commented Oct 5, 2026

Copy link
Copy Markdown
Member Author

Ok, I merged my PR. If there are bugs, well, we can fix them as usual :-)

@vstinner

vstinner commented Oct 5, 2026

Copy link
Copy Markdown
Member Author

Follow-up to test limited C API on Python 3.15 on Windows: #185.

@prathamhole14

Copy link
Copy Markdown

Yes, here is the PR to test numpy/numpy#32871

The CI says "92 successful checks". So it means that there is no big red warning from numpy side at least? (I would just like a confirmation :-))

Yes, in numpy LIMITED API build is tested in debug mode since everything is green there is nothing to worry about! Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support the limited API?

3 participants