Skip to content

Commit 921b4b8

Browse files
committed
Deploying to gh-pages from @ ba7e03a 🚀
1 parent 40d045b commit 921b4b8

File tree

607 files changed

+11732
-9212
lines changed

Some content is hidden

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

607 files changed

+11732
-9212
lines changed

.buildinfo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Sphinx build info version 1
22
# This file records the configuration used when building these files. When it is not found, a full rebuild will be done.
3-
config: c9ddeb59dab60e7bd89cb228c5129b61
3+
config: 53b1d72fa8dd5b628c5bce15e491c5e4
44
tags: 645f666f9bcd5a90fca523b33c5a78b7

_sources/c-api/code.rst.txt

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,67 @@ bound into a function.
211211
.. versionadded:: 3.12
212212
213213
214+
.. _c_codeobject_flags:
215+
216+
Code Object Flags
217+
-----------------
218+
219+
Code objects contain a bit-field of flags, which can be retrieved as the
220+
:attr:`~codeobject.co_flags` Python attribute (for example using
221+
:c:func:`PyObject_GetAttrString`), and set using a *flags* argument to
222+
:c:func:`PyUnstable_Code_New` and similar functions.
223+
224+
Flags whose names start with ``CO_FUTURE_`` correspond to features normally
225+
selectable by :ref:`future statements <future>`. These flags can be used in
226+
:c:member:`PyCompilerFlags.cf_flags`.
227+
Note that many ``CO_FUTURE_`` flags are mandatory in current versions of
228+
Python, and setting them has no effect.
229+
230+
The following flags are available.
231+
For their meaning, see the linked documentation of their Python equivalents.
232+
233+
234+
.. list-table::
235+
:widths: auto
236+
:header-rows: 1
237+
238+
* * Flag
239+
* Meaning
240+
* * .. c:macro:: CO_OPTIMIZED
241+
* :py:data:`inspect.CO_OPTIMIZED`
242+
* * .. c:macro:: CO_NEWLOCALS
243+
* :py:data:`inspect.CO_NEWLOCALS`
244+
* * .. c:macro:: CO_VARARGS
245+
* :py:data:`inspect.CO_VARARGS`
246+
* * .. c:macro:: CO_VARKEYWORDS
247+
* :py:data:`inspect.CO_VARKEYWORDS`
248+
* * .. c:macro:: CO_NESTED
249+
* :py:data:`inspect.CO_NESTED`
250+
* * .. c:macro:: CO_GENERATOR
251+
* :py:data:`inspect.CO_GENERATOR`
252+
* * .. c:macro:: CO_COROUTINE
253+
* :py:data:`inspect.CO_COROUTINE`
254+
* * .. c:macro:: CO_ITERABLE_COROUTINE
255+
* :py:data:`inspect.CO_ITERABLE_COROUTINE`
256+
* * .. c:macro:: CO_ASYNC_GENERATOR
257+
* :py:data:`inspect.CO_ASYNC_GENERATOR`
258+
259+
* * .. c:macro:: CO_FUTURE_DIVISION
260+
* no effect (:py:data:`__future__.division`)
261+
* * .. c:macro:: CO_FUTURE_ABSOLUTE_IMPORT
262+
* no effect (:py:data:`__future__.absolute_import`)
263+
* * .. c:macro:: CO_FUTURE_WITH_STATEMENT
264+
* no effect (:py:data:`__future__.with_statement`)
265+
* * .. c:macro:: CO_FUTURE_PRINT_FUNCTION
266+
* no effect (:py:data:`__future__.print_function`)
267+
* * .. c:macro:: CO_FUTURE_UNICODE_LITERALS
268+
* no effect (:py:data:`__future__.unicode_literals`)
269+
* * .. c:macro:: CO_FUTURE_GENERATOR_STOP
270+
* no effect (:py:data:`__future__.generator_stop`)
271+
* * .. c:macro:: CO_FUTURE_ANNOTATIONS
272+
* :py:data:`__future__.annotations`
273+
274+
214275
Extra information
215276
-----------------
216277

_sources/c-api/hash.rst.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ See also the :c:member:`PyTypeObject.tp_hash` member and :ref:`numeric-hash`.
5151
5252
Hash function definition used by :c:func:`PyHash_GetFuncDef`.
5353

54-
.. c::member:: Py_hash_t (*const hash)(const void *, Py_ssize_t)
54+
.. c:member:: Py_hash_t (*const hash)(const void *, Py_ssize_t)
5555
5656
Hash function.
5757

_sources/c-api/init.rst.txt

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,11 @@ Initializing and finalizing the interpreter
447447
freed. Some memory allocated by extension modules may not be freed. Some
448448
extensions may not work properly if their initialization routine is called more
449449
than once; this can happen if an application calls :c:func:`Py_Initialize` and
450-
:c:func:`Py_FinalizeEx` more than once.
450+
:c:func:`Py_FinalizeEx` more than once. :c:func:`Py_FinalizeEx` must not be
451+
called recursively from within itself. Therefore, it must not be called by
452+
any code that may be run as part of the interpreter shutdown process, such
453+
as :py:mod:`atexit` handlers, object finalizers, or any code that may be run
454+
while flushing the stdout and stderr files.
451455
452456
.. audit-event:: cpython._PySys_ClearAuditHooks "" c.Py_FinalizeEx
453457
@@ -1074,6 +1078,37 @@ thread, where the CPython global runtime was originally initialized.
10741078
The only exception is if :c:func:`exec` will be called immediately
10751079
after.
10761080
1081+
.. _cautions-regarding-runtime-finalization:
1082+
1083+
Cautions regarding runtime finalization
1084+
---------------------------------------
1085+
1086+
In the late stage of :term:`interpreter shutdown`, after attempting to wait for
1087+
non-daemon threads to exit (though this can be interrupted by
1088+
:class:`KeyboardInterrupt`) and running the :mod:`atexit` functions, the runtime
1089+
is marked as *finalizing*: :c:func:`Py_IsFinalizing` and
1090+
:func:`sys.is_finalizing` return true. At this point, only the *finalization
1091+
thread* that initiated finalization (typically the main thread) is allowed to
1092+
acquire the :term:`GIL`.
1093+
1094+
If any thread, other than the finalization thread, attempts to acquire the GIL
1095+
during finalization, either explicitly via :c:func:`PyGILState_Ensure`,
1096+
:c:macro:`Py_END_ALLOW_THREADS`, :c:func:`PyEval_AcquireThread`, or
1097+
:c:func:`PyEval_AcquireLock`, or implicitly when the interpreter attempts to
1098+
reacquire it after having yielded it, the thread enters **a permanently blocked
1099+
state** where it remains until the program exits. In most cases this is
1100+
harmless, but this can result in deadlock if a later stage of finalization
1101+
attempts to acquire a lock owned by the blocked thread, or otherwise waits on
1102+
the blocked thread.
1103+
1104+
Gross? Yes. This prevents random crashes and/or unexpectedly skipped C++
1105+
finalizations further up the call stack when such threads were forcibly exited
1106+
here in CPython 3.13.7 and earlier. The CPython runtime GIL acquiring C APIs
1107+
have never had any error reporting or handling expectations at GIL acquisition
1108+
time that would've allowed for graceful exit from this situation. Changing that
1109+
would require new stable C APIs and rewriting the majority of C code in the
1110+
CPython ecosystem to use those with error handling.
1111+
10771112
10781113
High-level API
10791114
--------------
@@ -1147,11 +1182,14 @@ code, or when embedding the Python interpreter:
11471182
ensues.
11481183
11491184
.. note::
1150-
Calling this function from a thread when the runtime is finalizing
1151-
will terminate the thread, even if the thread was not created by Python.
1152-
You can use :c:func:`Py_IsFinalizing` or :func:`sys.is_finalizing` to
1153-
check if the interpreter is in process of being finalized before calling
1154-
this function to avoid unwanted termination.
1185+
Calling this function from a thread when the runtime is finalizing will
1186+
hang the thread until the program exits, even if the thread was not
1187+
created by Python. Refer to
1188+
:ref:`cautions-regarding-runtime-finalization` for more details.
1189+
1190+
.. versionchanged:: next
1191+
Hangs the current thread, rather than terminating it, if called while the
1192+
interpreter is finalizing.
11551193
11561194
.. c:function:: PyThreadState* PyThreadState_Get()
11571195
@@ -1207,11 +1245,14 @@ with sub-interpreters:
12071245
to call arbitrary Python code. Failure is a fatal error.
12081246
12091247
.. note::
1210-
Calling this function from a thread when the runtime is finalizing
1211-
will terminate the thread, even if the thread was not created by Python.
1212-
You can use :c:func:`Py_IsFinalizing` or :func:`sys.is_finalizing` to
1213-
check if the interpreter is in process of being finalized before calling
1214-
this function to avoid unwanted termination.
1248+
Calling this function from a thread when the runtime is finalizing will
1249+
hang the thread until the program exits, even if the thread was not
1250+
created by Python. Refer to
1251+
:ref:`cautions-regarding-runtime-finalization` for more details.
1252+
1253+
.. versionchanged:: next
1254+
Hangs the current thread, rather than terminating it, if called while the
1255+
interpreter is finalizing.
12151256
12161257
.. c:function:: void PyGILState_Release(PyGILState_STATE)
12171258
@@ -1503,17 +1544,20 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
15031544
If this thread already has the lock, deadlock ensues.
15041545
15051546
.. note::
1506-
Calling this function from a thread when the runtime is finalizing
1507-
will terminate the thread, even if the thread was not created by Python.
1508-
You can use :c:func:`Py_IsFinalizing` or :func:`sys.is_finalizing` to
1509-
check if the interpreter is in process of being finalized before calling
1510-
this function to avoid unwanted termination.
1547+
Calling this function from a thread when the runtime is finalizing will
1548+
hang the thread until the program exits, even if the thread was not
1549+
created by Python. Refer to
1550+
:ref:`cautions-regarding-runtime-finalization` for more details.
15111551
15121552
.. versionchanged:: 3.8
15131553
Updated to be consistent with :c:func:`PyEval_RestoreThread`,
15141554
:c:func:`Py_END_ALLOW_THREADS`, and :c:func:`PyGILState_Ensure`,
15151555
and terminate the current thread if called while the interpreter is finalizing.
15161556
1557+
.. versionchanged:: next
1558+
Hangs the current thread, rather than terminating it, if called while the
1559+
interpreter is finalizing.
1560+
15171561
:c:func:`PyEval_RestoreThread` is a higher-level function which is always
15181562
available (even when threads have not been initialized).
15191563

_sources/c-api/veryhigh.rst.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ the same library that the Python runtime is using.
361361
:py:mod:`!ast` Python module, which exports these constants under
362362
the same names.
363363
364-
.. c:var:: int CO_FUTURE_DIVISION
365-
366-
This bit can be set in *flags* to cause division operator ``/`` to be
367-
interpreted as "true division" according to :pep:`238`.
364+
The "``PyCF``" flags above can be combined with "``CO_FUTURE``" flags such
365+
as :c:macro:`CO_FUTURE_ANNOTATIONS` to enable features normally
366+
selectable using :ref:`future statements <future>`.
367+
See :ref:`c_codeobject_flags` for a complete list.

_sources/extending/extending.rst.txt

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,37 @@ the module and a copyright notice if you like).
7575
See :ref:`arg-parsing-string-and-buffers` for a description of this macro.
7676

7777
All user-visible symbols defined by :file:`Python.h` have a prefix of ``Py`` or
78-
``PY``, except those defined in standard header files. For convenience, and
79-
since they are used extensively by the Python interpreter, ``"Python.h"``
80-
includes a few standard header files: ``<stdio.h>``, ``<string.h>``,
81-
``<errno.h>``, and ``<stdlib.h>``. If the latter header file does not exist on
82-
your system, it declares the functions :c:func:`malloc`, :c:func:`free` and
83-
:c:func:`realloc` directly.
78+
``PY``, except those defined in standard header files.
79+
80+
.. tip::
81+
82+
For backward compatibility, :file:`Python.h` includes several standard header files.
83+
C extensions should include the standard headers that they use,
84+
and should not rely on these implicit includes.
85+
If using the limited C API version 3.13 or newer, the implicit includes are:
86+
87+
* ``<assert.h>``
88+
* ``<intrin.h>`` (on Windows)
89+
* ``<inttypes.h>``
90+
* ``<limits.h>``
91+
* ``<math.h>``
92+
* ``<stdarg.h>``
93+
* ``<wchar.h>``
94+
* ``<sys/types.h>`` (if present)
95+
96+
If :c:macro:`Py_LIMITED_API` is not defined, or is set to version 3.12 or older,
97+
the headers below are also included:
98+
99+
* ``<ctype.h>``
100+
* ``<unistd.h>`` (on POSIX)
101+
102+
If :c:macro:`Py_LIMITED_API` is not defined, or is set to version 3.10 or older,
103+
the headers below are also included:
104+
105+
* ``<errno.h>``
106+
* ``<stdio.h>``
107+
* ``<stdlib.h>``
108+
* ``<string.h>``
84109

85110
The next thing we add to our module file is the C function that will be called
86111
when the Python expression ``spam.system(string)`` is evaluated (we'll see
@@ -214,7 +239,7 @@ and initialize it by calling :c:func:`PyErr_NewException` in the module's
214239

215240
SpamError = PyErr_NewException("spam.error", NULL, NULL);
216241

217-
Since :c:data:`!SpamError` is a global variable, it will be overwitten every time
242+
Since :c:data:`!SpamError` is a global variable, it will be overwritten every time
218243
the module is reinitialized, when the :c:data:`Py_mod_exec` function is called.
219244

220245
For now, let's avoid the issue: we will block repeated initialization by raising an

_sources/glossary.rst.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,11 @@ Glossary
402402
with :term:`abstract base classes <abstract base class>`.) Instead, it
403403
typically employs :func:`hasattr` tests or :term:`EAFP` programming.
404404

405+
dunder
406+
An informal short-hand for "double underscore", used when talking about a
407+
:term:`special method`. For example, ``__init__`` is often pronounced
408+
"dunder init".
409+
405410
EAFP
406411
Easier to ask for forgiveness than permission. This common Python coding
407412
style assumes the existence of valid keys or attributes and catches
@@ -1399,6 +1404,11 @@ Glossary
13991404
A computer defined entirely in software. Python's virtual machine
14001405
executes the :term:`bytecode` emitted by the bytecode compiler.
14011406

1407+
walrus operator
1408+
A light-hearted way to refer to the :ref:`assignment expression
1409+
<assignment-expressions>` operator ``:=`` because it looks a bit like a
1410+
walrus if you turn your head.
1411+
14021412
Zen of Python
14031413
Listing of Python design principles and philosophies that are helpful in
14041414
understanding and using the language. The listing can be found by typing

0 commit comments

Comments
 (0)