• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. highlight:: c
2
3.. _stable:
4
5***************
6C API Stability
7***************
8
9Python's C API is covered by the Backwards Compatibility Policy, :pep:`387`.
10While the C API will change with every minor release (e.g. from 3.9 to 3.10),
11most changes will be source-compatible, typically by only adding new API.
12Changing existing API or removing API is only done after a deprecation period
13or to fix serious issues.
14
15CPython's Application Binary Interface (ABI) is forward- and
16backwards-compatible across a minor release (if these are compiled the same
17way; see :ref:`stable-abi-platform` below).
18So, code compiled for Python 3.10.0 will work on 3.10.8 and vice versa,
19but will need to be compiled separately for 3.9.x and 3.10.x.
20
21Names prefixed by an underscore, such as ``_Py_InternalState``,
22are private API that can change without notice even in patch releases.
23
24
25Stable Application Binary Interface
26===================================
27
28Python 3.2 introduced the *Limited API*, a subset of Python's C API.
29Extensions that only use the Limited API can be
30compiled once and work with multiple versions of Python.
31Contents of the Limited API are :ref:`listed below <stable-abi-list>`.
32
33To enable this, Python provides a *Stable ABI*: a set of symbols that will
34remain compatible across Python 3.x versions. The Stable ABI contains symbols
35exposed in the Limited API, but also other ones – for example, functions
36necessary to support older versions of the Limited API.
37
38(For simplicity, this document talks about *extensions*, but the Limited API
39and Stable ABI work the same way for all uses of the API – for example,
40embedding Python.)
41
42.. c:macro:: Py_LIMITED_API
43
44   Define this macro before including ``Python.h`` to opt in to only use
45   the Limited API, and to select the Limited API version.
46
47   Define ``Py_LIMITED_API`` to the value of :c:data:`PY_VERSION_HEX`
48   corresponding to the lowest Python version your extension supports.
49   The extension will work without recompilation with all Python 3 releases
50   from the specified one onward, and can use Limited API introduced up to that
51   version.
52
53   Rather than using the ``PY_VERSION_HEX`` macro directly, hardcode a minimum
54   minor version (e.g. ``0x030A0000`` for Python 3.10) for stability when
55   compiling with future Python versions.
56
57   You can also define ``Py_LIMITED_API`` to ``3``. This works the same as
58   ``0x03020000`` (Python 3.2, the version that introduced Limited API).
59
60On Windows, extensions that use the Stable ABI should be linked against
61``python3.dll`` rather than a version-specific library such as
62``python39.dll``.
63
64On some platforms, Python will look for and load shared library files named
65with the ``abi3`` tag (e.g. ``mymodule.abi3.so``).
66It does not check if such extensions conform to a Stable ABI.
67The user (or their packaging tools) need to ensure that, for example,
68extensions built with the 3.10+ Limited API are not installed for lower
69versions of Python.
70
71All functions in the Stable ABI are present as functions in Python's shared
72library, not solely as macros. This makes them usable from languages that don't
73use the C preprocessor.
74
75
76Limited API Scope and Performance
77---------------------------------
78
79The goal for the Limited API is to allow everything that is possible with the
80full C API, but possibly with a performance penalty.
81
82For example, while :c:func:`PyList_GetItem` is available, its “unsafe” macro
83variant :c:func:`PyList_GET_ITEM` is not.
84The macro can be faster because it can rely on version-specific implementation
85details of the list object.
86
87Without ``Py_LIMITED_API`` defined, some C API functions are inlined or
88replaced by macros.
89Defining ``Py_LIMITED_API`` disables this inlining, allowing stability as
90Python's data structures are improved, but possibly reducing performance.
91
92By leaving out the ``Py_LIMITED_API`` definition, it is possible to compile
93a Limited API extension with a version-specific ABI. This can improve
94performance for that Python version, but will limit compatibility.
95Compiling with ``Py_LIMITED_API`` will then yield an extension that can be
96distributed where a version-specific one is not available – for example,
97for prereleases of an upcoming Python version.
98
99
100Limited API Caveats
101-------------------
102
103Note that compiling with ``Py_LIMITED_API`` is *not* a complete guarantee that
104code conforms to the Limited API or the Stable ABI. ``Py_LIMITED_API`` only
105covers definitions, but an API also includes other issues, such as expected
106semantics.
107
108One issue that ``Py_LIMITED_API`` does not guard against is calling a function
109with arguments that are invalid in a lower Python version.
110For example, consider a function that starts accepting ``NULL`` for an
111argument. In Python 3.9, ``NULL`` now selects a default behavior, but in
112Python 3.8, the argument will be used directly, causing a ``NULL`` dereference
113and crash. A similar argument works for fields of structs.
114
115Another issue is that some struct fields are currently not hidden when
116``Py_LIMITED_API`` is defined, even though they're part of the Limited API.
117
118For these reasons, we recommend testing an extension with *all* minor Python
119versions it supports, and preferably to build with the *lowest* such version.
120
121We also recommend reviewing documentation of all used API to check
122if it is explicitly part of the Limited API. Even with ``Py_LIMITED_API``
123defined, a few private declarations are exposed for technical reasons (or
124even unintentionally, as bugs).
125
126Also note that the Limited API is not necessarily stable: compiling with
127``Py_LIMITED_API`` with Python 3.8 means that the extension will
128run with Python 3.12, but it will not necessarily *compile* with Python 3.12.
129In particular, parts of the Limited API may be deprecated and removed,
130provided that the Stable ABI stays stable.
131
132
133.. _stable-abi-platform:
134
135Platform Considerations
136=======================
137
138ABI stability depends not only on Python, but also on the compiler used,
139lower-level libraries and compiler options. For the purposes of the Stable ABI,
140these details define a “platform”. They usually depend on the OS
141type and processor architecture
142
143It is the responsibility of each particular distributor of Python
144to ensure that all Python versions on a particular platform are built
145in a way that does not break the Stable ABI.
146This is the case with Windows and macOS releases from ``python.org`` and many
147third-party distributors.
148
149
150.. _stable-abi-list:
151
152Contents of Limited API
153=======================
154
155
156Currently, the Limited API includes the following items:
157
158.. limited-api-list::
159