@@ -62,6 +62,13 @@ definition in ``Include/internal/`` (or directly in a ``.c`` file):
62
62
a macro aliasing the old name to the new one.
63
63
See :pep: `"Finalizing the API" in PEP 590 <590#finalizing-the-api >` for an example.
64
64
65
+ Internal API Tests
66
+ ------------------
67
+
68
+ C tests for the internal C API live in ``Modules/_testinternalcapi.c ``.
69
+ Functions named ``test_* `` are used as tests directly.
70
+ Python parts of the tests live in various places in ``Lib/test ``.
71
+
65
72
66
73
.. _public-capi :
67
74
@@ -88,6 +95,40 @@ Guidelines for expanding/changing the public API:
88
95
fields, arguments and return values are well defined.
89
96
90
97
98
+ C API Tests
99
+ -----------
100
+
101
+ Tests for the public C API live in the ``_testcapi `` module.
102
+ Functions named ``test_* `` are used as tests directly.
103
+ Tests that need Python code (or are just easier to partially write in Python)
104
+ live in ``Lib/test ``, mainly in :file: `Lib/test/test_capi.py `.
105
+
106
+ Due to its size, the ``_testcapi `` module is defined in several source
107
+ files.
108
+ To add a new set of tests (or extract a set out of the monolithic
109
+ :file: `Modules/_testcapimodule.c `):
110
+
111
+ - Create a C file named ``Modules/_testcapi/yourfeature.c ``
112
+
113
+ - The file should define a module as usual, except:
114
+
115
+ - Instead of ``<Python.h> ``, include ``"parts.h" ``.
116
+ - Instead of ``PyInit_modname ``, define a ``_PyTestCapi_Init_yourfeature ``
117
+ function that *takes * the ``_testcapi `` module and adds functions/classes
118
+ to it. (You can use ``PyModule_AddFunctions `` to add functions.)
119
+
120
+ - Add the ``_PyTestCapi_Init_* `` function to ``Modules/_testcapi/parts.h ``
121
+
122
+ - Call the ``_PyTestCapi_Init_* `` from ``PyInit__testcapi `` in
123
+ ``Modules/_testcapimodule.c ``.
124
+
125
+ Note that all ``Modules/_testcapi/*.c `` sources initialize the same module,
126
+ so be careful about name collisions.
127
+
128
+ When moving existing tests, feel free to replace ``TestError `` with
129
+ ``PyExc_AssertionError `` unless actually testing custom exceptions.
130
+
131
+
91
132
Limited API
92
133
===========
93
134
@@ -228,3 +269,30 @@ Adding a new definition to the Limited API
228
269
.. code-block :: shell
229
270
230
271
./python ./Tools/scripts/stable_abi.py --all ./Misc/stable_abi.toml
272
+
273
+ - Add tests -- see below.
274
+
275
+
276
+ Limited API Tests
277
+ -----------------
278
+
279
+ Since Limited API is a subset of the C API, there's no need to test the
280
+ behavior of individual functions. Rather, the tests could verify that some
281
+ task is possible using the exposed subset, or exercise a feature that was
282
+ removed from the current Limited API but still needs to be supported for
283
+ older Limited API/Stable ABI versions.
284
+
285
+ To add a test file:
286
+
287
+ - Add a C file ``Modules/_testcapi/yourfeature_limited.c ``. If that file
288
+ already exists but its ``Py_LIMITED_API `` version is too low, add a version
289
+ postfix, e.g. ``yourfeature_limited_3_12.c `` for Python 3.12+.
290
+ - ``#define Py_LIMITED_API `` to the minimum limited API version needed.
291
+ - ``#include "parts.h" `` after the ``Py_LIMITED_API `` definition
292
+ - Enclose the entire rest of the file in ``#ifdef LIMITED_API_AVAILABLE ``,
293
+ so it's skipped on incompatible builds.
294
+ - Follow the general instructions for `C API tests `_. All additions go in the
295
+ sections guarded by ``#ifdef LIMITED_API_AVAILABLE ``.
296
+
297
+ Use the ``test.support.requires_limited_api `` decorator for Python tests
298
+ in ``Lib/test ``, so they're skipped on incompatible builds.
0 commit comments