The pythoncapi-compat project can be used to write a C extension supporting a wide range of Python versions with a single code base. https://pythoncapi-compat.readthedocs.io/
Find a file
Sam Gross 11cb80f265
Don't include structmember.h in pythoncapi_compat.h (#161)
Avoids conflicts due to names without "Py" prefixes.
2025-11-08 15:49:40 +01:00
.github/workflows Add CI builds for free-threaded Python versions (#150) 2025-09-01 16:15:41 +02:00
docs Make PyUnstable_Unicode_GET_CACHED_HASH return -1 on PyPy (#157) 2025-10-15 13:27:47 +02:00
tests Don't include structmember.h in pythoncapi_compat.h (#161) 2025-11-08 15:49:40 +01:00
.gitignore Add Sphinx documentation 2022-02-08 16:17:20 +01:00
.readthedocs.yaml Updated readthedocs to latest version of Python (#147) 2025-08-09 11:05:14 +02:00
CODE_OF_CONDUCT.md Creation of the project 2020-06-04 20:20:53 +02:00
COPYING Fix #19: Change the license from MIT to 0BSD (#20) 2022-02-11 11:40:50 +01:00
meson.build Add meson.build file (#151) 2025-10-02 20:37:25 +02:00
pythoncapi_compat.h Don't include structmember.h in pythoncapi_compat.h (#161) 2025-11-08 15:49:40 +01:00
README.rst Fix compatibility with C++03 (#42) 2022-06-14 16:35:06 +02:00
runtests.py Test PyPy 3.11 (#140) 2025-03-31 20:20:44 +02:00
upgrade_pythoncapi.py Fixed typo (#148) 2025-08-09 11:04:06 +02:00

++++++++++++++++++++++++++
Python C API compatibility
++++++++++++++++++++++++++

.. image:: https://github.com/python/pythoncapi-compat/actions/workflows/build.yml/badge.svg
   :alt: Build status of pythoncapi-compat on GitHub Actions
   :target: https://github.com/python/pythoncapi-compat/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
<https://pythoncapi-compat.readthedocs.io/en/latest/>`_
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
<https://opensource.org/licenses/0BSD>`_ and is covered by the `PSF Code of
Conduct <https://www.python.org/psf/codeofconduct/>`_.