Skip to content

Commit 4e0b8ec

Browse files
committed
Merge in the main branch (re-applying additions from #144279)
2 parents aa56752 + ffa6852 commit 4e0b8ec

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+3091
-1953
lines changed

Doc/c-api/intro.rst

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

159159

160-
Utilities
161-
---------
160+
Arithmetic Utilities
161+
--------------------
162162

163163
.. c:macro:: Py_ABS(x)
164164
@@ -184,11 +184,125 @@ Utilities
184184
185185
.. versionadded:: 3.3
186186
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``.
228+
229+
230+
Python-specific utilities
231+
-------------------------
232+
187233
.. c:macro:: Py_GETENV(s)
188234
189235
Like :samp:`getenv({s})`, but returns ``NULL`` if :option:`-E` was passed
190236
on the command line (see :c:member:`PyConfig.use_environment`).
191237

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
245+
246+
Docstring utilities
247+
^^^^^^^^^^^^^^^^^^^
248+
249+
.. c:macro:: PyDoc_STRVAR(name, str)
250+
251+
Creates a variable with name *name* that can be used in docstrings.
252+
If Python is built :option:`without docstrings <--without-doc-strings>`,
253+
the value will be an empty string.
254+
255+
Example::
256+
257+
PyDoc_STRVAR(pop_doc, "Remove and return the rightmost element.");
258+
259+
static PyMethodDef deque_methods[] = {
260+
// ...
261+
{"pop", (PyCFunction)deque_pop, METH_NOARGS, pop_doc},
262+
// ...
263+
}
264+
265+
Expands to :samp:`PyDoc_VAR({name}) = PyDoc_STR({str})`.
266+
267+
.. c:macro:: PyDoc_STR(str)
268+
269+
Expands to the given input string, or an empty string
270+
if docstrings are :option:`disabled <--without-doc-strings>`.
271+
272+
Example::
273+
274+
static PyMethodDef pysqlite_row_methods[] = {
275+
{"keys", (PyCFunction)pysqlite_row_keys, METH_NOARGS,
276+
PyDoc_STR("Returns the keys of the row.")},
277+
{NULL, NULL}
278+
};
279+
280+
.. c:macro:: PyDoc_VAR(name)
281+
282+
Declares a static character array variable with the given *name*.
283+
Expands to :samp:`static const char {name}[]`
284+
285+
For example::
286+
287+
PyDoc_VAR(python_doc) = PyDoc_STR(
288+
"A genus of constricting snakes in the Pythonidae family native "
289+
"to the tropics and subtropics of the Eastern Hemisphere.");
290+
291+
General Utilities
292+
-----------------
293+
294+
The following macros common tasks not specific to Python.
295+
296+
.. c:macro:: Py_UNUSED(arg)
297+
298+
Use this for unused arguments in a function definition to silence compiler
299+
warnings. Example: ``int func(int a, int Py_UNUSED(b)) { return a; }``.
300+
301+
.. versionadded:: 3.4
302+
303+
Assertions
304+
^^^^^^^^^^
305+
192306
.. c:macro:: Py_UNREACHABLE()
193307
194308
Use this when you have a code path that cannot be reached by design.
@@ -213,27 +327,6 @@ Utilities
213327

214328
.. versionadded:: 3.7
215329

216-
.. c:macro:: Py_MEMBER_SIZE(type, member)
217-
218-
Return the size of a structure (*type*) *member* in bytes.
219-
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])
236-
237330
.. c:macro:: Py_BUILD_ASSERT(cond)
238331
239332
Asserts a compile-time condition *cond*, as a statement.
@@ -259,78 +352,51 @@ Utilities
259352

260353
.. versionadded:: 3.3
261354

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`.
355+
Size helpers
356+
^^^^^^^^^^^^
288357

358+
.. c:macro:: Py_MEMBER_SIZE(type, member)
289359
290-
Docstring utilities
291-
-------------------
360+
Return the size of a structure (*type*) *member* in bytes.
292361

293-
.. c:macro:: PyDoc_STRVAR(name, str)
362+
Corresponds roughly to :samp:`sizeof((({type} *)NULL)->{member})`.
294363

295-
Creates a variable with name *name* that can be used in docstrings.
296-
If Python is built :option:`without docstrings <--without-doc-strings>`,
297-
the value will be an empty string.
364+
.. versionadded:: 3.6
298365

299-
Example::
366+
.. c:macro:: Py_ARRAY_LENGTH(array)
300367
301-
PyDoc_STRVAR(pop_doc, "Remove and return the rightmost element.");
368+
Compute the length of a statically allocated C array at compile time.
302369

303-
static PyMethodDef deque_methods[] = {
304-
// ...
305-
{"pop", (PyCFunction)deque_pop, METH_NOARGS, pop_doc},
306-
// ...
307-
}
370+
The *array* argument must be a C array with a size known at compile time.
371+
Passing an array with an unknown size, such as a heap-allocated array,
372+
will result in a compilation error on some compilers, or otherwise produce
373+
incorrect results.
308374

309-
Expands to :samp:`PyDoc_VAR({name}) = PyDoc_STR({str})`.
375+
This is roughly equivalent to::
310376

311-
.. c:macro:: PyDoc_STR(str)
377+
sizeof(array) / sizeof((array)[0])
312378

313-
Expands to the given input string, or an empty string
314-
if docstrings are :option:`disabled <--without-doc-strings>`.
379+
Macro helpers
380+
^^^^^^^^^^^^^
315381

316-
Example::
382+
.. c:macro:: Py_STRINGIFY(x)
317383
318-
static PyMethodDef pysqlite_row_methods[] = {
319-
{"keys", (PyCFunction)pysqlite_row_keys, METH_NOARGS,
320-
PyDoc_STR("Returns the keys of the row.")},
321-
{NULL, NULL}
322-
};
384+
Convert ``x`` to a C string. For example, ``Py_STRINGIFY(123)`` returns
385+
``"123"``.
323386

324-
.. c:macro:: PyDoc_VAR(name)
387+
.. versionadded:: 3.4
325388

326-
Declares a static character array variable with the given *name*.
327-
Expands to :samp:`static const char {name}[]`
389+
.. c:macro:: Py_FORCE_EXPANSION(X)
390+
This is equivalent to ``X``, which is useful for token-pasting in
391+
macros, as macro expansions in *X* are forcefully evaluated by the
392+
preprocessor.
328393

329-
For example::
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).
330397

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.");
398+
This expands to ``__attribute__((name))`` on a GCC compiler, and expands
399+
to nothing on compilers that don't support GCC attributes.
334400

335401

336402
Declaration utilities
@@ -383,7 +449,7 @@ to the C language.
383449

384450
.. c:macro:: Py_DEPRECATED(version)
385451
386-
Use this to declare APIs that were deprecated in a specific CPYthon version.
452+
Use this to declare APIs that were deprecated in a specific CPython version.
387453
The macro must be placed before the symbol name.
388454

389455
Example::
@@ -444,6 +510,48 @@ to the C language.
444510
extension modules should not use it for their own symbols.
445511

446512

513+
Outdated macros
514+
---------------
515+
516+
The following macros have been used to features that have been standardized
517+
in C11.
518+
They are still available for code that uses them.
519+
520+
.. c:macro:: Py_ALIGNED(num)
521+
Specify alignment to *num* bytes on compilers that support it.
522+
523+
Consider using the C11 standard ``_Alignas`` specifier over this macro.
524+
525+
.. c:macro:: Py_LL(number)
526+
Py_ULL(number)
527+
528+
Use *number* as a ``long long`` or ``unsigned long long`` integer literal,
529+
respectively.
530+
531+
Expands to *number* followed by `LL`` or ``LLU``, respectively, but will
532+
expand to some compiler-specific suffixes on some older compilers.
533+
534+
Consider using the C99 standard suffixes ``LL` and ``LLU`` directly.
535+
536+
.. c:macro:: Py_MEMCPY(dest, src, n)
537+
538+
This is a :term:`soft deprecated` alias to :c:func:`!memcpy`.
539+
Use :c:func:`!memcpy` directly instead.
540+
541+
.. deprecated:: 3.14
542+
The macro is :term:`soft deprecated`.
543+
544+
.. c:macro:: Py_VA_COPY
545+
546+
This is a :term:`soft deprecated` alias to the C99-standard ``va_copy``
547+
function.
548+
549+
Historically, this would use a compiler-specific method to copy a ``va_list``.
550+
551+
.. versionchanged:: 3.6
552+
This is now an alias to ``va_copy``.
553+
554+
447555
.. _api-objects:
448556
449557
Objects, Types and Reference Counts

Doc/glossary.rst

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,19 @@ Glossary
786786
An object that both finds and loads a module; both a
787787
:term:`finder` and :term:`loader` object.
788788

789+
index
790+
A numeric value that represents the position of an element in
791+
a :term:`sequence`.
792+
793+
In Python, indexing starts at zero.
794+
For example, ``things[0]`` names the *first* element of ``things``;
795+
``things[1]`` names the second one.
796+
797+
In some contexts, Python allows negative indexes for counting from the
798+
end of a sequence, and indexing using :term:`slices <slice>`.
799+
800+
See also :term:`subscript`.
801+
789802
interactive
790803
Python has an interactive interpreter which means you can enter
791804
statements and expressions at the interpreter prompt, immediately
@@ -863,6 +876,9 @@ Glossary
863876
CPython does not guarantee :term:`thread-safe` behavior of iterator
864877
operations.
865878

879+
key
880+
A value that identifies an entry in a :term:`mapping`.
881+
See also :term:`subscript`.
866882

867883
key function
868884
A key function or collation function is a callable that returns a value
@@ -1417,10 +1433,11 @@ Glossary
14171433
chosen based on the type of a single argument.
14181434

14191435
slice
1420-
An object usually containing a portion of a :term:`sequence`. A slice is
1421-
created using the subscript notation, ``[]`` with colons between numbers
1422-
when several are given, such as in ``variable_name[1:3:5]``. The bracket
1423-
(subscript) notation uses :class:`slice` objects internally.
1436+
An object of type :class:`slice`, used to describe a portion of
1437+
a :term:`sequence`.
1438+
A slice object is created when using the :ref:`slicing <slicings>` form
1439+
of :ref:`subscript notation <subscriptions>`, with colons inside square
1440+
brackets, such as in ``variable_name[1:3:5]``.
14241441

14251442
soft deprecated
14261443
A soft deprecated API should not be used in new code,
@@ -1478,6 +1495,14 @@ Glossary
14781495

14791496
See also :term:`borrowed reference`.
14801497

1498+
subscript
1499+
The expression in square brackets of a
1500+
:ref:`subscription expression <subscriptions>`, for example,
1501+
the ``3`` in ``items[3]``.
1502+
Usually used to select an element of a container.
1503+
Also called a :term:`key` when subscripting a :term:`mapping`,
1504+
or an :term:`index` when subscripting a :term:`sequence`.
1505+
14811506
synchronization primitive
14821507
A basic building block for coordinating (synchronizing) the execution of
14831508
multiple threads to ensure :term:`thread-safe` access to shared resources.

0 commit comments

Comments
 (0)