Skip to content

Commit ab66b72

Browse files
authored
Merge branch 'main' into include-patchlevel
2 parents ddeeb09 + f3759d2 commit ab66b72

File tree

86 files changed

+2639
-1380
lines changed

Some content is hidden

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

86 files changed

+2639
-1380
lines changed

Doc/c-api/apiabiversion.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@ See :ref:`stable` for a discussion of API and ABI stability across versions.
3434
This can be ``0xA`` for alpha, ``0xB`` for beta, ``0xC`` for release
3535
candidate or ``0xF`` for final.
3636

37+
38+
.. c:namespace:: NULL
39+
.. c:macro:: PY_RELEASE_LEVEL_ALPHA
40+
:no-typesetting:
41+
.. c:macro:: PY_RELEASE_LEVEL_BETA
42+
:no-typesetting:
43+
.. c:macro:: PY_RELEASE_LEVEL_GAMMA
44+
:no-typesetting:
45+
.. c:macro:: PY_RELEASE_LEVEL_FINAL
46+
:no-typesetting:
47+
48+
For completeness, the values are available as macros:
49+
:c:macro:`!PY_RELEASE_LEVEL_ALPHA` (``0xA``),
50+
:c:macro:`!PY_RELEASE_LEVEL_BETA` (``0xB``),
51+
:c:macro:`!PY_RELEASE_LEVEL_GAMMA` (``0xC``), and
52+
:c:macro:`!PY_RELEASE_LEVEL_FINAL` (``0xF``).
53+
3754
.. c:macro:: PY_RELEASE_SERIAL
3855
3956
The ``2`` in ``3.4.1a2``. Zero for final releases.
@@ -46,6 +63,10 @@ See :ref:`stable` for a discussion of API and ABI stability across versions.
4663
Use this for numeric comparisons, for example,
4764
``#if PY_VERSION_HEX >= ...``.
4865

66+
.. c:macro:: PY_VERSION
67+
68+
The Python version as a string, for example, ``"3.4.1a2"``.
69+
4970
These macros are defined in :source:`Include/patchlevel.h`.
5071

5172

Doc/c-api/module.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -820,15 +820,18 @@ struct:
820820
.. versionadded:: 3.5
821821
822822
.. c:macro:: PYTHON_API_VERSION
823+
PYTHON_API_STRING
823824
824-
The C API version. Defined for backwards compatibility.
825+
The C API version, as an integer (``1013``) and string (``"1013"``), respectively.
826+
Defined for backwards compatibility.
825827
826828
Currently, this constant is not updated in new Python versions, and is not
827829
useful for versioning. This may change in the future.
828830
829831
.. c:macro:: PYTHON_ABI_VERSION
832+
PYTHON_ABI_STRING
830833
831-
Defined as ``3`` for backwards compatibility.
834+
Defined as ``3`` and ``"3"``, respectively, for backwards compatibility.
832835
833836
Currently, this constant is not updated in new Python versions, and is not
834837
useful for versioning. This may change in the future.

Doc/library/array.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
--------------
1010

1111
This module defines an object type which can compactly represent an array of
12-
basic values: characters, integers, floating-point numbers. Arrays are sequence
12+
basic values: characters, integers, floating-point numbers. Arrays are mutable :term:`sequence`
1313
types and behave very much like lists, except that the type of objects stored in
1414
them is constrained. The type is specified at object creation time by using a
1515
:dfn:`type code`, which is a single character. The following type codes are
@@ -93,7 +93,7 @@ The module defines the following type:
9393
otherwise, the initializer's iterator is passed to the :meth:`extend` method
9494
to add initial items to the array.
9595

96-
Array objects support the ordinary sequence operations of indexing, slicing,
96+
Array objects support the ordinary :ref:`mutable <typesseq-mutable>` :term:`sequence` operations of indexing, slicing,
9797
concatenation, and multiplication. When using slice assignment, the assigned
9898
value must be an array object with the same type code; in all other cases,
9999
:exc:`TypeError` is raised. Array objects also implement the buffer interface,

Doc/library/concurrent.futures.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ or separate processes, using :class:`ProcessPoolExecutor`.
2121
Each implements the same interface, which is defined
2222
by the abstract :class:`Executor` class.
2323

24+
:class:`concurrent.futures.Future` must not be confused with
25+
:class:`asyncio.Future`, which is designed for use with :mod:`asyncio`
26+
tasks and coroutines. See the :doc:`asyncio's Future <asyncio-future>`
27+
documentation for a detailed comparison of the two.
28+
2429
.. include:: ../includes/wasm-notavail.rst
2530

2631
Executor Objects

Doc/library/itertools.rst

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Iterator Arguments Results
4747
Iterator Arguments Results Example
4848
============================ ============================ ================================================= =============================================================
4949
:func:`accumulate` p [,func] p0, p0+p1, p0+p1+p2, ... ``accumulate([1,2,3,4,5]) → 1 3 6 10 15``
50-
:func:`batched` p, n (p0, p1, ..., p_n-1), ... ``batched('ABCDEFG', n=2) → AB CD EF G``
50+
:func:`batched` p, n (p0, p1, ..., p_n-1), ... ``batched('ABCDEFG', n=3) → ABC DEF G``
5151
:func:`chain` p, q, ... p0, p1, ... plast, q0, q1, ... ``chain('ABC', 'DEF') → A B C D E F``
5252
:func:`chain.from_iterable` iterable p0, p1, ... plast, q0, q1, ... ``chain.from_iterable(['ABC', 'DEF']) → A B C D E F``
5353
:func:`compress` data, selectors (d[0] if s[0]), (d[1] if s[1]), ... ``compress('ABCDEF', [1,0,1,0,1,1]) → A C E F``
@@ -181,7 +181,7 @@ loops that truncate the stream.
181181
Roughly equivalent to::
182182

183183
def batched(iterable, n, *, strict=False):
184-
# batched('ABCDEFG', 2) → AB CD EF G
184+
# batched('ABCDEFG', 3) → ABC DEF G
185185
if n < 1:
186186
raise ValueError('n must be at least one')
187187
iterator = iter(iterable)
@@ -842,7 +842,7 @@ and :term:`generators <generator>` which incur interpreter overhead.
842842
from contextlib import suppress
843843
from functools import reduce
844844
from math import comb, prod, sumprod, isqrt
845-
from operator import itemgetter, getitem, mul, neg
845+
from operator import is_not, itemgetter, getitem, mul, neg
846846

847847
def take(n, iterable):
848848
"Return first n items of the iterable as a list."
@@ -978,6 +978,16 @@ and :term:`generators <generator>` which incur interpreter overhead.
978978
slices = starmap(slice, combinations(range(len(seq) + 1), 2))
979979
return map(getitem, repeat(seq), slices)
980980

981+
def derangements(iterable, r=None):
982+
"Produce r length permutations without fixed points."
983+
# derangements('ABCD') → BADC BCDA BDAC CADB CDAB CDBA DABC DCAB DCBA
984+
# Algorithm credited to Stefan Pochmann
985+
seq = tuple(iterable)
986+
pos = tuple(range(len(seq)))
987+
have_moved = map(map, repeat(is_not), repeat(pos), permutations(pos, r=r))
988+
valid_derangements = map(all, have_moved)
989+
return compress(permutations(seq, r=r), valid_derangements)
990+
981991
def iter_index(iterable, value, start=0, stop=None):
982992
"Return indices where a value occurs in a sequence or iterable."
983993
# iter_index('AABCADEAF', 'A') → 0 1 4 7
@@ -1663,6 +1673,36 @@ The following recipes have a more mathematical flavor:
16631673
['A', 'AB', 'ABC', 'ABCD', 'B', 'BC', 'BCD', 'C', 'CD', 'D']
16641674

16651675

1676+
>>> ' '.join(map(''.join, derangements('ABCD')))
1677+
'BADC BCDA BDAC CADB CDAB CDBA DABC DCAB DCBA'
1678+
>>> ' '.join(map(''.join, derangements('ABCD', 3)))
1679+
'BAD BCA BCD BDA CAB CAD CDA CDB DAB DCA DCB'
1680+
>>> ' '.join(map(''.join, derangements('ABCD', 2)))
1681+
'BA BC BD CA CD DA DC'
1682+
>>> ' '.join(map(''.join, derangements('ABCD', 1)))
1683+
'B C D'
1684+
>>> ' '.join(map(''.join, derangements('ABCD', 0)))
1685+
''
1686+
>>> # Compare number of derangements to https://oeis.org/A000166
1687+
>>> [len(list(derangements(range(n)))) for n in range(10)]
1688+
[1, 0, 1, 2, 9, 44, 265, 1854, 14833, 133496]
1689+
>>> # Verify that identical objects are treated as unique by position
1690+
>>> identical = 'X'
1691+
>>> distinct = 'x'
1692+
>>> seq1 = ('A', identical, 'B', identical)
1693+
>>> result1 = ' '.join(map(''.join, derangements(seq1)))
1694+
>>> result1
1695+
'XAXB XBXA XXAB BAXX BXAX BXXA XAXB XBAX XBXA'
1696+
>>> seq2 = ('A', identical, 'B', distinct)
1697+
>>> result2 = ' '.join(map(''.join, derangements(seq2)))
1698+
>>> result2
1699+
'XAxB XBxA XxAB BAxX BxAX BxXA xAXB xBAX xBXA'
1700+
>>> result1 == result2
1701+
False
1702+
>>> result1.casefold() == result2.casefold()
1703+
True
1704+
1705+
16661706
>>> list(powerset([1,2,3]))
16671707
[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
16681708
>>> all(len(list(powerset(range(n)))) == 2**n for n in range(18))

Doc/library/pathlib.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,10 @@ Reading directories
13311331
PosixPath('setup.py'),
13321332
PosixPath('test_pathlib.py')]
13331333

1334+
.. note::
1335+
The paths are returned in no particular order.
1336+
If you need a specific order, sort the results.
1337+
13341338
.. seealso::
13351339
:ref:`pathlib-pattern-language` documentation.
13361340

@@ -1365,6 +1369,10 @@ Reading directories
13651369
Glob the given relative *pattern* recursively. This is like calling
13661370
:func:`Path.glob` with "``**/``" added in front of the *pattern*.
13671371

1372+
.. note::
1373+
The paths are returned in no particular order.
1374+
If you need a specific order, sort the results.
1375+
13681376
.. seealso::
13691377
:ref:`pathlib-pattern-language` and :meth:`Path.glob` documentation.
13701378

Doc/library/stdtypes.rst

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,11 +1093,14 @@ Notes:
10931093
still ``0``.
10941094

10951095
(4)
1096-
The slice of *s* from *i* to *j* is defined as the sequence of items with index
1097-
*k* such that ``i <= k < j``. If *i* or *j* is greater than ``len(s)``, use
1098-
``len(s)``. If *i* is omitted or ``None``, use ``0``. If *j* is omitted or
1099-
``None``, use ``len(s)``. If *i* is greater than or equal to *j*, the slice is
1100-
empty.
1096+
The slice of *s* from *i* to *j* is defined as the sequence of items with
1097+
index *k* such that ``i <= k < j``.
1098+
1099+
* If *i* is omitted or ``None``, use ``0``.
1100+
* If *j* is omitted or ``None``, use ``len(s)``.
1101+
* If *i* or *j* is less than ``-len(s)``, use ``0``.
1102+
* If *i* or *j* is greater than ``len(s)``, use ``len(s)``.
1103+
* If *i* is greater than or equal to *j*, the slice is empty.
11011104

11021105
(5)
11031106
The slice of *s* from *i* to *j* with step *k* is defined as the sequence of
@@ -2369,7 +2372,9 @@ expression support in the :mod:`re` module).
23692372

23702373
If the string starts with the *prefix* string, return
23712374
``string[len(prefix):]``. Otherwise, return a copy of the original
2372-
string::
2375+
string:
2376+
2377+
.. doctest::
23732378

23742379
>>> 'TestHook'.removeprefix('Test')
23752380
'Hook'
@@ -2378,12 +2383,16 @@ expression support in the :mod:`re` module).
23782383

23792384
.. versionadded:: 3.9
23802385

2386+
See also :meth:`removesuffix` and :meth:`startswith`.
2387+
23812388

23822389
.. method:: str.removesuffix(suffix, /)
23832390

23842391
If the string ends with the *suffix* string and that *suffix* is not empty,
23852392
return ``string[:-len(suffix)]``. Otherwise, return a copy of the
2386-
original string::
2393+
original string:
2394+
2395+
.. doctest::
23872396

23882397
>>> 'MiscTests'.removesuffix('Tests')
23892398
'Misc'
@@ -2392,6 +2401,8 @@ expression support in the :mod:`re` module).
23922401

23932402
.. versionadded:: 3.9
23942403

2404+
See also :meth:`removeprefix` and :meth:`endswith`.
2405+
23952406

23962407
.. method:: str.replace(old, new, /, count=-1)
23972408

@@ -2416,6 +2427,16 @@ expression support in the :mod:`re` module).
24162427
Return the highest index in the string where substring *sub* is found, such
24172428
that *sub* is contained within ``s[start:end]``. Optional arguments *start*
24182429
and *end* are interpreted as in slice notation. Return ``-1`` on failure.
2430+
For example:
2431+
2432+
.. doctest::
2433+
2434+
>>> 'spam, spam, spam'.rfind('sp')
2435+
12
2436+
>>> 'spam, spam, spam'.rfind('sp', 0, 10)
2437+
6
2438+
2439+
See also :meth:`find` and :meth:`rindex`.
24192440

24202441

24212442
.. method:: str.rindex(sub[, start[, end]])

Doc/library/typing.rst

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2369,7 +2369,7 @@ These functions and classes should not be used directly as annotations.
23692369
Their intended purpose is to be building blocks for creating and declaring
23702370
types.
23712371

2372-
.. class:: NamedTuple
2372+
.. function:: NamedTuple
23732373

23742374
Typed version of :func:`collections.namedtuple`.
23752375

@@ -2442,6 +2442,10 @@ types.
24422442
Removed the ``_field_types`` attribute in favor of the more
24432443
standard ``__annotations__`` attribute which has the same information.
24442444

2445+
.. versionchanged:: 3.9
2446+
``NamedTuple`` is now a function rather than a class.
2447+
It can still be used as a class base, as described above.
2448+
24452449
.. versionchanged:: 3.11
24462450
Added support for generic namedtuples.
24472451

@@ -2585,10 +2589,10 @@ types.
25852589
for more details.
25862590

25872591

2588-
.. class:: TypedDict(dict)
2592+
.. function:: TypedDict
25892593

25902594
Special construct to add type hints to a dictionary.
2591-
At runtime it is a plain :class:`dict`.
2595+
At runtime ":class:`!TypedDict` instances" are simply :class:`dicts <dict>`.
25922596

25932597
``TypedDict`` declares a dictionary type that expects all of its
25942598
instances to have a certain set of keys, where each key is
@@ -2811,6 +2815,10 @@ types.
28112815

28122816
.. versionadded:: 3.8
28132817

2818+
.. versionchanged:: 3.9
2819+
``TypedDict`` is now a function rather than a class.
2820+
It can still be used as a class base, as described above.
2821+
28142822
.. versionchanged:: 3.11
28152823
Added support for marking individual keys as :data:`Required` or :data:`NotRequired`.
28162824
See :pep:`655`.

Doc/reference/datamodel.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,6 +1819,12 @@ Slice objects are used to represent slices for
18191819
:meth:`~object.__getitem__`
18201820
methods. They are also created by the built-in :func:`slice` function.
18211821

1822+
.. versionadded:: 3.15
1823+
1824+
The :func:`slice` type now supports :ref:`subscription <subscriptions>`. For
1825+
example, ``slice[float]`` may be used in type annotations to indicate a slice
1826+
containing :type:`float` objects.
1827+
18221828
.. index::
18231829
single: start (slice object attribute)
18241830
single: stop (slice object attribute)

Doc/whatsnew/3.13.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2340,7 +2340,7 @@ Limited C API Changes
23402340
* :c:func:`PySys_AuditTuple`
23412341
* :c:func:`PyType_GetModuleByDef`
23422342

2343-
(Contributed by Victor Stinner in :gh:`85283`, :gh:`85283`, and :gh:`116936`.)
2343+
(Contributed by Victor Stinner in :gh:`85283` and :gh:`116936`.)
23442344

23452345
* Python built with :option:`--with-trace-refs` (tracing references)
23462346
now supports the :ref:`Limited API <limited-c-api>`.

0 commit comments

Comments
 (0)