8000 Merge remote-tracking branch 'upstream/main' into cleanup/int-dos · python/cpython@65fa579 · GitHub
[go: up one dir, main page]

Skip to content

Commit 65fa579

Browse files
committed
Merge remote-tracking branch 'upstream/main' into cleanup/int-dos
2 parents e51cb02 + 16c33a9 commit 65fa579

File tree

169 files changed

+31259
-28203
lines changed

Some content is hidden

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

169 files changed

+31259
-28203
lines changed

Doc/c-api/function.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ There are a few functions specific to Python functions.
8383
Raises :exc:`SystemError` and returns ``-1`` on failure.
8484
8585
86+
.. c:function:: void PyFunction_SetVectorcall(PyFunctionObject *func, vectorcallfunc vectorcall)
87+
88+
Set the vectorcall field of a given function object *func*.
89+
90+
Warning: extensions using this API must preserve the behavior
91+
of the unaltered (default) vectorcall function!
92+
93+
.. versionadded:: 3.12
94+
8695
.. c:function:: PyObject* PyFunction_GetClosure(PyObject *op)
8796
8897
Return the closure associated with the function object *op*. This can be ``NULL``

Doc/library/asyncio-eventloop.rst

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,8 @@ Opening network connections
377377
local_addr=None, server_hostname=None, \
378378
ssl_handshake_timeout=None, \
379379
ssl_shutdown_timeout=None, \
380-
happy_eyeballs_delay=None, interleave=None)
380+
happy_eyeballs_delay=None, interleave=None, \
381+
all_errors=False)
381382
382383
Open a streaming transport connection to a given
383384
address specified by *host* and *port*.
@@ -468,6 +469,14 @@ Opening network connections
468469
to complete before aborting the connection. ``30.0`` seconds if ``None``
469470
(default).
470471

472+
* *all_errors* determines what exceptions are raised when a connection cannot
473+
be created. By default, only a single ``Exception`` is raised: the first
474+
exception if there is only one or all errors have same message, or a single
475+
``OSError`` with the error messages combined. When ``all_errors`` is ``True``,
476+
an ``ExceptionGroup`` will be raised containing all exceptions (even if there
477+
is only one).
478+
479+
471480
.. versionchanged:: 3.5
472481

473482
Added support for SSL/TLS in :class:`ProactorEventLoop`.
@@ -500,6 +509,9 @@ Opening network connections
500509

501510
Added the *ssl_shutdown_timeout* parameter.
502511

512+
.. versionchanged:: 3.12
513+
*all_errors* was added.
514+
503515
.. seealso::
504516

505517
The :func:`open_connection` function is a high-level alternative
@@ -1663,7 +1675,7 @@ event loop::
16631675
print('Hello World')
16641676
loop.stop()
16651677

1666-
loop = asyncio.get_event_loop()
1678+
loop = asyncio.new_event_loop()
16671679

16681680
# Schedule a call to hello_world()
16691681
loop.call_soon(hello_world, loop)
@@ -1699,7 +1711,7 @@ after 5 seconds, and then stops the event loop::
16991711
else:
17001712
loop.stop()
17011713

1702-
loop = asyncio.get_event_loop()
1714+
loop = asyncio.new_event_loop()
17031715

17041716
# Schedule the first call to display_date()
17051717
end_time = loop.time() + 5.0
@@ -1731,7 +1743,7 @@ Wait until a file descriptor received some data using the
17311743
# Create a pair of connected file descriptors
17321744
rsock, wsock = socketpair()
17331745

1734-
loop = asyncio.get_event_loop()
1746+
loop = asyncio.new_event_loop()
17351747

17361748
def reader():
17371749
data = rsock.recv(100)

Doc/library/asyncio-future.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Future Functions
5555
preferred way for creating new Tasks.
5656

5757
Save a reference to the result of this function, to avoid
58-
a task disappearing mid execution.
58+
a task disappearing mid-execution.
5959

6060
.. versionchanged:: 3.5.1
6161
The function accepts any :term:`awaitable` object.

Doc/library/asyncio-llapi-index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ See also the main documentation section about the
267267

268268
.. rubric:: Examples
269269

270-
* :ref:`Using asyncio.get_event_loop() and loop.run_forever()
270+
* :ref:`Using asyncio.new_event_loop() and loop.run_forever()
271271
<asyncio_example_lowlevel_helloworld>`.
272272

273273
* :ref:`Using loop.call_later() <asyncio_example_call_later>`.

Doc/library/asyncio-task.rst

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,9 @@ Creating Tasks
254254
.. important::
255255

256256
Save a reference to the result of this function, to avoid
257-
a task disappearing mid execution. The event loop only keeps
257+
a task disappearing mid-execution. The event loop only keeps
258258
weak references to tasks. A task that isn't referenced elsewhere
259-
may get garbage-collected at any time, even before it's done.
259+
may get garbage collected at any time, even before it's done.
260260
For reliable "fire-and-forget" background tasks, gather them in
261261
a collection::
262262

@@ -520,7 +520,8 @@ Shielding From Cancellation
520520

521521
The statement::
522522

523-
res = await shield(something())
523+
task = asyncio.create_task(something())
524+
res = await shield(task)
524525

525526
is equivalent to::
526527

@@ -539,11 +540,19 @@ Shielding From Cancellation
539540
the ``shield()`` function should be combined with a try/except
540541
clause, as follows::
541542

543+
task = asyncio.create_task(something())
542544
try:
543-
res = await shield(something())
545+
res = await shield(task)
544546
except CancelledError:
545547
res = None
546548

549+
.. important::
550+
551+
Save a reference to tasks passed to this function, to avoid
552+
a task disappearing mid-execution. The event loop only keeps
553+
weak references to tasks. A task that isn't referenced elsewhere
554+
may get garbage collected at any time, even before it's done.
555+
547556
.. versionchanged:: 3.10
548557
Removed the *loop* parameter.
549558

Doc/library/itertools.rst

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ loops that truncate the stream.
314314

315315
def count(start=0, step=1):
316316
# count(10) --> 10 11 12 13 14 ...
317-
# count(2.5, 0.5) -> 2.5 3.0 3.5 ...
317+
# count(2.5, 0.5) --> 2.5 3.0 3.5 ...
318318
n = start
319319
while True:
320320
yield n
@@ -739,7 +739,7 @@ which incur interpreter overhead.
739739

740740
def prepend(value, iterator):
741741
"Prepend a single value in front of an iterator"
742-
# prepend(1, [2, 3, 4]) -> 1 2 3 4
742+
# prepend(1, [2, 3, 4]) --> 1 2 3 4
743743
return chain([value], iterator)
744744

745745
def tabulate(function, start=0):
@@ -812,6 +812,16 @@ which incur interpreter overhead.
812812
for k in range(len(roots) + 1)
813813
]
814814

815+
def sieve(n):
816+
"Primes less than n"
817+
# sieve(30) --> 2 3 5 7 11 13 17 19 23 29
818+
data = bytearray([1]) * n
819+
data[:2] = 0, 0
820+
limit = math.isqrt(n) + 1
821+
for p in compress(count(), islice(data, limit)):
822+
data[p+p : n : p] = bytearray(len(range(p+p, n, p)))
823+
return compress(count(), data)
824+
815825
def flatten(list_of_lists):
816826
"Flatten one level of nesting"
817827
return chain.from_iterable(list_of_lists)
@@ -842,12 +852,12 @@ which incur interpreter overhead.
842852
843853
def triplewise(iterable):
844854
"Return overlapping triplets from an iterable"
845-
# triplewise('ABCDEFG') -> ABC BCD CDE DEF EFG
855+
# triplewise('ABCDEFG') --> ABC BCD CDE DEF EFG
846856
for (a, _), (b, c) in pairwise(pairwise(iterable)):
847857
yield a, b, c
848858

849859
def sliding_window(iterable, n):
850-
# sliding_window('ABCDEFG', 4) -> ABCD BCDE CDEF DEFG
860+
# sliding_window('ABCDEFG', 4) --> ABCD BCDE CDEF DEFG
851861
it = iter(iterable)
852862
window = collections.deque(islice(it, n), maxlen=n)
853863
if len(window) == n:
@@ -1079,6 +1089,7 @@ which incur interpreter overhead.
10791089
>>> import operator
10801090
>>> import collections
10811091
>>> import math
1092+
>>> import random
10821093

10831094
>>> take(10, count())
10841095
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
@@ -1128,7 +1139,6 @@ which incur interpreter overhead.
11281139
>>> list(repeatfunc(pow, 5, 2, 3))
11291140
[8, 8, 8, 8, 8]
11301141

1131-
>>> import random
11321142
>>> take(5, map(int, repeatfunc(random.random)))
11331143
[0, 0, 0, 0, 0]
11341144

@@ -1156,10 +1166,22 @@ which incur interpreter overhead.
11561166
>>> all(factored(x) == expanded(x) for x in range(-10, 11))
11571167
True
11581168

1169+
>>> list(sieve(30))
1170+
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
1171+
>>> len(list(sieve(100)))
1172+
25
1173+
>>> len(list(sieve(1_000)))
1174+
168
1175+
>>> len(list(sieve(10_000)))
1176+
1229
1177+
>>> len(list(sieve(100_000)))
1178+
9592
1179+
>>> len(list(sieve(1_000_000)))
1180+
78498
1181+
11591182
>>> list(flatten([('a', 'b'), (), ('c', 'd', 'e'), ('f',), ('g', 'h', 'i')]))
11601183
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
11611184

1162-
>>> import random
11631185
>>> random.seed(85753098575309)
11641186
>>> list(repeatfunc(random.random, 3))
11651187
[0.16370491282496968, 0.45889608687313455, 0.3747076837820118]

Doc/library/logging.config.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,25 @@ mnemonic that the corresponding value is a callable.
534534
The ``filters`` member of ``handlers`` and ``loggers`` can take
535535
filter instances in addition to ids.
536536

537+
You can also specify a special key ``'.'`` whose value is a dictionary is a
538+
mapping of attribute names to values. If found, the specified attributes will
539+
be set on the user-defined object before it is returned. Thus, with the
540+
following configuration::
541+
542+
{
543+
'()' : 'my.package.customFormatterFactory',
544+
'bar' : 'baz',
545+
'spam' : 99.9,
546+
'answer' : 42,
547+
'.' {
548+
'foo': 'bar',
549+
'baz': 'bozz'
550+
}
551+
}
552+
553+
the returned formatter will have attribute ``foo`` set to ``'bar'`` and
554+
attribute ``baz`` set to ``'bozz'``.
555+
537556

538557
.. _logging-config-dict-externalobj:
539558

Doc/library/logging.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,35 @@ Formatter Objects
662662
:func:`traceback.print_stack`, but with the last newline removed) as a
663663
string. This default implementation just returns the input value.
664664

665+
.. class:: BufferingFormatter(linefmt=None)
666+
667+
A base formatter class suitable for subclassing when you want to format a
668+
number of records. You can pass a :class:`Formatter` instance which you want
669+
to use to format each line (that corresponds to a single record). If not
670+
specified, the default formatter (which just outputs the event message) is
671+
used as the line formatter.
672+
673+
.. method:: formatHeader(records)
674+
675+
Return a header for a list of *records*. The base implementation just
676+
returns the empty string. You will need to override this method if you
677+
want specific behaviour, e.g. to show the count of records, a title or a
678+
separator line.
679+
680+
.. method:: formatFooter(records)
681+
682+
Return a footer for a list of *records*. The base implementation just
683+
returns the empty string. You will need to override this method if you
684+
want specific behaviour, e.g. to show the count of records or a separator
685+
line.
686+
687+
.. method:: format(records)
688+
689+
Return formatted text for a list of *records*. The base implementation
690+
just returns the empty string if there are no records; otherwise, it
691+
returns the concatenation of the header, each record formatted with the
692+
line formatter, and the footer.
693+
665694
.. _filter:
666695

667696
Filter Objects

Doc/library/reprlib.rst

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ This module provides a class, an instance, and a function:
1919

2020
.. class:: Repr(*, maxlevel=6, maxtuple=6, maxlist=6, maxarray=5, maxdict=4, \
2121
maxset=6, maxfrozenset=6, maxdeque=6, maxstring=30, maxlong=40, \
22-
maxother=30, fillvalue="...")
22+
maxother=30, fillvalue="...", indent=None)
2323

2424
Class which provides formatting services useful in implementing functions
2525
similar to the built-in :func:`repr`; size limits for different object types
@@ -142,6 +142,66 @@ which format specific object types.
142142
similar manner as :attr:`maxstring`. The default is ``20``.
143143

144144

145+
.. attribute:: Repr.indent
146+
147+
If this attribute is set to ``None`` (the default), the output is formatted
148+
with no line breaks or indentation, like the standard :func:`repr`.
149+
For example:
150+
151+
.. code-block:: pycon
152+
153+
>>> example = [
154+
1, 'spam', {'a': 2, 'b': 'spam eggs', 'c': {3: 4.5, 6: []}}, 'ham']
155+
>>> import reprlib
156+
>>> aRepr = reprlib.Repr()
157+
>>> print(aRepr.repr(example))
158+
[1, 'spam', {'a': 2, 'b': 'spam eggs', 'c': {3: 4.5, 6: []}}, 'ham']
159+
160+
If :attr:`~Repr.indent` is set to a string, each recursion level
161+
is placed on its own line, indented by that string:
162+
163+
.. code-block:: pycon
164+
165+
>>> aRepr.indent = '-->'
166+
>>> print(aRepr.repr(example))
167+
[
168+
-->1,
169+
-->'spam',
170+
-->{
171+
-->-->'a': 2,
172+
-->-->'b': 'spam eggs',
173+
-->-->'c': {
174+
-->-->-->3: 4.5,
175+
-->-->-->6: [],
176+
-->-->},
177+
-->},
178+
-->'ham',
179+
]
180+
181+
Setting :attr:`~Repr.indent` to a positive integer value behaves as if it
182+
was set to a string with that number of spaces:
183+
184+
.. code-block:: pycon
185+
186+
>>> aRepr.indent = 4
187+
>>> print(aRepr.repr(example))
188+
[
189+
1,
190+
'spam',
191+
{
192+
'a': 2,
193+
'b': 'spam eggs',
194+
'c': {
195+
3: 4.5,
196+
6: [],
197+
},
198+
},
199+
'ham',
200+
]
201+
202+
.. versionadded:: 3.12
203+
204+
145205
.. method:: Repr.repr(obj)
146206

147207
The equivalent to the built-in :func:`repr` that uses the formatting imposed by

0 commit comments

Comments
 (0)
0