DAGDA SEARCH | Back to results | Support

Changing Python’s C API

Source: https://devguide.python.org/developer-workflow/c-api/

Changing Python’s C API

The C API is divided into these tiers:

  1. The internal, private API, available with Py_BUILD_CORE defined. Ideally declared in Include/internal/. Any API named with a leading underscore is also considered private.

  2. The Unstable C API, identified by the PyUnstable_ name prefix. Ideally declared in Include/cpython/ along with the general public API.

  3. The “general” public C API, available when Include/Python.h is included normally. Ideally declared in Include/cpython/.

  4. The Limited C API, available with Py_LIMITED_API defined. Ideally declared directly under Include/.

Each tier has different stability and maintenance requirements to consider when you add or change definitions in it.

The public backwards compatibility guarantees for public C API are explained in the user documentation, Doc/c-api/stable.rst (C API and ABI Stability). C language compatibility guarantees are in Doc/c-api/intro.rst (Introduction).

As core developers, we need to be more careful about compatibility than what we promise publicly. See Public C API for details.

The internal API

Internal API is defined in Include/internal/ and is only available for building CPython itself, as indicated by a macro like Py_BUILD_CORE.

While internal API can be changed at any time, it’s still good to keep it stable: other API or other CPython developers may depend on it. For users, internal API is sometimes the best workaround for a thorny problem — though those use cases should be discussed on the C API Discourse category or an issue so we can try to find a supported way to serve them.

With PyAPI_FUNC or PyAPI_DATA

Functions or structures in Include/internal/ defined with PyAPI_FUNC or PyAPI_DATA are internal functions which are exposed only for specific use cases like debuggers and profilers. Ideally, these should be migrated to the Unstable C API.

With the extern keyword

Functions in Include/internal/ defined with the extern keyword must not and can not be used outside the CPython code base. Only built-in stdlib extensions (built with the Py_BUILD_CORE_BUILTIN macro defined) can use such functions.

When in doubt, new internal C functions should be defined in Include/internal using the extern keyword.

Private names

Any API named with a leading underscore is also considered internal. There is currently only one main use case for using such names rather than putting the definition in Include/internal/ (or directly in a .c file):

Note that historically, underscores were used for APIs that are better served by the Unstable C API:

Internal API tests

C tests for the internal C API live in Modules/_testinternalcapi.c. Functions named test_* are used as tests directly. Python parts of the tests live in various places in Lib/test.

Public C API

CPython’s public C API is available when Python.h is included normally (that is, without defining macros to select the other variants).

It should be defined in Include/cpython/ (unless part of the Limited API, see below).

Before adding new public API, please ask in the decisions repo of the C API workgroup. This helps us ensure newly added API is consistent and maintainable.

Also check with the C API WG before requiring a C feature not present in C99. While the public docs only promise compatibility with C11, in practice we only introduce C11 features individually as needed.

Guidelines for expanding/changing the public API

Please start a public discussion if these guidelines won’t work for your API.

Note

By return value, we mean the value returned by the C return statement.

C API tests

Tests for the public C API live in the _testcapi module. Functions named test_* are used as tests directly. Tests that need Python code (or are just easier to partially write in Python) live in Lib/test, mainly in Lib/test/test_capi.

Due to its size, the _testcapi module is defined in several source files. To add a new set of tests (or extract a set out of the monolithic Modules/_testcapimodule.c):

Note that all Modules/_testcapi/*.c sources initialize the same module, so be careful about name collisions.

When moving existing tests, feel free to replace TestError with PyExc_AssertionError unless actually testing custom exceptions.

Unstable C API

The unstable C API tier is meant for extensions that need tight integration with the interpreter, like debuggers and JIT compilers. Users of this tier may need to change their code with every feature release.

In many ways, this tier is like the general C API:

The differences are:

Despite being “unstable”, there are rules to make sure third-party code can use this API reliably:

Moving an API from the public tier to Unstable

The old name should continue to be available until an incompatible change is made. Per Python’s backwards compatibility policy (PEP 387), this deprecation needs to last at least two releases (modulo Steering Council exceptions).

The rules are relaxed for APIs that were introduced in Python versions before 3.12, when the official Unstable tier was added. You can make an incompatible change (and remove the old name) as if the function was already part of the Unstable tier for APIs introduced before Python 3.12 that are either:

Moving an API from the private tier to unstable

Moving an API from unstable to public

The old name should remain available until the new public name is deprecated or removed. There’s no need to deprecate the old name (it was unstable to begin with), but there’s also no need to break working code just because some function is now ready for a wider audience.

Limited API

The Limited API is a subset of the C API designed to guarantee ABI stability across Python 3 versions. Defining the macro Py_LIMITED_API will limit the exposed API to this subset.

No changes that break the Stable ABI are allowed.

The Limited API should be defined in Include/, excluding the cpython and internal subdirectories.

Guidelines for changing the Limited API, and removing items from it

While the Stable ABI must not be broken, the existing Limited API can be changed, and items can be removed from it, if:

This is tricky to do and requires careful thought. Some examples:

It is possible to remove items marked as part of the Stable ABI, but only if there was no way to use them in any past version of the Limited API.

Guidelines for adding to the Limited API

If following these guidelines would hurt performance, add a fast function (or macro) to the non-limited API and a stable equivalent to the Limited API.

If anything is unclear, or you have a good reason to break the guidelines, consider discussing the change in the C API category on Discourse.

Adding a new definition to the Limited API

Limited API tests

Since Limited API is a subset of the C API, there’s no need to test the behavior of individual functions. Rather, the tests could verify that some task is possible using the exposed subset, or exercise a feature that was removed from the current Limited API but still needs to be supported for older Limited API/Stable ABI versions.

To add a test file:

Use the test.support.requires_limited_api decorator for Python tests in Lib/test, so they’re skipped on incompatible builds.