Here is a text that I intend to turn into a PEP soon.
It renders OK-ish on Discourse, but it’s not Markdown. You can get a better rendered (and possibly updated) copy in my repo.
An earlier version was meanderingly discussed on capi-sig.
Update: this is now PEP 652.
Python’s Stable ABI and Limited API, introduced in :pep:
will be formalized in a single definitive file, tested, and documented.
384 defined a limited API and stable ABI, which allows extenders and
embedders of CPython to compile extension modules that are binary-compatible
with any subsequent version of 3.x.
In theory, this brings several advantages.
A module can be built only once per platform and support multiple versions
of Python, reducing time, power and maintainer attention needed for builds
(in exchange for potentially worse performance).
Binary wheels using the stable ABI work with new versions of CPython
throughout the pre-release period, and can be tested in environments where
building from source is not practical.
Also, if generators like Cython support the stable ABI, some projects
could offer stable-ABI wheels in addition to specific-ABI ones, in order
to support future (alpha/beta/early release) interpreters, while still
using version-specific optimizations on CPython versions they target
As a welcome side effect of the limited API’s hiding of implementation details
is that this API is becoming a viable target for alternate Python
implementations that would be incompatible with the full C API.
However, in hindsignt, PEP 384 and its implementation has several issues:
- It is ill-defined. According to PEP 384, functions are opt-out:
all functions not specially marked are part of the stable ABI.
In practice, for Windows there’s a list that’s opt-in.
For users there is a
#definethat should make only the stable ABI
available, but There is no process that ensures it is kept up-to date.
Neither is there a process for updating the documentation.
- Until recently, the stable ABI was not tested at all. It tends to break.
For example, changing a function to a macro can break the stable ABI as the
function symbol is removed.
- There is no way to deprecate parts of the limited API.
- It is incomplete. Some operations are not available in the stable ABI,
with little reason except “we forgot”.
(This last point is one the PEP will not help with, however.)
This PEP defines the limited API more clearly and introducess process
designed to make the stable ABI and limited API more useful and robust.
This PEP contains a lot of clarifications and definitions, but just one big
technical change: the stable ABI will be explicitly listed in
a human-maintained “manifest” file.
There have been efforts to collect such lists automatically, e.g. by scanning
the symbols exported from Python.
Such automation might seem easier to maintain than a handcrafted file,
but has major issues: for example, the set exported symbols has
Also, the cost of updating an explicit manifest is small compared
to the overall work that should go into changing API that will need to
be suppported forever (or until Python 3 reaches end of life, if that
This PEP proposes automatically generating things from the manifest:
initially documentation and DLL contents, with later possibilities
for also automating tests.
Stable ABI vs. Limited API
384 and this document deal with the Limited API and the Stable ABI,
two related but distinct concepts.
This section clarifies what they mean and defines some of their semantics
(either pre-existing or newly proposed here).
The word “Extensions” is used as a shorthand for all code that uses the
Python API, e.g. extension modules or software that embeds Python.
The CPython Stable ABI is a promise that extensions built against
a specific Stable ABI version will be usable with any newer interpreter of the
same major version, on the same platform and with the same compiler & settings.
For example, a extension built with the CPython 3.10 Stable ABI will be usable
with CPython 3.11, 3.12, and so on, but not necessarily with 4.0.
The Stable ABI is not generally forward-compatible: an extension built and
tested with CPython 3.10 will not generally be compatible with CPython 3.9.
For example, starting in Python 3.10, the
Py_tp_doc slot may be set to
NULL, while in older versions, a
NULL value will likely crash the
The Stable ABI trades performance for its stability.
For example, extensions built for a specific CPython version will automatically
use faster macros instead of functions in the stable ABI.
Future Python versions may deprecate some members of the Stable ABI.
Deprecated members will still work, but may suffer from issues like reduced
performance or, in the most extreme cases, memory/resource leaks.
Stable ABI guarantee holds for extensions compiled from code that restricts
itself to the Limited API, a subset of CPython’s C API.
Extensions that target the limited API should define the preprocessor macro
Py_LIMITED_API to either
3 or the current
This will enable stable ABI versions of several functions and limit definitions
to the limited API.
(However, note that the macro is not perfect: due to technical issues or
oversigt, some non-limited API might be exposed even with it defined.)
The Limited API is not guaranteed to be stable.
In the future, parts of the limited API may be deprecated.
They may even be removed, as long as the stable ABI is kept
stable and Python’s general backwards compatibility policy, :pep:
For example, a function declaration might be removed from public header
files but kept in the library.
This is currently a possibility for the future; this PEP does not to propose
a concrete process for deprecations and removals.
The goal for the limited API is to cover everything needed to interact
with the interpreter.
The main reason to not include a public API in the limited subset
should be that it needs implementation details that change between CPython
versions (like struct memory layouts) – usually for performance reasons.
The limited API is not limited to CPython. Other implementations are
encouraged to implement it and help drive its design.
To make the stable ABI more useful and robust, the following changes
Stable ABI Manifest
All members of the stable ABI – functions, typedefs, structs, data, macros,
and constants – will be explicitly listed in a single “manifest” file,
For structs, any fields that users of the stable ABI are allowed to access
will be listed explicitly.
The manifest will also serve as the definitive list of the Limited API.
Members that are not part of the Limited API, but are part of the Stable ABI
PyObject.ob_type, which is accessible by the
will be annotated as such.
For items that are only available on some systems, the manifest will record the
feature macro that determines their presence (such as
To make the implementation (and usage from non-C languages) easier,
all such macros will be simple names; if a future item needs a “negative” macro
or complex expression (such as a hypothetical
#ifndef MACOSX or
#if defined(POSIX) && !defined(LINUX)), a new feature macro will be derived.
The format of the manifest will be subject to change whenever needed.
It should be consumed only by scripts in the CPython repository.
If a stable list is needed, a script can be added to generate it.
The following wil be generated from the ABI manifest:
- Source for the Windows shared library
- Input for documentation,
- Test case that checks the runtime availablility of symbols (see below).
Runtime availablility of the ABI symbols will be checked using
Testing the Stable ABI below.
The following will be checked against the stable ABI manifest as part of
- The reference count summary,
Doc/data/refcounts.dat, includes all
function in the stable ABI (among others).
- The functions/structs declared and constants/macros defined
Python.his included with
(Initially Linux only; checks on other systems may be added in the future.)
After the initial implementation, details such as function arguments will be
added and the manifest will be checked for internal consistency (e.g. all
types used in function signatures are part of the API).
Contents of the Stable ABI
The initial stable ABI manifest will include:
- The Stable ABI specified in :pep:
- Everything listed in
- All structs (struct typedefs) which these functions return or take as
arguments. (Fields of such structs will not necessarily be added.)
- New type slots, such as
- The type flags
- The calling conventions
METH_*(except deprecated ones).
- All API needed by macros is the stable ABI (annotated as not being part of
the limited API).
Items that are no longer in CPython when this PEP is accepted will be removed
from the list.
Additional items may be aded to the initial manifest according to
the checklist below.
Documenting the Limited API
Notes saying “Part of the limited API” will be added to Python’s documentation
automatically, in a way similar to the notes on functions that return borrowed
Testing the Stable ABI
An automatically generated test module will be added to ensure that all symbols
included in the stable ABI are available at compile time.
Additionally, a test will be added that aims to call each function
in the stable ABI using
ctypes, with exceptions for e.g. functions related
to fatal errors and intepreter initialization/shutdown.
This should prevent regressions when a function is converted to a macro,
which keeps the same API but breaks the ABI.
It should also help ensure that the ABI of function signatures doesn’t change.
(Creating this test is expected to take longer than the rest of this PEP to
implement, possibly it’ll need several releases.)
Changing the Limited API
A checklist for changing the limited API, including adding new items to it
and removing existing ones, will be added to the
The checklist will 1) mention best practices and common pitfalls in Python
C API design and 2) guide the developer around the files that need changing and
scripts that need running when the limited API is changed.
Below is the initial proposal for the checklist.
(After the PEP is accepted, see the Devguide for the current version.)
Note that the checklist applies to new changes; several items
in the existing limited API are grandfathered and couldn’t be added today.
Make sure the change does not break the Stable ABI of any version of Python
Make sure no exposed names are private (i.e. begin with an underscore).
Make sure the new API is well documented.
Make sure the types of all parameters and return values of the added
function(s) and all fields of the added struct(s) are be part of the
limited API (or standard C).
Make sure the new API and its intended use follows standard C, not just
features of currently supported platforms.
Specifically, follow the C dialect specified in :pep:
- Do not cast a function pointer to
void*(a data pointer) or vice versa.
- Do not cast a function pointer to
Make sure the new API follows reference counting conventions. (Following them
makes the API easier to reason about, and easier use in other Python
- Do not return borrowed references from functions.
- Do not steal references to function arguments.
Make sure the ownership rules and lifetimes of all applicable struct fields,
arguments and return values are well defined.
Think about ease of use for the user. (In C, ease of use itself is not very
important; what is useful is reducing boilerplate code needed to use the
API. Bugs like to hide in boiler plates.)
- If a function will be often called with specific value for an argument,
consider making it default (used when
NULLis passed in).
- If a function will be often called with specific value for an argument,
Think about future extensions: for example, if it’s possible that future
Python versions will need to add a new field to your struct,
how will that be done?
Make as few assumptions as possible about details that might change in
future CPython versions or differ across C API implementations:
- The GIL
- Garbage collection
- Memory layout of PyObject, lists/tuples and other structures
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 at the
capi-sig_ mailing list.
… _capi-sig: Mailman 3 Info | email@example.com - python.org
- Move the declaration to a header file directly under
Include/, into a
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03yy0000block
yycorresponding to the target CPython version).
- Make an entry in the stable ABI manifest,
- For functions, add a test that calls the function using ctypes
(XXX: mention filename).
- Regenerate the autogenerated files using
(XXX: check non-Linux platforms)
- Build Python and run checks using
(XXX: check non-Linux platforms)
Advice for Extenders and Embedders
The following notes will be added to documentation.
Extension authors should test with all Python versions they support,
and preferably build with the lowest such version.
Py_LIMITED_API defined is not a guarantee that your code
conforms to the limited API or the stable ABI.
Py_LIMITED_API only covers definitions, but an API also includes other
issues, such as expected semantics.
Examples of issues that
Py_LIMITED_API does not guard against are:
- Calling a function with invalid arguments
- A function that started accepting
NULLvalues for an argument.
in Python 3.9 will fail if
NULLis passed to it under Python 3.8.
Only testing with 3.8 (or lower versions) will uncover this issue.
- Some structs include a few fields that are part of the stable ABI and other
fields that aren’t.
Py_LIMITED_APIdoes not filter out such “private” fields.
- Code that uses something that is not documented as part of the stable ABI,
but exposed even with
Py_LIMITED_APIdefined, may break in the future.
Despite the team’s best efforts, such issues may happen.
Backwards compatibility is one honking great idea!
This PEP aims at full compatibility with the existing stable ABI and limited
API, but defines them terms more explicitly.
It might not be consistent with some interpretations of what the existing
stable ABI/limited API is.
How to Teach This
Technical documentation will be provided in
and linked from the What’s New document.
Docs for CPython core developers will be added to the devguide.
Nothing presentable yet.
Defining a process for deprecations/removals
While this PEP acknowledges that parts of the limited API might be deprecated
or removed in the future, a process to do this is not in scope, and is left
to a possible future PEP.
None so far.
… _Devguide: https://devguide.python.org/
This document is placed in the public domain or under the
CC0-1.0-Universal license, whichever is more permissive.