Skip to content

Commit a4105c9

Browse files
authored
Merge branch 'main' into fix-issue-132551
2 parents 605c2ad + 5f50541 commit a4105c9

254 files changed

Lines changed: 7261 additions & 3163 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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Modules/Setup* @erlend-aasland
3030
Objects/set* @rhettinger
3131
Objects/dict* @methane @markshannon
3232
Objects/typevarobject.c @JelleZijlstra
33+
Objects/unionobject.c @JelleZijlstra
3334
Objects/type* @markshannon
3435
Objects/codeobject.c @markshannon
3536
Objects/frameobject.c @markshannon
@@ -167,6 +168,9 @@ Include/internal/pycore_time.h @pganssle @abalkin
167168
**/*imap* @python/email-team
168169
**/*poplib* @python/email-team
169170

171+
# Exclude .mailmap from being owned by @python/email-team
172+
/.mailmap
173+
170174
# Garbage collector
171175
/Modules/gcmodule.c @pablogsal
172176
/Doc/library/gc.rst @pablogsal

.github/zizmor.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ rules:
44
dangerous-triggers:
55
ignore:
66
- documentation-links.yml
7+
unpinned-uses:
8+
config:
9+
policies:
10+
"*": ref-pin

.mailmap

Lines changed: 1 addition & 0 deletions
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 <wchargin@gmail.com>
34
Amethyst Reese <amethyst@n7.gg> <john@noswap.com>

.pre-commit-config.yaml

Lines changed: 6 additions & 6 deletions
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.6
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]
@@ -24,7 +24,7 @@ repos:
2424
files: ^Doc/
2525

2626
- repo: https://github.com/psf/black-pre-commit-mirror
27-
rev: 24.10.0
27+
rev: 25.1.0
2828
hooks:
2929
- id: black
3030
name: Run Black on Tools/build/check_warnings.py
@@ -49,7 +49,7 @@ repos:
4949
types_or: [c, inc, python, rst]
5050

5151
- repo: https://github.com/python-jsonschema/check-jsonschema
52-
rev: 0.31.0
52+
rev: 0.33.0
5353
hooks:
5454
- id: check-dependabot
5555
- id: check-github-workflows
@@ -61,7 +61,7 @@ repos:
6161
- id: actionlint
6262

6363
- repo: https://github.com/woodruffw/zizmor-pre-commit
64-
rev: v1.1.1
64+
rev: v1.6.0
6565
hooks:
6666
- id: zizmor
6767

Doc/c-api/complex.rst

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,12 @@ pointers. This is consistent throughout the API.
4444
representation.
4545
4646
47-
.. c:function:: Py_complex _Py_cr_sum(Py_complex left, double right)
48-
49-
Return the sum of a complex number and a real number, using the C :c:type:`Py_complex`
50-
representation.
51-
52-
.. versionadded:: 3.14
53-
54-
5547
.. c:function:: Py_complex _Py_c_diff(Py_complex left, Py_complex right)
5648
5749
Return the difference between two complex numbers, using the C
5850
:c:type:`Py_complex` representation.
5951
6052
61-
.. c:function:: Py_complex _Py_cr_diff(Py_complex left, double right)
62-
63-
Return the difference between a complex number and a real number, using the C
64-
:c:type:`Py_complex` representation.
65-
66-
.. versionadded:: 3.14
67-
68-
69-
.. c:function:: Py_complex _Py_rc_diff(double left, Py_complex right)
70-
71-
Return the difference between a real number and a complex number, using the C
72-
:c:type:`Py_complex` representation.
73-
74-
.. versionadded:: 3.14
75-
76-
7753
.. c:function:: Py_complex _Py_c_neg(Py_complex num)
7854
7955
Return the negation of the complex number *num*, using the C
@@ -86,14 +62,6 @@ pointers. This is consistent throughout the API.
8662
representation.
8763
8864
89-
.. c:function:: Py_complex _Py_cr_prod(Py_complex left, double right)
90-
91-
Return the product of a complex number and a real number, using the C
92-
:c:type:`Py_complex` representation.
93-
94-
.. versionadded:: 3.14
95-
96-
9765
.. c:function:: Py_complex _Py_c_quot(Py_complex dividend, Py_complex divisor)
9866
9967
Return the quotient of two complex numbers, using the C :c:type:`Py_complex`
@@ -103,28 +71,6 @@ pointers. This is consistent throughout the API.
10371
:c:data:`errno` to :c:macro:`!EDOM`.
10472
10573
106-
.. c:function:: Py_complex _Py_cr_quot(Py_complex dividend, double divisor)
107-
108-
Return the quotient of a complex number and a real number, using the C
109-
:c:type:`Py_complex` representation.
110-
111-
If *divisor* is zero, this method returns zero and sets
112-
:c:data:`errno` to :c:macro:`!EDOM`.
113-
114-
.. versionadded:: 3.14
115-
116-
117-
.. c:function:: Py_complex _Py_rc_quot(double dividend, Py_complex divisor)
118-
119-
Return the quotient of a real number and a complex number, using the C
120-
:c:type:`Py_complex` representation.
121-
122-
If *divisor* is zero, this method returns zero and sets
123-
:c:data:`errno` to :c:macro:`!EDOM`.
124-
125-
.. versionadded:: 3.14
126-
127-
12874
.. c:function:: Py_complex _Py_c_pow(Py_complex num, Py_complex exp)
12975
13076
Return the exponentiation of *num* by *exp*, using the C :c:type:`Py_complex`

Doc/c-api/unicode.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,21 @@ APIs:
674674
.. versionadded:: 3.3
675675
676676
677+
.. c:function:: int PyUnicode_Resize(PyObject **unicode, Py_ssize_t length);
678+
679+
Resize a Unicode object *\*unicode* to the new *length* in code points.
680+
681+
Try to resize the string in place (which is usually faster than allocating
682+
a new string and copying characters), or create a new string.
683+
684+
*\*unicode* is modified to point to the new (resized) object and ``0`` is
685+
returned on success. Otherwise, ``-1`` is returned and an exception is set,
686+
and *\*unicode* is left untouched.
687+
688+
The function doesn't check string content, the result may not be a
689+
string in canonical representation.
690+
691+
677692
.. c:function:: Py_ssize_t PyUnicode_Fill(PyObject *unicode, Py_ssize_t start, \
678693
Py_ssize_t length, Py_UCS4 fill_char)
679694
@@ -1011,6 +1026,17 @@ generic ones are documented for simplicity.
10111026
Generic Codecs
10121027
""""""""""""""
10131028
1029+
The following macro is provided:
1030+
1031+
1032+
.. c:macro:: Py_UNICODE_REPLACEMENT_CHARACTER
1033+
1034+
The Unicode code point ``U+FFFD`` (replacement character).
1035+
1036+
This Unicode character is used as the replacement character during
1037+
decoding if the *errors* argument is set to "replace".
1038+
1039+
10141040
These are the generic codec APIs:
10151041
10161042

Doc/data/refcounts.dat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2794,6 +2794,10 @@ PyUnicode_CopyCharacters:PyObject*:from:0:
27942794
PyUnicode_CopyCharacters:Py_ssize_t:from_start::
27952795
PyUnicode_CopyCharacters:Py_ssize_t:how_many::
27962796

2797+
PyUnicode_Resize:int:::
2798+
PyUnicode_Resize:PyObject**:unicode:0:
2799+
PyUnicode_Resize:Py_ssize_t:length::
2800+
27972801
PyUnicode_Fill:Py_ssize_t:::
27982802
PyUnicode_Fill:PyObject*:unicode:0:
27992803
PyUnicode_Fill:Py_ssize_t:start::

Doc/data/stable_abi.dat

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Doc/howto/free-threading-extensions.rst

Lines changed: 135 additions & 0 deletions
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/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Python Library Reference.
3434
mro.rst
3535
free-threading-python.rst
3636
free-threading-extensions.rst
37+
remote_debugging.rst
3738

3839
General:
3940

@@ -66,3 +67,4 @@ Debugging and profiling:
6667
* :ref:`gdb`
6768
* :ref:`instrumentation`
6869
* :ref:`perf_profiling`
70+
* :ref:`remote-debugging`

0 commit comments

Comments
 (0)