Important
This PEP is a historical document. The up-to-date, canonical documentation can now be found atC API Stability (user docs) andChanging Python’s C API (development docs).
×
SeePEP 1 for how to propose changes.
CPython’s Limited C-API and Stable ABI, introduced inPEP 384,will be formalized in a single definitive file, tested, and documented.
PEP 384 defined a Limited API and Stable ABI, which allow extenders andembedders of CPython to compile extension modules that are binary-compatiblewith any subsequent version of 3.x.In theory, this brings several advantages:
However, in hindsight,PEP 384 and its implementation has several issues:
#define that should make only the Stable ABIavailable, but there is no process that ensures it is kept up-to date.Neither is there a process for updating the documentation.This PEP defines the Limited API more clearly and introducess processdesigned to make the Stable ABI and Limited API more useful and robust.
This PEP contains a lot of clarifications and definitions, but just one bigtechnical change: the Stable ABI will be explicitly listed ina human-maintained “manifest” file.
There have been efforts to collect such lists automatically, e.g. by scanningthe 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 hasplatform-specific variations.Also, the cost of updating an explicit manifest is small comparedto the overall work that should go into changing API that will need tobe supported forever (or until Python 3 reaches end of life, if thatcomes sooner).
This PEP proposes automatically generating thingsfrom the manifest:initially documentation and DLL contents, with later possibilitiesfor also automating tests.
PEP 384 and this document deal with theLimited API and theStable ABI,two related but distinct concepts. In short:
This section clarifies these terms 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 thePython API, e.g. extension modules or software that embeds Python.
The CPythonStable ABI is a promise that extensions built againsta specific Stable ABI version will be compatible with any newerinterpreter of the same major version.
The Stable ABI does not define a complete binary interface:important details like the layout of structures in memory or functioncalling conventions are determined by the platform and the compiler andits settings.The Stable ABI promise only applies if these lower-details are also stable.
For example, an extension built with the CPython 3.10 Stable ABI will be usablewith CPython 3.11, 3.12, etc.It will not necessarily be compatible with CPython 4.0, nor with CPython 3.10on a different platform.
The Stable ABI is not generally forward-compatible: an extension built andtested with CPython 3.10 will not generally be compatible with CPython 3.9.
Note
For example, starting in Python 3.10, thePy_tp_doc slot may be set toNULL, while in older versions, aNULL value will likely crash theinterpreter.
The Stable ABI trades performance for its stability.For example, extensions built for a specific CPython version will automaticallyuse 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 reducedperformance or, in the most extreme cases, memory/resource leaks.
The Stable ABI promise holds for extensions compiled from code that restrictsitself to theLimited API (application programming interface).The Limited API is a subset of CPython’s C API.
Extensions that target the Limited API should define the preprocessor macroPy_LIMITED_API to either3 or the currentPYTHON_API_VERSION.This will enable Stable ABI versions of several functions and limit definitionsto the Limited API.(However, note that the macro is not perfect: due to technical issues oroversights, some non-limited API might be exposed even with it defined.)
The Limited API is not guaranteed to bestable.In the future, parts of the Limited API may be deprecated.They may even be removed, as long as theStable ABI is keptstable and Python’s general backwards compatibility policy,PEP 387,is followed.
Note
For example, a function declaration might be removed from public headerfiles but kept in the library.This is currently a possibility for the future; this PEP does not to proposea concrete process for deprecations and removals.
The goal for the Limited API is to cover everything needed to interactwith the interpreter.The main reason to not include a public API in the Limited subsetshould be that it needs implementation details that change between CPythonversions (like struct memory layouts) – usually for performance reasons.
The Limited API is not limited to CPython. Other implementations areencouraged to implement it and help drive its design.
To make the Stable ABI more useful and robust, the following changesare proposed.
All members of the Stable ABI – functions, typedefs, structs, data, macros,and constants – will be explicitly listed in a single “manifest” file,Misc/stable_abi.txt.
For structs, any fields that users of the Stable ABI are allowed to accesswill 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(e.g.PyObject.ob_type, which is accessible by thePy_TYPE macro),will be annotated as such.
For items that are only available on some systems, the manifest will record thefeature macro that determines their presence (such asMS_WINDOWS orHAVE_FORK).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 ahypothetical#ifndefMACOSX or#ifdefined(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 will be generated from the ABI manifest:
PC/python3dll.c.The following will be checked against the Stable ABI manifest as part ofcontinuous integration:
Doc/data/refcounts.txt, includes allfunction in the Stable ABI (among others).Python.h is included withPy_LIMITED_API set.(Initially Linux only; checks on other systems may be added in the future.)After the initial implementation, details such as function arguments will beadded and the manifest will be checked for internal consistency (e.g. alltypes used in function signatures are part of the API).
The initial Stable ABI manifest will include:
PC/python3dll.c.Py_am_aiter.Py_TPFLAGS_DEFAULT,Py_TPFLAGS_BASETYPE,Py_TPFLAGS_HAVE_GC,Py_TPFLAGS_METHOD_DESCRIPTOR.METH_* (except deprecated ones).Items that are no longer in CPython when this PEP is accepted will be removedfrom the list.
Additional items may be added to the initial manifest according tothe checklist below.
Notes saying “Part of the Limited API” will be added to Python’s documentationautomatically, in a way similar to the notes on functions that return borrowedreferences.
A complete list of all members of the Limited API will also be added tothe documentation.
An automatically generated test module will be added to ensure that all symbolsincluded in the Stable ABI are available at compile time.
A checklist for changing the Limited API, including adding new items to itand removing existing ones, will be added to theDevguide.The checklist will 1) mention best practices and common pitfalls in PythonC API design and 2) guide the developer around the files that need changing andscripts 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 itemsin theexisting Limited API are grandfathered and couldn’t be added today.
Design considerations:
void* (a data pointer) or vice versa.NULL is passed in).
- 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 thecapi-sig mailing list.
Procedure:
Include/, into a#if!defined(Py_LIMITED_API)||Py_LIMITED_API+0>=0x03yy0000 block(with theyy corresponding to the target CPython version).Misc/stable_abi.txt.makeregen-all.(or the alternative for non-make platforms)makecheck-abi.(or the alternative for non-make platforms)The following notes will be added to documentation, along with betterinformation regarding this topic and what guarantees do we offer:
Extension authors should test with all Python versions they support,and preferably build with the lowest such version.
Compiling withPy_LIMITED_API defined isnot a guarantee that your codeconforms to the Limited API or the Stable ABI.Py_LIMITED_API only covers definitions, but an API also includes otherissues, such as expected semantics.
Examples of issues thatPy_LIMITED_API does not guard against are:
NULL values for an argumentin Python 3.9 will fail ifNULL is passed to it under Python 3.8.Only testing with 3.8 (or lower versions) will uncover this issue.Py_LIMITED_API does not filter out such “private” fields.Py_LIMITED_API defined, may break in the future.Despite the team’s best efforts, such issues may happen.The Stable ABI promise relies on stable underlying ABI details, such as thelayout of structures in memory and function calling conventions, whichare affected by the compiler and its settings.For the promise to hold, these details must not change between CPython 3.xreleases on a particular platform.
Backwards compatibility is one honking great idea!
This PEP aims at full compatibility with the existing Stable ABI and LimitedAPI, but defines them terms more explicitly.It might not be consistent with some interpretations of what the existingStable ABI/Limited API is.
None known.
Technical documentation will be provided inDoc/c-api/stableand linked from theWhat’s New document.Docs for CPython core developers will be added to the devguide.
Seeissue 43795.
The following issues are out of scope of this PEP, but show possiblefuture directions.
While this PEP acknowledges that parts of the Limited API might be deprecatedor removed in the future, a process to do this is not in scope, and is leftto a possible future PEP.
It might be useful to have the ABI manifest be a C header file, or togenerate header files from the manifest.Again, either are options for the future.
None so far.
This document is placed in the public domain or under theCC0-1.0-Universal license, whichever is more permissive.
Source:https://github.com/python/peps/blob/main/peps/pep-0652.rst
Last modified:2025-02-01 08:55:40 GMT