Skip to content

Commit c5740af

Browse files
committed
Add docs
1 parent c015606 commit c5740af

2 files changed

Lines changed: 175 additions & 3 deletions

File tree

Doc/c-api/module.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,28 @@ The available slot types are:
388388
389389
.. versionadded:: 3.13
390390
391+
.. c:macro:: Py_mod_abi
392+
393+
A pointer to a :c:struct:`PyABIInfo` structure that describes the ABI that
394+
the extension is using.
395+
396+
When the module is loaded, the :c:struct:`!PyABIInfo` in this slot is checked
397+
using :c:func:`PyABIInfo_Check`.
398+
399+
A suitable :c:struct:`!PyABIInfo` variable can be defined using the
400+
:c:macro:`PyABIInfo_VAR` macro, as in:
401+
402+
.. code-block:: c
403+
404+
PyABIInfo_VAR(abi_info);
405+
406+
static PyModuleDef_Slot mymodule_slots[] = {
407+
{Py_mod_abi, &abi_info},
408+
...
409+
};
410+
411+
.. versionadded:: 3.15
412+
391413
392414
.. _moduledef-dynamic:
393415

Doc/c-api/stable.rst

Lines changed: 153 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
.. _stable:
44

5-
***************
6-
C API Stability
7-
***************
5+
***********************
6+
C API and ABI Stability
7+
***********************
88

99
Unless documented otherwise, Python's C API is covered by the Backwards
1010
Compatibility Policy, :pep:`387`.
@@ -199,6 +199,156 @@ This is the case with Windows and macOS releases from ``python.org`` and many
199199
third-party distributors.
200200

201201

202+
ABI Checking
203+
============
204+
205+
.. versionadded:: next
206+
207+
Python includes a rudimentary check for ABI compatibility.
208+
209+
This check is not comprehensive.
210+
It only guards against common cases of incompatible modules being
211+
installed for the wrong interpreter.
212+
It also does not take :ref:`platform incompatibilities <stable-abi-platform>`
213+
into account.
214+
It can only be done after an extension is successfully loaded.
215+
216+
Despite these limitations, it is recommended that extension modules use this
217+
mechanism, so that detectable incompatibilities raise exceptions rather than
218+
crash.
219+
220+
Most modules can use this check via the :c:data:`Py_mod_abi`
221+
slot and the :c:macro:`PyABIInfo_VAR` macro.
222+
The full API is described below for advanced use cases.
223+
224+
.. c:function:: int PyABIInfo_Check(PyABIInfo *info, const char *module_name)
225+
226+
Verify that the given *info* is compatible with the currently running
227+
interpreter.
228+
229+
Return 0 on success. On failure, raise an exception and return -1.
230+
231+
If the ABI is incompatible, the raised exception will be :py:exc:`ImportError`.
232+
233+
The *module_name* argument can be ``NULL``, or point to a NUL-terminated
234+
UTF-8-encoded string used for error messages.
235+
236+
Note that if *info* describes the ABI taht the current code uses (as defined
237+
by :c:macro:`PyABIInfo_VAR`, for example), using any other Python C API
238+
may lead to crashes.
239+
In particular, it is not safe to examine the raised exception.
240+
241+
.. versionadded:: next
242+
243+
.. c:macro:: PyABIInfo_VAR(NAME)
244+
245+
Define a static :c:struct:`PyABIInfo` variable with the given *NAME* that
246+
describes the ABI that the current code will use.
247+
This macro expands to:
248+
249+
.. code-block:: c
250+
251+
static PyABIInfo NAME = {
252+
1, 0,
253+
PyABIInfo_DEFAULT_FLAGS,
254+
PY_VERSION_HEX,
255+
PyABIInfo_DEFAULT_ABI_VERSION
256+
}
257+
258+
.. versionadded:: next
259+
260+
.. c:type:: PyABIInfo
261+
262+
.. c:member:: uint8_t PyABIInfo.abiinfo_major_version
263+
264+
The major version of :c:struct:`PyABIInfo`. Can be set to:
265+
266+
* ``0`` to skip all checking, or
267+
* ``1`` to specify this version of :c:struct:`!PyABIInfo`.
268+
269+
.. c:member:: uint8_t PyABIInfo.abiinfo_minor_version
270+
271+
The major version of :c:struct:`PyABIInfo`.
272+
Must be set to ``0``; larger values are reserved for backwards-compatible
273+
future versions of :c:struct:`!PyABIInfo`.
274+
275+
.. c:member:: uint16_t PyABIInfo.flags
276+
277+
.. c:namespace:: NULL
278+
279+
This field is usually set to the following macro:
280+
281+
.. c:macro:: PyABIInfo_DEFAULT_FLAGS
282+
283+
Default flags, based on current values of macros such as
284+
:c:macro:`Py_LIMITED_API` and :c:macro:`Py_GIL_DISABLED`.
285+
286+
Alternately, the field can be set to to the following flags, combined
287+
by bitwise OR.
288+
Unused bits must be set to zero.
289+
290+
ABI variant -- one of:
291+
292+
.. c:macro:: PyABIInfo_STABLE
293+
294+
Specifies that the stable ABI is used.
295+
296+
.. c:macro:: PyABIInfo_INTERNAL
297+
298+
Specifies ABI specific to a particular build of CPython.
299+
Internal use only.
300+
301+
Free-threading compatibility -- one of:
302+
303+
.. c:macro:: PyABIInfo_FREETHREADED
304+
305+
Specifies ABI compatible with free-threading builds of CPython.
306+
(That is, ones compiled with :option:`--disable-gil`; with ``t``
307+
in :py:data:`sys.abiflags`)
308+
309+
.. c:macro:: PyABIInfo_GIL
310+
311+
Specifies ABI compatible with non-free-threading builds of CPython
312+
(ones compiled *without* :option:`--disable-gil`).
313+
314+
.. c:macro:: PyABIInfo_FREETHREADING_AGNOSTIC
315+
316+
Specifies ABI compatible with both free-threading and non-free-threading
317+
builds.
318+
319+
.. c:member:: uint32_t PyABIInfo.build_version
320+
321+
The version of the Python headers used to build the code, in the format
322+
used by :c:macro:`PY_VERSION_HEX`.
323+
324+
This can be set to ``0`` to skip any checks related to this field.
325+
This option is meant mainly for projects that do not use the CPython
326+
headers directly, and do not emulate a specific version of them.
327+
328+
.. c:member:: uint32_t PyABIInfo.abi_version
329+
330+
The ABI version.
331+
332+
For the Stable ABI, this field should be the value of
333+
:c:macro:`Py_LIMITED_API`
334+
(except if :c:macro:`Py_LIMITED_API` is ``3``; use
335+
:c:expr:`Py_PACK_VERSION(3, 2)` in that case).
336+
337+
Otherwise, it should be set to :c:macro:`PY_VERSION_HEX`.
338+
339+
It can also be set to ``0`` to skip any checks related to this field.
340+
341+
.. c:namespace:: NULL
342+
343+
.. c:macro:: PyABIInfo_DEFAULT_ABI_VERSION
344+
345+
The value that should be used for this field, based on current
346+
values of macros such as :c:macro:`Py_LIMITED_API`,
347+
:c:macro:`PY_VERSION_HEX` and :c:macro:`Py_GIL_DISABLED`.
348+
349+
.. versionadded:: next
350+
351+
202352
.. _limited-api-list:
203353

204354
Contents of Limited API

0 commit comments

Comments
 (0)