Skip to content
  • Sponsor python/pythoncapi-compat

  • Notifications You must be signed in to change notification settings
  • Fork 29

The pythoncapi-compat project can be used to write a C extension supporting a wide range of Python versions with a single code base.

License

Notifications You must be signed in to change notification settings

python/pythoncapi-compat

Folders and files

NameName
Last commit message
Last commit date
Mar 31, 2025
Jan 19, 2025
Apr 14, 2025
Feb 8, 2022
Oct 16, 2023
Jun 4, 2020
Feb 11, 2022
Jun 14, 2022
Feb 18, 2025
Mar 31, 2025
Apr 14, 2025

Python C API compatibility

Build status of pythoncapi-compat on GitHub Actions

The pythoncapi-compat project can be used to write a C or C++ extension supporting a wide range of Python versions with a single code base. It is made of the pythoncapi_compat.h header file and the upgrade_pythoncapi.py script.

upgrade_pythoncapi.py requires Python 3.6 or newer.

See the documentation at ReadTheDocs for more details.

Getting started

To upgrade a specific file:

python3 upgrade_pythoncapi.py module.c

To upgrade all C/C++ files in a directory:

python3 upgrade_pythoncapi.py src/

Select operations

To only replace op->ob_type with Py_TYPE(op), select the Py_TYPE operation with:

python3 upgrade_pythoncapi.py -o Py_TYPE module.c

Or the opposite, to apply all operations but leave op->ob_type unchanged, deselect the Py_TYPE operation with:

python3 upgrade_pythoncapi.py -o all,-Py_TYPE module.c

Download pythoncapi_compat.h

If you want to pythoncapi_compat.h to your code base, use the upgrade_pythoncapi.py tool to fetch it:

python3 upgrade_pythoncapi.py --download PATH

This project is distributed under the Zero Clause BSD (0BSD) license and is covered by the PSF Code of Conduct.

About

The pythoncapi-compat project can be used to write a C extension supporting a wide range of Python versions with a single code base.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published

Contributors 13