Movatterモバイル変換


[0]ホーム

URL:


Following system colour schemeSelected dark colour schemeSelected light colour scheme

Python Enhancement Proposals

PEP 689 – Unstable C API tier

PEP 689 – Unstable C API tier

Author:
Petr Viktorin <encukou at gmail.com>
Discussions-To:
Discourse thread
Status:
Final
Type:
Standards Track
Requires:
523
Created:
22-Apr-2022
Python-Version:
3.12
Post-History:
27-Apr-2022,25-Aug-2022,27-Oct-2022
Resolution:
Discourse message

Table of Contents

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.

Abstract

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.

Motivation & Rationale

Unstable C API tier

The Python C-API is currently divided intothree stability tiers:

  • Limited API, with high compatibility expectations
  • Public API, which follows thebackwards compatibility policy, and requires deprecation warnings before changes
  • Internal (private) API, which can change at any time.

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.

Reserving leading underscores for Private API

Currently, CPython developers don’t agree on the exact meaning of a leadingunderscore in API names.It is used to mean two different things:

  • API that may change between minor releases, as in the Unstable tier proposedhere (e.g. functions introduced inPEP 523).
  • API that isprivate and should not be used outside of CPython at all(e.g. because it may change without notice, or it relies on undocumentedassumptions that non-CPython code cannot guarantee).

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.

Not breaking code unnecessarily

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.

Specification

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:

  • Unstable API should have no backwards-incompatiblechanges across patch releases, but may change or be removed in minorreleases (3.x.0, including Alpha and Beta releases of 3.x.0).Such changes must be documented and mentioned in the What’s New document.
  • Backwards-incompatible changes to these APIs should be made so thatcode that uses them will need to be updated to compile withthe new version (e.g. arguments should be added/removed, or a function shouldbe renamed, but the semantic meaning of an argument should not change).
  • Unstable API should be documented and tested.
  • To move an API from the public tier to the unstable tier, it should beexposed under the newPyUnstable_* 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:

    • Any API introduced before Python 3.12 that isdocumented to be lessstable than default.
    • Any API introduced before Python 3.12 that was named with a leadingunderscore.

    For examples, see theinitial unstable APIspecified in this PEP.

  • To move aninternal API to the unstable tier, it should beexposed under the newPyUnstable_* 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).

  • To move an API from the unstable tier to the public tier, it should beexposed without thePyUnstable_* prefix.

    The old name should remain available until the API is deprecated or removed.

  • Adding new unstable APIfor existing features is allowed even afterBeta feature freeze, up until the first Release Candidate.Consensus on Core Development Discourse or is needed in the Beta period.

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.

Leading underscore

C API named with a leading underscore, as well as API only available withPy_BUILD_CORE, will be consideredinternal.This means:

  • It may change or be removedwithout notice in minorreleases (3.x.0, including Alpha and Beta releases of 3.x.0).API changes in patch releases or Release Candidates should only be done ifabsolutely necessary.
  • It should be documented in source comments or Devguide only, not in thepublic documentation.
  • API introduced before Python 3.12 that is documented or widely usedexternally should be moved to the Unstable tier as explained above.

    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.

Initial unstable API

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.

Backwards Compatibility

The C API backwards compatibility expectations will be made clearer.

All renamed API will be available under old names for as long as feasible.

How to Teach This

The changes affect advanced C programmers, who should consult theupdated reference documentation, devguide and/or What’s New document.

Reference Implementation

https://github.com/python/cpython/compare/main…encukou:unstable-tier

Rejected Ideas

No special prefix

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.

Underscore prefix

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.

New header directory

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.

Python API

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.

Copyright

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


[8]ページ先頭

©2009-2026 Movatter.jp