@@ -62,18 +62,13 @@ versions of Python, without recompilation.
6262
6363.. note ::
6464
65- For simplicity, this document talks about *extensions *, but the Stable ABI
65+ For simplicity, this document talks about *extensions *, but Stable ABI
6666 works the same way for all uses of the API – for example, embedding Python.
6767
68- A Stable ABI is *versioned * using the first two numbers of the Python version.
69- For example, Stable ABI 3.14 corresponds to Python 3.14.
70- An extension compiled for Stable ABI 3.x is ABI-compatible with Python 3.x
71- and above.
72-
7368There are two Stable ABIs:
7469
7570- ``abi3 ``, introduced in Pyton 3.2, is compatible with
76- non-:term: `free threaded <free-threaded build> ` builds of CPython.
71+ ** non ** -:term: `free threaded <free-threaded build> ` builds of CPython.
7772
7873- ``abi3t ``, introduced in Pyton 3.15, is compatible with
7974 :term: `free threaded <free-threaded build> ` builds of CPython.
@@ -84,50 +79,47 @@ There are two Stable ABIs:
8479 ``abi3t `` was added in :pep: `803 `
8580
8681It is possible for an extension to be compiled for *both * ``abi3 `` and
87- ``abi3t `` at the same time.
82+ ``abi3t `` at the same time; the result will be compatible with
83+ both free-threaded and non-free-threaded builds of Python.
8884Currently, this has no downsides compared to compiling for ``abi3t `` only.
8985
90- The Stable ABIs come with several caveats:
91-
92- - Extensions that target a stable ABI must only use a limited subset of
93- the C API. This subset is known as the :dfn: `Limited API `; its contents
94- are :ref: `listed below <limited-api-list >`.
95-
96- - Compiling for a Stable ABI will disable some optimizations.
97- In particular, common functions cannot be inlined to take advantage of the
98- internal implementation details.
86+ Each Stable ABI is versioned using the first two numbers of the Python version.
87+ For example, Stable ABI 3.14 corresponds to Python 3.14.
88+ An extension compiled for Stable ABI 3.x is ABI-compatible with Python 3.x
89+ and above.
9990
100- - Stable ABI prevents *ABI * issues, like linker errors due to missing
101- symbols or data corruption due to changes in structure layouts or function
102- signatures.
103- However, other changes in Python can change the *behavior * of extensions.
104- See Python's Backwards Compatibility Policy (:pep: `387 `) for details.
91+ Extensions that target a stable ABI must only use a limited subset of
92+ the C API. This subset is known as the :dfn: `Limited API `; its contents
93+ are :ref: `listed below <limited-api-list >`.
10594
10695On Windows, extensions that use a Stable ABI should be linked against
10796``python3.dll `` rather than a version-specific library such as
10897``python39.dll ``.
10998This library only exposes the relevant symbols.
11099
111100On some platforms, Python will look for and load shared library files named
112- with the ``abi3 `` or ``abi3t `` tag (e.g. ``mymodule.abi3.so ``).
101+ with the ``abi3 `` or ``abi3t `` tag (for example, ``mymodule.abi3.so ``).
113102:term: `Free threaded <free-threaded build> ` interpreters only recognize the
114103``abi3t `` tag, while non-free threaded ones will prefer ``abi3 `` but fall back
115104to ``abi3t ``.
116- Thus, extensions compatible with both flavors should use the ``abi3t `` tag.
105+ Thus, extensions compatible with both ABIs should use the ``abi3t `` tag.
117106
118- Python does not check if such extensions conform to a Stable ABI.
119- Extension authors are encouraged to check using the :c:macro: `Py_mod_abi `
120- slot or the :c:func: `PyABIInfo_Check ` function, but, the user
107+ Python does not necessarily check that extensions it loads
108+ have compatible ABI.
109+ Extension authors are encouraged to add a check using the :c:macro: `Py_mod_abi `
110+ slot or the :c:func: `PyABIInfo_Check ` function, but the user
121111(or their packaging tool) is ultimately responsible for ensuring that,
122112for example, extensions built for Stable ABI 3.10 are not installed for lower
123113versions of Python.
124114
125- All functions in the Stable ABI are present as functions in Python's shared
115+ All functions in Stable ABI are present as functions in Python's shared
126116library, not solely as macros.
127- They are usable from languages that don't use the C preprocessor,
128- such as Python via :py:mod: `ctypes `.
117+ This makes them usable are usable from languages that don't use the C
118+ preprocessor, including Python's :py:mod: `ctypes `.
129119
130120
121+ .. _abi3-compiling :
122+
131123Compiling for Stable ABI
132124------------------------
133125
@@ -138,22 +130,23 @@ Compiling for Stable ABI
138130 extension filenames and other metadata.
139131 Prefer using the tool's options over defining the macros manually.
140132
141- The rest of this section is relevant for tool authors, and for people who
142- compile extensions manually.
133+ The rest of this section is mainly relevant for tool authors, and for
134+ people who compile extensions manually.
143135
144136 .. seealso :: `list of recommended tools`_ in the Python Packaging User Guide
145137
146138 .. _list of recommended tools : https://packaging.python.org/en/latest/guides/tool-recommendations/#build-backends-for-extension-modules
147139
148140To compile for a Stable ABI, define one or both of the following macros
149- before including `` Python.h `` to the lowest Python version your extension
150- should support, in :c:macro: `Py_PACK_VERSION ` format.
141+ to the lowest Python version your extension should support, in
142+ :c:macro: `Py_PACK_VERSION ` format.
151143Typically, you should choose a specific value rather than the version of
152144the Python headers you are compiling against.
153145
154- Since the :c:macro: `Py_PACK_VERSION ` is not available before including
155- ``Python.h ``, you will need to use the number directly.
156- For reference, the values for a few Python versions are:
146+ The macro(s) must be defined before including ``Python.h ``.
147+ Since :c:macro: `Py_PACK_VERSION ` is not available at this point, you
148+ will need to use the numeric value directly.
149+ For reference, the values for a few recent Python versions are:
157150
158151.. version-hex-cheatsheet ::
159152
@@ -166,16 +159,18 @@ visible to the compiler but should not be used directly.
166159
167160 Target ``abi3 ``, that is,
168161 non-:term: `free threaded <free-threaded build> ` builds of CPython.
162+ See :ref: `above <abi3-compiling >` for common information.
169163
170164.. c :macro :: Py_TARGET_ABI3T
171165
172166 Target ``abi3t ``, that is,
173167 :term: `free threaded <free-threaded build> ` builds of CPython.
168+ See :ref: `above <abi3-compiling >` for common information.
174169
175170 .. versionadded :: next
176171
177- Despite the different naming, the macros are similar;
178- the name :c:macro: ` !Py_LIMITED_API ` is kept for backwards compatibility.
172+ Both macros specify a target ABI; the different naming style is due to
173+ backwards compatibility.
179174
180175.. admonition :: Historical note
181176
@@ -212,30 +207,33 @@ functions are inlined or replaced by macros.
212207Compiling for Stable ABI disables this inlining, allowing stability as
213208Python's data structures are improved, but possibly reducing performance.
214209
215- By leaving out the :c:macro: `!Py_LIMITED_API` ` or :c:macro: `!Py_TARGET_ABI3T `
210+ By leaving out the :c:macro: `!Py_LIMITED_API ` or :c:macro: `!Py_TARGET_ABI3T `
216211definition, it is possible to compile Stable-ABI-compatible source
217- for a version-specific ABI, possibly improving performance for a specific
218- Python version.
219-
220-
221- Limited API Caveats
222- -------------------
223-
224- Note that compiling with :c:macro: `Py_LIMITED_API ` or :c:macro: `Py_TARGET_ABI3T `
225- is *not * a complete guarantee that code will be compatible with the
226- expected Python versions.
227- The macros only cover definitions, not other issues such as expected semantics.
228-
229- One issue that the macros do not guard against is calling a function
230- with arguments that are invalid in a lower Python version.
212+ for a version-specific ABI.
213+ A potentially faster version-specific extension can then be distributed
214+ alongside a version compiled for Stable ABI -- a slower but more compatible
215+ fallback.
216+
217+
218+ Stable ABI Caveats
219+ ------------------
220+
221+ Note that compiling for Stable ABI is *not * a complete guarantee that code will
222+ be compatible with the expected Python versions.
223+ Stable ABI prevents *ABI * issues, like linker errors due to missing
224+ symbols or data corruption due to changes in structure layouts or function
225+ signatures.
226+ However, other changes in Python can change the *behavior * of extensions.
227+ See Python's Backwards Compatibility Policy (:pep: `387 `) for details.
228+
229+ One issue that the :c:macro: `Py_TARGET_ABI3T ` and :c:macro: `Py_LIMITED_API `
230+ macros do not guard against is calling a function with arguments that are
231+ invalid in a lower Python version.
231232For example, consider a function that starts accepting ``NULL `` for an
232233argument. In Python 3.9, ``NULL `` now selects a default behavior, but in
233234Python 3.8, the argument will be used directly, causing a ``NULL `` dereference
234235and crash. A similar argument works for fields of structs.
235236
236- Another issue is that some struct fields are currently not hidden when
237- the macros are defined, even though they're part of the Limited API.
238-
239237For these reasons, we recommend testing an extension with *all * minor Python
240238versions it supports.
241239
@@ -245,11 +243,11 @@ defined, a few private declarations are exposed for technical reasons (or
245243even unintentionally, as bugs).
246244
247245Also note that while compiling with ``Py_LIMITED_API `` 3.8 means that the
248- extension will *load * on Python 3.12, and *compile * with Python 3.12,
246+ extension should *load * on Python 3.12, and *compile * with Python 3.12,
249247the same source will not necessarily compile with ``Py_LIMITED_API ``
250248set to 3.12.
251- In general: parts of the Limited API may be deprecated and removed,
252- provided that the Stable ABI stays stable.
249+ In general, parts of the Limited API may be deprecated and removed,
250+ provided that Stable ABI stays stable.
253251
254252
255253.. _stable-abi-platform :
@@ -372,7 +370,7 @@ The full API is described below for advanced use cases.
372370
373371 .. c :macro :: PyABIInfo_STABLE
374372
375- Specifies that the stable ABI is used.
373+ Specifies that Stable ABI is used.
376374
377375 .. c :macro :: PyABIInfo_INTERNAL
378376
@@ -383,15 +381,22 @@ The full API is described below for advanced use cases.
383381
384382 .. c :macro :: PyABIInfo_FREETHREADED
385383
386- Specifies ABI compatible with free-threading builds of CPython.
384+ Specifies ABI compatible with :term: `free-threaded builds
385+ <free-threaded build> ` of CPython.
387386 (That is, ones compiled with :option: `--disable-gil `; with ``t ``
388387 in :py:data: `sys.abiflags `)
389388
390389 .. c :macro :: PyABIInfo_GIL
391390
392- Specifies ABI compatible with non-free-threading builds of CPython
391+ Specifies ABI compatible with non-free-threaded builds of CPython
393392 (ones compiled *without * :option: `--disable-gil `).
394393
394+ .. c :macro :: PyABIInfo_FREETHREADING_AGNOSTIC
395+
396+ Specifies ABI compatible with both free-threaded and
397+ non-free-threaded builds of CPython, that is, both
398+ ``abi3 `` and ``abi3t ``.
399+
395400 .. c :member :: uint32_t build_version
396401
397402 The version of the Python headers used to build the code, in the format
@@ -405,10 +410,11 @@ The full API is described below for advanced use cases.
405410
406411 The ABI version.
407412
408- For the Stable ABI, this field should be the value of
409- :c:macro: `Py_LIMITED_API `
410- (except if :c:macro: `Py_LIMITED_API ` is ``3 ``; use
411- :c:expr: `Py_PACK_VERSION(3, 2) ` in that case).
413+ For Stable ABI, this field should be the value of
414+ :c:macro: `Py_LIMITED_API ` or :c:macro: `Py_TARGET_ABI3T `.
415+ If both are defined, use the smaller value.
416+ (If :c:macro: `Py_LIMITED_API ` is ``3 ``; use
417+ :c:expr: `Py_PACK_VERSION(3, 2) ` instead of ``3 ``.)
412418
413419 Otherwise, it should be set to :c:macro: `PY_VERSION_HEX `.
414420
0 commit comments