Skip to content

Commit 914fbec

Browse files
ZeroIntensitypicnixzvstinner
authored
gh-141004: Document remaining pyport.h utility macros (GH-144279)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent e423e0c commit 914fbec

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
@@ -167,6 +167,35 @@ complete listing.
167167

168168
.. versionadded:: 3.3
169169

170+
.. c:macro:: Py_ALIGNED(num)
171+
172+
Specify alignment to *num* bytes on compilers that support it.
173+
174+
Consider using the C11 standard ``_Alignas`` specifier over this macro.
175+
176+
.. c:macro:: Py_ARITHMETIC_RIGHT_SHIFT(type, integer, positions)
177+
178+
Similar to ``integer >> positions``, but forces sign extension, as the C
179+
standard does not define whether a right-shift of a signed integer will
180+
perform sign extension or a zero-fill.
181+
182+
*integer* should be any signed integer type.
183+
*positions* is the number of positions to shift to the right.
184+
185+
Both *integer* and *positions* can be evaluated more than once;
186+
consequently, avoid directly passing a function call or some other
187+
operation with side-effects to this macro. Instead, store the result as a
188+
variable and then pass it.
189+
190+
*type* is unused and only kept for backwards compatibility. Historically,
191+
*type* was used to cast *integer*.
192+
193+
.. versionchanged:: 3.1
194+
195+
This macro is now valid for all signed integer types, not just those for
196+
which ``unsigned type`` is legal. As a result, *type* is no longer
197+
used.
198+
170199
.. c:macro:: Py_ALWAYS_INLINE
171200
172201
Ask the compiler to always inline a static inline function. The compiler can
@@ -189,6 +218,15 @@ complete listing.
189218

190219
.. versionadded:: 3.11
191220

221+
.. c:macro:: Py_CAN_START_THREADS
222+
223+
If this macro is defined, then the current system is able to start threads.
224+
225+
Currently, all systems supported by CPython (per :pep:`11`), with the
226+
exception of some WebAssembly platforms, support starting threads.
227+
228+
.. versionadded:: 3.13
229+
192230
.. c:macro:: Py_CHARMASK(c)
193231
194232
Argument must be a character or an integer in the range [-128, 127] or [0,
@@ -206,11 +244,35 @@ complete listing.
206244
.. versionchanged:: 3.8
207245
MSVC support was added.
208246

247+
.. c:macro:: Py_FORCE_EXPANSION(X)
248+
249+
This is equivalent to ``X``, which is useful for token-pasting in
250+
macros, as macro expansions in *X* are forcefully evaluated by the
251+
preprocessor.
252+
253+
.. c:macro:: Py_GCC_ATTRIBUTE(name)
254+
255+
Use a GCC attribute *name*, hiding it from compilers that don't support GCC
256+
attributes (such as MSVC).
257+
258+
This expands to ``__attribute__((name))`` on a GCC compiler, and expands
259+
to nothing on compilers that don't support GCC attributes.
260+
209261
.. c:macro:: Py_GETENV(s)
210262
211263
Like ``getenv(s)``, but returns ``NULL`` if :option:`-E` was passed on the
212264
command line (see :c:member:`PyConfig.use_environment`).
213265

266+
.. c:macro:: Py_LL(number)
267+
268+
Use *number* as a ``long long`` integer literal.
269+
270+
This usally expands to *number* followed by ``LL``, but will expand to some
271+
compiler-specific suffixes (such as ``I64``) on older compilers.
272+
273+
In modern versions of Python, this macro is not very useful, as C99 and
274+
later require the ``LL`` suffix to be valid for an integer.
275+
214276
.. c:macro:: Py_LOCAL(type)
215277
216278
Declare a function returning the specified *type* using a fast-calling
@@ -268,13 +330,37 @@ complete listing.
268330

269331
.. versionadded:: 3.11
270332

333+
.. c:macro:: Py_SAFE_DOWNCAST(value, larger, smaller)
334+
335+
Cast *value* to type *smaller* from type *larger*, validating that no
336+
information was lost.
337+
338+
On release builds of Python, this is roughly equivalent to
339+
``(smaller) value`` (in C++, ``static_cast<smaller>(value)`` will be
340+
used instead).
341+
342+
On debug builds (implying that :c:macro:`Py_DEBUG` is defined), this asserts
343+
that no information was lost with the cast from *larger* to *smaller*.
344+
345+
*value*, *larger*, and *smaller* may all be evaluated more than once in the
346+
expression; consequently, do not pass an expression with side-effects directly to
347+
this macro.
348+
271349
.. c:macro:: Py_STRINGIFY(x)
272350
273351
Convert ``x`` to a C string. E.g. ``Py_STRINGIFY(123)`` returns
274352
``"123"``.
275353

276354
.. versionadded:: 3.4
277355

356+
.. c:macro:: Py_ULL(number)
357+
358+
Similar to :c:macro:`Py_LL`, but *number* will be an ``unsigned long long``
359+
literal instead. This is done by appending ``U`` to the result of ``Py_LL``.
360+
361+
In modern versions of Python, this macro is not very useful, as C99 and
362+
later require the ``ULL``/``LLU`` suffixes to be valid for an integer.
363+
278364
.. c:macro:: Py_UNREACHABLE()
279365
280366
Use this when you have a code path that cannot be reached by design.
@@ -415,6 +501,16 @@ complete listing.
415501
This macro is intended for defining CPython's C API itself;
416502
extension modules should not use it for their own symbols.
417503

504+
.. c:macro:: Py_VA_COPY
505+
506+
This is a :term:`soft deprecated` alias to the C99-standard ``va_copy``
507+
function.
508+
509+
Historically, this would use a compiler-specific method to copy a ``va_list``.
510+
511+
.. versionchanged:: 3.6
512+
This is now an alias to ``va_copy``.
513+
418514

419515
.. _api-objects:
420516

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)