Skip to content

Commit 44f9a3d

Browse files
committed
Reorganize and reword the 'Useful macros' section
- Group the macros - Roughly order them to put the most important ones first - Add expansions where it makes sense; especially if there's an equivalent in modern C or a common compiler
1 parent 34e5a63 commit 44f9a3d

File tree

1 file changed

+153
-125
lines changed

1 file changed

+153
-125
lines changed

Doc/c-api/intro.rst

Lines changed: 153 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -157,123 +157,37 @@ Others of a more general utility are defined here. This is not necessarily a
157157
complete listing.
158158

159159

160+
Utilities
161+
---------
162+
160163
.. c:macro:: Py_ABS(x)
161164
162165
Return the absolute value of ``x``.
166+
The argument may be evaluated twice.
163167

164168
If the result cannot be represented (for example, if ``x`` has
165169
:c:macro:`!INT_MIN` value for :c:expr:`int` type), the behavior is
166170
undefined.
167171

168-
.. versionadded:: 3.3
169-
170-
.. c:macro:: Py_ALWAYS_INLINE
171-
172-
Ask the compiler to always inline a static inline function. The compiler can
173-
ignore it and decide to not inline the function.
174-
175-
It can be used to inline performance critical static inline functions when
176-
building Python in debug mode with function inlining disabled. For example,
177-
MSC disables function inlining when building in debug mode.
178-
179-
Marking blindly a static inline function with Py_ALWAYS_INLINE can result in
180-
worse performances (due to increased code size for example). The compiler is
181-
usually smarter than the developer for the cost/benefit analysis.
182-
183-
If Python is :ref:`built in debug mode <debug-build>` (if the :c:macro:`Py_DEBUG`
184-
macro is defined), the :c:macro:`Py_ALWAYS_INLINE` macro does nothing.
185-
186-
It must be specified before the function return type. Usage::
187-
188-
static inline Py_ALWAYS_INLINE int random(void) { return 4; }
189-
190-
.. versionadded:: 3.11
191-
192-
.. c:macro:: Py_CHARMASK(c)
193-
194-
Argument must be a character or an integer in the range [-128, 127] or [0,
195-
255]. This macro returns ``c`` cast to an ``unsigned char``.
196-
197-
.. c:macro:: Py_DEPRECATED(version)
198-
199-
Use this for deprecated declarations. The macro must be placed before the
200-
symbol name.
201-
202-
Example::
203-
204-
Py_DEPRECATED(3.8) PyAPI_FUNC(int) Py_OldFunction(void);
205-
206-
.. versionchanged:: 3.8
207-
MSVC support was added.
208-
209-
.. c:macro:: Py_GETENV(s)
210-
211-
Like ``getenv(s)``, but returns ``NULL`` if :option:`-E` was passed on the
212-
command line (see :c:member:`PyConfig.use_environment`).
213-
214-
.. c:macro:: Py_LOCAL(type)
215-
216-
Declare a function returning the specified *type* using a fast-calling
217-
qualifier for functions that are local to the current file.
218-
Semantically, this is equivalent to ``static type``.
219-
220-
.. c:macro:: Py_LOCAL_INLINE(type)
221-
222-
Equivalent to :c:macro:`Py_LOCAL` but additionally requests the function
223-
be inlined.
224-
225-
.. c:macro:: Py_LOCAL_SYMBOL
226-
227-
Macro used to declare a symbol as local to the shared library (hidden).
228-
On supported platforms, it ensures the symbol is not exported.
229-
230-
On compatible versions of GCC/Clang, it
231-
expands to ``__attribute__((visibility("hidden")))``.
232-
233-
.. c:macro:: Py_MAX(x, y)
234-
235-
Return the maximum value between ``x`` and ``y``.
172+
Corresponds roughly to :samp:`(({x}) < 0 ? -({x}) : ({x}))`
236173

237174
.. versionadded:: 3.3
238175

239-
.. c:macro:: Py_MEMBER_SIZE(type, member)
240-
241-
Return the size of a structure (``type``) ``member`` in bytes.
242-
243-
.. versionadded:: 3.6
244-
245-
.. c:macro:: Py_MEMCPY(dest, src, n)
246-
247-
This is a :term:`soft deprecated` alias to :c:func:`!memcpy`.
248-
Use :c:func:`!memcpy` directly instead.
249-
250-
.. deprecated:: 3.14
251-
The macro is :term:`soft deprecated`.
176+
.. c:macro:: Py_MAX(x, y)
177+
Py_MIN(x, y)
252178
253-
.. c:macro:: Py_MIN(x, y)
179+
Return the larger or smaller of the arguments, respectively.
180+
Any arguments may be evaluated twice.
254181
255-
Return the minimum value between ``x`` and ``y``.
182+
:c:macro:`!Py_MAX` corresponds roughly to
183+
:samp:`((({x}) > ({y})) ? ({x}) : ({y}))`.
256184
257185
.. versionadded:: 3.3
258186
259-
.. c:macro:: Py_NO_INLINE
260-
261-
Disable inlining on a function. For example, it reduces the C stack
262-
consumption: useful on LTO+PGO builds which heavily inline code (see
263-
:issue:`33720`).
264-
265-
Usage::
266-
267-
Py_NO_INLINE static int random(void) { return 4; }
268-
269-
.. versionadded:: 3.11
270-
271-
.. c:macro:: Py_STRINGIFY(x)
272-
273-
Convert ``x`` to a C string. E.g. ``Py_STRINGIFY(123)`` returns
274-
``"123"``.
187+
.. c:macro:: Py_GETENV(s)
275188
276-
.. versionadded:: 3.4
189+
Like :c:expr:`getenv(s)`, but returns ``NULL`` if :option:`-E` was passed on the
190+
command line (see :c:member:`PyConfig.use_environment`).
277191
278192
.. c:macro:: Py_UNREACHABLE()
279193
@@ -286,8 +200,10 @@ complete listing.
286200
avoids a warning about unreachable code. For example, the macro is
287201
implemented with ``__builtin_unreachable()`` on GCC in release mode.
288202
203+
In debug mode, the macro compiles to a call to :c:func:`Py_FatalError`.
204+
289205
A use for ``Py_UNREACHABLE()`` is following a call a function that
290-
never returns but that is not declared :c:macro:`_Py_NO_RETURN`.
206+
never returns but that is not declared ``_Noreturn``.
291207
292208
If a code path is very unlikely code but can be reached under exceptional
293209
case, this macro must not be used. For example, under low memory condition
@@ -297,18 +213,34 @@ complete listing.
297213
298214
.. versionadded:: 3.7
299215
300-
.. c:macro:: Py_UNUSED(arg)
216+
.. c:macro:: Py_MEMBER_SIZE(type, member)
301217
302-
Use this for unused arguments in a function definition to silence compiler
303-
warnings. Example: ``int func(int a, int Py_UNUSED(b)) { return a; }``.
218+
Return the size of a structure (*type*) *member* in bytes.
304219
305-
.. versionadded:: 3.4
220+
Corresponds roughly to :samp:`sizeof((({type} *)NULL)->{member})`.
221+
222+
.. versionadded:: 3.6
223+
224+
.. c:macro:: Py_ARRAY_LENGTH(array)
225+
226+
Compute the length of a statically allocated C array at compile time.
227+
228+
The *array* argument must be a C array with a size known at compile time.
229+
Passing an array with an unknown size, such as a heap-allocated array,
230+
will result in a compilation error on some compilers, or otherwise produce
231+
incorrect results.
232+
233+
This is roughly equivalent to::
234+
235+
sizeof(array) / sizeof((array)[0])
306236
307237
.. c:macro:: Py_BUILD_ASSERT(cond)
308238
309239
Asserts a compile-time condition *cond*, as a statement.
310240
The build will fail if the condition is false or cannot be evaluated at compile time.
311241
242+
Corresponds roughly to :samp:`static_assert({cond})` on C23 and above.
243+
312244
For example::
313245
314246
Py_BUILD_ASSERT(sizeof(PyTime_t) == sizeof(int64_t));
@@ -327,13 +259,42 @@ complete listing.
327259
328260
.. versionadded:: 3.3
329261
262+
.. c:macro:: Py_STRINGIFY(x)
263+
264+
Convert ``x`` to a C string. For example, ``Py_STRINGIFY(123)`` returns
265+
``"123"``.
266+
267+
.. versionadded:: 3.4
268+
269+
.. c:macro:: Py_UNUSED(arg)
270+
271+
Use this for unused arguments in a function definition to silence compiler
272+
warnings. Example: ``int func(int a, int Py_UNUSED(b)) { return a; }``.
273+
274+
.. versionadded:: 3.4
275+
276+
.. c:macro:: Py_CHARMASK(c)
277+
278+
Argument must be a character or an integer in the range [-128, 127] or [0,
279+
255]. This macro returns ``c`` cast to an ``unsigned char``.
280+
281+
.. c:macro:: Py_MEMCPY(dest, src, n)
282+
283+
This is a :term:`soft deprecated` alias to :c:func:`!memcpy`.
284+
Use :c:func:`!memcpy` directly instead.
285+
286+
.. deprecated:: 3.14
287+
The macro is :term:`soft deprecated`.
288+
289+
290+
Docstring utilities
291+
-------------------
292+
330293
.. c:macro:: PyDoc_STRVAR(name, str)
331294
332295
Creates a variable with name *name* that can be used in docstrings.
333-
If Python is built without docstrings, the value will be empty.
334-
335-
Use :c:macro:`PyDoc_STRVAR` for docstrings to support building
336-
Python without docstrings, as specified in :pep:`7`.
296+
If Python is built :option:`without docstrings <--without-doc-strings>`,
297+
the value will be an empty string.
337298
338299
Example::
339300
@@ -345,13 +306,12 @@ complete listing.
345306
// ...
346307
}
347308
348-
.. c:macro:: PyDoc_STR(str)
309+
Expands to :samp:`PyDoc_VAR({name}) = PyDoc_STR({str})`.
349310
350-
Creates a docstring for the given input string or an empty string
351-
if docstrings are disabled.
311+
.. c:macro:: PyDoc_STR(str)
352312
353-
Use :c:macro:`PyDoc_STR` in specifying docstrings to support
354-
building Python without docstrings, as specified in :pep:`7`.
313+
Expands to the given input string, or an empty string
314+
if docstrings are :option:`disabled <--without-doc-strings>`.
355315
356316
Example::
357317
@@ -363,26 +323,94 @@ complete listing.
363323
364324
.. c:macro:: PyDoc_VAR(name)
365325
366-
Declares a static character array variable with the given name *name*.
326+
Declares a static character array variable with the given *name*.
327+
Expands to :samp:`static const char {name}[]`
367328
368329
For example::
369330
370-
PyDoc_VAR(python_doc) = PyDoc_STR("A genus of constricting snakes in the Pythonidae family native "
371-
"to the tropics and subtropics of the Eastern Hemisphere.");
331+
PyDoc_VAR(python_doc) = PyDoc_STR(
332+
"A genus of constricting snakes in the Pythonidae family native "
333+
"to the tropics and subtropics of the Eastern Hemisphere.");
372334
373-
.. c:macro:: Py_ARRAY_LENGTH(array)
374335
375-
Compute the length of a statically allocated C array at compile time.
336+
Declaration utilities
337+
---------------------
376338
377-
The *array* argument must be a C array with a size known at compile time.
378-
Passing an array with an unknown size, such as a heap-allocated array,
379-
will result in a compilation error on some compilers, or otherwise produce
380-
incorrect results.
339+
The following macros can be used in declarations.
340+
They are most useful for defining the C API itself, and have limited use
341+
for extension authors.
342+
Most of them expand to compiler-specific spellings of common extensions
343+
to the C language.
381344
382-
This is roughly equivalent to::
345+
.. c:macro:: Py_ALWAYS_INLINE
383346
384-
sizeof(array) / sizeof((array)[0])
347+
Ask the compiler to always inline a static inline function. The compiler can
348+
ignore it and decide to not inline the function.
349+
350+
Corresponds to ``always_inline`` attribute in GCC and ``__forceinline``
351+
in MSVC.
352+
353+
It can be used to inline performance critical static inline functions when
354+
building Python in debug mode with function inlining disabled. For example,
355+
MSC disables function inlining when building in debug mode.
356+
357+
Marking blindly a static inline function with Py_ALWAYS_INLINE can result in
358+
worse performances (due to increased code size for example). The compiler is
359+
usually smarter than the developer for the cost/benefit analysis.
360+
361+
If Python is :ref:`built in debug mode <debug-build>` (if the :c:macro:`Py_DEBUG`
362+
macro is defined), the :c:macro:`Py_ALWAYS_INLINE` macro does nothing.
363+
364+
It must be specified before the function return type. Usage::
365+
366+
static inline Py_ALWAYS_INLINE int random(void) { return 4; }
367+
368+
.. versionadded:: 3.11
369+
370+
.. c:macro:: Py_NO_INLINE
371+
372+
Disable inlining on a function. For example, it reduces the C stack
373+
consumption: useful on LTO+PGO builds which heavily inline code (see
374+
:issue:`33720`).
375+
376+
Corresponds to the ``noinline`` attribute/specification on GCC and MSVC.
377+
378+
Usage::
379+
380+
Py_NO_INLINE static int random(void) { return 4; }
381+
382+
.. versionadded:: 3.11
385383

384+
.. c:macro:: Py_DEPRECATED(version)
385+
386+
Use this to declare APIs that were deprecated in a specific CPYthon version.
387+
The macro must be placed before the symbol name.
388+
389+
Example::
390+
391+
Py_DEPRECATED(3.8) PyAPI_FUNC(int) Py_OldFunction(void);
392+
393+
.. versionchanged:: 3.8
394+
MSVC support was added.
395+
396+
.. c:macro:: Py_LOCAL(type)
397+
398+
Declare a function returning the specified *type* using a fast-calling
399+
qualifier for functions that are local to the current file.
400+
Semantically, this is equivalent to :samp:`static {type}`.
401+
402+
.. c:macro:: Py_LOCAL_INLINE(type)
403+
404+
Equivalent to :c:macro:`Py_LOCAL` but additionally requests the function
405+
be inlined.
406+
407+
.. c:macro:: Py_LOCAL_SYMBOL
408+
409+
Macro used to declare a symbol as local to the shared library (hidden).
410+
On supported platforms, it ensures the symbol is not exported.
411+
412+
On compatible versions of GCC/Clang, it
413+
expands to ``__attribute__((visibility("hidden")))``.
386414

387415
.. c:macro:: Py_EXPORTED_SYMBOL
388416

0 commit comments

Comments
 (0)