|
| 1 | +======================= |
| 2 | +Changing Python's C API |
| 3 | +======================= |
| 4 | + |
| 5 | +The C API is divided into three sections: |
| 6 | + |
| 7 | +1. The internal, private API, available with ``Py_BUILD_CORE`` defined. |
| 8 | + Ideally declared in ``Include/internal/``. Any API named with a leading |
| 9 | + underscore is also considered private. |
| 10 | +2. The public C API, available when ``Python.h`` is included normally. |
| 11 | + Ideally declared in ``Include/cpython/``. |
| 12 | +3. The Limited API, available with ``Py_LIMITED_API`` defined. |
| 13 | + Ideally declared directly under ``Include/``. |
| 14 | + |
| 15 | +Each section has higher stability & maintenance requirements, and you will |
| 16 | +need to think about more issues when you add or change definitions in it. |
| 17 | + |
| 18 | +The compatibility guarantees for public C API are explained in the |
| 19 | +user documentation, ``Doc/c-api/stable.rst`` (:ref:`python:stable`). |
| 20 | + |
| 21 | + |
| 22 | +The internal API |
| 23 | +================ |
| 24 | + |
| 25 | +Internal API is defined in ``Include/internal/`` and is only available |
| 26 | +for building CPython itself, as indicated by a macro like ``Py_BUILD_CORE``. |
| 27 | + |
| 28 | +While internal API can be changed at any time, it's still good to keep it |
| 29 | +stable: other API or other CPython developers may depend on it. |
| 30 | + |
| 31 | +With PyAPI_FUNC or PyAPI_DATA |
| 32 | +----------------------------- |
| 33 | + |
| 34 | +Functions or structures in ``Include/internal/`` defined with |
| 35 | +``PyAPI_FUNC`` or ``PyAPI_DATA`` are internal functions which are |
| 36 | +exposed only for specific use cases like debuggers and profilers. |
| 37 | + |
| 38 | + |
| 39 | +With the extern keyword |
| 40 | +----------------------- |
| 41 | + |
| 42 | +Functions in ``Include/internal/`` defined with the ``extern`` keyword |
| 43 | +*must not and can not* be used outside the CPython code base. Only |
| 44 | +built-in stdlib extensions (built with the ``Py_BUILD_CORE_BUILTIN`` |
| 45 | +macro defined) can use such functions. |
| 46 | + |
| 47 | +When in doubt, new internal C functions should be defined in |
| 48 | +``Include/internal`` using the ``extern`` keyword. |
| 49 | + |
| 50 | +Private names |
| 51 | +-------------- |
| 52 | + |
| 53 | +Any API named with a leading underscore is also considered internal. |
| 54 | +There are two main use cases for using such names rather than putting the |
| 55 | +definition in ``Include/internal/`` (or directly in a ``.c`` file): |
| 56 | + |
| 57 | +* Internal helpers for other public API; users should not use these directly; |
| 58 | +* “Provisional” API, included in a Python release to test real-world usage |
| 59 | + of new API. Such names should be renamed when stabilized; preferably with |
| 60 | + a macro aliasing the old name to the new one. |
| 61 | + See `"Finalizing the API" in PEP 590`_ for an example. |
| 62 | + |
| 63 | +.. _"Finalizing the API" in PEP 590: https://www.python.org/dev/peps/pep-0590/#finalizing-the-api |
| 64 | + |
| 65 | + |
| 66 | +.. _public-capi: |
| 67 | + |
| 68 | +Public C API |
| 69 | +============ |
| 70 | + |
| 71 | +CPython's public C API is available when ``Python.h`` is included normally |
| 72 | +(that is, without defining macros to select the other variants). |
| 73 | + |
| 74 | +It should be defined in ``Include/cpython/`` (unless part of the Limited API, |
| 75 | +see below). |
| 76 | + |
| 77 | +Guidelines for expanding/changing the public API: |
| 78 | + |
| 79 | +- Make sure the new API follows reference counting conventions. |
| 80 | + (Following them makes the API easier to reason about, and easier use |
| 81 | + in other Python implementations.) |
| 82 | + |
| 83 | + - Functions *must not* steal references |
| 84 | + - Functions *must not* return borrowed references |
| 85 | + - Functions returning references *must* return a strong reference |
| 86 | + |
| 87 | +- Make sure the ownership rules and lifetimes of all applicable struct |
| 88 | + fields, arguments and return values are well defined. |
| 89 | + |
| 90 | + |
| 91 | +Limited API |
| 92 | +=========== |
| 93 | + |
| 94 | +The Limited API is a subset of the C API designed to guarantee ABI |
| 95 | +stability across Python 3 versions. |
| 96 | +Defining the macro ``Py_LIMITED_API`` will limit the exposed API to |
| 97 | +this subset. |
| 98 | + |
| 99 | +No changes that break the Stable ABI are allowed. |
| 100 | + |
| 101 | +The Limited API should be defined in ``Include/``, excluding the |
| 102 | +``cpython`` and ``internal`` subdirectories. |
| 103 | + |
| 104 | +Guidelines for changing the Limited API |
| 105 | +--------------------------------------- |
| 106 | + |
| 107 | +- Guidelines for the general :ref:`public-capi` apply. |
| 108 | + |
| 109 | +- New Limited API should only be defined if ``Py_LIMITED_API`` is set |
| 110 | + to the version the API was added in or higher. |
| 111 | + (See below for the proper ``#if`` guard.) |
| 112 | + |
| 113 | +- All parameter types, return values, struct members, etc. need to be part |
| 114 | + of the Limited API. |
| 115 | + |
| 116 | + - Functions that deal with ``FILE*`` (or other types with ABI portability |
| 117 | + issues) should not be added. |
| 118 | + |
| 119 | +- Think twice when defining macros. |
| 120 | + |
| 121 | + - Macros should not expose implementation details |
| 122 | + - Functions must be exported as actual functions, not (only) |
| 123 | + as functions-like macros. |
| 124 | + - If possible, avoid macros. This makes the Limited API more usable in |
| 125 | + languages that don't use the C preprocessor. |
| 126 | + |
| 127 | +- Please start a public discussion before expanding the Limited API |
| 128 | + |
| 129 | +- The Limited API and must follow standard C, not just features of currently |
| 130 | + supported platforms. The exact C dialect is described in :pep:`7`. |
| 131 | + |
| 132 | + - Documentation examples (and more generally: the intended use of the API) |
| 133 | + should also follow standard C. |
| 134 | + - In particular, do not cast a function pointer to ``void*`` (a data pointer) |
| 135 | + or vice versa. |
| 136 | + |
| 137 | +- Think about ease of use for the user. |
| 138 | + |
| 139 | + - In C, ease of use itself is not very important; what is useful is |
| 140 | + reducing boilerplate code needed to use the API. Bugs like to hide in |
| 141 | + boiler plates. |
| 142 | + |
| 143 | + - If a function will be often called with specific value for an argument, |
| 144 | + consider making it default (used when ``NULL`` is passed in). |
| 145 | + - The Limited API needs to be well documented. |
| 146 | + |
| 147 | +- Think about future extensions |
| 148 | + |
| 149 | + - If it's possible that future Python versions will need to add a new |
| 150 | + field to your struct, make sure it can be done. |
| 151 | + - Make as few assumptions as possible about implementation details that |
| 152 | + might change in future CPython versions or differ across C API |
| 153 | + implementations. The most important CPython-specific implementation |
| 154 | + details involve: |
| 155 | + |
| 156 | + - The GIL |
| 157 | + - :ref:`Garbage collection <gc>` |
| 158 | + - Memory layout of PyObject, lists/tuples and other structures |
| 159 | + |
| 160 | +If following these guidelines would hurt performance, add a fast function |
| 161 | +(or macro) to the non-limited API and a stable equivalent to the Limited |
| 162 | +API. |
| 163 | + |
| 164 | +If anything is unclear, or you have a good reason to break the guidelines, |
| 165 | +consider discussing the change at the `capi-sig`_ mailing list. |
| 166 | + |
| 167 | +.. _capi-sig: https://mail.python.org/mailman3/lists/capi-sig.python.org/ |
| 168 | + |
| 169 | +Adding a new definition to the Limited API |
| 170 | +------------------------------------------ |
| 171 | + |
| 172 | +- Add the declaration to a header file directly under ``Include/``, into a |
| 173 | + block guarded with the following: |
| 174 | + |
| 175 | + .. code-block:: c |
| 176 | +
|
| 177 | + #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03yy0000 |
| 178 | +
|
| 179 | + with the ``yy`` corresponding to the target CPython version, e.g. |
| 180 | + ``0x030A0000`` for Python 3.10. |
| 181 | +- Append an entry to the Stable ABI manifest, ``Misc/stable_abi.txt``. |
| 182 | +- Regenerate the autogenerated files using ``make regen-limited-abi``. |
| 183 | + On platforms without ``make``, run this command directly: |
| 184 | + |
| 185 | + .. code-block:: shell |
| 186 | +
|
| 187 | + ./python ./Tools/scripts/stable_abi.py --generate-all ./Misc/stable_abi.txt |
| 188 | +
|
| 189 | +- Build Python and check the using ``make check-limited-abi``. |
| 190 | + On platforms without ``make``, run this command directly: |
| 191 | + |
| 192 | + .. code-block:: shell |
| 193 | +
|
| 194 | + ./python ./Tools/scripts/stable_abi.py --all ./Misc/stable_abi.txt |
0 commit comments