C API Stability

特記のない限り、Python の C API には後方互換性ポリシー PEP 387 が適用されます。 変更の大半はソース互換性を保ちます (通常は新しい API の追加だけを行います)。 既存 API の変更や削除は、非推奨の期間を経た後、または重大な問題を修正するためにのみ行います。

CPython のアプリケーションバイナリインターフェース (ABI) は、同じマイナーリリース内で前方互換性と後方互換性を持ちます (同じ方法でコンパイルされている場合。下記の プラットフォームで考慮すべき点 を参照)。 したがって、Python 3.10.0 向けにコンパイルしたコードは 3.10.8 で動作し、その逆も同様ですが、3.9.x や 3.11.x 向けには別にコンパイルする必要があります。

C API には、期待される安定性が異なる二つの階層があります。

  • 不安定な API は、非推奨の期間なしにマイナーバージョン間で変更される可能性があります。 名前の接頭辞 PyUnstable で識別できます。

  • Limited API は、複数のマイナーリリースにわたって互換性を持ちます。 Py_LIMITED_API を定義すると、Python.h から公開されるのはこの部分集合だけになります。

以下でそれぞれを詳しく説明します。

_Py_InternalState のようにアンダースコアで始まる名前はプライベート API で、パッチリリースでも予告なく変更される可能性があります。 この API を使う必要がある場合は、CPython 開発者 に連絡し、用途に合う公開 API の追加を相談することを検討してください。

不安定な C API

接頭辞 PyUnstable を持つ API は CPython の実装の詳細を公開しており、マイナーリリースごとに (例えば 3.9 から 3.10 で) 非推奨の警告なしに変更される可能性があります。 ただし、バグ修正リリース (例えば 3.10.0 から 3.10.1) では変更されません。

通常はデバッガのような、低レベルの専門的なツールを対象としています。

この API を使うプロジェクトには、CPython の開発を追い、変更への対応に追加の労力をかけることが期待されます。

Stable Application Binary Interface

For simplicity, this document talks about extensions, but the Limited API and Stable ABI work the same way for all uses of the API – for example, embedding Python.

Limited C API

Python 3.2 introduced the Limited API, a subset of Python's C API. Extensions that only use the Limited API can be compiled once and be loaded on multiple versions of Python. Contents of the Limited API are listed below.

Py_LIMITED_API

Define this macro before including Python.h to opt in to only use the Limited API, and to select the Limited API version.

Define Py_LIMITED_API to the value of PY_VERSION_HEX corresponding to the lowest Python version your extension supports. The extension will be ABI-compatible with all Python 3 releases from the specified one onward, and can use Limited API introduced up to that version.

Rather than using the PY_VERSION_HEX macro directly, hardcode a minimum minor version (e.g. 0x030A0000 for Python 3.10) for stability when compiling with future Python versions.

You can also define Py_LIMITED_API to 3. This works the same as 0x03020000 (Python 3.2, the version that introduced Limited API).

Stable ABI

To enable this, Python provides a Stable ABI: a set of symbols that will remain ABI-compatible across Python 3.x versions.

注釈

The Stable ABI prevents ABI issues, like linker errors due to missing symbols or data corruption due to changes in structure layouts or function signatures. However, other changes in Python can change the behavior of extensions. See Python's Backwards Compatibility Policy (PEP 387) for details.

The Stable ABI contains symbols exposed in the Limited API, but also other ones – for example, functions necessary to support older versions of the Limited API.

On Windows, extensions that use the Stable ABI should be linked against python3.dll rather than a version-specific library such as python39.dll.

On some platforms, Python will look for and load shared library files named with the abi3 tag (e.g. mymodule.abi3.so). It does not check if such extensions conform to a Stable ABI. The user (or their packaging tools) need to ensure that, for example, extensions built with the 3.10+ Limited API are not installed for lower versions of Python.

All functions in the Stable ABI are present as functions in Python's shared library, not solely as macros. This makes them usable from languages that don't use the C preprocessor.

Limited API Scope and Performance

The goal for the Limited API is to allow everything that is possible with the full C API, but possibly with a performance penalty.

For example, while PyList_GetItem() is available, its “unsafe” macro variant PyList_GET_ITEM() is not. The macro can be faster because it can rely on version-specific implementation details of the list object.

Without Py_LIMITED_API defined, some C API functions are inlined or replaced by macros. Defining Py_LIMITED_API disables this inlining, allowing stability as Python's data structures are improved, but possibly reducing performance.

By leaving out the Py_LIMITED_API definition, it is possible to compile a Limited API extension with a version-specific ABI. This can improve performance for that Python version, but will limit compatibility. Compiling with Py_LIMITED_API will then yield an extension that can be distributed where a version-specific one is not available – for example, for prereleases of an upcoming Python version.

Limited API Caveats

Note that compiling with Py_LIMITED_API is not a complete guarantee that 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.

One issue that Py_LIMITED_API does not guard against is calling a function with arguments that are invalid in a lower Python version. For example, consider a function that starts accepting NULL for an argument. In Python 3.9, NULL now selects a default behavior, but in Python 3.8, the argument will be used directly, causing a NULL dereference and crash. A similar argument works for fields of structs.

Another issue is that some struct fields are currently not hidden when Py_LIMITED_API is defined, even though they're part of the Limited API.

For these reasons, we recommend testing an extension with all minor Python versions it supports, and preferably to build with the lowest such version.

また、使用するすべての API のドキュメントを確認し、それが明示的に Limited API の一部であるかどうかをチェックすることをお勧めします。Py_LIMITED_API が定義されていても、技術的な理由で(あるいはバグとして意図せず)いくつかのプライベート宣言が公開されることがあります。

Also note that the Limited API is not necessarily stable: compiling with Py_LIMITED_API with Python 3.8 means that the extension will run with Python 3.12, but it will not necessarily compile with Python 3.12. In particular, parts of the Limited API may be deprecated and removed, provided that the Stable ABI stays stable.

プラットフォームで考慮すべき点

ABI stability depends not only on Python, but also on the compiler used, lower-level libraries and compiler options. For the purposes of the Stable ABI, these details define a “platform”. They usually depend on the OS type and processor architecture

It is the responsibility of each particular distributor of Python to ensure that all Python versions on a particular platform are built in a way that does not break the Stable ABI. This is the case with Windows and macOS releases from python.org and many third-party distributors.

限定版APIの内容

Currently, the Limited API includes the following items: