Skip to content

Commit e3d05d2

Browse files
miss-islingtonZeroIntensitypicnixzvstinner
authored
[3.14] gh-141004: Document remaining pyport.h utility macros (GH-144279) (GH-144477)
gh-141004: Document remaining `pyport.h` utility macros (GH-144279) (cherry picked from commit 914fbec) Co-authored-by: Peter Bierma <zintensitydev@gmail.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent fddd858 commit e3d05d2

File tree

2 files changed

+96
-9
lines changed

2 files changed

+96
-9
lines changed

Doc/c-api/intro.rst

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,35 @@ complete listing.
127127

128128
.. versionadded:: 3.3
129129

130+
.. c:macro:: Py_ALIGNED(num)
131+
132+
Specify alignment to *num* bytes on compilers that support it.
133+
134+
Consider using the C11 standard ``_Alignas`` specifier over this macro.
135+
136+
.. c:macro:: Py_ARITHMETIC_RIGHT_SHIFT(type, integer, positions)
137+
138+
Similar to ``integer >> positions``, but forces sign extension, as the C
139+
standard does not define whether a right-shift of a signed integer will
140+
perform sign extension or a zero-fill.
141+
142+
*integer* should be any signed integer type.
143+
*positions* is the number of positions to shift to the right.
144+
145+
Both *integer* and *positions* can be evaluated more than once;
146+
consequently, avoid directly passing a function call or some other
147+
operation with side-effects to this macro. Instead, store the result as a
148+
variable and then pass it.
149+
150+
*type* is unused and only kept for backwards compatibility. Historically,
151+
*type* was used to cast *integer*.
152+
153+
.. versionchanged:: 3.1
154+
155+
This macro is now valid for all signed integer types, not just those for
156+
which ``unsigned type`` is legal. As a result, *type* is no longer
157+
used.
158+
130159
.. c:macro:: Py_ALWAYS_INLINE
131160
132161
Ask the compiler to always inline a static inline function. The compiler can
@@ -149,6 +178,15 @@ complete listing.
149178

150179
.. versionadded:: 3.11
151180

181+
.. c:macro:: Py_CAN_START_THREADS
182+
183+
If this macro is defined, then the current system is able to start threads.
184+
185+
Currently, all systems supported by CPython (per :pep:`11`), with the
186+
exception of some WebAssembly platforms, support starting threads.
187+
188+
.. versionadded:: 3.13
189+
152190
.. c:macro:: Py_CHARMASK(c)
153191
154192
Argument must be a character or an integer in the range [-128, 127] or [0,
@@ -166,11 +204,35 @@ complete listing.
166204
.. versionchanged:: 3.8
167205
MSVC support was added.
168206

207+
.. c:macro:: Py_FORCE_EXPANSION(X)
208+
209+
This is equivalent to ``X``, which is useful for token-pasting in
210+
macros, as macro expansions in *X* are forcefully evaluated by the
211+
preprocessor.
212+
213+
.. c:macro:: Py_GCC_ATTRIBUTE(name)
214+
215+
Use a GCC attribute *name*, hiding it from compilers that don't support GCC
216+
attributes (such as MSVC).
217+
218+
This expands to ``__attribute__((name))`` on a GCC compiler, and expands
219+
to nothing on compilers that don't support GCC attributes.
220+
169221
.. c:macro:: Py_GETENV(s)
170222
171223
Like ``getenv(s)``, but returns ``NULL`` if :option:`-E` was passed on the
172224
command line (see :c:member:`PyConfig.use_environment`).
173225

226+
.. c:macro:: Py_LL(number)
227+
228+
Use *number* as a ``long long`` integer literal.
229+
230+
This usally expands to *number* followed by ``LL``, but will expand to some
231+
compiler-specific suffixes (such as ``I64``) on older compilers.
232+
233+
In modern versions of Python, this macro is not very useful, as C99 and
234+
later require the ``LL`` suffix to be valid for an integer.
235+
174236
.. c:macro:: Py_LOCAL(type)
175237
176238
Declare a function returning the specified *type* using a fast-calling
@@ -228,13 +290,37 @@ complete listing.
228290

229291
.. versionadded:: 3.11
230292

293+
.. c:macro:: Py_SAFE_DOWNCAST(value, larger, smaller)
294+
295+
Cast *value* to type *smaller* from type *larger*, validating that no
296+
information was lost.
297+
298+
On release builds of Python, this is roughly equivalent to
299+
``(smaller) value`` (in C++, ``static_cast<smaller>(value)`` will be
300+
used instead).
301+
302+
On debug builds (implying that :c:macro:`Py_DEBUG` is defined), this asserts
303+
that no information was lost with the cast from *larger* to *smaller*.
304+
305+
*value*, *larger*, and *smaller* may all be evaluated more than once in the
306+
expression; consequently, do not pass an expression with side-effects directly to
307+
this macro.
308+
231309
.. c:macro:: Py_STRINGIFY(x)
232310
233311
Convert ``x`` to a C string. E.g. ``Py_STRINGIFY(123)`` returns
234312
``"123"``.
235313

236314
.. versionadded:: 3.4
237315

316+
.. c:macro:: Py_ULL(number)
317+
318+
Similar to :c:macro:`Py_LL`, but *number* will be an ``unsigned long long``
319+
literal instead. This is done by appending ``U`` to the result of ``Py_LL``.
320+
321+
In modern versions of Python, this macro is not very useful, as C99 and
322+
later require the ``ULL``/``LLU`` suffixes to be valid for an integer.
323+
238324
.. c:macro:: Py_UNREACHABLE()
239325
240326
Use this when you have a code path that cannot be reached by design.
@@ -375,6 +461,16 @@ complete listing.
375461
This macro is intended for defining CPython's C API itself;
376462
extension modules should not use it for their own symbols.
377463

464+
.. c:macro:: Py_VA_COPY
465+
466+
This is a :term:`soft deprecated` alias to the C99-standard ``va_copy``
467+
function.
468+
469+
Historically, this would use a compiler-specific method to copy a ``va_list``.
470+
471+
.. versionchanged:: 3.6
472+
This is now an alias to ``va_copy``.
473+
378474

379475
.. _api-objects:
380476

Tools/check-c-api-docs/ignored_c_api.txt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,6 @@ Py_TPFLAGS_IS_ABSTRACT
3131
PyExpat_CAPI_MAGIC
3232
PyExpat_CAPSULE_NAME
3333
# pyport.h
34-
Py_ALIGNED
35-
Py_ARITHMETIC_RIGHT_SHIFT
36-
Py_CAN_START_THREADS
37-
Py_FORCE_EXPANSION
38-
Py_GCC_ATTRIBUTE
39-
Py_LL
40-
Py_SAFE_DOWNCAST
41-
Py_ULL
42-
Py_VA_COPY
4334
PYLONG_BITS_IN_DIGIT
4435
PY_DWORD_MAX
4536
PY_FORMAT_SIZE_T

0 commit comments

Comments
 (0)