@@ -93,7 +93,10 @@ CPython's public C API is available when ``Python.h`` is included normally
93
93
It should be defined in ``Include/cpython/ `` (unless part of the Limited API,
94
94
see below).
95
95
96
- Guidelines for expanding/changing the public API:
96
+ .. _public-api-guidelines :
97
+
98
+ Guidelines for expanding/changing the public API
99
+ ------------------------------------------------
97
100
98
101
- Make sure the new API follows reference counting conventions.
99
102
(Following them makes the API easier to reason about, and easier use
@@ -106,6 +109,24 @@ Guidelines for expanding/changing the public API:
106
109
- Make sure the ownership rules and lifetimes of all applicable struct
107
110
fields, arguments and return values are well defined.
108
111
112
+ - Functions returning ``PyObject * `` must return a valid pointer on success,
113
+ and ``NULL `` with an exception raised on error.
114
+ Most other API must return ``-1 `` with an exception raised on error,
115
+ and ``0 `` on success.
116
+
117
+ - APIs with lesser and greater results must return ``0 `` for the lesser result,
118
+ and ``1 `` for the greater result.
119
+ Consider a lookup function with a three-way return:
120
+
121
+ - ``return -1 ``: internal error or API misuse; exception raised
122
+ - ``return 0 ``: lookup succeeded; no item was found
123
+ - ``return 1 ``: lookup succeeded; item was found
124
+
125
+ Please start a public discussion if these guidelines won't work for your API.
126
+
127
+ .. note ::
128
+
129
+ By *return value *, we mean the value returned by the *C return statement *.
109
130
110
131
C API Tests
111
132
-----------
@@ -292,10 +313,13 @@ It is possible to remove items marked as part of the Stable ABI, but only
292
313
if there was no way to use them in any past version of the Limited API.
293
314
294
315
316
+ .. _limited-api-guidelines :
317
+
295
318
Guidelines for adding to the Limited API
296
319
----------------------------------------
297
320
298
321
- Guidelines for the general :ref: `public-capi ` apply.
322
+ See :ref: `public-api-guidelines `.
299
323
300
324
- New Limited API should only be defined if ``Py_LIMITED_API `` is set
301
325
to the version the API was added in or higher.
0 commit comments