Skip to content

Commit 58df0a4

Browse files
authored
Merge branch 'main' into sem-macosx-multiprocessing-module-C
2 parents 89676c1 + c4d37ee commit 58df0a4

File tree

65 files changed

+1202
-492
lines changed

Some content is hidden

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

65 files changed

+1202
-492
lines changed

Doc/library/concurrent.futures.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,30 @@ to a :class:`ProcessPoolExecutor` will result in deadlock.
415415
require the *fork* start method for :class:`ProcessPoolExecutor` you must
416416
explicitly pass ``mp_context=multiprocessing.get_context("fork")``.
417417

418+
.. method:: terminate_workers()
419+
420+
Attempt to terminate all living worker processes immediately by calling
421+
:meth:`Process.terminate <multiprocessing.Process.terminate>` on each of them.
422+
Internally, it will also call :meth:`Executor.shutdown` to ensure that all
423+
other resources associated with the executor are freed.
424+
425+
After calling this method the caller should no longer submit tasks to the
426+
executor.
427+
428+
.. versionadded:: next
429+
430+
.. method:: kill_workers()
431+
432+
Attempt to kill all living worker processes immediately by calling
433+
:meth:`Process.kill <multiprocessing.Process.kill>` on each of them.
434+
Internally, it will also call :meth:`Executor.shutdown` to ensure that all
435+
other resources associated with the executor are freed.
436+
437+
After calling this method the caller should no longer submit tasks to the
438+
executor.
439+
440+
.. versionadded:: next
441+
418442
.. _processpoolexecutor-example:
419443

420444
ProcessPoolExecutor Example

Doc/library/io.rst

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,55 @@ Text I/O
11471147
It inherits from :class:`codecs.IncrementalDecoder`.
11481148

11491149

1150+
Static Typing
1151+
-------------
1152+
1153+
The following protocols can be used for annotating function and method
1154+
arguments for simple stream reading or writing operations. They are decorated
1155+
with :deco:`typing.runtime_checkable`.
1156+
1157+
.. class:: Reader[T]
1158+
1159+
Generic protocol for reading from a file or other input stream. ``T`` will
1160+
usually be :class:`str` or :class:`bytes`, but can be any type that is
1161+
read from the stream.
1162+
1163+
.. versionadded:: next
1164+
1165+
.. method:: read()
1166+
read(size, /)
1167+
1168+
Read data from the input stream and return it. If *size* is
1169+
specified, it should be an integer, and at most *size* items
1170+
(bytes/characters) will be read.
1171+
1172+
For example::
1173+
1174+
def read_it(reader: Reader[str]):
1175+
data = reader.read(11)
1176+
assert isinstance(data, str)
1177+
1178+
.. class:: Writer[T]
1179+
1180+
Generic protocol for writing to a file or other output stream. ``T`` will
1181+
usually be :class:`str` or :class:`bytes`, but can be any type that can be
1182+
written to the stream.
1183+
1184+
.. versionadded:: next
1185+
1186+
.. method:: write(data, /)
1187+
1188+
Write *data* to the output stream and return the number of items
1189+
(bytes/characters) written.
1190+
1191+
For example::
1192+
1193+
def write_binary(writer: Writer[bytes]):
1194+
writer.write(b"Hello world!\n")
1195+
1196+
See :ref:`typing-io` for other I/O related protocols and classes that can be
1197+
used for static type checking.
1198+
11501199
Performance
11511200
-----------
11521201

Doc/library/typing.rst

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ provides backports of these new features to older versions of Python.
5353
should broadly apply to most Python type checkers. (Some parts may still
5454
be specific to mypy.)
5555

56-
`"Static Typing with Python" <https://typing.readthedocs.io/en/latest/>`_
56+
`"Static Typing with Python" <https://typing.python.org/en/latest/>`_
5757
Type-checker-agnostic documentation written by the community detailing
5858
type system features, useful typing related tools and typing best
5959
practices.
@@ -64,7 +64,7 @@ Specification for the Python Type System
6464
========================================
6565

6666
The canonical, up-to-date specification of the Python type system can be
67-
found at `"Specification for the Python type system" <https://typing.readthedocs.io/en/latest/spec/index.html>`_.
67+
found at `"Specification for the Python type system" <https://typing.python.org/en/latest/spec/index.html>`_.
6868

6969
.. _type-aliases:
7070

@@ -2398,6 +2398,10 @@ types.
23982398
.. versionchanged:: 3.11
23992399
Added support for generic namedtuples.
24002400

2401+
.. versionchanged:: next
2402+
Using :func:`super` (and the ``__class__`` :term:`closure variable`) in methods of ``NamedTuple`` subclasses
2403+
is unsupported and causes a :class:`TypeError`.
2404+
24012405
.. deprecated-removed:: 3.13 3.15
24022406
The undocumented keyword argument syntax for creating NamedTuple classes
24032407
(``NT = NamedTuple("NT", x=int)``) is deprecated, and will be disallowed
@@ -2830,17 +2834,35 @@ with :func:`@runtime_checkable <runtime_checkable>`.
28302834
An ABC with one abstract method ``__round__``
28312835
that is covariant in its return type.
28322836

2833-
ABCs for working with IO
2834-
------------------------
2837+
.. _typing-io:
28352838

2836-
.. class:: IO
2837-
TextIO
2838-
BinaryIO
2839+
ABCs and Protocols for working with I/O
2840+
---------------------------------------
28392841

2840-
Generic type ``IO[AnyStr]`` and its subclasses ``TextIO(IO[str])``
2842+
.. class:: IO[AnyStr]
2843+
TextIO[AnyStr]
2844+
BinaryIO[AnyStr]
2845+
2846+
Generic class ``IO[AnyStr]`` and its subclasses ``TextIO(IO[str])``
28412847
and ``BinaryIO(IO[bytes])``
28422848
represent the types of I/O streams such as returned by
2843-
:func:`open`.
2849+
:func:`open`. Please note that these classes are not protocols, and
2850+
their interface is fairly broad.
2851+
2852+
The protocols :class:`io.Reader` and :class:`io.Writer` offer a simpler
2853+
alternative for argument types, when only the ``read()`` or ``write()``
2854+
methods are accessed, respectively::
2855+
2856+
def read_and_write(reader: Reader[str], writer: Writer[bytes]):
2857+
data = reader.read()
2858+
writer.write(data.encode())
2859+
2860+
Also consider using :class:`collections.abc.Iterable` for iterating over
2861+
the lines of an input stream::
2862+
2863+
def read_config(stream: Iterable[str]):
2864+
for line in stream:
2865+
...
28442866

28452867
Functions and decorators
28462868
------------------------
@@ -2912,7 +2934,7 @@ Functions and decorators
29122934

29132935
.. seealso::
29142936
`Unreachable Code and Exhaustiveness Checking
2915-
<https://typing.readthedocs.io/en/latest/guides/unreachable.html>`__ has more
2937+
<https://typing.python.org/en/latest/guides/unreachable.html>`__ has more
29162938
information about exhaustiveness checking with static typing.
29172939

29182940
.. versionadded:: 3.11

Doc/tutorial/controlflow.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ similar to a switch statement in C, Java or JavaScript (and many
289289
other languages), but it's more similar to pattern matching in
290290
languages like Rust or Haskell. Only the first pattern that matches
291291
gets executed and it can also extract components (sequence elements
292-
or object attributes) from the value into variables.
292+
or object attributes) from the value into variables. If no case matches,
293+
none of the branches is executed.
293294

294295
The simplest form compares a subject value against one or more literals::
295296

@@ -305,7 +306,7 @@ The simplest form compares a subject value against one or more literals::
305306
return "Something's wrong with the internet"
306307

307308
Note the last block: the "variable name" ``_`` acts as a *wildcard* and
308-
never fails to match. If no case matches, none of the branches is executed.
309+
never fails to match.
309310

310311
You can combine several literals in a single pattern using ``|`` ("or")::
311312

Doc/whatsnew/3.14.rst

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ It uses tail calls between small C functions that implement individual
270270
Python opcodes, rather than one large C case statement.
271271
For certain newer compilers, this interpreter provides
272272
significantly better performance. Preliminary numbers on our machines suggest
273-
anywhere from -3% to 30% faster Python code, and a geometric mean of 9-15%
273+
anywhere up to 30% faster Python code, and a geometric mean of 3-5%
274274
faster on ``pyperformance`` depending on platform and architecture. The
275275
baseline is Python 3.14 built with Clang 19 without this new interpreter.
276276

@@ -295,6 +295,19 @@ For further information on how to build Python, see
295295

296296
__ https://en.wikipedia.org/wiki/Tail_call
297297

298+
.. attention::
299+
300+
This section previously reported a 9-15% geomean speedup. This number has since been
301+
cautiously revised down to 3-5%. While we expect performance results to be better
302+
than what we report, our estimates are more conservative due to a
303+
`compiler bug <https://github.com/llvm/llvm-project/issues/106846>`_ found in
304+
Clang/LLVM 19, which causes the normal interpreter to be slower. We were unaware of this bug,
305+
resulting in inaccurate results. We sincerely apologize for
306+
communicating results that were only accurate for certain versions of LLVM 19
307+
and 20. At the time of writing, this bug has not yet been fixed in LLVM 19-21. Thus
308+
any benchmarks with those versions of LLVM may produce inaccurate numbers.
309+
(Thanks to Nelson Elhage for bringing this to light.)
310+
298311
(Contributed by Ken Jin in :gh:`128563`, with ideas on how to implement this
299312
in CPython by Mark Shannon, Garrett Gu, Haoran Xu, and Josh Haberman.)
300313

@@ -437,6 +450,10 @@ concurrent.futures
437450

438451
(Contributed by Gregory P. Smith in :gh:`84559`.)
439452

453+
* Add :meth:`concurrent.futures.ProcessPoolExecutor.terminate_workers` and
454+
:meth:`concurrent.futures.ProcessPoolExecutor.kill_workers` as
455+
ways to terminate or kill all living worker processes in the given pool.
456+
(Contributed by Charles Machalow in :gh:`130849`.)
440457

441458
contextvars
442459
-----------
@@ -602,6 +619,11 @@ io
602619
:exc:`BlockingIOError` if the operation cannot immediately return bytes.
603620
(Contributed by Giovanni Siragusa in :gh:`109523`.)
604621

622+
* Add protocols :class:`io.Reader` and :class:`io.Writer` as a simpler
623+
alternatives to the pseudo-protocols :class:`typing.IO`,
624+
:class:`typing.TextIO`, and :class:`typing.BinaryIO`.
625+
(Contributed by Sebastian Rittau in :gh:`127648`.)
626+
605627

606628
json
607629
----

Include/cpython/pystate.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,14 @@ struct _ts {
8383
unsigned int bound_gilstate:1;
8484
/* Currently in use (maybe holds the GIL). */
8585
unsigned int active:1;
86-
/* Currently holds the GIL. */
87-
unsigned int holds_gil:1;
8886

8987
/* various stages of finalization */
9088
unsigned int finalizing:1;
9189
unsigned int cleared:1;
9290
unsigned int finalized:1;
9391

9492
/* padding to align to 4 bytes */
95-
unsigned int :23;
93+
unsigned int :24;
9694
} _status;
9795
#ifdef Py_BUILD_CORE
9896
# define _PyThreadState_WHENCE_NOTSET -1
@@ -103,6 +101,10 @@ struct _ts {
103101
# define _PyThreadState_WHENCE_GILSTATE 4
104102
# define _PyThreadState_WHENCE_EXEC 5
105103
#endif
104+
105+
/* Currently holds the GIL. Must be its own field to avoid data races */
106+
int holds_gil;
107+
106108
int _whence;
107109

108110
/* Thread state (_Py_THREAD_ATTACHED, _Py_THREAD_DETACHED, _Py_THREAD_SUSPENDED).

Include/internal/pycore_interp.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,10 @@ struct _is {
296296

297297
#if !defined(Py_GIL_DISABLED) && defined(Py_STACKREF_DEBUG)
298298
uint64_t next_stackref;
299-
_Py_hashtable_t *stackref_debug_table;
299+
_Py_hashtable_t *open_stackrefs_table;
300+
# ifdef Py_STACKREF_CLOSE_DEBUG
301+
_Py_hashtable_t *closed_stackrefs_table;
302+
# endif
300303
#endif
301304
};
302305

Include/internal/pycore_magic_number.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ Known values:
270270
Python 3.14a5 3615 (CALL_FUNCTION_EX always take a kwargs argument)
271271
Python 3.14a5 3616 (Remove BINARY_SUBSCR and family. Make them BINARY_OPs)
272272
Python 3.14a6 3617 (Branch monitoring for async for loops)
273+
Python 3.14a6 3618 (Renumber RESUME opcode from 149 to 128)
273274
274275
Python 3.15 will start with 3650
275276
@@ -282,7 +283,7 @@ PC/launcher.c must also be updated.
282283
283284
*/
284285

285-
#define PYC_MAGIC_NUMBER 3617
286+
#define PYC_MAGIC_NUMBER 3618
286287
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
287288
(little-endian) and then appending b'\r\n'. */
288289
#define PYC_MAGIC_NUMBER_TOKEN \

Include/internal/pycore_opcode_metadata.h

Lines changed: 22 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)