Skip to content

Commit adb59ef

Browse files
committed
Merge branch 'main' into pythongh-115999-thread-local-bytecode
2 parents b6380de + a5fc509 commit adb59ef

File tree

137 files changed

+6976
-12941
lines changed

Some content is hidden

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

137 files changed

+6976
-12941
lines changed

Doc/Makefile

+4-2
Original file line numberDiff line numberDiff line change
@@ -305,13 +305,15 @@ serve:
305305

306306
# for development releases: always build
307307
.PHONY: autobuild-dev
308+
autobuild-dev: DISTVERSION = $(shell $(PYTHON) tools/extensions/patchlevel.py --short)
308309
autobuild-dev:
309-
$(MAKE) dist-no-html SPHINXOPTS='$(SPHINXOPTS) -Ea -A daily=1'
310+
$(MAKE) dist-no-html SPHINXOPTS='$(SPHINXOPTS) -Ea -A daily=1' DISTVERSION=$(DISTVERSION)
310311

311312
# for HTML-only rebuilds
312313
.PHONY: autobuild-dev-html
314+
autobuild-dev-html: DISTVERSION = $(shell $(PYTHON) tools/extensions/patchlevel.py --short)
313315
autobuild-dev-html:
314-
$(MAKE) dist-html SPHINXOPTS='$(SPHINXOPTS) -Ea -A daily=1'
316+
$(MAKE) dist-html SPHINXOPTS='$(SPHINXOPTS) -Ea -A daily=1' DISTVERSION=$(DISTVERSION)
315317

316318
# for stable releases: only build if not in pre-release stage (alpha, beta)
317319
# release candidate downloads are okay, since the stable tree can be in that stage

Doc/c-api/init.rst

+61-17
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,11 @@ Initializing and finalizing the interpreter
430430
Some memory allocated by extension modules may not be freed. Some extensions may not
431431
work properly if their initialization routine is called more than once; this can
432432
happen if an application calls :c:func:`Py_Initialize` and :c:func:`Py_FinalizeEx`
433-
more than once.
433+
more than once. :c:func:`Py_FinalizeEx` must not be called recursively from
434+
within itself. Therefore, it must not be called by any code that may be run
435+
as part of the interpreter shutdown process, such as :py:mod:`atexit`
436+
handlers, object finalizers, or any code that may be run while flushing the
437+
stdout and stderr files.
434438

435439
.. audit-event:: cpython._PySys_ClearAuditHooks "" c.Py_FinalizeEx
436440

@@ -960,6 +964,37 @@ thread, where the CPython global runtime was originally initialized.
960964
The only exception is if :c:func:`exec` will be called immediately
961965
after.
962966
967+
.. _cautions-regarding-runtime-finalization:
968+
969+
Cautions regarding runtime finalization
970+
---------------------------------------
971+
972+
In the late stage of :term:`interpreter shutdown`, after attempting to wait for
973+
non-daemon threads to exit (though this can be interrupted by
974+
:class:`KeyboardInterrupt`) and running the :mod:`atexit` functions, the runtime
975+
is marked as *finalizing*: :c:func:`_Py_IsFinalizing` and
976+
:func:`sys.is_finalizing` return true. At this point, only the *finalization
977+
thread* that initiated finalization (typically the main thread) is allowed to
978+
acquire the :term:`GIL`.
979+
980+
If any thread, other than the finalization thread, attempts to acquire the GIL
981+
during finalization, either explicitly via :c:func:`PyGILState_Ensure`,
982+
:c:macro:`Py_END_ALLOW_THREADS`, :c:func:`PyEval_AcquireThread`, or
983+
:c:func:`PyEval_AcquireLock`, or implicitly when the interpreter attempts to
984+
reacquire it after having yielded it, the thread enters **a permanently blocked
985+
state** where it remains until the program exits. In most cases this is
986+
harmless, but this can result in deadlock if a later stage of finalization
987+
attempts to acquire a lock owned by the blocked thread, or otherwise waits on
988+
the blocked thread.
989+
990+
Gross? Yes. This prevents random crashes and/or unexpectedly skipped C++
991+
finalizations further up the call stack when such threads were forcibly exited
992+
here in CPython 3.13 and earlier. The CPython runtime GIL acquiring C APIs
993+
have never had any error reporting or handling expectations at GIL acquisition
994+
time that would've allowed for graceful exit from this situation. Changing that
995+
would require new stable C APIs and rewriting the majority of C code in the
996+
CPython ecosystem to use those with error handling.
997+
963998
964999
High-level API
9651000
--------------
@@ -1033,11 +1068,14 @@ code, or when embedding the Python interpreter:
10331068
ensues.
10341069
10351070
.. note::
1036-
Calling this function from a thread when the runtime is finalizing
1037-
will terminate the thread, even if the thread was not created by Python.
1038-
You can use :c:func:`Py_IsFinalizing` or :func:`sys.is_finalizing` to
1039-
check if the interpreter is in process of being finalized before calling
1040-
this function to avoid unwanted termination.
1071+
Calling this function from a thread when the runtime is finalizing will
1072+
hang the thread until the program exits, even if the thread was not
1073+
created by Python. Refer to
1074+
:ref:`cautions-regarding-runtime-finalization` for more details.
1075+
1076+
.. versionchanged:: next
1077+
Hangs the current thread, rather than terminating it, if called while the
1078+
interpreter is finalizing.
10411079
10421080
.. c:function:: PyThreadState* PyThreadState_Get()
10431081
@@ -1092,11 +1130,14 @@ with sub-interpreters:
10921130
to call arbitrary Python code. Failure is a fatal error.
10931131
10941132
.. note::
1095-
Calling this function from a thread when the runtime is finalizing
1096-
will terminate the thread, even if the thread was not created by Python.
1097-
You can use :c:func:`Py_IsFinalizing` or :func:`sys.is_finalizing` to
1098-
check if the interpreter is in process of being finalized before calling
1099-
this function to avoid unwanted termination.
1133+
Calling this function from a thread when the runtime is finalizing will
1134+
hang the thread until the program exits, even if the thread was not
1135+
created by Python. Refer to
1136+
:ref:`cautions-regarding-runtime-finalization` for more details.
1137+
1138+
.. versionchanged:: next
1139+
Hangs the current thread, rather than terminating it, if called while the
1140+
interpreter is finalizing.
11001141
11011142
.. c:function:: void PyGILState_Release(PyGILState_STATE)
11021143
@@ -1224,7 +1265,7 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
12241265
.. c:function:: void PyThreadState_DeleteCurrent(void)
12251266
12261267
Destroy the current thread state and release the global interpreter lock.
1227-
Like :c:func:`PyThreadState_Delete`, the global interpreter lock need not
1268+
Like :c:func:`PyThreadState_Delete`, the global interpreter lock must
12281269
be held. The thread state must have been reset with a previous call
12291270
to :c:func:`PyThreadState_Clear`.
12301271
@@ -1374,17 +1415,20 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
13741415
If this thread already has the lock, deadlock ensues.
13751416
13761417
.. note::
1377-
Calling this function from a thread when the runtime is finalizing
1378-
will terminate the thread, even if the thread was not created by Python.
1379-
You can use :c:func:`Py_IsFinalizing` or :func:`sys.is_finalizing` to
1380-
check if the interpreter is in process of being finalized before calling
1381-
this function to avoid unwanted termination.
1418+
Calling this function from a thread when the runtime is finalizing will
1419+
hang the thread until the program exits, even if the thread was not
1420+
created by Python. Refer to
1421+
:ref:`cautions-regarding-runtime-finalization` for more details.
13821422
13831423
.. versionchanged:: 3.8
13841424
Updated to be consistent with :c:func:`PyEval_RestoreThread`,
13851425
:c:func:`Py_END_ALLOW_THREADS`, and :c:func:`PyGILState_Ensure`,
13861426
and terminate the current thread if called while the interpreter is finalizing.
13871427
1428+
.. versionchanged:: next
1429+
Hangs the current thread, rather than terminating it, if called while the
1430+
interpreter is finalizing.
1431+
13881432
:c:func:`PyEval_RestoreThread` is a higher-level function which is always
13891433
available (even when threads have not been initialized).
13901434

Doc/library/argparse.rst

+32-9
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Quick Links for ArgumentParser
3030
========================= =========================================================================================================== ==================================================================================
3131
Name Description Values
3232
========================= =========================================================================================================== ==================================================================================
33-
prog_ The name of the program Defaults to ``os.path.basename(sys.argv[0])``
33+
prog_ The name of the program
3434
usage_ The string describing the program usage
3535
description_ A brief description of what the program does
3636
epilog_ Additional description of the program after the argument help
@@ -214,8 +214,8 @@ ArgumentParser objects
214214
as keyword arguments. Each parameter has its own more detailed description
215215
below, but in short they are:
216216

217-
* prog_ - The name of the program (default:
218-
``os.path.basename(sys.argv[0])``)
217+
* prog_ - The name of the program (default: generated from the ``__main__``
218+
module attributes and ``sys.argv[0]``)
219219

220220
* usage_ - The string describing the program usage (default: generated from
221221
arguments added to parser)
@@ -268,10 +268,18 @@ The following sections describe how each of these are used.
268268
prog
269269
^^^^
270270

271-
By default, :class:`ArgumentParser` objects use the base name
272-
(see :func:`os.path.basename`) of ``sys.argv[0]`` to determine
273-
how to display the name of the program in help messages. This default is almost
274-
always desirable because it will make the help messages match the name that was
271+
By default, :class:`ArgumentParser` calculates the name of the program
272+
to display in help messages depending on the way the Python inerpreter was run:
273+
274+
* The :func:`base name <os.path.basename>` of ``sys.argv[0]`` if a file was
275+
passed as argument.
276+
* The Python interpreter name followed by ``sys.argv[0]`` if a directory or
277+
a zipfile was passed as argument.
278+
* The Python interpreter name followed by ``-m`` followed by the
279+
module or package name if the :option:`-m` option was used.
280+
281+
This default is almost
282+
always desirable because it will make the help messages match the string that was
275283
used to invoke the program on the command line. For example, consider a file
276284
named ``myprogram.py`` with the following code::
277285

@@ -281,7 +289,7 @@ named ``myprogram.py`` with the following code::
281289
args = parser.parse_args()
282290

283291
The help for this program will display ``myprogram.py`` as the program name
284-
(regardless of where the program was invoked from):
292+
(regardless of where the program was invoked from) if it is run as a script:
285293

286294
.. code-block:: shell-session
287295
@@ -299,6 +307,17 @@ The help for this program will display ``myprogram.py`` as the program name
299307
-h, --help show this help message and exit
300308
--foo FOO foo help
301309
310+
If it is executed via the :option:`-m` option, the help will display a corresponding command line:
311+
312+
.. code-block:: shell-session
313+
314+
$ /usr/bin/python3 -m subdir.myprogram --help
315+
usage: python3 -m subdir.myprogram [-h] [--foo FOO]
316+
317+
options:
318+
-h, --help show this help message and exit
319+
--foo FOO foo help
320+
302321
To change this default behavior, another value can be supplied using the
303322
``prog=`` argument to :class:`ArgumentParser`::
304323

@@ -309,7 +328,8 @@ To change this default behavior, another value can be supplied using the
309328
options:
310329
-h, --help show this help message and exit
311330

312-
Note that the program name, whether determined from ``sys.argv[0]`` or from the
331+
Note that the program name, whether determined from ``sys.argv[0]``,
332+
from the ``__main__`` module attributes or from the
313333
``prog=`` argument, is available to help messages using the ``%(prog)s`` format
314334
specifier.
315335

@@ -324,6 +344,9 @@ specifier.
324344
-h, --help show this help message and exit
325345
--foo FOO foo of the myprogram program
326346

347+
.. versionchanged:: 3.14
348+
The default ``prog`` value now reflects how ``__main__`` was actually executed,
349+
rather than always being ``os.path.basename(sys.argv[0])``.
327350

328351
usage
329352
^^^^^

Doc/library/dataclasses.rst

+9-1
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ Module contents
399399
:func:`!astuple` raises :exc:`TypeError` if *obj* is not a dataclass
400400
instance.
401401

402-
.. function:: make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False, weakref_slot=False, module=None)
402+
.. function:: make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False, weakref_slot=False, module=None, decorator=dataclass)
403403

404404
Creates a new dataclass with name *cls_name*, fields as defined
405405
in *fields*, base classes as given in *bases*, and initialized
@@ -415,6 +415,11 @@ Module contents
415415
of the dataclass is set to that value.
416416
By default, it is set to the module name of the caller.
417417

418+
The *decorator* parameter is a callable that will be used to create the dataclass.
419+
It should take the class object as a first argument and the same keyword arguments
420+
as :func:`@dataclass <dataclass>`. By default, the :func:`@dataclass <dataclass>`
421+
function is used.
422+
418423
This function is not strictly required, because any Python
419424
mechanism for creating a new class with :attr:`!__annotations__` can
420425
then apply the :func:`@dataclass <dataclass>` function to convert that class to
@@ -438,6 +443,9 @@ Module contents
438443
def add_one(self):
439444
return self.x + 1
440445

446+
.. versionadded:: 3.14
447+
Added the *decorator* parameter.
448+
441449
.. function:: replace(obj, /, **changes)
442450

443451
Creates a new object of the same type as *obj*, replacing

Doc/library/datetime.rst

+15-1
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,20 @@ Instance attributes (read-only):
295295

296296
Between 0 and 86,399 inclusive.
297297

298+
.. caution::
299+
300+
It is a somewhat common bug for code to unintentionally use this attribute
301+
when it is actually intended to get a :meth:`~timedelta.total_seconds`
302+
value instead:
303+
304+
.. doctest::
305+
306+
>>> from datetime import timedelta
307+
>>> duration = timedelta(seconds=11235813)
308+
>>> duration.days, duration.seconds
309+
(130, 3813)
310+
>>> duration.total_seconds()
311+
11235813.0
298312

299313
.. attribute:: timedelta.microseconds
300314

@@ -351,7 +365,7 @@ Supported operations:
351365
| | same value. (2) |
352366
+--------------------------------+-----------------------------------------------+
353367
| ``-t1`` | Equivalent to ``timedelta(-t1.days, |
354-
| | -t1.seconds*, -t1.microseconds)``, |
368+
| | -t1.seconds, -t1.microseconds)``, |
355369
| | and to ``t1 * -1``. (1)(4) |
356370
+--------------------------------+-----------------------------------------------+
357371
| ``abs(t)`` | Equivalent to ``+t`` when ``t.days >= 0``, |

Doc/library/functools.rst

+1-2
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,7 @@ The :mod:`functools` module defines the following functions:
347347

348348
def partial(func, /, *args, **keywords):
349349
def newfunc(*more_args, **more_keywords):
350-
keywords_union = {**keywords, **more_keywords}
351-
return func(*args, *more_args, **keywords_union)
350+
return func(*args, *more_args, **(keywords | more_keywords))
352351
newfunc.func = func
353352
newfunc.args = args
354353
newfunc.keywords = keywords

Doc/library/sys.monitoring.rst

+5-7
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,14 @@ Registering and using tools
5050
*tool_id* must be in the range 0 to 5 inclusive.
5151
Raises a :exc:`ValueError` if *tool_id* is in use.
5252

53-
.. function:: free_tool_id(tool_id: int, /) -> None
53+
.. function:: clear_tool_id(tool_id: int, /) -> None
5454

55-
Should be called once a tool no longer requires *tool_id*.
55+
Unregister all events and callback functions associated with *tool_id*.
5656

57-
.. note::
57+
.. function:: free_tool_id(tool_id: int, /) -> None
5858

59-
:func:`free_tool_id` will not disable global or local events associated
60-
with *tool_id*, nor will it unregister any callback functions. This
61-
function is only intended to be used to notify the VM that the
62-
particular *tool_id* is no longer in use.
59+
Should be called once a tool no longer requires *tool_id*.
60+
Will call :func:`clear_tool_id` before releasing *tool_id*.
6361

6462
.. function:: get_tool(tool_id: int, /) -> str | None
6563

Doc/tools/extensions/patchlevel.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,8 @@ def get_version_info():
7474

7575

7676
if __name__ == "__main__":
77-
print(format_version_info(get_header_version_info())[0])
77+
short_ver, full_ver = format_version_info(get_header_version_info())
78+
if sys.argv[1:2] == ["--short"]:
79+
print(short_ver)
80+
else:
81+
print(full_ver)

Doc/using/windows.rst

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ available for application-local distributions.
2323

2424
As specified in :pep:`11`, a Python release only supports a Windows platform
2525
while Microsoft considers the platform under extended support. This means that
26-
Python |version| supports Windows 8.1 and newer. If you require Windows 7
27-
support, please install Python 3.8.
26+
Python |version| supports Windows 10 and newer. If you require Windows 7
27+
support, please install Python 3.8. If you require Windows 8.1 support,
28+
please install Python 3.12.
2829

2930
There are a number of different installers available for Windows, each with
3031
certain benefits and downsides.

0 commit comments

Comments
 (0)