Important
This PEP is a historical document. The up-to-date, canonical documentation can now be found atChanging Python’s C API.
×
User-facing documentation is atUnstable C API.
SeePEP 1 for how to propose changes.
Some functions and types of the C-API are designatedunstable,meaning that they will not change in patch (bugfix/security) releases,but may change between minor releases (e.g. between 3.11 and 3.12) withoutdeprecation warnings.
Any C API with a leading underscore is designatedinternal, meaning that itmay change or disappear without any notice.
The Python C-API is currently divided intothree stability tiers:
Tools requiring access to CPython internals (e.g. advanceddebuggers and JIT compilers) are often built for minor series releasesof CPython, and assume that the C-API internals used do not changein patch releases. To support these tools, we need a tier between thePublic and Private C-API, with guarantees on stability throughoutthe minor-series release: the proposedUnstable tier.
Some functions, likePyCode_New(), are documented as unstable(“Calling [it] directly can bind you to a precise Python version”),and also often change in practice.The unstable tier should make their status obvious even to people who don’tread the docs carefully enough, making them hard to use accidentally.
Currently, CPython developers don’t agree on the exact meaning of a leadingunderscore in API names.It is used to mean two different things:
The unclear meaning makes the underscore less useful than it could be.If it only markedprivate API, CPython developers could change underscoredfunctions, or remove unused ones, without researching how they’redocumented or used outside CPython.
With the introduction of a dedicated unstable tier, we can clarify the meaningof the leading underscore. It should mark private API only.
This PEP specifies that API in the unstable tier should have a special nameprefix. This means functions (macros, etc.) will need to be renamed.After a rename, the old name should continue to be available untilan incompatible change is made (i.e. until call sites need to be updatedanyway).In other words, just changing the tier of a function shouldn’t break users’code.
The C API is divided by stability expectations intothree “sections”(internal, public, and limited).We’ll now call thesestability tiers, ortiers for short.
AnUnstable tier will be added.
APIs (functions, types, etc.) in this tier will named with thePyUnstable_prefix, with no leading underscore.
They will be declared in headers used for public API (Include/*.h,rather than in a subdirectory likeInclude/unstable/).
Several rules for dealing with the unstable tier will be introduced:
PyUnstable_* name.The old name should be deprecated (e.g. withPy_DEPRECATED), butcontinue to be available until an incompatible change is made to the API.Per Python’s backwards compatibility policy (PEP 387), this deprecationneeds to lastat least two releases (without an SC exceptions).But it can also last indefinitely – for example, ifPEP 590’s“provisional”_PyObject_Vectorcall was added today, it would be initially namedPyUnstable_Object_Vectorcall and there would be no plan to removethis name.
In the following cases, an incompatible change (and thus removing thedeprecated name) is allowed without an SC exception, as if the function wasalready part of the Unstable tier:
For examples, see theinitial unstable APIspecified in this PEP.
PyUnstable_* name.If the old name is documented, or widely used externally,it should continue to be available until anincompatible change is made (and call sites need to be updated).It should start raising deprecation warnings (e.g. usingPy_DEPRECATED).
PyUnstable_* prefix.The old name should remain available until the API is deprecated or removed.
These rules will be documented in thedevguide,anduser documentationwill be updated accordingly.
Reference docs for C API namedPyUnstable_* will automatically shownotes with links to the unstable tier documentation.
C API named with a leading underscore, as well as API only available withPy_BUILD_CORE, will be consideredinternal.This means:
This might happen long after this PEP is accepted.Consequently, for a few years core devs should do some research beforechanging underscored API, especially if it doesn’t needPy_BUILD_CORE.
Users of the C API are encouraged to search their codebase for_Py and_PY identifier prefixes, and treat any hits as issues to be eventuallyfixed – either by switching to an existing alternative, or by openinga CPython issue to request exposing public API for their use case,and eventually switching to that.
The following API will be moved to the Unstable tier in the initialimplementation as proof of the concept.
Code object constructors:
PyUnstable_Code_New() (renamed fromPyCode_New)PyUnstable_Code_NewWithPosOnlyArgs() (renamed fromPyCode_NewWithPosOnlyArgs)Code extra information (PEP 523):
PyUnstable_Eval_RequestCodeExtraIndex() (renamed from_PyEval_RequestCodeExtraIndex)PyUnstable_Code_GetExtra() (renamed from_PyCode_GetExtra)PyUnstable_Code_SetExtra() (renamed from_PyCode_SetExtra)More are expected in Python 3.12, without the need for another PEP.
The C API backwards compatibility expectations will be made clearer.
All renamed API will be available under old names for as long as feasible.
The changes affect advanced C programmers, who should consult theupdated reference documentation, devguide and/or What’s New document.
https://github.com/python/cpython/compare/main…encukou:unstable-tier
In the initial version of this PEP, unstable API didn’t have thePyUnstableprefix.Instead, definingPy_USING_UNSTABLE_API made the API available in a givensource file, signifying acknowledgement that the file as a whole willpotentially need to be revisited for each Python release.
However, it was decided that unstable-ness needs to be exposedin the individual names.
It would be possible to mark both private and unstable API withleading underscores.However, that would dilute the meaning of_Py prefix.Reserving the prefix for internal API only makes it trivial to search for.
Other API tiers have dedicated directories for headers(Include/cpython/,Include/internal/).
Since the unstable tier uses a very obvious naming conventionand the names are always available,a directory likeInclude/unstable/ is unnecessary.
It might be good to add a similar tier in the Python (not C) API,e.g. fortypes.CodeType.However, the mechanism for that would need to be different.This is outside the scope of the PEP.
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-0689.rst
Last modified:2025-02-01 08:55:40 GMT