Skip to content

Commit a58fa54

Browse files
authored
Merge branch 'main' into video-mimes
2 parents 832af81 + 12a20da commit a58fa54

74 files changed

Lines changed: 2812 additions & 1612 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.

Doc/c-api/synchronization.rst

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,6 @@ there is no :c:type:`PyObject` -- for example, when working with a C type that
8484
does not extend or wrap :c:type:`PyObject` but still needs to call into the C
8585
API in a manner that might lead to deadlocks.
8686
87-
The functions and structs used by the macros are exposed for cases
88-
where C macros are not available. They should only be used as in the
89-
given macro expansions. Note that the sizes and contents of the structures may
90-
change in future Python versions.
91-
9287
.. note::
9388
9489
Operations that need to lock two objects at once must use
@@ -114,12 +109,15 @@ section API avoids potential deadlocks due to reentrancy and lock ordering
114109
by allowing the runtime to temporarily suspend the critical section if the
115110
code triggered by the finalizer blocks and calls :c:func:`PyEval_SaveThread`.
116111
112+
.. _critical-section-macros:
113+
117114
.. c:macro:: Py_BEGIN_CRITICAL_SECTION(op)
118115
119116
Acquires the per-object lock for the object *op* and begins a
120117
critical section.
121118
122-
In the free-threaded build, this macro expands to::
119+
In the free-threaded build, and when building for the
120+
:ref:`Stable ABI <stable-abi>`, this macro expands to::
123121
124122
{
125123
PyCriticalSection _py_cs;
@@ -150,7 +148,8 @@ code triggered by the finalizer blocks and calls :c:func:`PyEval_SaveThread`.
150148
151149
Ends the critical section and releases the per-object lock.
152150
153-
In the free-threaded build, this macro expands to::
151+
In the free-threaded build, and when building for the
152+
:ref:`Stable ABI <stable-abi>`, this macro expands to::
154153
155154
PyCriticalSection_End(&_py_cs);
156155
}
@@ -179,7 +178,8 @@ code triggered by the finalizer blocks and calls :c:func:`PyEval_SaveThread`.
179178
180179
Locks the mutexes *m1* and *m2* and begins a critical section.
181180
182-
In the free-threaded build, this macro expands to::
181+
In the free-threaded build, and when building for the
182+
:ref:`Stable ABI <stable-abi>`, this macro expands to::
183183
184184
{
185185
PyCriticalSection2 _py_cs2;
@@ -196,7 +196,8 @@ code triggered by the finalizer blocks and calls :c:func:`PyEval_SaveThread`.
196196
197197
Ends the critical section and releases the per-object locks.
198198
199-
In the free-threaded build, this macro expands to::
199+
In the free-threaded build, and when building for the
200+
:ref:`Stable ABI <stable-abi>`, this macro expands to::
200201
201202
PyCriticalSection2_End(&_py_cs2);
202203
}
@@ -205,6 +206,48 @@ code triggered by the finalizer blocks and calls :c:func:`PyEval_SaveThread`.
205206
206207
.. versionadded:: 3.13
207208
209+
Low-level critical section API
210+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
211+
212+
The following functions and structs are exposed for cases where C macros
213+
are not available.
214+
215+
.. c:function:: void PyCriticalSection_Begin(PyCriticalSection *c, PyObject *op)
216+
void PyCriticalSection_End(PyCriticalSection *c)
217+
void PyCriticalSection2_Begin(PyCriticalSection2 *c, PyObject *a, PyObject *b)
218+
void PyCriticalSection2_End(PyCriticalSection2 *c);
219+
220+
To be used only as in the macro expansions
221+
listed :ref:`earlier in this section <critical-section-macros>`.
222+
223+
In non-:term:`free-threaded <free threading>` builds of CPython, these
224+
functions do nothing.
225+
226+
.. versionadded:: 3.13
227+
228+
.. c:type:: PyCriticalSection
229+
PyCriticalSection2
230+
231+
To be used only as in the macro expansions
232+
listed :ref:`earlier in this section <critical-section-macros>`.
233+
Note that the contents of the structures are private and their meaning may
234+
change in future Python versions.
235+
236+
.. versionadded:: 3.13
237+
238+
.. c:function:: void PyCriticalSection_BeginMutex(PyCriticalSection *c, PyMutex *m);
239+
void PyCriticalSection2_BeginMutex(PyCriticalSection2 *c, PyMutex *m1, PyMutex *m2);
240+
241+
.. (These need to be in a separate section without a Stable ABI anotation.)
242+
243+
To be used only as in the macro expansions
244+
listed :ref:`earlier in this section <critical-section-macros>`.
245+
246+
In non-:term:`free-threaded <free threading>` builds of CPython, these
247+
functions do nothing.
248+
249+
.. versionadded:: 3.14
250+
208251
209252
Legacy locking APIs
210253
-------------------

Doc/data/stable_abi.dat

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

Doc/library/array.rst

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ defined:
4848
+-----------+--------------------+-------------------+-----------------------+-------+
4949
| ``'d'`` | double | float | 8 | |
5050
+-----------+--------------------+-------------------+-----------------------+-------+
51-
| ``'F'`` | float complex | complex | 8 | \(4) |
52-
+-----------+--------------------+-------------------+-----------------------+-------+
53-
| ``'D'`` | double complex | complex | 16 | \(4) |
54-
+-----------+--------------------+-------------------+-----------------------+-------+
5551
| ``'Zf'`` | float complex | complex | 8 | \(4) |
5652
+-----------+--------------------+-------------------+-----------------------+-------+
5753
| ``'Zd'`` | double complex | complex | 16 | \(4) |
@@ -84,7 +80,7 @@ Notes:
8480
.. versionadded:: 3.15
8581

8682
(4)
87-
Complex types (``F``, ``D``, ``Zf`` and ``Zd``) are available unconditionally,
83+
Complex types (``Zf`` and ``Zd``) are available unconditionally,
8884
regardless on support for complex types (the Annex G of the C11 standard)
8985
by the C compiler.
9086
As specified in the C11 standard, each complex type is represented by a

Doc/library/urllib.robotparser.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
This module provides a single class, :class:`RobotFileParser`, which answers
1919
questions about whether or not a particular user agent can fetch a URL on the
2020
website that published the :file:`robots.txt` file. For more details on the
21-
structure of :file:`robots.txt` files, see http://www.robotstxt.org/orig.html.
21+
structure of :file:`robots.txt` files, see :rfc:`9309`.
2222

2323

2424
.. class:: RobotFileParser(url='')

Doc/whatsnew/3.15.rst

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,8 @@ Other language changes
671671
(Contributed by James Hilton-Balfe in :gh:`128335`.)
672672

673673
* The class :class:`memoryview` now supports the :c:expr:`float complex` and
674-
:c:expr:`double complex` C types: formatting characters ``'F'``/``'Zf'``
675-
and ``'D'``/``'Zd'`` respectively.
674+
:c:expr:`double complex` C types: formatting characters ``'Zf'`` and ``'Zd'``
675+
respectively.
676676
(Contributed by Victor Stinner in :gh:`146151` and :gh:`148675`.)
677677

678678
* Allow the *count* argument of :meth:`bytes.replace` to be a keyword.
@@ -724,7 +724,7 @@ array
724724
-----
725725

726726
* Support the :c:expr:`float complex` and :c:expr:`double complex` C types:
727-
formatting characters ``'F'``/``'Zf'`` and ``'D'``/``'Zd'`` respectively.
727+
formatting characters ``'Zf'`` and ``'Zd'`` respectively.
728728
(Contributed by Victor Stinner in :gh:`146151` and :gh:`148675`.)
729729

730730
* Support half-floats (16-bit IEEE 754 binary interchange format): formatting
@@ -1045,7 +1045,7 @@ mimetypes
10451045
(Contributed by Benedikt Johannes, Charlie Lin, Foolbar, Gil Forcada and
10461046
John Franey
10471047
in :gh:`144217`, :gh:`145720`, :gh:`140937`, :gh:`139959`, :gh:`145698`,
1048-
:gh:`145718`, :gh:`146342`, and :gh:`144213`.)
1048+
:gh:`145718`, :gh:`146342`, :gh:`145918`, and :gh:`144213`.)
10491049
* Rename ``application/x-texinfo`` to ``application/texinfo``.
10501050
(Contributed by Charlie Lin in :gh:`140165`.)
10511051
* Changed the MIME type for ``.ai`` files to ``application/pdf``.
@@ -2098,6 +2098,11 @@ New features
20982098

20992099
(Contributed by Victor Stinner in :gh:`129813`.)
21002100

2101+
* :c:type:`PyCriticalSection` and related functions are added to the Stable
2102+
ABI.
2103+
2104+
(Contributed in :gh:`149227`.)
2105+
21012106
* Add a new :c:func:`PyImport_CreateModuleFromInitfunc` C-API for creating
21022107
a module from a *spec* and *initfunc*.
21032108
(Contributed by Itamar Oren in :gh:`116146`.)

Include/cpython/critical_section.h

Lines changed: 7 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@
22
# error "this header file must not be included directly"
33
#endif
44

5-
// Python critical sections
6-
//
7-
// Conceptually, critical sections are a deadlock avoidance layer on top of
8-
// per-object locks. These helpers, in combination with those locks, replace
9-
// our usage of the global interpreter lock to provide thread-safety for
10-
// otherwise thread-unsafe objects, such as dict.
11-
//
12-
// NOTE: These APIs are no-ops in non-free-threaded builds.
13-
//
145
// Straightforward per-object locking could introduce deadlocks that were not
156
// present when running with the GIL. Threads may hold locks for multiple
167
// objects simultaneously because Python operations can nest. If threads were
@@ -43,52 +34,19 @@
4334
// `_PyThreadState_Attach()`, it resumes the top-most (i.e., most recent)
4435
// critical section by reacquiring the associated lock or locks. See
4536
// `_PyCriticalSection_Resume()`.
46-
//
47-
// NOTE: Only the top-most critical section is guaranteed to be active.
48-
// Operations that need to lock two objects at once must use
49-
// `Py_BEGIN_CRITICAL_SECTION2()`. You *CANNOT* use nested critical sections
50-
// to lock more than one object at once, because the inner critical section
51-
// may suspend the outer critical sections. This API does not provide a way
52-
// to lock more than two objects at once (though it could be added later
53-
// if actually needed).
54-
//
55-
// NOTE: Critical sections implicitly behave like reentrant locks because
56-
// attempting to acquire the same lock will suspend any outer (earlier)
57-
// critical sections. However, they are less efficient for this use case than
58-
// purposefully designed reentrant locks.
59-
//
60-
// Example usage:
61-
// Py_BEGIN_CRITICAL_SECTION(op);
62-
// ...
63-
// Py_END_CRITICAL_SECTION();
64-
//
65-
// To lock two objects at once:
66-
// Py_BEGIN_CRITICAL_SECTION2(op1, op2);
67-
// ...
68-
// Py_END_CRITICAL_SECTION2();
69-
70-
typedef struct PyCriticalSection PyCriticalSection;
71-
typedef struct PyCriticalSection2 PyCriticalSection2;
72-
73-
PyAPI_FUNC(void)
74-
PyCriticalSection_Begin(PyCriticalSection *c, PyObject *op);
7537

7638
PyAPI_FUNC(void)
7739
PyCriticalSection_BeginMutex(PyCriticalSection *c, PyMutex *m);
7840

79-
PyAPI_FUNC(void)
80-
PyCriticalSection_End(PyCriticalSection *c);
81-
82-
PyAPI_FUNC(void)
83-
PyCriticalSection2_Begin(PyCriticalSection2 *c, PyObject *a, PyObject *b);
84-
8541
PyAPI_FUNC(void)
8642
PyCriticalSection2_BeginMutex(PyCriticalSection2 *c, PyMutex *m1, PyMutex *m2);
8743

88-
PyAPI_FUNC(void)
89-
PyCriticalSection2_End(PyCriticalSection2 *c);
90-
9144
#ifndef Py_GIL_DISABLED
45+
#undef Py_BEGIN_CRITICAL_SECTION
46+
#undef Py_END_CRITICAL_SECTION
47+
#undef Py_BEGIN_CRITICAL_SECTION2
48+
#undef Py_END_CRITICAL_SECTION2
49+
9250
# define Py_BEGIN_CRITICAL_SECTION(op) \
9351
{
9452
# define Py_BEGIN_CRITICAL_SECTION_MUTEX(mutex) \
@@ -101,54 +59,17 @@ PyCriticalSection2_End(PyCriticalSection2 *c);
10159
{
10260
# define Py_END_CRITICAL_SECTION2() \
10361
}
104-
#else /* !Py_GIL_DISABLED */
105-
106-
// NOTE: the contents of this struct are private and may change betweeen
107-
// Python releases without a deprecation period.
108-
struct PyCriticalSection {
109-
// Tagged pointer to an outer active critical section (or 0).
110-
uintptr_t _cs_prev;
111-
112-
// Mutex used to protect critical section
113-
PyMutex *_cs_mutex;
114-
};
115-
116-
// A critical section protected by two mutexes. Use
117-
// Py_BEGIN_CRITICAL_SECTION2 and Py_END_CRITICAL_SECTION2.
118-
// NOTE: the contents of this struct are private and may change betweeen
119-
// Python releases without a deprecation period.
120-
struct PyCriticalSection2 {
121-
PyCriticalSection _cs_base;
12262

123-
PyMutex *_cs_mutex2;
124-
};
125-
126-
# define Py_BEGIN_CRITICAL_SECTION(op) \
127-
{ \
128-
PyCriticalSection _py_cs; \
129-
PyCriticalSection_Begin(&_py_cs, _PyObject_CAST(op))
63+
#else /* !Py_GIL_DISABLED */
13064

13165
# define Py_BEGIN_CRITICAL_SECTION_MUTEX(mutex) \
13266
{ \
13367
PyCriticalSection _py_cs; \
13468
PyCriticalSection_BeginMutex(&_py_cs, mutex)
13569

136-
# define Py_END_CRITICAL_SECTION() \
137-
PyCriticalSection_End(&_py_cs); \
138-
}
139-
140-
# define Py_BEGIN_CRITICAL_SECTION2(a, b) \
141-
{ \
142-
PyCriticalSection2 _py_cs2; \
143-
PyCriticalSection2_Begin(&_py_cs2, _PyObject_CAST(a), _PyObject_CAST(b))
144-
14570
# define Py_BEGIN_CRITICAL_SECTION2_MUTEX(m1, m2) \
14671
{ \
14772
PyCriticalSection2 _py_cs2; \
14873
PyCriticalSection2_BeginMutex(&_py_cs2, m1, m2)
14974

150-
# define Py_END_CRITICAL_SECTION2() \
151-
PyCriticalSection2_End(&_py_cs2); \
152-
}
153-
154-
#endif
75+
#endif /* !Py_GIL_DISABLED */

0 commit comments

Comments
 (0)