Skip to content

Commit 393ae89

Browse files
authored
Merge branch 'main' into fix-issue-143546
2 parents d95abed + 4766237 commit 393ae89

93 files changed

Lines changed: 2994 additions & 1507 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.

.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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ See :ref:`stable` for a discussion of API and ABI stability across versions.
6767
6868
The Python version as a string, for example, ``"3.4.1a2"``.
6969

70+
These macros are defined in :source:`Include/patchlevel.h`.
71+
7072

7173
Run-time version
7274
----------------

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: 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/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.

Doc/library/stdtypes.rst

Lines changed: 111 additions & 5 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
@@ -1438,6 +1441,109 @@ application).
14381441
list appear empty for the duration, and raises :exc:`ValueError` if it can
14391442
detect that the list has been mutated during a sort.
14401443

1444+
.. admonition:: Thread safety
1445+
1446+
Reading a single element from a :class:`list` is
1447+
:term:`atomic <atomic operation>`:
1448+
1449+
.. code-block::
1450+
:class: green
1451+
1452+
lst[i] # list.__getitem__
1453+
1454+
The following methods traverse the list and use :term:`atomic <atomic operation>`
1455+
reads of each item to perform their function. That means that they may
1456+
return results affected by concurrent modifications:
1457+
1458+
.. code-block::
1459+
:class: maybe
1460+
1461+
item in lst
1462+
lst.index(item)
1463+
lst.count(item)
1464+
1465+
All of the above methods/operations are also lock-free. They do not block
1466+
concurrent modifications. Other operations that hold a lock will not block
1467+
these from observing intermediate states.
1468+
1469+
All other operations from here on block using the per-object lock.
1470+
1471+
Writing a single item via ``lst[i] = x`` is safe to call from multiple
1472+
threads and will not corrupt the list.
1473+
1474+
The following operations return new objects and appear
1475+
:term:`atomic <atomic operation>` to other threads:
1476+
1477+
.. code-block::
1478+
:class: good
1479+
1480+
lst1 + lst2 # concatenates two lists into a new list
1481+
x * lst # repeats lst x times into a new list
1482+
lst.copy() # returns a shallow copy of the list
1483+
1484+
Methods that only operate on a single elements with no shifting required are
1485+
:term:`atomic <atomic operation>`:
1486+
1487+
.. code-block::
1488+
:class: good
1489+
1490+
lst.append(x) # append to the end of the list, no shifting required
1491+
lst.pop() # pop element from the end of the list, no shifting required
1492+
1493+
The :meth:`~list.clear` method is also :term:`atomic <atomic operation>`.
1494+
Other threads cannot observe elements being removed.
1495+
1496+
The :meth:`~list.sort` method is not :term:`atomic <atomic operation>`.
1497+
Other threads cannot observe intermediate states during sorting, but the
1498+
list appears empty for the duration of the sort.
1499+
1500+
The following operations may allow lock-free operations to observe
1501+
intermediate states since they modify multiple elements in place:
1502+
1503+
.. code-block::
1504+
:class: maybe
1505+
1506+
lst.insert(idx, item) # shifts elements
1507+
lst.pop(idx) # idx not at the end of the list, shifts elements
1508+
lst *= x # copies elements in place
1509+
1510+
The :meth:`~list.remove` method may allow concurrent modifications since
1511+
element comparison may execute arbitrary Python code (via
1512+
:meth:`~object.__eq__`).
1513+
1514+
:meth:`~list.extend` is safe to call from multiple threads. However, its
1515+
guarantees depend on the iterable passed to it. If it is a :class:`list`, a
1516+
:class:`tuple`, a :class:`set`, a :class:`frozenset`, a :class:`dict` or a
1517+
:ref:`dictionary view object <dict-views>` (but not their subclasses), the
1518+
``extend`` operation is safe from concurrent modifications to the iterable.
1519+
Otherwise, an iterator is created which can be concurrently modified by
1520+
another thread. The same applies to inplace concatenation of a list with
1521+
other iterables when using ``lst += iterable``.
1522+
1523+
Similarly, assigning to a list slice with ``lst[i:j] = iterable`` is safe
1524+
to call from multiple threads, but ``iterable`` is only locked when it is
1525+
also a :class:`list` (but not its subclasses).
1526+
1527+
Operations that involve multiple accesses, as well as iteration, are never
1528+
atomic. For example:
1529+
1530+
.. code-block::
1531+
:class: bad
1532+
1533+
# NOT atomic: read-modify-write
1534+
lst[i] = lst[i] + 1
1535+
1536+
# NOT atomic: check-then-act
1537+
if lst:
1538+
item = lst.pop()
1539+
1540+
# NOT thread-safe: iteration while modifying
1541+
for item in lst:
1542+
process(item) # another thread may modify lst
1543+
1544+
Consider external synchronization when sharing :class:`list` instances
1545+
across threads. See :ref:`freethreading-python-howto` for more information.
1546+
14411547

14421548
.. _typesseq-tuple:
14431549

0 commit comments

Comments
 (0)