Skip to content

Commit 360333d

Browse files
committed
Shuffle the macros around
1 parent b6a72a0 commit 360333d

File tree

1 file changed

+121
-108
lines changed

1 file changed

+121
-108
lines changed

Doc/c-api/intro.rst

Lines changed: 121 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -156,95 +156,23 @@ defined closer to where they are useful (for example, :c:macro:`Py_RETURN_NONE`,
156156
Others of a more general utility are defined here. This is not necessarily a
157157
complete listing.
158158

159+
.. c:macro:: Py_CAN_START_THREADS
159160
160-
Arithmetic Utilities
161-
--------------------
162-
163-
.. c:macro:: Py_ABS(x)
164-
165-
Return the absolute value of ``x``.
166-
The argument may be evaluated twice.
167-
168-
If the result cannot be represented (for example, if ``x`` has
169-
:c:macro:`!INT_MIN` value for :c:expr:`int` type), the behavior is
170-
undefined.
171-
172-
Corresponds roughly to :samp:`(({x}) < 0 ? -({x}) : ({x}))`
173-
174-
.. versionadded:: 3.3
175-
176-
.. c:macro:: Py_MAX(x, y)
177-
Py_MIN(x, y)
178-
179-
Return the larger or smaller of the arguments, respectively.
180-
Any arguments may be evaluated twice.
181-
182-
:c:macro:`!Py_MAX` corresponds roughly to
183-
:samp:`((({x}) > ({y})) ? ({x}) : ({y}))`.
184-
185-
.. versionadded:: 3.3
186-
187-
.. c:macro:: Py_ARITHMETIC_RIGHT_SHIFT(type, integer, positions)
188-
Similar to ``integer >> positions``, but forces sign extension, as the C
189-
standard does not define whether a right-shift of a signed integer will
190-
perform sign extension or a zero-fill.
191-
192-
*integer* should be any signed integer type.
193-
*positions* is the number of positions to shift to the right.
194-
195-
Both *integer* and *positions* can be evaluated more than once;
196-
consequently, avoid directly passing a function call or some other
197-
operation with side-effects to this macro. Instead, store the result as a
198-
variable and then pass it.
199-
200-
*type* is unused and only kept for backwards compatibility. Historically,
201-
*type* was used to cast *integer*.
202-
203-
.. versionchanged:: 3.1
204-
205-
This macro is now valid for all signed integer types, not just those for
206-
which ``unsigned type`` is legal. As a result, *type* is no longer
207-
used.
208-
209-
.. c:macro:: Py_SAFE_DOWNCAST(value, larger, smaller)
210-
Cast *value* to type *smaller* from type *larger*, validating that no
211-
information was lost.
212-
213-
On release builds of Python, this is roughly equivalent to
214-
``(smaller) value`` (in C++, ``static_cast<smaller>(value)`` will be
215-
used instead).
216-
217-
On debug builds (implying that :c:macro:`Py_DEBUG` is defined), this asserts
218-
that no information was lost with the cast from *larger* to *smaller*.
219-
220-
*value*, *larger*, and *smaller* may all be evaluated more than once in the
221-
expression; consequently, do not pass an expression with side-effects
222-
directly to this macro.
223-
224-
.. c:macro:: Py_CHARMASK(c)
225-
226-
Argument must be a character or an integer in the range [-128, 127] or [0,
227-
255]. This macro returns ``c`` cast to an ``unsigned char``.
161+
If this macro is defined, then the current system is able to start threads.
228162

163+
Currently, all systems supported by CPython (per :pep:`11`), with the
164+
exception of some WebAssembly platforms, support starting threads.
229165

230-
Python-specific utilities
231-
-------------------------
166+
.. versionadded:: 3.13
232167

233168
.. c:macro:: Py_GETENV(s)
234169
235170
Like :samp:`getenv({s})`, but returns ``NULL`` if :option:`-E` was passed
236171
on the command line (see :c:member:`PyConfig.use_environment`).
237172

238-
.. c:macro:: Py_CAN_START_THREADS
239-
If this macro is defined, then the current system is able to start threads.
240-
241-
Currently, all systems supported by CPython (per :pep:`11`), with the
242-
exception of some WebAssembly platforms, support starting threads.
243-
244-
.. versionadded:: 3.13
245173

246-
Docstring utilities
247-
^^^^^^^^^^^^^^^^^^^
174+
Docstring macros
175+
----------------
248176

249177
.. c:macro:: PyDoc_STRVAR(name, str)
250178
@@ -288,8 +216,9 @@ Docstring utilities
288216
"A genus of constricting snakes in the Pythonidae family native "
289217
"to the tropics and subtropics of the Eastern Hemisphere.");
290218

291-
General Utilities
292-
-----------------
219+
220+
General utility macros
221+
----------------------
293222

294223
The following macros common tasks not specific to Python.
295224

@@ -300,8 +229,79 @@ The following macros common tasks not specific to Python.
300229

301230
.. versionadded:: 3.4
302231

303-
Assertions
304-
^^^^^^^^^^
232+
.. c:macro:: Py_GCC_ATTRIBUTE(name)
233+
234+
Use a GCC attribute *name*, hiding it from compilers that don't support GCC
235+
attributes (such as MSVC).
236+
237+
This expands to :samp:`__attribute__(({name)})` on a GCC compiler,
238+
and expands to nothing on compilers that don't support GCC attributes.
239+
240+
241+
Numeric utilities
242+
^^^^^^^^^^^^^^^^^
243+
244+
.. c:macro:: Py_ABS(x)
245+
246+
Return the absolute value of ``x``.
247+
248+
The argument may be evaluated more than once.
249+
Consequently, do not pass an expression with side-effects directly
250+
to this macro.
251+
252+
If the result cannot be represented (for example, if ``x`` has
253+
:c:macro:`!INT_MIN` value for :c:expr:`int` type), the behavior is
254+
undefined.
255+
256+
Corresponds roughly to :samp:`(({x}) < 0 ? -({x}) : ({x}))`
257+
258+
.. versionadded:: 3.3
259+
260+
.. c:macro:: Py_MAX(x, y)
261+
Py_MIN(x, y)
262+
263+
Return the larger or smaller of the arguments, respectively.
264+
265+
Any arguments may be evaluated more than once.
266+
Consequently, do not pass an expression with side-effects directly
267+
to this macro.
268+
269+
:c:macro:`!Py_MAX` corresponds roughly to
270+
:samp:`((({x}) > ({y})) ? ({x}) : ({y}))`.
271+
272+
.. versionadded:: 3.3
273+
274+
.. c:macro:: Py_ARITHMETIC_RIGHT_SHIFT(type, integer, positions)
275+
276+
Similar to :samp:`{integer} >> {positions{`, but forces sign extension,
277+
as the C standard does not define whether a right-shift of a signed
278+
integer will perform sign extension or a zero-fill.
279+
280+
*integer* should be any signed integer type.
281+
*positions* is the number of positions to shift to the right.
282+
283+
Both *integer* and *positions* can be evaluated more than once;
284+
consequently, avoid directly passing a function call or some other
285+
operation with side-effects to this macro. Instead, store the result as a
286+
variable and then pass it.
287+
288+
*type* is unused and only kept for backwards compatibility. Historically,
289+
*type* was used to cast *integer*.
290+
291+
.. versionchanged:: 3.1
292+
293+
This macro is now valid for all signed integer types, not just those for
294+
which ``unsigned type`` is legal. As a result, *type* is no longer
295+
used.
296+
297+
.. c:macro:: Py_CHARMASK(c)
298+
299+
Argument must be a character or an integer in the range [-128, 127] or [0,
300+
255]. This macro returns ``c`` cast to an ``unsigned char``.
301+
302+
303+
Assertion utilities
304+
^^^^^^^^^^^^^^^^^^^
305305
306306
.. c:macro:: Py_UNREACHABLE()
307307
@@ -314,7 +314,8 @@ Assertions
314314
avoids a warning about unreachable code. For example, the macro is
315315
implemented with ``__builtin_unreachable()`` on GCC in release mode.
316316
317-
In debug mode, the macro compiles to a call to :c:func:`Py_FatalError`.
317+
In debug mode, and on unsupported compilers, the macro expands to a call to
318+
:c:func:`Py_FatalError`.
318319
319320
A use for ``Py_UNREACHABLE()`` is following a call a function that
320321
never returns but that is not declared ``_Noreturn``.
@@ -327,6 +328,22 @@ Assertions
327328
328329
.. versionadded:: 3.7
329330
331+
.. c:macro:: Py_SAFE_DOWNCAST(value, larger, smaller)
332+
333+
Cast *value* to type *smaller* from type *larger*, validating that no
334+
information was lost.
335+
336+
On release builds of Python, this is roughly equivalent to
337+
:samp:`(({smaller}) {value})`
338+
(in C++, :samp:`static_cast<{smaller}>({value})` will be used instead).
339+
340+
On debug builds (implying that :c:macro:`Py_DEBUG` is defined), this asserts
341+
that no information was lost with the cast from *larger* to *smaller*.
342+
343+
*value*, *larger*, and *smaller* may all be evaluated more than once in the
344+
expression; consequently, do not pass an expression with side-effects
345+
directly to this macro.
346+
330347
.. c:macro:: Py_BUILD_ASSERT(cond)
331348
332349
Asserts a compile-time condition *cond*, as a statement.
@@ -352,16 +369,9 @@ Assertions
352369
353370
.. versionadded:: 3.3
354371
355-
Size helpers
356-
^^^^^^^^^^^^
357-
358-
.. c:macro:: Py_MEMBER_SIZE(type, member)
359372
360-
Return the size of a structure (*type*) *member* in bytes.
361-
362-
Corresponds roughly to :samp:`sizeof((({type} *)NULL)->{member})`.
363-
364-
.. versionadded:: 3.6
373+
Type size utilities
374+
^^^^^^^^^^^^^^^^^^^
365375
366376
.. c:macro:: Py_ARRAY_LENGTH(array)
367377
@@ -376,27 +386,30 @@ Size helpers
376386
377387
sizeof(array) / sizeof((array)[0])
378388
379-
Macro helpers
380-
^^^^^^^^^^^^^
389+
.. c:macro:: Py_MEMBER_SIZE(type, member)
381390
382-
.. c:macro:: Py_STRINGIFY(x)
391+
Return the size of a structure (*type*) *member* in bytes.
383392
384-
Convert ``x`` to a C string. For example, ``Py_STRINGIFY(123)`` returns
385-
``"123"``.
393+
Corresponds roughly to :samp:`sizeof((({type} *)NULL)->{member})`.
386394
387-
.. versionadded:: 3.4
395+
.. versionadded:: 3.6
396+
397+
398+
Macro definition utilities
399+
^^^^^^^^^^^^^^^^^^^^^^^^^^
388400
389401
.. c:macro:: Py_FORCE_EXPANSION(X)
390-
This is equivalent to ``X``, which is useful for token-pasting in
402+
403+
This is equivalent to :samp:`{X}`, which is useful for token-pasting in
391404
macros, as macro expansions in *X* are forcefully evaluated by the
392405
preprocessor.
393406
394-
.. c:macro:: Py_GCC_ATTRIBUTE(name)
395-
Use a GCC attribute *name*, hiding it from compilers that don't support GCC
396-
attributes (such as MSVC).
407+
.. c:macro:: Py_STRINGIFY(x)
397408
398-
This expands to ``__attribute__((name))`` on a GCC compiler, and expands
399-
to nothing on compilers that don't support GCC attributes.
409+
Convert ``x`` to a C string. For example, ``Py_STRINGIFY(123)`` returns
410+
``"123"``.
411+
412+
.. versionadded:: 3.4
400413
401414
402415
Declaration utilities
@@ -515,9 +528,9 @@ Outdated macros
515528
516529
The following macros have been used to features that have been standardized
517530
in C11.
518-
They are still available for code that uses them.
519531
520532
.. c:macro:: Py_ALIGNED(num)
533+
521534
Specify alignment to *num* bytes on compilers that support it.
522535
523536
Consider using the C11 standard ``_Alignas`` specifier over this macro.
@@ -528,10 +541,10 @@ They are still available for code that uses them.
528541
Use *number* as a ``long long`` or ``unsigned long long`` integer literal,
529542
respectively.
530543
531-
Expands to *number* followed by `LL`` or ``LLU``, respectively, but will
544+
Expands to *number* followed by ``LL`` or ``LLU``, respectively, but will
532545
expand to some compiler-specific suffixes on some older compilers.
533546
534-
Consider using the C99 standard suffixes ``LL` and ``LLU`` directly.
547+
Consider using the C99 standard suffixes ``LL`` and ``LLU`` directly.
535548
536549
.. c:macro:: Py_MEMCPY(dest, src, n)
537550

0 commit comments

Comments
 (0)