Skip to content

Commit 92405e4

Browse files
committed
Merge remote-tracking branch 'upstream' into delete-global
2 parents f7a4aaf + c393ab6 commit 92405e4

273 files changed

Lines changed: 8736 additions & 4268 deletions

File tree

Some content is hidden

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

Doc/c-api/threads.rst

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,61 @@ a thread state that was previously attached for the current thread.
242242
.. seealso::
243243
:pep:`788`
244244

245+
.. _c-api-reuse-thread-state:
246+
247+
Reusing a thread state across repeated calls
248+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
249+
250+
Creating and destroying a :c:type:`PyThreadState` is not free, and is more
251+
expensive on a :term:`free-threaded build`. A foreign thread that calls into
252+
the interpreter many times -- for example, a worker thread in a native thread
253+
pool -- should avoid creating a fresh thread state on every entry and
254+
destroying it on every exit. Instead, set up one thread state when the thread
255+
starts (or lazily on its first call into Python), attach and detach it around
256+
each call, and tear it down once when the thread exits.
257+
258+
Manage the thread state explicitly with :c:func:`PyThreadState_New`, attaching
259+
and detaching it with :c:func:`PyEval_RestoreThread` and
260+
:c:func:`PyEval_SaveThread`. This happens in three distinct phases, at
261+
different points in the thread's life.
262+
263+
When the thread starts, create one thread state for it. ``interp`` is the
264+
target interpreter, captured by the code that created this thread while it held
265+
an attached thread state (for example via :c:func:`PyInterpreterState_Get`)::
266+
267+
PyThreadState *tstate = PyThreadState_New(interp);
268+
269+
Then, on each call into Python -- which may happen many times over the thread's
270+
life -- attach the thread state, make the Python C API calls that require it,
271+
and detach again so the thread does not hold the GIL while off doing non-Python
272+
work::
273+
274+
PyEval_RestoreThread(tstate);
275+
result = CallSomeFunction(); /* your Python C API calls go here */
276+
PyEval_SaveThread();
277+
278+
When the thread is finished calling into Python, destroy the thread state once::
279+
280+
PyEval_RestoreThread(tstate);
281+
PyThreadState_Clear(tstate);
282+
PyThreadState_DeleteCurrent();
283+
284+
The general-purpose entry points for calling in from a foreign thread --
285+
:c:func:`PyThreadState_Ensure` and the older :c:func:`PyGILState_Ensure` -- do
286+
*not* guarantee a persistent thread state: their thread-state lifetime is
287+
deliberately implementation-defined, so a matched acquire/release pair may
288+
create and destroy a thread state each time. Use :c:func:`PyThreadState_New`,
289+
as shown here, whenever you specifically want to reuse one thread state across
290+
calls.
291+
292+
The code that created the foreign thread must arrange for the shutdown sequence
293+
to run before the thread exits, and before :c:func:`Py_FinalizeEx` is called.
294+
If interpreter finalization begins first, the shutdown
295+
:c:func:`PyEval_RestoreThread` call will hang the thread rather than return (see
296+
:ref:`cautions-regarding-runtime-finalization`). If the thread exits without
297+
running the shutdown sequence, the thread state is leaked for the remainder of
298+
the process.
299+
245300
.. _c-api-attach-detach:
246301

247302
Attaching/detaching thread states

Doc/c-api/type.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ but need extra remarks for use as slots:
639639
in the following situations:
640640
641641
- The base is not variable-sized (its
642-
:c:member:`~PyTypeObject.tp_itemsize`).
642+
:c:member:`~PyTypeObject.tp_itemsize` is zero).
643643
- The requested :c:member:`PyType_Spec.basicsize` is positive,
644644
suggesting that the memory layout of the base class is known.
645645
- The requested :c:member:`PyType_Spec.basicsize` is zero,

Doc/deprecations/pending-removal-in-3.16.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ Pending removal in Python 3.16
3333
* :mod:`asyncio` policy system is deprecated and will be removed in Python 3.16.
3434
In particular, the following classes and functions are deprecated:
3535

36-
* :class:`asyncio.AbstractEventLoopPolicy`
37-
* :class:`asyncio.DefaultEventLoopPolicy`
38-
* :class:`asyncio.WindowsSelectorEventLoopPolicy`
39-
* :class:`asyncio.WindowsProactorEventLoopPolicy`
40-
* :func:`asyncio.get_event_loop_policy`
41-
* :func:`asyncio.set_event_loop_policy`
36+
* :class:`!asyncio.AbstractEventLoopPolicy`
37+
* :class:`!asyncio.DefaultEventLoopPolicy`
38+
* :class:`!asyncio.WindowsSelectorEventLoopPolicy`
39+
* :class:`!asyncio.WindowsProactorEventLoopPolicy`
40+
* :func:`!asyncio.get_event_loop_policy`
41+
* :func:`!asyncio.set_event_loop_policy`
4242

4343
Users should use :func:`asyncio.run` or :class:`asyncio.Runner` with
4444
*loop_factory* to use the desired event loop implementation.

Doc/deprecations/pending-removal-in-3.21.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ Pending removal in Python 3.21
1717
are not generated by the parser or accepted by the code generator.
1818
* The ``dims`` property of ``ast.Tuple`` will be removed in Python 3.21. Use
1919
the ``ast.Tuple.elts`` property instead.
20+
21+
* :mod:`struct`:
22+
23+
* Soft-deprecated since Python 3.15, using ``'F'`` and ``'D'`` type codes are now
24+
deprecated. These codes will be removed in Python 3.21. Use instead
25+
two-letter forms ``'Zf'`` and ``'Zd'``.

Doc/deprecations/soft-deprecations.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,3 @@ There are no plans to remove :term:`soft deprecated` APIs.
1919

2020
(Contributed by Gregory P. Smith in :gh:`86519` and
2121
Hugo van Kemenade in :gh:`148100`.)
22-
23-
* Using ``'F'`` and ``'D'`` format type codes of the :mod:`struct` module
24-
now are :term:`soft deprecated` in favor of two-letter forms ``'Zf'``
25-
and ``'Zd'``.
26-
(Contributed by Sergey B Kirpichev in :gh:`121249`.)

Doc/faq/design.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ In general, structured switch statements execute one block of code
263263
when an expression has a particular value or set of values.
264264
Since Python 3.10 one can easily match literal values, or constants
265265
within a namespace, with a ``match ... case`` statement.
266+
See :ref:`the specification <match>` and :ref:`the tutorial <tut-match>`
267+
for more information about :keyword:`match` statements.
266268
An older alternative is a sequence of ``if... elif... elif... else``.
267269

268270
For cases where you need to choose from a very large number of possibilities,

Doc/howto/curses.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ code, all the functions described here will probably be available. The older
5252
versions of curses carried by some proprietary Unixes may not support
5353
everything, though.
5454

55-
The Windows version of Python doesn't include the :mod:`curses`
56-
module. A ported version called :pypi:`UniCurses` is available.
55+
The Windows version of Python doesn't include the :mod:`curses` module.
56+
The third-party :pypi:`windows-curses` package provides the same interface on Windows.
5757

5858

5959
The Python curses module

Doc/howto/mro.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The Python 2.3 Method Resolution Order
1010
The Method Resolution Order discussed here was *introduced* in Python 2.3,
1111
but it is still used in later versions -- including Python 3.
1212

13-
By `Michele Simionato <https://www.phyast.pitt.edu/~micheles/>`__.
13+
By `Michele Simionato <https://github.com/micheles>`__.
1414

1515
:Abstract:
1616

Doc/library/asyncio-eventloop.rst

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ an event loop:
4848
running event loop.
4949

5050
If there is no running event loop set, the function will return
51-
the result of the ``get_event_loop_policy().get_event_loop()`` call.
51+
the loop set by :func:`set_event_loop`, or raise a :exc:`RuntimeError`
52+
if no loop has been set.
5253

53-
Because this function has rather complex behavior (especially
54-
when custom event loop policies are in use), using the
54+
Because this function has rather complex behavior, using the
5555
:func:`get_running_loop` function is preferred to :func:`get_event_loop`
5656
in coroutines and callbacks.
5757

@@ -62,13 +62,6 @@ an event loop:
6262
.. versionchanged:: 3.14
6363
Raises a :exc:`RuntimeError` if there is no current event loop.
6464

65-
.. note::
66-
67-
The :mod:`!asyncio` policy system is deprecated and will be removed
68-
in Python 3.16; from there on, this function will return the current
69-
running event loop if present else it will return the
70-
loop set by :func:`set_event_loop`.
71-
7265
.. function:: set_event_loop(loop)
7366

7467
Set *loop* as the current event loop for the current OS thread.
@@ -77,10 +70,6 @@ an event loop:
7770

7871
Create and return a new event loop object.
7972

80-
Note that the behaviour of :func:`get_event_loop`, :func:`set_event_loop`,
81-
and :func:`new_event_loop` functions can be altered by
82-
:ref:`setting a custom event loop policy <asyncio-policies>`.
83-
8473

8574
.. rubric:: Contents
8675

Doc/library/asyncio-llapi-index.rst

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ Obtaining the Event Loop
1919
- The **preferred** function to get the running event loop.
2020

2121
* - :func:`asyncio.get_event_loop`
22-
- Get an event loop instance (running or current via the current policy).
22+
- Get the running event loop or the event loop set for the current thread.
2323

2424
* - :func:`asyncio.set_event_loop`
25-
- Set the event loop as current via the current policy.
25+
- Set the event loop for the current thread.
2626

2727
* - :func:`asyncio.new_event_loop`
2828
- Create a new event loop.
@@ -497,27 +497,3 @@ Protocol classes can implement the following **callback methods**:
497497
- Called when the child process has exited. It can be called before
498498
:meth:`~SubprocessProtocol.pipe_data_received` and
499499
:meth:`~SubprocessProtocol.pipe_connection_lost` methods.
500-
501-
502-
Event Loop Policies
503-
===================
504-
505-
Policies is a low-level mechanism to alter the behavior of
506-
functions like :func:`asyncio.get_event_loop`. See also
507-
the main :ref:`policies section <asyncio-policies>` for more
508-
details.
509-
510-
511-
.. rubric:: Accessing Policies
512-
.. list-table::
513-
:widths: 50 50
514-
:class: full-width-table
515-
516-
* - :meth:`asyncio.get_event_loop_policy`
517-
- Return the current process-wide policy.
518-
519-
* - :meth:`asyncio.set_event_loop_policy`
520-
- Set a new process-wide policy.
521-
522-
* - :class:`AbstractEventLoopPolicy`
523-
- Base class for policy objects.

0 commit comments

Comments
 (0)