Skip to content

Commit e3f1df8

Browse files
authored
Merge branch 'main' into patch-6
2 parents 14cdf29 + 58d2b30 commit e3f1df8

151 files changed

Lines changed: 2722 additions & 2531 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/object.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,15 @@ Object Protocol
179179
If *o1* and *o2* are the same object, :c:func:`PyObject_RichCompareBool`
180180
will always return ``1`` for :const:`Py_EQ` and ``0`` for :const:`Py_NE`.
181181
182+
.. c:function:: PyObject* PyObject_Format(PyObject *obj, PyObject *format_spec)
183+
184+
Format *obj* using *format_spec*. This is equivalent to the Python
185+
expression ``format(obj, format_spec)``.
186+
187+
*format_spec* may be ``NULL``. In this case the call is equivalent
188+
to ``format(obj)``.
189+
Returns the formatted string on success, ``NULL`` on failure.
190+
182191
.. c:function:: PyObject* PyObject_Repr(PyObject *o)
183192
184193
.. index:: builtin: repr

Doc/library/asyncio-task.rst

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,17 @@ in the task at the next opportunity.
300300
It is recommended that coroutines use ``try/finally`` blocks to robustly
301301
perform clean-up logic. In case :exc:`asyncio.CancelledError`
302302
is explicitly caught, it should generally be propagated when
303-
clean-up is complete. Most code can safely ignore :exc:`asyncio.CancelledError`.
303+
clean-up is complete. :exc:`asyncio.CancelledError` directly subclasses
304+
:exc:`BaseException` so most code will not need to be aware of it.
304305

305306
The asyncio components that enable structured concurrency, like
306307
:class:`asyncio.TaskGroup` and :func:`asyncio.timeout`,
307308
are implemented using cancellation internally and might misbehave if
308309
a coroutine swallows :exc:`asyncio.CancelledError`. Similarly, user code
309-
should not call :meth:`uncancel <asyncio.Task.uncancel>`.
310+
should not generally call :meth:`uncancel <asyncio.Task.uncancel>`.
311+
However, in cases when suppressing :exc:`asyncio.CancelledError` is
312+
truly desired, it is necessary to also call ``uncancel()`` to completely
313+
remove the cancellation state.
310314

311315
.. _taskgroups:
312316

@@ -620,32 +624,26 @@ Timeouts
620624
The context manager produced by :func:`asyncio.timeout` can be
621625
rescheduled to a different deadline and inspected.
622626

623-
.. class:: Timeout()
627+
.. class:: Timeout(when)
624628

625629
An :ref:`asynchronous context manager <async-context-managers>`
626-
that limits time spent inside of it.
630+
for cancelling overdue coroutines.
627631

628-
.. versionadded:: 3.11
632+
``when`` should be an absolute time at which the context should time out,
633+
as measured by the event loop's clock:
634+
635+
- If ``when`` is ``None``, the timeout will never trigger.
636+
- If ``when < loop.time()``, the timeout will trigger on the next
637+
iteration of the event loop.
629638

630639
.. method:: when() -> float | None
631640

632641
Return the current deadline, or ``None`` if the current
633642
deadline is not set.
634643

635-
The deadline is a float, consistent with the time returned by
636-
:meth:`loop.time`.
637-
638644
.. method:: reschedule(when: float | None)
639645

640-
Change the time the timeout will trigger.
641-
642-
If *when* is ``None``, any current deadline will be removed, and the
643-
context manager will wait indefinitely.
644-
645-
If *when* is a float, it is set as the new deadline.
646-
647-
if *when* is in the past, the timeout will trigger on the next
648-
iteration of the event loop.
646+
Reschedule the timeout.
649647

650648
.. method:: expired() -> bool
651649

@@ -1148,7 +1146,9 @@ Task Object
11481146
Therefore, unlike :meth:`Future.cancel`, :meth:`Task.cancel` does
11491147
not guarantee that the Task will be cancelled, although
11501148
suppressing cancellation completely is not common and is actively
1151-
discouraged.
1149+
discouraged. Should the coroutine nevertheless decide to suppress
1150+
the cancellation, it needs to call :meth:`Task.uncancel` in addition
1151+
to catching the exception.
11521152

11531153
.. versionchanged:: 3.9
11541154
Added the *msg* parameter.
@@ -1238,6 +1238,10 @@ Task Object
12381238
with :meth:`uncancel`. :class:`TaskGroup` context managers use
12391239
:func:`uncancel` in a similar fashion.
12401240

1241+
If end-user code is, for some reason, suppresing cancellation by
1242+
catching :exc:`CancelledError`, it needs to call this method to remove
1243+
the cancellation state.
1244+
12411245
.. method:: cancelling()
12421246

12431247
Return the number of pending cancellation requests to this Task, i.e.,

Doc/library/dis.rst

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,30 +1152,6 @@ iterations of the loop.
11521152
.. versionchanged:: 3.12
11531153
This is no longer a pseudo-instruction.
11541154

1155-
1156-
.. opcode:: JUMP_IF_TRUE_OR_POP (delta)
1157-
1158-
If ``STACK[-1]`` is true, increments the bytecode counter by *delta* and leaves
1159-
``STACK[-1]`` on the stack. Otherwise (``STACK[-1]`` is false), ``STACK[-1]``
1160-
is popped.
1161-
1162-
.. versionadded:: 3.1
1163-
1164-
.. versionchanged:: 3.11
1165-
The oparg is now a relative delta rather than an absolute target.
1166-
1167-
.. opcode:: JUMP_IF_FALSE_OR_POP (delta)
1168-
1169-
If ``STACK[-1]`` is false, increments the bytecode counter by *delta* and leaves
1170-
``STACK[-1]`` on the stack. Otherwise (``STACK[-1]`` is true), ``STACK[-1]`` is
1171-
popped.
1172-
1173-
.. versionadded:: 3.1
1174-
1175-
.. versionchanged:: 3.11
1176-
The oparg is now a relative delta rather than an absolute target.
1177-
1178-
11791155
.. opcode:: FOR_ITER (delta)
11801156

11811157
``STACK[-1]`` is an :term:`iterator`. Call its :meth:`~iterator.__next__` method.

Doc/library/email.utils.rst

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,17 @@ module:
1313

1414
.. function:: localtime(dt=None)
1515

16-
Return local time as an aware datetime object. If called without
17-
arguments, return current time. Otherwise *dt* argument should be a
18-
:class:`~datetime.datetime` instance, and it is converted to the local time
19-
zone according to the system time zone database. If *dt* is naive (that
20-
is, ``dt.tzinfo`` is ``None``), it is assumed to be in local time. In this
21-
case, a positive or zero value for *isdst* causes ``localtime`` to presume
22-
initially that summer time (for example, Daylight Saving Time) is or is not
23-
(respectively) in effect for the specified time. A negative value for
24-
*isdst* causes the ``localtime`` to attempt to divine whether summer time
25-
is in effect for the specified time.
26-
27-
.. versionadded:: 3.3
16+
Return local time as an aware datetime object. If called without
17+
arguments, return current time. Otherwise *dt* argument should be a
18+
:class:`~datetime.datetime` instance, and it is converted to the local time
19+
zone according to the system time zone database. If *dt* is naive (that
20+
is, ``dt.tzinfo`` is ``None``), it is assumed to be in local time. The
21+
*isdst* parameter is ignored.
2822

23+
.. versionadded:: 3.3
24+
25+
.. deprecated-removed:: 3.12 3.14
26+
The *isdst* parameter.
2927

3028
.. function:: make_msgid(idstring=None, domain=None)
3129

Doc/library/exceptions.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -948,8 +948,8 @@ their subgroups based on the types of the contained exceptions.
948948
these fields do not need to be updated by :meth:`derive`. ::
949949

950950
>>> class MyGroup(ExceptionGroup):
951-
... def derive(self, exc):
952-
... return MyGroup(self.message, exc)
951+
... def derive(self, excs):
952+
... return MyGroup(self.message, excs)
953953
...
954954
>>> e = MyGroup("eg", [ValueError(1), TypeError(2)])
955955
>>> e.add_note("a note")

Doc/library/itertools.rst

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ loops that truncate the stream.
195195
if n < 1:
196196
raise ValueError('n must be at least one')
197197
it = iter(iterable)
198-
while (batch := tuple(islice(it, n))):
198+
while batch := tuple(islice(it, n)):
199199
yield batch
200200

201201
.. versionadded:: 3.12
@@ -872,11 +872,23 @@ which incur interpreter overhead.
872872
(x - 5) (x + 4) (x - 3) expands to: x³ -4x² -17x + 60
873873
"""
874874
# polynomial_from_roots([5, -4, 3]) --> [1, -4, -17, 60]
875-
roots = list(map(operator.neg, roots))
876-
return [
877-
sum(map(math.prod, combinations(roots, k)))
878-
for k in range(len(roots) + 1)
879-
]
875+
expansion = [1]
876+
for r in roots:
877+
expansion = convolve(expansion, (1, -r))
878+
return list(expansion)
879+
880+
def polynomial_eval(coefficients, x):
881+
"""Evaluate a polynomial at a specific value.
882+
883+
Computes with better numeric stability than Horner's method.
884+
"""
885+
# Evaluate x³ -4x² -17x + 60 at x = 2.5
886+
# polynomial_eval([1, -4, -17, 60], x=2.5) --> 8.125
887+
n = len(coefficients)
888+
if n == 0:
889+
return x * 0 # coerce zero to the type of x
890+
powers = map(pow, repeat(x), reversed(range(n)))
891+
return math.sumprod(coefficients, powers)
880892

881893
def iter_index(iterable, value, start=0):
882894
"Return indices where a value occurs in a sequence or iterable."
@@ -1245,6 +1257,37 @@ which incur interpreter overhead.
12451257
>>> list(convolve(data, [1, -2, 1]))
12461258
[20, 0, -36, 24, -20, 20, -20, -4, 16]
12471259

1260+
>>> from fractions import Fraction
1261+
>>> from decimal import Decimal
1262+
>>> polynomial_eval([1, -4, -17, 60], x=2)
1263+
18
1264+
>>> x = 2; x**3 - 4*x**2 -17*x + 60
1265+
18
1266+
>>> polynomial_eval([1, -4, -17, 60], x=2.5)
1267+
8.125
1268+
>>> x = 2.5; x**3 - 4*x**2 -17*x + 60
1269+
8.125
1270+
>>> polynomial_eval([1, -4, -17, 60], x=Fraction(2, 3))
1271+
Fraction(1274, 27)
1272+
>>> x = Fraction(2, 3); x**3 - 4*x**2 -17*x + 60
1273+
Fraction(1274, 27)
1274+
>>> polynomial_eval([1, -4, -17, 60], x=Decimal('1.75'))
1275+
Decimal('23.359375')
1276+
>>> x = Decimal('1.75'); x**3 - 4*x**2 -17*x + 60
1277+
Decimal('23.359375')
1278+
>>> polynomial_eval([], 2)
1279+
0
1280+
>>> polynomial_eval([], 2.5)
1281+
0.0
1282+
>>> polynomial_eval([], Fraction(2, 3))
1283+
Fraction(0, 1)
1284+
>>> polynomial_eval([], Decimal('1.75'))
1285+
Decimal('0.00')
1286+
>>> polynomial_eval([11], 7) == 11
1287+
True
1288+
>>> polynomial_eval([11, 2], 7) == 11 * 7 + 2
1289+
True
1290+
12481291
>>> polynomial_from_roots([5, -4, 3])
12491292
[1, -4, -17, 60]
12501293
>>> factored = lambda x: (x - 5) * (x + 4) * (x - 3)

Doc/library/os.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3951,7 +3951,7 @@ to be ignored.
39513951

39523952
.. note::
39533953

3954-
The standard way to exit is ``sys.exit(n)``. :func:`_exit` should
3954+
The standard way to exit is :func:`sys.exit(n) <sys.exit>`. :func:`!_exit` should
39553955
normally only be used in the child process after a :func:`fork`.
39563956

39573957
The following exit codes are defined and can be used with :func:`_exit`,

Doc/library/pdb.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ can be overridden by the local file.
513513
:file:`.pdbrc` file)::
514514

515515
# Print instance variables (usage "pi classInst")
516-
alias pi for k in %1.__dict__.keys(): print("%1.",k,"=",%1.__dict__[k])
516+
alias pi for k in %1.__dict__.keys(): print(f"%1.{k} = {%1.__dict__[k]}")
517517
# Print instance variables in self
518518
alias ps pi self
519519

Doc/library/random.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,8 @@ Alternative Generator
404404
Class that implements the default pseudo-random number generator used by the
405405
:mod:`random` module.
406406

407-
.. deprecated:: 3.9
408-
In the future, the *seed* must be one of the following types:
407+
.. deprecated-removed:: 3.9 3.11
408+
Formerly the *seed* could be any hashable object. Now it is limited to:
409409
:class:`NoneType`, :class:`int`, :class:`float`, :class:`str`,
410410
:class:`bytes`, or :class:`bytearray`.
411411

@@ -423,7 +423,7 @@ Notes on Reproducibility
423423
------------------------
424424

425425
Sometimes it is useful to be able to reproduce the sequences given by a
426-
pseudo-random number generator. By re-using a seed value, the same sequence should be
426+
pseudo-random number generator. By reusing a seed value, the same sequence should be
427427
reproducible from run to run as long as multiple threads are not running.
428428

429429
Most of the random module's algorithms and seeding functions are subject to

Doc/library/shutil.rst

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -292,15 +292,15 @@ Directory and files operations
292292
.. versionadded:: 3.8
293293
The *dirs_exist_ok* parameter.
294294

295-
.. function:: rmtree(path, ignore_errors=False, onerror=None, *, dir_fd=None)
295+
.. function:: rmtree(path, ignore_errors=False, onerror=None, *, onexc=None, dir_fd=None)
296296

297297
.. index:: single: directory; deleting
298298

299299
Delete an entire directory tree; *path* must point to a directory (but not a
300300
symbolic link to a directory). If *ignore_errors* is true, errors resulting
301301
from failed removals will be ignored; if false or omitted, such errors are
302-
handled by calling a handler specified by *onerror* or, if that is omitted,
303-
they raise an exception.
302+
handled by calling a handler specified by *onexc* or *onerror* or, if both
303+
are omitted, exceptions are propagated to the caller.
304304

305305
This function can support :ref:`paths relative to directory descriptors
306306
<dir_fd>`.
@@ -315,14 +315,17 @@ Directory and files operations
315315
otherwise. Applications can use the :data:`rmtree.avoids_symlink_attacks`
316316
function attribute to determine which case applies.
317317

318-
If *onerror* is provided, it must be a callable that accepts three
319-
parameters: *function*, *path*, and *excinfo*.
318+
If *onexc* is provided, it must be a callable that accepts three parameters:
319+
*function*, *path*, and *excinfo*.
320320

321321
The first parameter, *function*, is the function which raised the exception;
322322
it depends on the platform and implementation. The second parameter,
323323
*path*, will be the path name passed to *function*. The third parameter,
324-
*excinfo*, will be the exception information returned by
325-
:func:`sys.exc_info`. Exceptions raised by *onerror* will not be caught.
324+
*excinfo*, is the exception that was raised. Exceptions raised by *onexc*
325+
will not be caught.
326+
327+
The deprecated *onerror* is similar to *onexc*, except that the third
328+
parameter it receives is the tuple returned from :func:`sys.exc_info`.
326329

327330
.. audit-event:: shutil.rmtree path,dir_fd shutil.rmtree
328331

@@ -337,6 +340,9 @@ Directory and files operations
337340
.. versionchanged:: 3.11
338341
The *dir_fd* parameter.
339342

343+
.. versionchanged:: 3.12
344+
Added the *onexc* parameter, deprecated *onerror*.
345+
340346
.. attribute:: rmtree.avoids_symlink_attacks
341347

342348
Indicates whether the current platform and implementation provides a
@@ -509,7 +515,7 @@ rmtree example
509515
~~~~~~~~~~~~~~
510516

511517
This example shows how to remove a directory tree on Windows where some
512-
of the files have their read-only bit set. It uses the onerror callback
518+
of the files have their read-only bit set. It uses the onexc callback
513519
to clear the readonly bit and reattempt the remove. Any subsequent failure
514520
will propagate. ::
515521

@@ -521,7 +527,7 @@ will propagate. ::
521527
os.chmod(path, stat.S_IWRITE)
522528
func(path)
523529

524-
shutil.rmtree(directory, onerror=remove_readonly)
530+
shutil.rmtree(directory, onexc=remove_readonly)
525531

526532
.. _archiving-operations:
527533

0 commit comments

Comments
 (0)