Skip to content

Commit 3767b74

Browse files
authored
Merge branch 'main' into issue143513-removed
2 parents ef016f5 + 5a45279 commit 3767b74

File tree

113 files changed

+3521
-1777
lines changed

Some content is hidden

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

113 files changed

+3521
-1777
lines changed

.github/CODEOWNERS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ Misc/externals.spdx.json @sethmlarson
143143
Misc/sbom.spdx.json @sethmlarson
144144
Tools/build/generate_sbom.py @sethmlarson
145145

146+
# ABI check
147+
Misc/libabigail.abignore @encukou
148+
146149

147150
# ----------------------------------------------------------------------------
148151
# Platform Support

Doc/c-api/apiabiversion.rst

Lines changed: 23 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,12 @@ 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+
70+
These macros are defined in :source:`Include/patchlevel.h`.
71+
4972

5073
Run-time version
5174
----------------

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/c-api/unicode.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,27 @@ Python:
6565
.. versionadded:: 3.3
6666

6767

68+
The structure of a particular object can be determined using the following
69+
macros.
70+
The macros cannot fail; their behavior is undefined if their argument
71+
is not a Python Unicode object.
72+
73+
.. c:namespace:: NULL
74+
75+
.. c:macro:: PyUnicode_IS_COMPACT(o)
76+
77+
True if *o* uses the :c:struct:`PyCompactUnicodeObject` structure.
78+
79+
.. versionadded:: 3.3
80+
81+
82+
.. c:macro:: PyUnicode_IS_COMPACT_ASCII(o)
83+
84+
True if *o* uses the :c:struct:`PyASCIIObject` structure.
85+
86+
.. versionadded:: 3.3
87+
88+
6889
The following APIs are C macros and static inlined functions for fast checks and
6990
access to internal read-only data of Unicode objects:
7091

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/enum.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ Module Contents
153153

154154
Return a list of all power-of-two integers contained in a flag.
155155

156+
:func:`enum.bin`
157+
158+
Like built-in :func:`bin`, except negative values are represented in
159+
two's complement, and the leading bit always indicates sign
160+
(``0`` implies positive, ``1`` implies negative).
161+
156162

157163
.. versionadded:: 3.6 ``Flag``, ``IntFlag``, ``auto``
158164
.. versionadded:: 3.11 ``StrEnum``, ``EnumCheck``, ``ReprEnum``, ``FlagBoundary``, ``property``, ``member``, ``nonmember``, ``global_enum``, ``show_flag_values``
@@ -1035,6 +1041,20 @@ Utilities and Decorators
10351041

10361042
.. versionadded:: 3.11
10371043

1044+
.. function:: bin(num, max_bits=None)
1045+
1046+
Like built-in :func:`bin`, except negative values are represented in
1047+
two's complement, and the leading bit always indicates sign
1048+
(``0`` implies positive, ``1`` implies negative).
1049+
1050+
>>> import enum
1051+
>>> enum.bin(10)
1052+
'0b0 1010'
1053+
>>> enum.bin(~10) # ~10 is -11
1054+
'0b1 0101'
1055+
1056+
.. versionadded:: 3.10
1057+
10381058
---------------
10391059

10401060
Notes

Doc/library/functions.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ are always available. They are listed here in alphabetical order.
138138
>>> f'{14:#b}', f'{14:b}'
139139
('0b1110', '1110')
140140

141+
See also :func:`enum.bin` to represent negative values as twos-complement.
142+
141143
See also :func:`format` for more information.
142144

143145

Doc/library/itertools.rst

Lines changed: 65 additions & 15 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)
@@ -819,7 +819,7 @@ well as with the built-in itertools such as ``map()``, ``filter()``,
819819

820820
A secondary purpose of the recipes is to serve as an incubator. The
821821
``accumulate()``, ``compress()``, and ``pairwise()`` itertools started out as
822-
recipes. Currently, the ``sliding_window()``, ``iter_index()``, and ``sieve()``
822+
recipes. Currently, the ``sliding_window()``, ``derangements()``, and ``sieve()``
823823
recipes are being tested to see whether they prove their worth.
824824

825825
Substantially all of these recipes and many, many others can be installed from
@@ -838,11 +838,16 @@ and :term:`generators <generator>` which incur interpreter overhead.
838838

839839
.. testcode::
840840

841+
from itertools import (accumulate, batched, chain, combinations, compress,
842+
count, cycle, filterfalse, groupby, islice, permutations, product,
843+
repeat, starmap, tee, zip_longest)
841844
from collections import Counter, deque
842845
from contextlib import suppress
843846
from functools import reduce
844-
from math import comb, prod, sumprod, isqrt
845-
from operator import itemgetter, getitem, mul, neg
847+
from math import comb, isqrt, prod, sumprod
848+
from operator import getitem, is_not, itemgetter, mul, neg
849+
850+
# ==== Basic one liners ====
846851

847852
def take(n, iterable):
848853
"Return first n items of the iterable as a list."
@@ -899,15 +904,17 @@ and :term:`generators <generator>` which incur interpreter overhead.
899904

900905
def first_true(iterable, default=False, predicate=None):
901906
"Returns the first true value or the *default* if there is no true value."
902-
# first_true([a,b,c], x) → a or b or c or x
903-
# first_true([a,b], x, f) → a if f(a) else b if f(b) else x
907+
# first_true([a, b, c], x) → a or b or c or x
908+
# first_true([a, b], x, f) → a if f(a) else b if f(b) else x
904909
return next(filter(predicate, iterable), default)
905910

906911
def all_equal(iterable, key=None):
907912
"Returns True if all the elements are equal to each other."
908913
# all_equal('4٤௪౪໔', key=int) → True
909914
return len(take(2, groupby(iterable, key))) <= 1
910915

916+
# ==== Data pipelines ====
917+
911918
def unique_justseen(iterable, key=None):
912919
"Yield unique elements, preserving order. Remember only the element just seen."
913920
# unique_justseen('AAAABBBCCDAABBB') → A B C D A B
@@ -940,7 +947,7 @@ and :term:`generators <generator>` which incur interpreter overhead.
940947

941948
def sliding_window(iterable, n):
942949
"Collect data into overlapping fixed-length chunks or blocks."
943-
# sliding_window('ABCDEFG', 4) → ABCD BCDE CDEF DEFG
950+
# sliding_window('ABCDEFG', 3) → ABC BCD CDE DEF EFG
944951
iterator = iter(iterable)
945952
window = deque(islice(iterator, n - 1), maxlen=n)
946953
for x in iterator:
@@ -949,7 +956,7 @@ and :term:`generators <generator>` which incur interpreter overhead.
949956

950957
def grouper(iterable, n, *, incomplete='fill', fillvalue=None):
951958
"Collect data into non-overlapping fixed-length chunks or blocks."
952-
# grouper('ABCDEFG', 3, fillvalue='x') → ABC DEF Gxx
959+
# grouper('ABCDEFG', 3, fillvalue='x') → ABC DEF Gxx
953960
# grouper('ABCDEFG', 3, incomplete='strict') → ABC DEF ValueError
954961
# grouper('ABCDEFG', 3, incomplete='ignore') → ABC DEF
955962
iterators = [iter(iterable)] * n
@@ -978,6 +985,16 @@ and :term:`generators <generator>` which incur interpreter overhead.
978985
slices = starmap(slice, combinations(range(len(seq) + 1), 2))
979986
return map(getitem, repeat(seq), slices)
980987

988+
def derangements(iterable, r=None):
989+
"Produce r length permutations without fixed points."
990+
# derangements('ABCD') → BADC BCDA BDAC CADB CDAB CDBA DABC DCAB DCBA
991+
# Algorithm credited to Stefan Pochmann
992+
seq = tuple(iterable)
993+
pos = tuple(range(len(seq)))
994+
have_moved = map(map, repeat(is_not), repeat(pos), permutations(pos, r=r))
995+
valid_derangements = map(all, have_moved)
996+
return compress(permutations(seq, r=r), valid_derangements)
997+
981998
def iter_index(iterable, value, start=0, stop=None):
982999
"Return indices where a value occurs in a sequence or iterable."
9831000
# iter_index('AABCADEAF', 'A') → 0 1 4 7
@@ -1004,10 +1021,7 @@ and :term:`generators <generator>` which incur interpreter overhead.
10041021
while True:
10051022
yield function()
10061023

1007-
1008-
The following recipes have a more mathematical flavor:
1009-
1010-
.. testcode::
1024+
# ==== Mathematical operations ====
10111025

10121026
def multinomial(*counts):
10131027
"Number of distinct arrangements of a multiset."
@@ -1026,9 +1040,11 @@ The following recipes have a more mathematical flavor:
10261040
# sum_of_squares([10, 20, 30]) → 1400
10271041
return sumprod(*tee(iterable))
10281042
1043+
# ==== Matrix operations ====
1044+
10291045
def reshape(matrix, columns):
10301046
"Reshape a 2-D matrix to have a given number of columns."
1031-
# reshape([(0, 1), (2, 3), (4, 5)], 3) → (0, 1, 2), (3, 4, 5)
1047+
# reshape([(0, 1), (2, 3), (4, 5)], 3) → (0, 1, 2) (3, 4, 5)
10321048
return batched(chain.from_iterable(matrix), columns, strict=True)
10331049

10341050
def transpose(matrix):
@@ -1038,10 +1054,12 @@ The following recipes have a more mathematical flavor:
10381054
10391055
def matmul(m1, m2):
10401056
"Multiply two matrices."
1041-
# matmul([(7, 5), (3, 5)], [(2, 5), (7, 9)]) → (49, 80), (41, 60)
1057+
# matmul([(7, 5), (3, 5)], [(2, 5), (7, 9)]) → (49, 80) (41, 60)
10421058
n = len(m2[0])
10431059
return batched(starmap(sumprod, product(m1, transpose(m2))), n)
10441060

1061+
# ==== Polynomial arithmetic ====
1062+
10451063
def convolve(signal, kernel):
10461064
"""Discrete linear convolution of two iterables.
10471065
Equivalent to polynomial multiplication.
@@ -1096,6 +1114,8 @@ The following recipes have a more mathematical flavor:
10961114
powers = reversed(range(1, n))
10971115
return list(map(mul, coefficients, powers))
10981116

1117+
# ==== Number theory ====
1118+
10991119
def sieve(n):
11001120
"Primes less than n."
11011121
# sieve(30) → 2 3 5 7 11 13 17 19 23 29
@@ -1663,6 +1683,36 @@ The following recipes have a more mathematical flavor:
16631683
['A', 'AB', 'ABC', 'ABCD', 'B', 'BC', 'BCD', 'C', 'CD', 'D']
16641684

16651685

1686+
>>> ' '.join(map(''.join, derangements('ABCD')))
1687+
'BADC BCDA BDAC CADB CDAB CDBA DABC DCAB DCBA'
1688+
>>> ' '.join(map(''.join, derangements('ABCD', 3)))
1689+
'BAD BCA BCD BDA CAB CAD CDA CDB DAB DCA DCB'
1690+
>>> ' '.join(map(''.join, derangements('ABCD', 2)))
1691+
'BA BC BD CA CD DA DC'
1692+
>>> ' '.join(map(''.join, derangements('ABCD', 1)))
1693+
'B C D'
1694+
>>> ' '.join(map(''.join, derangements('ABCD', 0)))
1695+
''
1696+
>>> # Compare number of derangements to https://oeis.org/A000166
1697+
>>> [len(list(derangements(range(n)))) for n in range(10)]
1698+
[1, 0, 1, 2, 9, 44, 265, 1854, 14833, 133496]
1699+
>>> # Verify that identical objects are treated as unique by position
1700+
>>> identical = 'X'
1701+
>>> distinct = 'x'
1702+
>>> seq1 = ('A', identical, 'B', identical)
1703+
>>> result1 = ' '.join(map(''.join, derangements(seq1)))
1704+
>>> result1
1705+
'XAXB XBXA XXAB BAXX BXAX BXXA XAXB XBAX XBXA'
1706+
>>> seq2 = ('A', identical, 'B', distinct)
1707+
>>> result2 = ' '.join(map(''.join, derangements(seq2)))
1708+
>>> result2
1709+
'XAxB XBxA XxAB BAxX BxAX BxXA xAXB xBAX xBXA'
1710+
>>> result1 == result2
1711+
False
1712+
>>> result1.casefold() == result2.casefold()
1713+
True
1714+
1715+
16661716
>>> list(powerset([1,2,3]))
16671717
[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
16681718
>>> 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/readline.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,3 +403,9 @@ support history save/restore. ::
403403
def save_history(self, histfile):
404404
readline.set_history_length(1000)
405405
readline.write_history_file(histfile)
406+
407+
.. note::
408+
409+
The new :term:`REPL` introduced in version 3.13 doesn't support readline.
410+
However, readline can still be used by setting the :envvar:`PYTHON_BASIC_REPL`
411+
environment variable.

0 commit comments

Comments
 (0)