17db96d56Sopenharmony_ci.. highlight:: c
27db96d56Sopenharmony_ci
37db96d56Sopenharmony_ci.. _stable:
47db96d56Sopenharmony_ci
57db96d56Sopenharmony_ci***************
67db96d56Sopenharmony_ciC API Stability
77db96d56Sopenharmony_ci***************
87db96d56Sopenharmony_ci
97db96d56Sopenharmony_ciPython's C API is covered by the Backwards Compatibility Policy, :pep:`387`.
107db96d56Sopenharmony_ciWhile the C API will change with every minor release (e.g. from 3.9 to 3.10),
117db96d56Sopenharmony_cimost changes will be source-compatible, typically by only adding new API.
127db96d56Sopenharmony_ciChanging existing API or removing API is only done after a deprecation period
137db96d56Sopenharmony_cior to fix serious issues.
147db96d56Sopenharmony_ci
157db96d56Sopenharmony_ciCPython's Application Binary Interface (ABI) is forward- and
167db96d56Sopenharmony_cibackwards-compatible across a minor release (if these are compiled the same
177db96d56Sopenharmony_ciway; see :ref:`stable-abi-platform` below).
187db96d56Sopenharmony_ciSo, code compiled for Python 3.10.0 will work on 3.10.8 and vice versa,
197db96d56Sopenharmony_cibut will need to be compiled separately for 3.9.x and 3.10.x.
207db96d56Sopenharmony_ci
217db96d56Sopenharmony_ciNames prefixed by an underscore, such as ``_Py_InternalState``,
227db96d56Sopenharmony_ciare private API that can change without notice even in patch releases.
237db96d56Sopenharmony_ci
247db96d56Sopenharmony_ci
257db96d56Sopenharmony_ciStable Application Binary Interface
267db96d56Sopenharmony_ci===================================
277db96d56Sopenharmony_ci
287db96d56Sopenharmony_ciPython 3.2 introduced the *Limited API*, a subset of Python's C API.
297db96d56Sopenharmony_ciExtensions that only use the Limited API can be
307db96d56Sopenharmony_cicompiled once and work with multiple versions of Python.
317db96d56Sopenharmony_ciContents of the Limited API are :ref:`listed below <stable-abi-list>`.
327db96d56Sopenharmony_ci
337db96d56Sopenharmony_ciTo enable this, Python provides a *Stable ABI*: a set of symbols that will
347db96d56Sopenharmony_ciremain compatible across Python 3.x versions. The Stable ABI contains symbols
357db96d56Sopenharmony_ciexposed in the Limited API, but also other ones – for example, functions
367db96d56Sopenharmony_cinecessary to support older versions of the Limited API.
377db96d56Sopenharmony_ci
387db96d56Sopenharmony_ci(For simplicity, this document talks about *extensions*, but the Limited API
397db96d56Sopenharmony_ciand Stable ABI work the same way for all uses of the API – for example,
407db96d56Sopenharmony_ciembedding Python.)
417db96d56Sopenharmony_ci
427db96d56Sopenharmony_ci.. c:macro:: Py_LIMITED_API
437db96d56Sopenharmony_ci
447db96d56Sopenharmony_ci   Define this macro before including ``Python.h`` to opt in to only use
457db96d56Sopenharmony_ci   the Limited API, and to select the Limited API version.
467db96d56Sopenharmony_ci
477db96d56Sopenharmony_ci   Define ``Py_LIMITED_API`` to the value of :c:data:`PY_VERSION_HEX`
487db96d56Sopenharmony_ci   corresponding to the lowest Python version your extension supports.
497db96d56Sopenharmony_ci   The extension will work without recompilation with all Python 3 releases
507db96d56Sopenharmony_ci   from the specified one onward, and can use Limited API introduced up to that
517db96d56Sopenharmony_ci   version.
527db96d56Sopenharmony_ci
537db96d56Sopenharmony_ci   Rather than using the ``PY_VERSION_HEX`` macro directly, hardcode a minimum
547db96d56Sopenharmony_ci   minor version (e.g. ``0x030A0000`` for Python 3.10) for stability when
557db96d56Sopenharmony_ci   compiling with future Python versions.
567db96d56Sopenharmony_ci
577db96d56Sopenharmony_ci   You can also define ``Py_LIMITED_API`` to ``3``. This works the same as
587db96d56Sopenharmony_ci   ``0x03020000`` (Python 3.2, the version that introduced Limited API).
597db96d56Sopenharmony_ci
607db96d56Sopenharmony_ciOn Windows, extensions that use the Stable ABI should be linked against
617db96d56Sopenharmony_ci``python3.dll`` rather than a version-specific library such as
627db96d56Sopenharmony_ci``python39.dll``.
637db96d56Sopenharmony_ci
647db96d56Sopenharmony_ciOn some platforms, Python will look for and load shared library files named
657db96d56Sopenharmony_ciwith the ``abi3`` tag (e.g. ``mymodule.abi3.so``).
667db96d56Sopenharmony_ciIt does not check if such extensions conform to a Stable ABI.
677db96d56Sopenharmony_ciThe user (or their packaging tools) need to ensure that, for example,
687db96d56Sopenharmony_ciextensions built with the 3.10+ Limited API are not installed for lower
697db96d56Sopenharmony_civersions of Python.
707db96d56Sopenharmony_ci
717db96d56Sopenharmony_ciAll functions in the Stable ABI are present as functions in Python's shared
727db96d56Sopenharmony_cilibrary, not solely as macros. This makes them usable from languages that don't
737db96d56Sopenharmony_ciuse the C preprocessor.
747db96d56Sopenharmony_ci
757db96d56Sopenharmony_ci
767db96d56Sopenharmony_ciLimited API Scope and Performance
777db96d56Sopenharmony_ci---------------------------------
787db96d56Sopenharmony_ci
797db96d56Sopenharmony_ciThe goal for the Limited API is to allow everything that is possible with the
807db96d56Sopenharmony_cifull C API, but possibly with a performance penalty.
817db96d56Sopenharmony_ci
827db96d56Sopenharmony_ciFor example, while :c:func:`PyList_GetItem` is available, its “unsafe” macro
837db96d56Sopenharmony_civariant :c:func:`PyList_GET_ITEM` is not.
847db96d56Sopenharmony_ciThe macro can be faster because it can rely on version-specific implementation
857db96d56Sopenharmony_cidetails of the list object.
867db96d56Sopenharmony_ci
877db96d56Sopenharmony_ciWithout ``Py_LIMITED_API`` defined, some C API functions are inlined or
887db96d56Sopenharmony_cireplaced by macros.
897db96d56Sopenharmony_ciDefining ``Py_LIMITED_API`` disables this inlining, allowing stability as
907db96d56Sopenharmony_ciPython's data structures are improved, but possibly reducing performance.
917db96d56Sopenharmony_ci
927db96d56Sopenharmony_ciBy leaving out the ``Py_LIMITED_API`` definition, it is possible to compile
937db96d56Sopenharmony_cia Limited API extension with a version-specific ABI. This can improve
947db96d56Sopenharmony_ciperformance for that Python version, but will limit compatibility.
957db96d56Sopenharmony_ciCompiling with ``Py_LIMITED_API`` will then yield an extension that can be
967db96d56Sopenharmony_cidistributed where a version-specific one is not available – for example,
977db96d56Sopenharmony_cifor prereleases of an upcoming Python version.
987db96d56Sopenharmony_ci
997db96d56Sopenharmony_ci
1007db96d56Sopenharmony_ciLimited API Caveats
1017db96d56Sopenharmony_ci-------------------
1027db96d56Sopenharmony_ci
1037db96d56Sopenharmony_ciNote that compiling with ``Py_LIMITED_API`` is *not* a complete guarantee that
1047db96d56Sopenharmony_cicode conforms to the Limited API or the Stable ABI. ``Py_LIMITED_API`` only
1057db96d56Sopenharmony_cicovers definitions, but an API also includes other issues, such as expected
1067db96d56Sopenharmony_cisemantics.
1077db96d56Sopenharmony_ci
1087db96d56Sopenharmony_ciOne issue that ``Py_LIMITED_API`` does not guard against is calling a function
1097db96d56Sopenharmony_ciwith arguments that are invalid in a lower Python version.
1107db96d56Sopenharmony_ciFor example, consider a function that starts accepting ``NULL`` for an
1117db96d56Sopenharmony_ciargument. In Python 3.9, ``NULL`` now selects a default behavior, but in
1127db96d56Sopenharmony_ciPython 3.8, the argument will be used directly, causing a ``NULL`` dereference
1137db96d56Sopenharmony_ciand crash. A similar argument works for fields of structs.
1147db96d56Sopenharmony_ci
1157db96d56Sopenharmony_ciAnother issue is that some struct fields are currently not hidden when
1167db96d56Sopenharmony_ci``Py_LIMITED_API`` is defined, even though they're part of the Limited API.
1177db96d56Sopenharmony_ci
1187db96d56Sopenharmony_ciFor these reasons, we recommend testing an extension with *all* minor Python
1197db96d56Sopenharmony_civersions it supports, and preferably to build with the *lowest* such version.
1207db96d56Sopenharmony_ci
1217db96d56Sopenharmony_ciWe also recommend reviewing documentation of all used API to check
1227db96d56Sopenharmony_ciif it is explicitly part of the Limited API. Even with ``Py_LIMITED_API``
1237db96d56Sopenharmony_cidefined, a few private declarations are exposed for technical reasons (or
1247db96d56Sopenharmony_cieven unintentionally, as bugs).
1257db96d56Sopenharmony_ci
1267db96d56Sopenharmony_ciAlso note that the Limited API is not necessarily stable: compiling with
1277db96d56Sopenharmony_ci``Py_LIMITED_API`` with Python 3.8 means that the extension will
1287db96d56Sopenharmony_cirun with Python 3.12, but it will not necessarily *compile* with Python 3.12.
1297db96d56Sopenharmony_ciIn particular, parts of the Limited API may be deprecated and removed,
1307db96d56Sopenharmony_ciprovided that the Stable ABI stays stable.
1317db96d56Sopenharmony_ci
1327db96d56Sopenharmony_ci
1337db96d56Sopenharmony_ci.. _stable-abi-platform:
1347db96d56Sopenharmony_ci
1357db96d56Sopenharmony_ciPlatform Considerations
1367db96d56Sopenharmony_ci=======================
1377db96d56Sopenharmony_ci
1387db96d56Sopenharmony_ciABI stability depends not only on Python, but also on the compiler used,
1397db96d56Sopenharmony_cilower-level libraries and compiler options. For the purposes of the Stable ABI,
1407db96d56Sopenharmony_cithese details define a “platform”. They usually depend on the OS
1417db96d56Sopenharmony_citype and processor architecture
1427db96d56Sopenharmony_ci
1437db96d56Sopenharmony_ciIt is the responsibility of each particular distributor of Python
1447db96d56Sopenharmony_cito ensure that all Python versions on a particular platform are built
1457db96d56Sopenharmony_ciin a way that does not break the Stable ABI.
1467db96d56Sopenharmony_ciThis is the case with Windows and macOS releases from ``python.org`` and many
1477db96d56Sopenharmony_cithird-party distributors.
1487db96d56Sopenharmony_ci
1497db96d56Sopenharmony_ci
1507db96d56Sopenharmony_ci.. _stable-abi-list:
1517db96d56Sopenharmony_ci
1527db96d56Sopenharmony_ciContents of Limited API
1537db96d56Sopenharmony_ci=======================
1547db96d56Sopenharmony_ci
1557db96d56Sopenharmony_ci
1567db96d56Sopenharmony_ciCurrently, the Limited API includes the following items:
1577db96d56Sopenharmony_ci
1587db96d56Sopenharmony_ci.. limited-api-list::
159