Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34k
gh-141004: Reorganize and reword the 'Useful macros' section#144471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -157,123 +157,37 @@ | ||
| complete listing. | ||
| Utilities | ||
| --------- | ||
| .. c:macro:: Py_ABS(x) | ||
| Return the absolute value of ``x``. | ||
| The argument may be evaluated twice. | ||
| If the result cannot be represented (for example, if ``x`` has | ||
| :c:macro:`!INT_MIN` value for :c:expr:`int` type), the behavior is | ||
| undefined. | ||
| Corresponds roughly to :samp:`(({x}) < 0 ? -({x}) : ({x}))` | ||
| .. versionadded:: 3.3 | ||
| .. c:macro:: Py_MAX(x, y) | ||
| Py_MIN(x, y) | ||
| Return the larger or smaller of the arguments, respectively. | ||
| Any arguments may be evaluated twice. | ||
| :c:macro:`!Py_MAX` corresponds roughly to | ||
| :samp:`((({x}) > ({y})) ? ({x}) : ({y}))`. | ||
| .. versionadded:: 3.3 | ||
| .. c:macro:: Py_GETENV(s) | ||
| Like :c:expr:`getenv(s)`, but returns ``NULL`` if :option:`-E` was passed on the | ||
Check warning on line 189 in Doc/c-api/intro.rst
| ||
| command line (see :c:member:`PyConfig.use_environment`). | ||
| .. c:macro:: Py_UNREACHABLE() | ||
| @@ -286,8 +200,10 @@ | ||
| avoids a warning about unreachable code. For example, the macro is | ||
| implemented with ``__builtin_unreachable()`` on GCC in release mode. | ||
| In debug mode, the macro compiles to a call to :c:func:`Py_FatalError`. | ||
| A use for ``Py_UNREACHABLE()`` is following a call a function that | ||
| never returns but that is not declared``_Noreturn``. | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Note: C11 added MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I'm aware, but don't want to get in the details too much. | ||
| If a code path is very unlikely code but can be reached under exceptional | ||
| case, this macro must not be used. For example, under low memory condition | ||
| @@ -297,18 +213,34 @@ | ||
| .. versionadded:: 3.7 | ||
| .. c:macro::Py_MEMBER_SIZE(type, member) | ||
| Return the size of a structure (*type*) *member* in bytes. | ||
| Corresponds roughly to :samp:`sizeof((({type} *)NULL)->{member})`. | ||
| .. versionadded:: 3.6 | ||
| .. c:macro:: Py_ARRAY_LENGTH(array) | ||
| Compute the length of a statically allocated C array at compile time. | ||
| The *array* argument must be a C array with a size known at compile time. | ||
| Passing an array with an unknown size, such as a heap-allocated array, | ||
| will result in a compilation error on some compilers, or otherwise produce | ||
| incorrect results. | ||
| This is roughly equivalent to:: | ||
| sizeof(array) / sizeof((array)[0]) | ||
| .. c:macro:: Py_BUILD_ASSERT(cond) | ||
| Asserts a compile-time condition *cond*, as a statement. | ||
| The build will fail if the condition is false or cannot be evaluated at compile time. | ||
| Corresponds roughly to :samp:`static_assert({cond})` on C23 and above. | ||
| For example:: | ||
| Py_BUILD_ASSERT(sizeof(PyTime_t) == sizeof(int64_t)); | ||
| @@ -327,13 +259,42 @@ | ||
| .. versionadded:: 3.3 | ||
| .. c:macro:: Py_STRINGIFY(x) | ||
| Convert ``x`` to a C string. For example, ``Py_STRINGIFY(123)`` returns | ||
| ``"123"``. | ||
| .. versionadded:: 3.4 | ||
| .. c:macro:: Py_UNUSED(arg) | ||
| Use this for unused arguments in a function definition to silence compiler | ||
| warnings. Example: ``int func(int a, int Py_UNUSED(b)) { return a; }``. | ||
| .. versionadded:: 3.4 | ||
| .. c:macro:: Py_CHARMASK(c) | ||
| Argument must be a character or an integer in the range [-128, 127] or [0, | ||
| 255]. This macro returns ``c`` cast to an ``unsigned char``. | ||
| .. c:macro:: Py_MEMCPY(dest, src, n) | ||
| This is a :term:`soft deprecated` alias to :c:func:`!memcpy`. | ||
| Use :c:func:`!memcpy` directly instead. | ||
| .. deprecated:: 3.14 | ||
| The macro is :term:`soft deprecated`. | ||
| Docstring utilities | ||
| ------------------- | ||
| .. c:macro:: PyDoc_STRVAR(name, str) | ||
| Creates a variable with name *name* that can be used in docstrings. | ||
| If Python is built :option:`without docstrings <--without-doc-strings>`, | ||
| the value will be an empty string. | ||
| Example:: | ||
| @@ -345,13 +306,12 @@ | ||
| // ... | ||
| } | ||
| Expands to :samp:`PyDoc_VAR({name}) =PyDoc_STR({str})`. | ||
| .. c:macro:: PyDoc_STR(str) | ||
| Expands to the given input string, or an empty string | ||
| ifdocstrings are :option:`disabled <--without-doc-strings>`. | ||
| Example:: | ||
| @@ -363,26 +323,94 @@ | ||
| .. c:macro:: PyDoc_VAR(name) | ||
| Declares a static character array variable with the given *name*. | ||
| Expands to :samp:`static const char {name}[]` | ||
| For example:: | ||
| PyDoc_VAR(python_doc) = PyDoc_STR( | ||
| "A genus of constricting snakes in the Pythonidae family native " | ||
| "to the tropics and subtropics of the Eastern Hemisphere."); | ||
| Declaration utilities | ||
| --------------------- | ||
| The following macros can be used in declarations. | ||
| They are most useful for defining the C API itself, and have limited use | ||
| for extension authors. | ||
| Most of them expand to compiler-specific spellings of common extensions | ||
| to the C language. | ||
| .. c:macro:: Py_ALWAYS_INLINE | ||
| Ask the compiler to always inline a static inline function. The compiler can | ||
| ignore it and decide to not inline the function. | ||
| Corresponds to ``always_inline`` attribute in GCC and ``__forceinline`` | ||
| in MSVC. | ||
| It can be used to inline performance critical static inline functions when | ||
| building Python in debug mode with function inlining disabled. For example, | ||
| MSC disables function inlining when building in debug mode. | ||
| Marking blindly a static inline function with Py_ALWAYS_INLINE can result in | ||
| worse performances (due to increased code size for example). The compiler is | ||
| usually smarter than the developer for the cost/benefit analysis. | ||
| If Python is :ref:`built in debug mode <debug-build>` (if the :c:macro:`Py_DEBUG` | ||
| macro is defined), the :c:macro:`Py_ALWAYS_INLINE` macro does nothing. | ||
| It must be specified before the function return type. Usage:: | ||
| static inline Py_ALWAYS_INLINE int random(void) { return 4; } | ||
| .. versionadded:: 3.11 | ||
| .. c:macro:: Py_NO_INLINE | ||
| Disable inlining on a function. For example, it reduces the C stack | ||
| consumption: useful on LTO+PGO builds which heavily inline code (see | ||
| :issue:`33720`). | ||
| Corresponds to the ``noinline`` attribute/specification on GCC and MSVC. | ||
| Usage:: | ||
| Py_NO_INLINE static int random(void) { return 4; } | ||
| .. versionadded:: 3.11 | ||
| .. c:macro:: Py_DEPRECATED(version) | ||
| Use this to declare APIs that were deprecated in a specific CPython version. | ||
| The macro must be placed before the symbol name. | ||
| Example:: | ||
| Py_DEPRECATED(3.8) PyAPI_FUNC(int) Py_OldFunction(void); | ||
| .. versionchanged:: 3.8 | ||
| MSVC support was added. | ||
| .. c:macro:: Py_LOCAL(type) | ||
| Declare a function returning the specified *type* using a fast-calling | ||
| qualifier for functions that are local to the current file. | ||
| Semantically, this is equivalent to :samp:`static {type}`. | ||
| .. c:macro:: Py_LOCAL_INLINE(type) | ||
| Equivalent to :c:macro:`Py_LOCAL` but additionally requests the function | ||
| be inlined. | ||
| .. c:macro:: Py_LOCAL_SYMBOL | ||
| Macro used to declare a symbol as local to the shared library (hidden). | ||
| On supported platforms, it ensures the symbol is not exported. | ||
| On compatible versions of GCC/Clang, it | ||
| expands to ``__attribute__((visibility("hidden")))``. | ||
| .. c:macro:: Py_EXPORTED_SYMBOL | ||
Uh oh!
There was an error while loading.Please reload this page.