Skip to content

Commit ca1a1a6

Browse files
committed
Merge branch 'main' into issue-133403
2 parents 7c10f27 + 8467026 commit ca1a1a6

File tree

131 files changed

+8480
-861
lines changed

Some content is hidden

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

131 files changed

+8480
-861
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ Include/internal/pycore_time.h @pganssle @abalkin
188188

189189
# AST
190190
Python/ast.c @isidentical @JelleZijlstra @eclips4
191-
Python/ast_opt.c @isidentical @eclips4
191+
Python/ast_preprocess.c @isidentical @eclips4
192192
Parser/asdl.py @isidentical @JelleZijlstra @eclips4
193193
Parser/asdl_c.py @isidentical @JelleZijlstra @eclips4
194194
Lib/ast.py @isidentical @JelleZijlstra @eclips4

.github/workflows/reusable-context.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ jobs:
9797
run: python Tools/build/compute-changes.py
9898
env:
9999
GITHUB_DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
100+
CCF_TARGET_REF: ${{ github.base_ref || github.event.repository.default_branch }}
101+
CCF_HEAD_REF: ${{ github.event.pull_request.head.sha || github.sha }}
100102

101103
- name: Compute hash for config cache key
102104
id: config-hash

Doc/library/annotationlib.rst

Lines changed: 91 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ The :func:`get_annotations` function is the main entry point for
4040
retrieving annotations. Given a function, class, or module, it returns
4141
an annotations dictionary in the requested format. This module also provides
4242
functionality for working directly with the :term:`annotate function`
43-
that is used to evaluate annotations, such as :func:`get_annotate_function`
43+
that is used to evaluate annotations, such as :func:`get_annotate_from_class_namespace`
4444
and :func:`call_annotate_function`, as well as the
4545
:func:`call_evaluate_function` function for working with
4646
:term:`evaluate functions <evaluate function>`.
@@ -132,7 +132,7 @@ Classes
132132

133133
Values are real annotation values (as per :attr:`Format.VALUE` format)
134134
for defined values, and :class:`ForwardRef` proxies for undefined
135-
values. Real objects may contain references to, :class:`ForwardRef`
135+
values. Real objects may contain references to :class:`ForwardRef`
136136
proxy objects.
137137

138138
.. attribute:: STRING
@@ -172,14 +172,21 @@ Classes
172172
:class:`~ForwardRef`. The string may not be exactly equivalent
173173
to the original source.
174174

175-
.. method:: evaluate(*, owner=None, globals=None, locals=None, type_params=None)
175+
.. method:: evaluate(*, owner=None, globals=None, locals=None, type_params=None, format=Format.VALUE)
176176

177177
Evaluate the forward reference, returning its value.
178178

179-
This may throw an exception, such as :exc:`NameError`, if the forward
179+
If the *format* argument is :attr:`~Format.VALUE` (the default),
180+
this method may throw an exception, such as :exc:`NameError`, if the forward
180181
reference refers to a name that cannot be resolved. The arguments to this
181182
method can be used to provide bindings for names that would otherwise
182-
be undefined.
183+
be undefined. If the *format* argument is :attr:`~Format.FORWARDREF`,
184+
the method will never throw an exception, but may return a :class:`~ForwardRef`
185+
instance. For example, if the forward reference object contains the code
186+
``list[undefined]``, where ``undefined`` is a name that is not defined,
187+
evaluating it with the :attr:`~Format.FORWARDREF` format will return
188+
``list[ForwardRef('undefined')]``. If the *format* argument is
189+
:attr:`~Format.STRING`, the method will return :attr:`~ForwardRef.__forward_arg__`.
183190

184191
The *owner* parameter provides the preferred mechanism for passing scope
185192
information to this method. The owner of a :class:`~ForwardRef` is the
@@ -300,15 +307,13 @@ Functions
300307

301308
.. versionadded:: 3.14
302309

303-
.. function:: get_annotate_function(obj)
310+
.. function:: get_annotate_from_class_namespace(namespace)
304311

305-
Retrieve the :term:`annotate function` for *obj*. Return :const:`!None`
306-
if *obj* does not have an annotate function. *obj* may be a class, function,
307-
module, or a namespace dictionary for a class. The last case is useful during
308-
class creation, e.g. in the ``__new__`` method of a metaclass.
309-
310-
This is usually equivalent to accessing the :attr:`~object.__annotate__`
311-
attribute of *obj*, but access through this public function is preferred.
312+
Retrieve the :term:`annotate function` from a class namespace dictionary *namespace*.
313+
Return :const:`!None` if the namespace does not contain an annotate function.
314+
This is primarily useful before the class has been fully created (e.g., in a metaclass);
315+
after the class exists, the annotate function can be retrieved with ``cls.__annotate__``.
316+
See :ref:`below <annotationlib-metaclass>` for an example using this function in a metaclass.
312317

313318
.. versionadded:: 3.14
314319

@@ -407,3 +412,76 @@ Functions
407412

408413
.. versionadded:: 3.14
409414

415+
416+
Recipes
417+
-------
418+
419+
.. _annotationlib-metaclass:
420+
421+
Using annotations in a metaclass
422+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
423+
424+
A :ref:`metaclass <metaclasses>` may want to inspect or even modify the annotations
425+
in a class body during class creation. Doing so requires retrieving annotations
426+
from the class namespace dictionary. For classes created with
427+
``from __future__ import annotations``, the annotations will be in the ``__annotations__``
428+
key of the dictionary. For other classes with annotations,
429+
:func:`get_annotate_from_class_namespace` can be used to get the
430+
annotate function, and :func:`call_annotate_function` can be used to call it and
431+
retrieve the annotations. Using the :attr:`~Format.FORWARDREF` format will usually
432+
be best, because this allows the annotations to refer to names that cannot yet be
433+
resolved when the class is created.
434+
435+
To modify the annotations, it is best to create a wrapper annotate function
436+
that calls the original annotate function, makes any necessary adjustments, and
437+
returns the result.
438+
439+
Below is an example of a metaclass that filters out all :class:`typing.ClassVar`
440+
annotations from the class and puts them in a separate attribute:
441+
442+
.. code-block:: python
443+
444+
import annotationlib
445+
import typing
446+
447+
class ClassVarSeparator(type):
448+
def __new__(mcls, name, bases, ns):
449+
if "__annotations__" in ns: # from __future__ import annotations
450+
annotations = ns["__annotations__"]
451+
classvar_keys = {
452+
key for key, value in annotations.items()
453+
# Use string comparison for simplicity; a more robust solution
454+
# could use annotationlib.ForwardRef.evaluate
455+
if value.startswith("ClassVar")
456+
}
457+
classvars = {key: annotations[key] for key in classvar_keys}
458+
ns["__annotations__"] = {
459+
key: value for key, value in annotations.items()
460+
if key not in classvar_keys
461+
}
462+
wrapped_annotate = None
463+
elif annotate := annotationlib.get_annotate_from_class_namespace(ns):
464+
annotations = annotationlib.call_annotate_function(
465+
annotate, format=annotationlib.Format.FORWARDREF
466+
)
467+
classvar_keys = {
468+
key for key, value in annotations.items()
469+
if typing.get_origin(value) is typing.ClassVar
470+
}
471+
classvars = {key: annotations[key] for key in classvar_keys}
472+
473+
def wrapped_annotate(format):
474+
annos = annotationlib.call_annotate_function(annotate, format, owner=typ)
475+
return {key: value for key, value in annos.items() if key not in classvar_keys}
476+
477+
else: # no annotations
478+
classvars = {}
479+
wrapped_annotate = None
480+
typ = super().__new__(mcls, name, bases, ns)
481+
482+
if wrapped_annotate is not None:
483+
# Wrap the original __annotate__ with a wrapper that removes ClassVars
484+
typ.__annotate__ = wrapped_annotate
485+
typ.classvars = classvars # Store the ClassVars in a separate attribute
486+
return typ
487+

Doc/library/asyncio-eventloop.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ Creating Futures and Tasks
361361

362362
.. versionadded:: 3.5.2
363363

364-
.. method:: loop.create_task(coro, *, name=None, context=None)
364+
.. method:: loop.create_task(coro, *, name=None, context=None, eager_start=None)
365365

366366
Schedule the execution of :ref:`coroutine <coroutine>` *coro*.
367367
Return a :class:`Task` object.
@@ -377,12 +377,20 @@ Creating Futures and Tasks
377377
custom :class:`contextvars.Context` for the *coro* to run in.
378378
The current context copy is created when no *context* is provided.
379379

380+
An optional keyword-only *eager_start* argument allows specifying
381+
if the task should execute eagerly during the call to create_task,
382+
or be scheduled later. If *eager_start* is not passed the mode set
383+
by :meth:`loop.set_task_factory` will be used.
384+
380385
.. versionchanged:: 3.8
381386
Added the *name* parameter.
382387

383388
.. versionchanged:: 3.11
384389
Added the *context* parameter.
385390

391+
.. versionchanged:: next
392+
Added the *eager_start* parameter.
393+
386394
.. method:: loop.set_task_factory(factory)
387395

388396
Set a task factory that will be used by

Doc/library/curses.rst

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ The module :mod:`curses` defines the following exception:
6868
The module :mod:`curses` defines the following functions:
6969

7070

71+
.. function:: assume_default_colors(fg, bg)
72+
73+
Allow use of default values for colors on terminals supporting this feature.
74+
Use this to support transparency in your application.
75+
76+
* Assign terminal default foreground/background colors to color number ``-1``.
77+
So ``init_pair(x, COLOR_RED, -1)`` will initialize pair *x* as red
78+
on default background and ``init_pair(x, -1, COLOR_BLUE)`` will
79+
initialize pair *x* as default foreground on blue.
80+
81+
* Change the definition of the color-pair ``0`` to ``(fg, bg)``.
82+
83+
.. versionadded:: next
84+
85+
7186
.. function:: baudrate()
7287

7388
Return the output speed of the terminal in bits per second. On software
@@ -290,9 +305,11 @@ The module :mod:`curses` defines the following functions:
290305
Change the definition of a color-pair. It takes three arguments: the number of
291306
the color-pair to be changed, the foreground color number, and the background
292307
color number. The value of *pair_number* must be between ``1`` and
293-
``COLOR_PAIRS - 1`` (the ``0`` color pair is wired to white on black and cannot
294-
be changed). The value of *fg* and *bg* arguments must be between ``0`` and
295-
``COLORS - 1``, or, after calling :func:`use_default_colors`, ``-1``.
308+
``COLOR_PAIRS - 1`` (the ``0`` color pair can only be changed by
309+
:func:`use_default_colors` and :func:`assume_default_colors`).
310+
The value of *fg* and *bg* arguments must be between ``0`` and
311+
``COLORS - 1``, or, after calling :func:`!use_default_colors` or
312+
:func:`!assume_default_colors`, ``-1``.
296313
If the color-pair was previously initialized, the screen is
297314
refreshed and all occurrences of that color-pair are changed to the new
298315
definition.
@@ -678,11 +695,7 @@ The module :mod:`curses` defines the following functions:
678695

679696
.. function:: use_default_colors()
680697

681-
Allow use of default values for colors on terminals supporting this feature. Use
682-
this to support transparency in your application. The default color is assigned
683-
to the color number ``-1``. After calling this function, ``init_pair(x,
684-
curses.COLOR_RED, -1)`` initializes, for instance, color pair *x* to a red
685-
foreground color on the default background.
698+
Equivalent to ``assume_default_colors(-1, -1)``.
686699

687700

688701
.. function:: wrapper(func, /, *args, **kwargs)

Doc/library/os.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2338,6 +2338,7 @@ features:
23382338
This function can support specifying *src_dir_fd* and/or *dst_dir_fd* to
23392339
supply :ref:`paths relative to directory descriptors <dir_fd>`, and :ref:`not
23402340
following symlinks <follow_symlinks>`.
2341+
The default value of *follow_symlinks* is ``False`` on Windows.
23412342

23422343
.. audit-event:: os.link src,dst,src_dir_fd,dst_dir_fd os.link
23432344

Doc/library/pdb.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ The ``run*`` functions and :func:`set_trace` are aliases for instantiating the
243243
access further features, you have to do this yourself:
244244

245245
.. class:: Pdb(completekey='tab', stdin=None, stdout=None, skip=None, \
246-
nosigint=False, readrc=True, mode=None, backend=None)
246+
nosigint=False, readrc=True, mode=None, backend=None, colorize=False)
247247

248248
:class:`Pdb` is the debugger class.
249249

@@ -273,6 +273,9 @@ access further features, you have to do this yourself:
273273
is passed, the default backend will be used. See :func:`set_default_backend`.
274274
Otherwise the supported backends are ``'settrace'`` and ``'monitoring'``.
275275

276+
The *colorize* argument, if set to ``True``, will enable colorized output in the
277+
debugger, if color is supported. This will highlight source code displayed in pdb.
278+
276279
Example call to enable tracing with *skip*::
277280

278281
import pdb; pdb.Pdb(skip=['django.*']).set_trace()
@@ -295,6 +298,9 @@ access further features, you have to do this yourself:
295298
.. versionadded:: 3.14
296299
Added the *backend* argument.
297300

301+
.. versionadded:: 3.14
302+
Added the *colorize* argument.
303+
298304
.. versionchanged:: 3.14
299305
Inline breakpoints like :func:`breakpoint` or :func:`pdb.set_trace` will
300306
always stop the program at calling frame, ignoring the *skip* pattern (if any).

Doc/library/subprocess.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,24 @@ handling consistency are valid for these functions.
15251525
Notes
15261526
-----
15271527

1528+
.. _subprocess-timeout-behavior:
1529+
1530+
Timeout Behavior
1531+
^^^^^^^^^^^^^^^^
1532+
1533+
When using the ``timeout`` parameter in functions like :func:`run`,
1534+
:meth:`Popen.wait`, or :meth:`Popen.communicate`,
1535+
users should be aware of the following behaviors:
1536+
1537+
1. **Process Creation Delay**: The initial process creation itself cannot be interrupted
1538+
on many platform APIs. This means that even when specifying a timeout, you are not
1539+
guaranteed to see a timeout exception until at least after however long process
1540+
creation takes.
1541+
1542+
2. **Extremely Small Timeout Values**: Setting very small timeout values (such as a few
1543+
milliseconds) may result in almost immediate :exc:`TimeoutExpired` exceptions because
1544+
process creation and system scheduling inherently require time.
1545+
15281546
.. _converting-argument-sequence:
15291547

15301548
Converting an argument sequence to a string on Windows

Doc/license.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,3 +1132,40 @@ The file is distributed under the 2-Clause BSD License::
11321132
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
11331133
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
11341134
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1135+
1136+
1137+
Zstandard bindings
1138+
------------------
1139+
1140+
Zstandard bindings in :file:`Modules/_zstd` and :file:`Lib/compression/zstd`
1141+
are based on code from the
1142+
`pyzstd library <https://github.com/Rogdham/pyzstd/>`_, copyright Ma Lin and
1143+
contributors. The pyzstd code is distributed under the 3-Clause BSD License::
1144+
1145+
Copyright (c) 2020-present, Ma Lin and contributors.
1146+
All rights reserved.
1147+
1148+
Redistribution and use in source and binary forms, with or without
1149+
modification, are permitted provided that the following conditions are met:
1150+
1151+
1. Redistributions of source code must retain the above copyright notice, this
1152+
list of conditions and the following disclaimer.
1153+
1154+
2. Redistributions in binary form must reproduce the above copyright notice,
1155+
this list of conditions and the following disclaimer in the documentation
1156+
and/or other materials provided with the distribution.
1157+
1158+
3. Neither the name of the copyright holder nor the names of its
1159+
contributors may be used to endorse or promote products derived from
1160+
this software without specific prior written permission.
1161+
1162+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1163+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1164+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1165+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
1166+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1167+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
1168+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
1169+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
1170+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1171+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Doc/reference/datamodel.rst

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,15 +1228,9 @@ Special attributes
12281228
:attr:`__annotations__ attributes <object.__annotations__>`.
12291229

12301230
For best practices on working with :attr:`~object.__annotations__`,
1231-
please see :mod:`annotationlib`.
1232-
1233-
.. caution::
1234-
1235-
Accessing the :attr:`!__annotations__` attribute of a class
1236-
object directly may yield incorrect results in the presence of
1237-
metaclasses. In addition, the attribute may not exist for
1238-
some classes. Use :func:`annotationlib.get_annotations` to
1239-
retrieve class annotations safely.
1231+
please see :mod:`annotationlib`. Where possible, use
1232+
:func:`annotationlib.get_annotations` instead of accessing this
1233+
attribute directly.
12401234

12411235
.. versionchanged:: 3.14
12421236
Annotations are now :ref:`lazily evaluated <lazy-evaluation>`.
@@ -1247,13 +1241,6 @@ Special attributes
12471241
if the class has no annotations.
12481242
See also: :attr:`__annotate__ attributes <object.__annotate__>`.
12491243

1250-
.. caution::
1251-
1252-
Accessing the :attr:`!__annotate__` attribute of a class
1253-
object directly may yield incorrect results in the presence of
1254-
metaclasses. Use :func:`annotationlib.get_annotate_function` to
1255-
retrieve the annotate function safely.
1256-
12571244
.. versionadded:: 3.14
12581245

12591246
* - .. attribute:: type.__type_params__

0 commit comments

Comments
 (0)