Skip to content

[Python] Start switching ROOT pythonizations to Python Limited C API#21680

Merged
guitargeek merged 1 commit intoroot-project:masterfrom
guitargeek:limited_api_1
Mar 25, 2026
Merged

[Python] Start switching ROOT pythonizations to Python Limited C API#21680
guitargeek merged 1 commit intoroot-project:masterfrom
guitargeek:limited_api_1

Conversation

@guitargeek
Copy link
Copy Markdown
Contributor

This patch replaces usage of CPython macros and non-stable APIs with functions compatible with the Python Limited C API (stable ABI).

Replace direct macros with API functions:

  • PyBytes_AS_STRING : PyBytes_AsString
  • PyBytes_GET_SIZE : PyBytes_Size
  • PyTuple_GET_SIZE : PyTuple_Size
  • PyTuple_SET_ITEM : PyTuple_SetItem
  • PyList_GET_ITEM : PyList_GetItem
  • PyList_GET_SIZE : PyList_Size
  • PyInt_AS_LONG : PyInt_AsLong
  • Py_DECREF : Py_DecRef
  • Py_XDECREF : Py_DecRef

Furthermore, replace PyUnicode_AsUTF8 with a helper based on the stable PyUnicode_AsUTF8AndSize. Also, replace <Python.h> with "PythonLimitedAPI.h", where the Py_LIMITED_API macro is set.

This change is part of enabling PyROOT to build against the Python stable ABI.

@github-actions
Copy link
Copy Markdown

Test Results

    20 files      20 suites   2d 22h 3m 19s ⏱️
 3 810 tests  3 809 ✅ 1 💤 0 ❌
69 592 runs  69 583 ✅ 9 💤 0 ❌

Results for commit d7056b8.

Copy link
Copy Markdown
Member

@vepadulano vepadulano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice! I believe the changes are already good as is, nonetheless I left a couple of minor comments for consideration. Make sure the PR is merged only once the CI is green.

This patch replaces usage of CPython macros and non-stable APIs with
functions compatible with the Python Limited C API (stable ABI).

Replace direct macros with API functions:

  * `PyBytes_AS_STRING`   : `PyBytes_AsString`
  * `PyBytes_GET_SIZE`    : `PyBytes_Size`
  * `PyTuple_GET_SIZE`    : `PyTuple_Size`
  * `PyTuple_SET_ITEM`    : `PyTuple_SetItem`
  * `PyList_GET_ITEM`     : `PyList_GetItem`
  * `PyList_GET_SIZE`     : `PyList_Size`
  * `PyInt_AS_LONG`       : `PyInt_AsLong`
  * `Py_DECREF`           : `Py_DecRef`
  * `Py_XDECREF`          : `Py_DecRef`

Furthermore, replace `PyUnicode_AsUTF8` with a helper based on the
stable `PyUnicode_AsUTF8AndSize`. Also, replace `<Python.h>` with
`"PythonLimitedAPI.h"`, where the `Py_LIMITED_API` macro is set.

This change is part of enabling PyROOT to build against the Python stable ABI.
@guitargeek
Copy link
Copy Markdown
Contributor Author

Thanks @vepadulano, you are right about the error handling. I have fixed this, and then the code also becomes such that a helper function for getting the unicode strings inline becomes unnecessary, because one has to do the error checking separately anyway.

@guitargeek guitargeek merged commit 4e0c7bf into root-project:master Mar 25, 2026
28 of 29 checks passed
@guitargeek guitargeek deleted the limited_api_1 branch March 25, 2026 20:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants