C API and ABI Stability¶
مگر آنکه خلاف آن مستند شده باشد، C API پایتون مشمول سیاست سازگاری رو به عقب، PEP 387، است. بیشتر تغییرات آن با کد منبع سازگار هستند (source-compatible) (معمولاً فقط با افزودن APIهای جدید). تغییر APIهای موجود یا حذف API تنها پس از یک دورهی منسوخسازی یا برای رفع مشکلات جدی انجام میشود.
رابط دودویی برنامه (ABI) سیپایتون در طول یک انتشار جزئی، به جلو و به عقب سازگار است (اگر اینها به یک شکل کامپایل شده باشند؛ به ملاحظات پلتفرم در ادامه مراجعه کنید). بنابراین، کدی که برای پایتون 3.10.0 کامپایل شده باشد، روی 3.10.8 کار خواهد کرد و برعکس، اما برای 3.9.x و 3.11.x باید جداگانه کامپایل شود.
دو سطح از C API با انتظارات پایداری متفاوت وجود دارد:
API ناپایدار، ممکن است در نسخههای جزئی بدون دورهی منسوخسازی تغییر کند. این API با پیشوند
PyUnstableدر نامها مشخص میشود.API محدود، در چندین نسخهی فرعی سازگار است. هنگامی که
Py_LIMITED_APIتعریف شود، فقط این زیرمجموعه ازPython.hدر دسترس قرار میگیرد.
این موارد در ادامه با جزئیات بیشتر بررسی میشوند.
نامهایی که با خط زیر آغاز میشوند، مانند _Py_InternalState، API خصوصی هستند و میتوانند حتی در نسخههای وصله (patch) بدون اطلاع قبلی تغییر کنند. اگر نیاز به استفاده از این API دارید، در نظر بگیرید که با توسعهدهندگان سیپایتون ارتباط برقرار کنید تا دربارهی افزودن API عمومی برای مورد استفادهی خود گفتوگو کنید.
API ناپایدار C¶
هر API که با پیشوند PyUnstable نامگذاری شده باشد، جزئیات پیادهسازی سیپایتون را آشکار میکند و ممکن است در هر انتشار جزئی (برای مثال از 3.9 به 3.10) بدون هیچ هشدار منسوخشدن تغییر کند. با این حال، در انتشار رفع اشکال (برای مثال از 3.10.0 به 3.10.1) تغییر نخواهد کرد.
این بهطور کلی برای ابزارهای تخصصی و سطح پایین مانند اشکالزداها در نظر گرفته شده است.
انتظار میرود پروژههایی که از این API استفاده میکنند، توسعهی سیپایتون را دنبال کنند و برای سازگاری با تغییرات تلاش اضافی صرف کنند.
Stable Application Binary Interfaces¶
Python's Stable ABI allows extensions to be compatible with multiple versions of Python, without recompilation.
توجه
For simplicity, this document talks about extensions, but Stable ABI works the same way for all uses of the API – for example, 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.
On Windows, extensions that use a Stable ABI should be linked against
python3.dll rather than a version-specific library such as
python39.dll.
This library only exposes the relevant symbols.
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.
All functions in Stable ABI are present as functions in Python's shared
library, not solely as macros.
This makes them usable in languages that don't use the C
preprocessor, including Python's 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:
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) */
0x3100000 /* Py_PACK_VERSION(3,16) */
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
You can also define Py_LIMITED_API as 3. This works the same as
0x03020000 (Python 3.2, the version that introduced Stable 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.
Stable ABI Scope and Performance¶
The goal for Stable ABI is to allow everything that is possible with the full C API, but possibly with a performance penalty. Generally, compatibility with Stable ABI will require some changes to an extension's source code.
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.
For another example, when not compiling for Stable ABI, some C API functions are inlined or replaced by macros. Compiling for Stable ABI disables this inlining, allowing stability as Python's data structures are improved, but possibly reducing performance.
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.
Stable ABI Caveats¶
Note that compiling for Stable ABI is not a complete guarantee that code will be compatible with the expected Python versions. 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.
One issue that the Py_TARGET_ABI3T and Py_LIMITED_API
macros do 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.
For these reasons, we recommend testing an extension with all minor Python versions it supports.
همچنین توصیه میکنیم مستندات تمام APIهای استفادهشده را بررسی کنید تا مشخص شود که آیا بهطور صریح بخشی از API محدود هستند یا خیر. حتی با تعریف Py_LIMITED_API، چند اعلان خصوصی به دلایل فنی (یا حتی ناخواسته، بهعنوان باگ) در دسترس قرار میگیرند.
Also note that while compiling with Py_LIMITED_API 3.8 means that the
extension should load on Python 3.12, and compile with Python 3.12,
the same source will not necessarily compile with Py_LIMITED_API
set to 3.12.
In general, parts of the Limited API may be deprecated and removed,
provided that 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 ABIs, 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 ABIs, or the version-specific ABIs.
This is the case with Windows and macOS releases from python.org and many
third-party distributors.
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)¶
- قسمتی از 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 محدود¶
This is the definitive list of Limited API for Python 3.16:
PyWeakReferencePy_intptr_tPy_uintptr_tssizessizeargfuncssizessizeobjargprocsymtable