|
| 1 | +The Python C API |
| 2 | +================ |
| 3 | + |
| 4 | +The C API is divided into three sections: |
| 5 | + |
| 6 | +1. ``Include/`` |
| 7 | +2. ``Include/cpython/`` |
| 8 | +3. ``Include/internal/`` |
| 9 | + |
| 10 | + |
| 11 | +Include: Limited API |
| 12 | +==================== |
| 13 | + |
| 14 | +``Include/``, excluding the ``cpython`` and ``internal`` subdirectories, |
| 15 | +contains the public Limited API (Application Programming Interface). |
| 16 | +The Limited API is a subset of the C API, designed to guarantee ABI |
| 17 | +stability across Python 3 versions, and is defined in :pep:`384`. |
| 18 | + |
| 19 | +Guidelines for expanding the Limited API: |
| 20 | + |
| 21 | +- Functions *must not* steal references |
| 22 | +- Functions *must not* return borrowed references |
| 23 | +- Functions returning references *must* return a strong reference |
| 24 | +- Macros should not expose implementation details |
| 25 | +- Please start a public discussion before expanding the API |
| 26 | +- Functions or macros with a ``_Py`` prefix do not belong in ``Include/``. |
| 27 | + |
| 28 | +It is possible to add a function or macro to the Limited API from a |
| 29 | +given Python version. For example, to add a function to the Limited API |
| 30 | +from Python 3.10 and onwards, wrap it with |
| 31 | +``#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000``. |
| 32 | + |
| 33 | + |
| 34 | +Include/cpython: CPython implementation details |
| 35 | +=============================================== |
| 36 | + |
| 37 | +``Include/cpython/`` contains the public API that is excluded from the |
| 38 | +Limited API and the Stable ABI. |
| 39 | + |
| 40 | +Guidelines for expanding the public API: |
| 41 | + |
| 42 | +- Functions *must not* steal references |
| 43 | +- Functions *must not* return borrowed references |
| 44 | +- Functions returning references *must* return a strong reference |
| 45 | + |
| 46 | + |
| 47 | +Include/internal: The internal API |
| 48 | +================================== |
| 49 | + |
| 50 | + |
| 51 | +With PyAPI_FUNC or PyAPI_DATA |
| 52 | +----------------------------- |
| 53 | + |
| 54 | +Functions or structures in ``Include/internal/`` defined with |
| 55 | +``PyAPI_FUNC`` or ``PyAPI_DATA`` are internal functions which are |
| 56 | +exposed only for specific use cases like debuggers and profilers. |
| 57 | + |
| 58 | + |
| 59 | +With the extern keyword |
| 60 | +----------------------- |
| 61 | + |
| 62 | +Functions in ``Include/internal/`` defined with the ``extern`` keyword |
| 63 | +*must not and can not* be used outside the CPython code base. Only |
| 64 | +built-in stdlib extensions (built with the ``Py_BUILD_CORE_BUILTIN`` |
| 65 | +macro defined) can use such functions. |
| 66 | + |
| 67 | +When in doubt, new internal C functions should be defined in |
| 68 | +``Include/internal`` using the ``extern`` keyword. |
0 commit comments