Skip to content

Commit 270b87b

Browse files
committed
Merge branch 'main' into gh-131586-lookup-special-extra
2 parents 4c0315d + ea8ec95 commit 270b87b

File tree

183 files changed

+4211
-2104
lines changed

Some content is hidden

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

183 files changed

+4211
-2104
lines changed

.github/CODEOWNERS

+3
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ Include/internal/pycore_time.h @pganssle @abalkin
167167
**/*imap* @python/email-team
168168
**/*poplib* @python/email-team
169169

170+
# Exclude .mailmap from being owned by @python/email-team
171+
/.mailmap
172+
170173
# Garbage collector
171174
/Modules/gcmodule.c @pablogsal
172175
/Doc/library/gc.rst @pablogsal

.mailmap

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# This file sets the canonical name for contributors to the repository.
22
# Documentation: https://git-scm.com/docs/gitmailmap
3+
Willow Chargin <[email protected]>
34

.pre-commit-config.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/astral-sh/ruff-pre-commit
3-
rev: v0.9.1
3+
rev: v0.11.4
44
hooks:
55
- id: ruff
66
name: Run Ruff (lint) on Doc/
@@ -11,9 +11,9 @@ repos:
1111
args: [--exit-non-zero-on-fix]
1212
files: ^Lib/test/
1313
- id: ruff
14-
name: Run Ruff (lint) on Tools/build/check_warnings.py
14+
name: Run Ruff (lint) on Tools/build/
1515
args: [--exit-non-zero-on-fix, --config=Tools/build/.ruff.toml]
16-
files: ^Tools/build/check_warnings.py
16+
files: ^Tools/build/
1717
- id: ruff
1818
name: Run Ruff (lint) on Argument Clinic
1919
args: [--exit-non-zero-on-fix, --config=Tools/clinic/.ruff.toml]

Doc/data/stable_abi.dat

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Doc/howto/free-threading-extensions.rst

+135
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,141 @@ depend on your extension, but some common patterns include:
243243
`thread-local storage <https://en.cppreference.com/w/c/language/storage_duration>`_.
244244

245245

246+
Critical Sections
247+
=================
248+
249+
.. _critical-sections:
250+
251+
In the free-threaded build, CPython provides a mechanism called "critical
252+
sections" to protect data that would otherwise be protected by the GIL.
253+
While extension authors may not interact with the internal critical section
254+
implementation directly, understanding their behavior is crucial when using
255+
certain C API functions or managing shared state in the free-threaded build.
256+
257+
What Are Critical Sections?
258+
...........................
259+
260+
Conceptually, critical sections act as a deadlock avoidance layer built on
261+
top of simple mutexes. Each thread maintains a stack of active critical
262+
sections. When a thread needs to acquire a lock associated with a critical
263+
section (e.g., implicitly when calling a thread-safe C API function like
264+
:c:func:`PyDict_SetItem`, or explicitly using macros), it attempts to acquire
265+
the underlying mutex.
266+
267+
Using Critical Sections
268+
.......................
269+
270+
The primary APIs for using critical sections are:
271+
272+
* :c:macro:`Py_BEGIN_CRITICAL_SECTION` and :c:macro:`Py_END_CRITICAL_SECTION` -
273+
For locking a single object
274+
275+
* :c:macro:`Py_BEGIN_CRITICAL_SECTION2` and :c:macro:`Py_END_CRITICAL_SECTION2`
276+
- For locking two objects simultaneously
277+
278+
These macros must be used in matching pairs and must appear in the same C
279+
scope, since they establish a new local scope. These macros are no-ops in
280+
non-free-threaded builds, so they can be safely added to code that needs to
281+
support both build types.
282+
283+
A common use of a critical section would be to lock an object while accessing
284+
an internal attribute of it. For example, if an extension type has an internal
285+
count field, you could use a critical section while reading or writing that
286+
field::
287+
288+
// read the count, returns new reference to internal count value
289+
PyObject *result;
290+
Py_BEGIN_CRITICAL_SECTION(obj);
291+
result = Py_NewRef(obj->count);
292+
Py_END_CRITICAL_SECTION();
293+
return result;
294+
295+
// write the count, consumes reference from new_count
296+
Py_BEGIN_CRITICAL_SECTION(obj);
297+
obj->count = new_count;
298+
Py_END_CRITICAL_SECTION();
299+
300+
301+
How Critical Sections Work
302+
..........................
303+
304+
Unlike traditional locks, critical sections do not guarantee exclusive access
305+
throughout their entire duration. If a thread would block while holding a
306+
critical section (e.g., by acquiring another lock or performing I/O), the
307+
critical section is temporarily suspended—all locks are released—and then
308+
resumed when the blocking operation completes.
309+
310+
This behavior is similar to what happens with the GIL when a thread makes a
311+
blocking call. The key differences are:
312+
313+
* Critical sections operate on a per-object basis rather than globally
314+
315+
* Critical sections follow a stack discipline within each thread (the "begin" and
316+
"end" macros enforce this since they must be paired and within the same scope)
317+
318+
* Critical sections automatically release and reacquire locks around potential
319+
blocking operations
320+
321+
Deadlock Avoidance
322+
..................
323+
324+
Critical sections help avoid deadlocks in two ways:
325+
326+
1. If a thread tries to acquire a lock that's already held by another thread,
327+
it first suspends all of its active critical sections, temporarily releasing
328+
their locks
329+
330+
2. When the blocking operation completes, only the top-most critical section is
331+
reacquired first
332+
333+
This means you cannot rely on nested critical sections to lock multiple objects
334+
at once, as the inner critical section may suspend the outer ones. Instead, use
335+
:c:macro:`Py_BEGIN_CRITICAL_SECTION2` to lock two objects simultaneously.
336+
337+
Note that the locks described above are only :c:type:`!PyMutex` based locks.
338+
The critical section implementation does not know about or affect other locking
339+
mechanisms that might be in use, like POSIX mutexes. Also note that while
340+
blocking on any :c:type:`!PyMutex` causes the critical sections to be
341+
suspended, only the mutexes that are part of the critical sections are
342+
released. If :c:type:`!PyMutex` is used without a critical section, it will
343+
not be released and therefore does not get the same deadlock avoidance.
344+
345+
Important Considerations
346+
........................
347+
348+
* Critical sections may temporarily release their locks, allowing other threads
349+
to modify the protected data. Be careful about making assumptions about the
350+
state of the data after operations that might block.
351+
352+
* Because locks can be temporarily released (suspended), entering a critical
353+
section does not guarantee exclusive access to the protected resource
354+
throughout the section's duration. If code within a critical section calls
355+
another function that blocks (e.g., acquires another lock, performs blocking
356+
I/O), all locks held by the thread via critical sections will be released.
357+
This is similar to how the GIL can be released during blocking calls.
358+
359+
* Only the lock(s) associated with the most recently entered (top-most)
360+
critical section are guaranteed to be held at any given time. Locks for
361+
outer, nested critical sections might have been suspended.
362+
363+
* You can lock at most two objects simultaneously with these APIs. If you need
364+
to lock more objects, you'll need to restructure your code.
365+
366+
* While critical sections will not deadlock if you attempt to lock the same
367+
object twice, they are less efficient than purpose-built reentrant locks for
368+
this use case.
369+
370+
* When using :c:macro:`Py_BEGIN_CRITICAL_SECTION2`, the order of the objects
371+
doesn't affect correctness (the implementation handles deadlock avoidance),
372+
but it's good practice to always lock objects in a consistent order.
373+
374+
* Remember that the critical section macros are primarily for protecting access
375+
to *Python objects* that might be involved in internal CPython operations
376+
susceptible to the deadlock scenarios described above. For protecting purely
377+
internal extension state, standard mutexes or other synchronization
378+
primitives might be more appropriate.
379+
380+
246381
Building Extensions for the Free-Threaded Build
247382
===============================================
248383

Doc/howto/perf_profiling.rst

+23-8
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,28 @@ files in the current directory which are ELF images for all the JIT trampolines
254254
that were created by Python.
255255

256256
.. warning::
257-
Notice that when using ``--call-graph dwarf`` the ``perf`` tool will take
257+
When using ``--call-graph dwarf``, the ``perf`` tool will take
258258
snapshots of the stack of the process being profiled and save the
259-
information in the ``perf.data`` file. By default the size of the stack dump
260-
is 8192 bytes but the user can change the size by passing the size after
261-
comma like ``--call-graph dwarf,4096``. The size of the stack dump is
262-
important because if the size is too small ``perf`` will not be able to
263-
unwind the stack and the output will be incomplete. On the other hand, if
264-
the size is too big, then ``perf`` won't be able to sample the process as
265-
frequently as it would like as the overhead will be higher.
259+
information in the ``perf.data`` file. By default, the size of the stack dump
260+
is 8192 bytes, but you can change the size by passing it after
261+
a comma like ``--call-graph dwarf,16384``.
266262

263+
The size of the stack dump is important because if the size is too small
264+
``perf`` will not be able to unwind the stack and the output will be
265+
incomplete. On the other hand, if the size is too big, then ``perf`` won't
266+
be able to sample the process as frequently as it would like as the overhead
267+
will be higher.
268+
269+
The stack size is particularly important when profiling Python code compiled
270+
with low optimization levels (like ``-O0``), as these builds tend to have
271+
larger stack frames. If you are compiling Python with ``-O0`` and not seeing
272+
Python functions in your profiling output, try increasing the stack dump
273+
size to 65528 bytes (the maximum)::
274+
275+
$ perf record -F 9999 -g -k 1 --call-graph dwarf,65528 -o perf.data python -Xperf_jit my_script.py
276+
277+
Different compilation flags can significantly impact stack sizes:
278+
279+
- Builds with ``-O0`` typically have much larger stack frames than those with ``-O1`` or higher
280+
- Adding optimizations (``-O1``, ``-O2``, etc.) typically reduces stack size
281+
- Frame pointers (``-fno-omit-frame-pointer``) generally provide more reliable stack unwinding

Doc/library/ast.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,7 @@ Control flow
11901190

11911191
.. doctest::
11921192

1193-
>> print(ast.dump(ast.parse("""
1193+
>>> print(ast.dump(ast.parse("""
11941194
... while x:
11951195
... ...
11961196
... else:

Doc/library/dis.rst

+3-9
Original file line numberDiff line numberDiff line change
@@ -1354,9 +1354,6 @@ iterations of the loop.
13541354
If ``STACK[-1]`` is not ``None``, increments the bytecode counter by *delta*.
13551355
``STACK[-1]`` is popped.
13561356

1357-
This opcode is a pseudo-instruction, replaced in final bytecode by
1358-
the directed versions (forward/backward).
1359-
13601357
.. versionadded:: 3.11
13611358

13621359
.. versionchanged:: 3.12
@@ -1368,9 +1365,6 @@ iterations of the loop.
13681365
If ``STACK[-1]`` is ``None``, increments the bytecode counter by *delta*.
13691366
``STACK[-1]`` is popped.
13701367

1371-
This opcode is a pseudo-instruction, replaced in final bytecode by
1372-
the directed versions (forward/backward).
1373-
13741368
.. versionadded:: 3.11
13751369

13761370
.. versionchanged:: 3.12
@@ -1673,7 +1667,7 @@ iterations of the loop.
16731667
* ``oparg == 2``: call :func:`repr` on *value*
16741668
* ``oparg == 3``: call :func:`ascii` on *value*
16751669

1676-
Used for implementing formatted literal strings (f-strings).
1670+
Used for implementing formatted string literals (f-strings).
16771671

16781672
.. versionadded:: 3.13
16791673

@@ -1686,7 +1680,7 @@ iterations of the loop.
16861680
result = value.__format__("")
16871681
STACK.append(result)
16881682

1689-
Used for implementing formatted literal strings (f-strings).
1683+
Used for implementing formatted string literals (f-strings).
16901684

16911685
.. versionadded:: 3.13
16921686

@@ -1699,7 +1693,7 @@ iterations of the loop.
16991693
result = value.__format__(spec)
17001694
STACK.append(result)
17011695

1702-
Used for implementing formatted literal strings (f-strings).
1696+
Used for implementing formatted string literals (f-strings).
17031697

17041698
.. versionadded:: 3.13
17051699

Doc/library/hashlib.rst

+6-7
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@
2020

2121
--------------
2222

23-
This module implements a common interface to many different secure hash and
24-
message digest algorithms. Included are the FIPS secure hash algorithms SHA1,
25-
SHA224, SHA256, SHA384, SHA512, (defined in `the FIPS 180-4 standard`_),
26-
the SHA-3 series (defined in `the FIPS 202 standard`_) as well as RSA's MD5
27-
algorithm (defined in internet :rfc:`1321`). The terms "secure hash" and
28-
"message digest" are interchangeable. Older algorithms were called message
29-
digests. The modern term is secure hash.
23+
This module implements a common interface to many different hash algorithms.
24+
Included are the FIPS secure hash algorithms SHA224, SHA256, SHA384, SHA512,
25+
(defined in `the FIPS 180-4 standard`_), the SHA-3 series (defined in `the FIPS
26+
202 standard`_) as well as the legacy algorithms SHA1 (`formerly part of FIPS`_)
27+
and the MD5 algorithm (defined in internet :rfc:`1321`).
3028

3129
.. note::
3230

@@ -812,6 +810,7 @@ Domain Dedication 1.0 Universal:
812810
.. _the FIPS 180-4 standard: https://csrc.nist.gov/pubs/fips/180-4/upd1/final
813811
.. _the FIPS 202 standard: https://csrc.nist.gov/pubs/fips/202/final
814812
.. _HACL\* project: https://github.com/hacl-star/hacl-star
813+
.. _formerly part of FIPS: https://csrc.nist.gov/news/2023/decision-to-revise-fips-180-4
815814

816815

817816
.. _hashlib-seealso:

Doc/library/itertools.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,12 @@ The following recipes have a more mathematical flavor:
10091009

10101010
.. testcode::
10111011

1012+
def multinomial(*counts):
1013+
"Number of distinct arrangements of a multiset."
1014+
# Counter('abracadabra').values() → 5 2 2 1 1
1015+
# multinomial(5, 2, 2, 1, 1) → 83160
1016+
return prod(map(comb, accumulate(counts), counts))
1017+
10121018
def powerset(iterable):
10131019
"Subsequences of the iterable from shortest to longest."
10141020
# powerset([1,2,3]) → () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)
@@ -1127,12 +1133,6 @@ The following recipes have a more mathematical flavor:
11271133
n -= n // prime
11281134
return n
11291135

1130-
def multinomial(*counts):
1131-
"Number of distinct arrangements of a multiset."
1132-
# Counter('abracadabra').values() → 5 2 2 1 1
1133-
# multinomial(5, 2, 2, 1, 1) → 83160
1134-
return prod(map(comb, accumulate(counts), counts))
1135-
11361136

11371137
.. doctest::
11381138
:hide:

Doc/library/shutil.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ Directory and files operations
473473
This is also applied when *cmd* is a path that contains a directory
474474
component::
475475

476-
>> shutil.which("C:\\Python33\\python")
476+
>>> shutil.which("C:\\Python33\\python")
477477
'C:\\Python33\\python.EXE'
478478

479479
.. versionadded:: 3.3

Doc/library/socket.rst

+3
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,9 @@ Constants
498498
.. versionchanged:: 3.11
499499
NetBSD support was added.
500500

501+
.. versionchanged:: next
502+
Restored missing ``CAN_RAW_ERR_FILTER`` on Linux.
503+
501504
.. data:: CAN_BCM
502505
CAN_BCM_*
503506

0 commit comments

Comments
 (0)