C API 與 ABI 穩定性¶
除非有另外記錄於文件,Python 的 C API 被包含在向後相容性策略 PEP 387 中。大多數改動都是相容於原始碼的(通常只會增加新的 API)。更改現有 API 或刪除 API 僅在棄用期後或修復嚴重問題時進行。
CPython 的應用程式二進位介面 (Application Binary Interface, ABI) 在次要版本中是向前和向後相容的(如果它們以相同的方式編譯;請參閱下面的平台注意事項)。因此,為 Python 3.10.0 編譯的程式碼將能夠在 3.10.8 上運行,反之亦然,但 3.9.x 和 3.11.x 就需要分別編譯。
C API 有兩層級,有不同的穩定性期望:
不穩定 API,可能會在次要版本中發生變化,而沒有棄用階段。會在名稱中以
PyUnstable前綴來標記。受限 API,在多個次要版本之間相容。當有定義
Py_LIMITED_API時,只有這個子集會從Python.h公開。
下面將更詳細地討論這些內容。
帶有底線前綴的名稱是私有 API (private API),像是 _Py_InternalState,即使在補丁版本 (patch release) 中也可能被更改,不會另行通知。如果你需要使用這個 API,可以聯繫 CPython 開發者 並針對你的使用方法來討論是否新增公開的 API。
不穩定的 C API¶
任何以 PyUnstable 前綴命名的 API 都會公開 CPython 實作細節,並可能在每個次要版本中進行更改(例如從 3.9 到 3.10),而不會出現任何棄用警告。但是它不會在錯誤修復發布版本中發生變化(例如從 3.10.0 到 3.10.1)。
它通常用於專門的低階工具,例如偵錯器。
使用此 API 的專案應該要遵循 CPython 開發細節,並花費額外的力氣來針對這些變動來做調整。
穩定的應用程式二進位介面¶
Python's Stable ABI allows extensions to be compatible with multiple versions of Python, without recompilation.
備註
為簡單起見,本文件討論擴充 (extension),但穩定 ABI 在所有 API 使用方式中都以相同的方式運作 -- 例如在嵌入式 Python (embedding Python) 中。
There are two Stable ABIs:
abi3, introduced in Python 3.2, is compatible with non-free-threaded builds of CPython.abi3t, introduced in Python 3.15, is compatible with free-threaded builds of CPython. It has stricter API limitations thanabi3.在 3.15 版被加入:
abi3twas added in PEP 803
It is possible for an extension to be compiled for both abi3 and
abi3t at the same time; the result will be compatible with
both free-threaded and non-free-threaded builds of Python.
Currently, this has no downsides compared to compiling for abi3t only.
Each Stable ABI is versioned using the first two numbers of the Python version. For example, Stable ABI 3.14 corresponds to Python 3.14. An extension compiled for Stable ABI 3.x is ABI-compatible with Python 3.x and above.
Extensions that target a stable ABI must only use a limited subset of the C API. This subset is known as the Limited API; its contents are listed below.
在 Windows 上,使用穩定 ABI 的擴充應該連接到 python3.dll 而不是特定版本的函式庫,例如 python39.dll。此函式庫只會公開相關的符號。
On some platforms, Python will look for and load shared library files named
with the abi3 or abi3t tag (for example, mymodule.abi3.so).
Free-threaded interpreters only recognize the
abi3t tag, while non-free-threaded ones will prefer abi3 but fall back
to abi3t.
Thus, extensions compatible with both ABIs should use the abi3t tag.
Python does not necessarily check that extensions it loads
have compatible ABI.
Extension authors are encouraged to add a check using the Py_mod_abi
slot or the PyABIInfo_Check() function, but the user
(or their packaging tool) is ultimately responsible for ensuring that,
for example, extensions built for Stable ABI 3.10 are not installed for lower
versions of Python.
穩定 ABI 中的所有函式都作為函式存在於 Python 的共享函式庫中,而不僅是作為巨集。這使得它們可被用於不使用 C 預處理器 (preprocessor) 的語言,包括 Python 的 ctypes。
Compiling for Stable ABI¶
備註
Build tools (such as, for example, meson-python, scikit-build-core, or Setuptools) often have a mechanism for setting macros and synchronizing them with extension filenames and other metadata. Prefer using such a mechanism, if it exists, over defining the macros manually.
The rest of this section is mainly relevant for tool authors, and for people who compile extensions manually.
也參考
list of recommended tools in the Python Packaging User Guide
To compile for a Stable ABI, define one or both of the following macros
to the lowest Python version your extension should support, in
Py_PACK_VERSION format.
Typically, you should choose a specific value rather than the version of
the Python headers you are compiling against.
The macros must be defined before including Python.h.
Since Py_PACK_VERSION is not available at this point, you
will need to use the numeric value directly.
For reference, the values for a few recent Python versions are:
0x30a0000 /* Py_PACK_VERSION(3,10) */
0x30b0000 /* Py_PACK_VERSION(3,11) */
0x30c0000 /* Py_PACK_VERSION(3,12) */
0x30d0000 /* Py_PACK_VERSION(3,13) */
0x30e0000 /* Py_PACK_VERSION(3,14) */
0x30f0000 /* Py_PACK_VERSION(3,15) */
When one of the macros is defined, Python.h will only expose API that is
compatible with the given Stable ABI -- that is, the
Limited API plus some definitions that need to be
visible to the compiler but should not be used directly.
When both are defined, Python.h will only expose API compatible with
both Stable ABIs.
-
Py_LIMITED_API¶
Target
abi3, that is, non-free-threaded builds of CPython. See above for common information.
-
Py_TARGET_ABI3T¶
Target
abi3t, that is, free-threaded builds of CPython. See above for common information.在 3.15 版被加入.
Both macros specify a target ABI; the different naming style is due to backwards compatibility.
Historical note
你還可以將 Py_LIMITED_API 定義為 3,這與 0x03020000(Python 3.2,引入了穩定 ABI 的版本)相同。
When both are defined, Python.h may, or may not, redefine
Py_LIMITED_API to match Py_TARGET_ABI3T.
On a free-threaded build -- that is, when
Py_GIL_DISABLED is defined -- Py_TARGET_ABI3T
defaults to the value of Py_LIMITED_API.
This means that there are two ways to build for both abi3 and abi3t:
define both
Py_LIMITED_APIandPy_TARGET_ABI3T, ordefine only
Py_LIMITED_APIand:on Windows, define
Py_GIL_DISABLED;on other systems, use the headers of free-threaded build of Python.
穩定 ABI 範圍和效能¶
穩定 ABI 的目標是允許使用完整的 C API 進行所有可能的操作,但可能會降低效能。一般來說,要與穩定 ABI 相容,會需要對擴充的原始碼做一些修改。
例如,雖然 PyList_GetItem() 可用,但它的「不安全」巨集變體 PyList_GET_ITEM() 為不可用。巨集運行可以更快,因為它可以依賴 list 物件的特定版本實作細節。
另一個例子是,如果不是針對穩定 ABI 進行編譯,一些 C API 函式將被嵌入或被替換為巨集。針對穩定 ABI 進行編譯會禁用嵌入,從而隨著 Python 資料結構的改進而提高穩定性,但可能會降低效能。
By leaving out the Py_LIMITED_API or Py_TARGET_ABI3T
definition, it is possible to compile Stable-ABI-compatible source
for a version-specific ABI.
A potentially faster version-specific extension can then be distributed
alongside a version compiled for Stable ABI -- a slower but more compatible
fallback.
穩定 ABI 注意事項¶
請注意,針對穩定 ABI 進行編譯並不完全保證程式碼會與預期的 Python 版本相容。穩定 ABI 可以防止 ABI 問題,例如因缺少符號而導致的連結器 (linker) 錯誤,或因結構布局或函式簽名變更而導致的資料損壞。然而,Python 的其他變化可能會改變擴充的行為。
Py_TARGET_ABI3T 和 Py_LIMITED_API 巨集無法防範的一個問題是使用在較低 Python 版本中無效的引數來呼叫函式。例如一個開始接受 NULL 作為引數的函式。在 Python 3.9 中,NULL 現在代表選擇預設行為,但在 Python 3.8 中,引數將被直接使用,導致 NULL 取消參照 (dereference) 且崩潰 (crash)。類似的論點也適用於結構 (struct) 的欄位。
出於這些原因,我們建議要以它支援的所有次要 Python 版本來測試擴充。
我們也建議要查看所有使用過的 API 的文件,檢查它是否明確屬於受限 API。即使有定義 Py_LIMITED_API,一些私有聲明也會因為技術原因(或者甚至是無意地,例如臭蟲)而被公開出來。
另請注意,雖然使用 Py_LIMITED_API 3.8 進行編譯意味著擴充應該能在 Python 3.12 上載入,並能以 Python 3.12 編譯,但同樣的原始碼不一定能在 Py_LIMITED_API 設為 3.12 時編譯。一般來說,只要穩定 ABI 保持穩定,部分受限 API 可能會被棄用和刪除。
平台注意事項¶
ABI 穩定性不僅取決於 Python,還取決於使用的編譯器、低階函式庫和編譯器選項。出於穩定 ABI 的目的,這些細節定義了一個「平台」。它們通常取決於作業系統種類和處理器架構
每個特定的 Python 發布者都有責任確保特定平台上的所有 Python 版本都以不破壞穩定 ABI 或特定版本 ABI 的方式建置。python.org 和許多第三方發布者發布的 Windows 和 macOS 版本就是這種情況。
ABI Checking¶
在 3.15 版被加入.
Python includes a rudimentary check for ABI compatibility.
This check is not comprehensive. It only guards against common cases of incompatible modules being installed for the wrong interpreter. It also does not take platform incompatibilities into account. It can only be done after an extension is successfully loaded.
Despite these limitations, it is recommended that extension modules use this mechanism, so that detectable incompatibilities raise exceptions rather than crash.
Most modules can use this check via the Py_mod_abi
slot and the PyABIInfo_VAR macro, for example like this:
PyABIInfo_VAR(abi_info);
static PyModuleDef_Slot mymodule_slots[] = {
{Py_mod_abi, &abi_info},
...
};
The full API is described below for advanced use cases.
-
int PyABIInfo_Check(PyABIInfo *info, const char *module_name)¶
- 為 穩定 ABI 的一部分 自 3.15 版本開始.
Verify that the given info is compatible with the currently running interpreter.
Return 0 on success. On failure, raise an exception and return -1.
If the ABI is incompatible, the raised exception will be
ImportError.The module_name argument can be
NULL, or point to a NUL-terminated UTF-8-encoded string used for error messages.Note that if info describes the ABI that the current code uses (as defined by
PyABIInfo_VAR, for example), using any other Python C API may lead to crashes. In particular, it is not safe to examine the raised exception.在 3.15 版被加入.
-
PyABIInfo_VAR(NAME)¶
- This macro is available when compiling for the 穩定 ABI 的一部分 自 3.15 版本開始.
Define a static
PyABIInfovariable with the given NAME that describes the ABI that the current code will use. This macro expands to:static PyABIInfo NAME = { 1, 0, PyABIInfo_DEFAULT_FLAGS, PY_VERSION_HEX, PyABIInfo_DEFAULT_ABI_VERSION }
在 3.15 版被加入.
-
type PyABIInfo¶
- 為 穩定 ABI 的一部分 (包含所有成員) 自 3.15 版本開始.
-
uint8_t abiinfo_major_version¶
The major version of
PyABIInfo. Can be set to:0to skip all checking, or1to specify this version ofPyABIInfo.
-
uint8_t abiinfo_minor_version¶
The minor version of
PyABIInfo. Must be set to0; larger values are reserved for backwards-compatible future versions ofPyABIInfo.
-
uint16_t flags¶
This field is usually set to the following macro:
-
PyABIInfo_DEFAULT_FLAGS¶
- 為 穩定 ABI 的一部分 自 3.15 版本開始.
Default flags, based on current values of macros such as
Py_LIMITED_APIandPy_GIL_DISABLED.
Alternately, the field can be set to the following flags, combined by bitwise OR. Unused bits must be set to zero.
ABI variant -- one of:
-
PyABIInfo_STABLE¶
- 為 穩定 ABI 的一部分 自 3.15 版本開始.
Specifies that Stable ABI is used.
-
PyABIInfo_INTERNAL¶
Specifies ABI specific to a particular build of CPython. Internal use only.
Free-threading compatibility -- one of:
-
PyABIInfo_FREETHREADED¶
- 為 穩定 ABI 的一部分 自 3.15 版本開始.
Specifies ABI compatible with free-threaded builds of CPython. (That is, ones compiled with
--disable-gil; withtinsys.abiflags)
-
PyABIInfo_GIL¶
- 為 穩定 ABI 的一部分 自 3.15 版本開始.
Specifies ABI compatible with non-free-threaded builds of CPython (ones compiled without
--disable-gil).
-
PyABIInfo_FREETHREADING_AGNOSTIC¶
- 為 穩定 ABI 的一部分 自 3.15 版本開始.
Specifies ABI compatible with both free-threaded and non-free-threaded builds of CPython, that is, both
abi3andabi3t.
-
PyABIInfo_DEFAULT_FLAGS¶
-
uint32_t build_version¶
The version of the Python headers used to build the code, in the format used by
PY_VERSION_HEX.This can be set to
0to skip any checks related to this field. This option is meant mainly for projects that do not use the CPython headers directly, and do not emulate a specific version of them.
-
uint32_t abi_version¶
The ABI version.
For Stable ABI, this field should be the value of
Py_LIMITED_APIorPy_TARGET_ABI3T. If both are defined, use the smaller value. (IfPy_LIMITED_APIis3; use Py_PACK_VERSION(3, 2) instead of3.)Otherwise, it should be set to
PY_VERSION_HEX.It can also be set to
0to skip any checks related to this field.-
PyABIInfo_DEFAULT_ABI_VERSION¶
- 為 穩定 ABI 的一部分 自 3.15 版本開始.
The value that should be used for this field, based on current values of macros such as
Py_LIMITED_API,PY_VERSION_HEXandPy_GIL_DISABLED.
-
PyABIInfo_DEFAULT_ABI_VERSION¶
在 3.15 版被加入.
-
uint8_t abiinfo_major_version¶
受限 API 的內容¶
這是 Python 3.15 的受限 API 的權威清單:
PyWeakReferencePy_FileSystemDefaultEncodeErrorsPy_FileSystemDefaultEncodingPy_HasFileSystemDefaultEncodingPy_UTF8ModePy_intptr_tPy_uintptr_tssizessizeargfuncssizessizeobjargprocsymtable