Skip to content

Commit e9301f3

Browse files
authored
Merge branch 'main' into issue-120501
2 parents 08c26d3 + 96ead91 commit e9301f3

File tree

336 files changed

+8690
-5057
lines changed

Some content is hidden

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

336 files changed

+8690
-5057
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Python/bytecodes.c @markshannon
4242
Python/optimizer*.c @markshannon
4343
Python/optimizer_analysis.c @Fidget-Spinner
4444
Python/optimizer_bytecodes.c @Fidget-Spinner
45-
Python/symtable.c @JelleZijlstra @carljm
45+
Python/symtable.c @JelleZijlstra @carljm
4646
Lib/_pyrepl/* @pablogsal @lysnikolaou @ambv
4747
Lib/test/test_patma.py @brandtbucher
4848
Lib/test/test_type_*.py @JelleZijlstra
@@ -201,7 +201,6 @@ Doc/c-api/stable.rst @encukou
201201
**/*itertools* @rhettinger
202202
**/*collections* @rhettinger
203203
**/*random* @rhettinger
204-
**/*queue* @rhettinger
205204
**/*bisect* @rhettinger
206205
**/*heapq* @rhettinger
207206
**/*functools* @rhettinger

Doc/c-api/init.rst

Lines changed: 157 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ The following functions can be safely called before Python is initialized:
5555
* :c:func:`PyMem_RawCalloc`
5656
* :c:func:`PyMem_RawFree`
5757

58+
* Synchronization:
59+
60+
* :c:func:`PyMutex_Lock`
61+
* :c:func:`PyMutex_Unlock`
62+
5863
.. note::
5964

6065
The following functions **should not be called** before
@@ -391,9 +396,16 @@ Initializing and finalizing the interpreter
391396
:c:func:`Py_NewInterpreter` below) that were created and not yet destroyed since
392397
the last call to :c:func:`Py_Initialize`. Ideally, this frees all memory
393398
allocated by the Python interpreter. This is a no-op when called for a second
394-
time (without calling :c:func:`Py_Initialize` again first). Normally the
395-
return value is ``0``. If there were errors during finalization
396-
(flushing buffered data), ``-1`` is returned.
399+
time (without calling :c:func:`Py_Initialize` again first).
400+
401+
Since this is the reverse of :c:func:`Py_Initialize`, it should be called
402+
in the same thread with the same interpreter active. That means
403+
the main thread and the main interpreter.
404+
This should never be called while :c:func:`Py_RunMain` is running.
405+
406+
Normally the return value is ``0``.
407+
If there were errors during finalization (flushing buffered data),
408+
``-1`` is returned.
397409
398410
This function is provided for a number of reasons. An embedding application
399411
might want to restart Python without having to restart the application itself.
@@ -2152,3 +2164,145 @@ be used in new code.
21522164
.. c:function:: void PyThread_delete_key_value(int key)
21532165
.. c:function:: void PyThread_ReInitTLS()
21542166
2167+
Synchronization Primitives
2168+
==========================
2169+
2170+
The C-API provides a basic mutual exclusion lock.
2171+
2172+
.. c:type:: PyMutex
2173+
2174+
A mutual exclusion lock. The :c:type:`!PyMutex` should be initialized to
2175+
zero to represent the unlocked state. For example::
2176+
2177+
PyMutex mutex = {0};
2178+
2179+
Instances of :c:type:`!PyMutex` should not be copied or moved. Both the
2180+
contents and address of a :c:type:`!PyMutex` are meaningful, and it must
2181+
remain at a fixed, writable location in memory.
2182+
2183+
.. note::
2184+
2185+
A :c:type:`!PyMutex` currently occupies one byte, but the size should be
2186+
considered unstable. The size may change in future Python releases
2187+
without a deprecation period.
2188+
2189+
.. versionadded:: 3.13
2190+
2191+
.. c:function:: void PyMutex_Lock(PyMutex *m)
2192+
2193+
Lock mutex *m*. If another thread has already locked it, the calling
2194+
thread will block until the mutex is unlocked. While blocked, the thread
2195+
will temporarily release the :term:`GIL` if it is held.
2196+
2197+
.. versionadded:: 3.13
2198+
2199+
.. c:function:: void PyMutex_Unlock(PyMutex *m)
2200+
2201+
Unlock mutex *m*. The mutex must be locked --- otherwise, the function will
2202+
issue a fatal error.
2203+
2204+
.. versionadded:: 3.13
2205+
2206+
.. _python-critical-section-api:
2207+
2208+
Python Critical Section API
2209+
---------------------------
2210+
2211+
The critical section API provides a deadlock avoidance layer on top of
2212+
per-object locks for :term:`free-threaded <free threading>` CPython. They are
2213+
intended to replace reliance on the :term:`global interpreter lock`, and are
2214+
no-ops in versions of Python with the global interpreter lock.
2215+
2216+
Critical sections avoid deadlocks by implicitly suspending active critical
2217+
sections and releasing the locks during calls to :c:func:`PyEval_SaveThread`.
2218+
When :c:func:`PyEval_RestoreThread` is called, the most recent critical section
2219+
is resumed, and its locks reacquired. This means the critical section API
2220+
provides weaker guarantees than traditional locks -- they are useful because
2221+
their behavior is similar to the :term:`GIL`.
2222+
2223+
The functions and structs used by the macros are exposed for cases
2224+
where C macros are not available. They should only be used as in the
2225+
given macro expansions. Note that the sizes and contents of the structures may
2226+
change in future Python versions.
2227+
2228+
.. note::
2229+
2230+
Operations that need to lock two objects at once must use
2231+
:c:macro:`Py_BEGIN_CRITICAL_SECTION2`. You *cannot* use nested critical
2232+
sections to lock more than one object at once, because the inner critical
2233+
section may suspend the outer critical sections. This API does not provide
2234+
a way to lock more than two objects at once.
2235+
2236+
Example usage::
2237+
2238+
static PyObject *
2239+
set_field(MyObject *self, PyObject *value)
2240+
{
2241+
Py_BEGIN_CRITICAL_SECTION(self);
2242+
Py_SETREF(self->field, Py_XNewRef(value));
2243+
Py_END_CRITICAL_SECTION();
2244+
Py_RETURN_NONE;
2245+
}
2246+
2247+
In the above example, :c:macro:`Py_SETREF` calls :c:macro:`Py_DECREF`, which
2248+
can call arbitrary code through an object's deallocation function. The critical
2249+
section API avoids potentital deadlocks due to reentrancy and lock ordering
2250+
by allowing the runtime to temporarily suspend the critical section if the
2251+
code triggered by the finalizer blocks and calls :c:func:`PyEval_SaveThread`.
2252+
2253+
.. c:macro:: Py_BEGIN_CRITICAL_SECTION(op)
2254+
2255+
Acquires the per-object lock for the object *op* and begins a
2256+
critical section.
2257+
2258+
In the free-threaded build, this macro expands to::
2259+
2260+
{
2261+
PyCriticalSection _py_cs;
2262+
PyCriticalSection_Begin(&_py_cs, (PyObject*)(op))
2263+
2264+
In the default build, this macro expands to ``{``.
2265+
2266+
.. versionadded:: 3.13
2267+
2268+
.. c:macro:: Py_END_CRITICAL_SECTION()
2269+
2270+
Ends the critical section and releases the per-object lock.
2271+
2272+
In the free-threaded build, this macro expands to::
2273+
2274+
PyCriticalSection_End(&_py_cs);
2275+
}
2276+
2277+
In the default build, this macro expands to ``}``.
2278+
2279+
.. versionadded:: 3.13
2280+
2281+
.. c:macro:: Py_BEGIN_CRITICAL_SECTION2(a, b)
2282+
2283+
Acquires the per-objects locks for the objects *a* and *b* and begins a
2284+
critical section. The locks are acquired in a consistent order (lowest
2285+
address first) to avoid lock ordering deadlocks.
2286+
2287+
In the free-threaded build, this macro expands to::
2288+
2289+
{
2290+
PyCriticalSection2 _py_cs2;
2291+
PyCriticalSection_Begin2(&_py_cs2, (PyObject*)(a), (PyObject*)(b))
2292+
2293+
In the default build, this macro expands to ``{``.
2294+
2295+
.. versionadded:: 3.13
2296+
2297+
.. c:macro:: Py_END_CRITICAL_SECTION2()
2298+
2299+
Ends the critical section and releases the per-object locks.
2300+
2301+
In the free-threaded build, this macro expands to::
2302+
2303+
PyCriticalSection_End2(&_py_cs2);
2304+
}
2305+
2306+
In the default build, this macro expands to ``}``.
2307+
2308+
.. versionadded:: 3.13

Doc/c-api/typeobj.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,8 +1328,8 @@ and :c:data:`PyType_Type` effectively act as defaults.)
13281328
To indicate that a class has changed call :c:func:`PyType_Modified`
13291329

13301330
.. warning::
1331-
This flag is present in header files, but is an internal feature and should
1332-
not be used. It will be removed in a future version of CPython
1331+
This flag is present in header files, but is not be used.
1332+
It will be removed in a future version of CPython
13331333

13341334

13351335
.. c:member:: const char* PyTypeObject.tp_doc

Doc/c-api/unicode.rst

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,3 +1502,116 @@ They all return ``NULL`` or ``-1`` if an exception occurs.
15021502
:c:func:`PyUnicode_InternInPlace`, returning either a new Unicode string
15031503
object that has been interned, or a new ("owned") reference to an earlier
15041504
interned string object with the same value.
1505+
1506+
PyUnicodeWriter
1507+
^^^^^^^^^^^^^^^
1508+
1509+
The :c:type:`PyUnicodeWriter` API can be used to create a Python :class:`str`
1510+
object.
1511+
1512+
.. versionadded:: 3.14
1513+
1514+
.. c:type:: PyUnicodeWriter
1515+
1516+
A Unicode writer instance.
1517+
1518+
The instance must be destroyed by :c:func:`PyUnicodeWriter_Finish` on
1519+
success, or :c:func:`PyUnicodeWriter_Discard` on error.
1520+
1521+
.. c:function:: PyUnicodeWriter* PyUnicodeWriter_Create(Py_ssize_t length)
1522+
1523+
Create a Unicode writer instance.
1524+
1525+
Set an exception and return ``NULL`` on error.
1526+
1527+
.. c:function:: PyObject* PyUnicodeWriter_Finish(PyUnicodeWriter *writer)
1528+
1529+
Return the final Python :class:`str` object and destroy the writer instance.
1530+
1531+
Set an exception and return ``NULL`` on error.
1532+
1533+
.. c:function:: void PyUnicodeWriter_Discard(PyUnicodeWriter *writer)
1534+
1535+
Discard the internal Unicode buffer and destroy the writer instance.
1536+
1537+
.. c:function:: int PyUnicodeWriter_WriteChar(PyUnicodeWriter *writer, Py_UCS4 ch)
1538+
1539+
Write the single Unicode character *ch* into *writer*.
1540+
1541+
On success, return ``0``.
1542+
On error, set an exception, leave the writer unchanged, and return ``-1``.
1543+
1544+
.. c:function:: int PyUnicodeWriter_WriteUTF8(PyUnicodeWriter *writer, const char *str, Py_ssize_t size)
1545+
1546+
Decode the string *str* from UTF-8 in strict mode and write the output into *writer*.
1547+
1548+
*size* is the string length in bytes. If *size* is equal to ``-1``, call
1549+
``strlen(str)`` to get the string length.
1550+
1551+
On success, return ``0``.
1552+
On error, set an exception, leave the writer unchanged, and return ``-1``.
1553+
1554+
See also :c:func:`PyUnicodeWriter_DecodeUTF8Stateful`.
1555+
1556+
.. c:function:: int PyUnicodeWriter_WriteWideChar(PyUnicodeWriter *writer, const wchar_t *str, Py_ssize_t size)
1557+
1558+
Writer the wide string *str* into *writer*.
1559+
1560+
*size* is a number of wide characters. If *size* is equal to ``-1``, call
1561+
``wcslen(str)`` to get the string length.
1562+
1563+
On success, return ``0``.
1564+
On error, set an exception, leave the writer unchanged, and return ``-1``.
1565+
1566+
.. c:function:: int PyUnicodeWriter_WriteStr(PyUnicodeWriter *writer, PyObject *obj)
1567+
1568+
Call :c:func:`PyObject_Str` on *obj* and write the output into *writer*.
1569+
1570+
On success, return ``0``.
1571+
On error, set an exception, leave the writer unchanged, and return ``-1``.
1572+
1573+
.. c:function:: int PyUnicodeWriter_WriteRepr(PyUnicodeWriter *writer, PyObject *obj)
1574+
1575+
Call :c:func:`PyObject_Repr` on *obj* and write the output into *writer*.
1576+
1577+
On success, return ``0``.
1578+
On error, set an exception, leave the writer unchanged, and return ``-1``.
1579+
1580+
.. c:function:: int PyUnicodeWriter_WriteSubstring(PyUnicodeWriter *writer, PyObject *str, Py_ssize_t start, Py_ssize_t end)
1581+
1582+
Write the substring ``str[start:end]`` into *writer*.
1583+
1584+
*str* must be Python :class:`str` object. *start* must be greater than or
1585+
equal to 0, and less than or equal to *end*. *end* must be less than or
1586+
equal to *str* length.
1587+
1588+
On success, return ``0``.
1589+
On error, set an exception, leave the writer unchanged, and return ``-1``.
1590+
1591+
.. c:function:: int PyUnicodeWriter_Format(PyUnicodeWriter *writer, const char *format, ...)
1592+
1593+
Similar to :c:func:`PyUnicode_FromFormat`, but write the output directly into *writer*.
1594+
1595+
On success, return ``0``.
1596+
On error, set an exception, leave the writer unchanged, and return ``-1``.
1597+
1598+
.. c:function:: int PyUnicodeWriter_DecodeUTF8Stateful(PyUnicodeWriter *writer, const char *string, Py_ssize_t length, const char *errors, Py_ssize_t *consumed)
1599+
1600+
Decode the string *str* from UTF-8 with *errors* error handler and write the
1601+
output into *writer*.
1602+
1603+
*size* is the string length in bytes. If *size* is equal to ``-1``, call
1604+
``strlen(str)`` to get the string length.
1605+
1606+
*errors* is an error handler name, such as ``"replace"``. If *errors* is
1607+
``NULL``, use the strict error handler.
1608+
1609+
If *consumed* is not ``NULL``, set *\*consumed* to the number of decoded
1610+
bytes on success.
1611+
If *consumed* is ``NULL``, treat trailing incomplete UTF-8 byte sequences
1612+
as an error.
1613+
1614+
On success, return ``0``.
1615+
On error, set an exception, leave the writer unchanged, and return ``-1``.
1616+
1617+
See also :c:func:`PyUnicodeWriter_WriteUTF8`.

Doc/c-api/weakref.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,19 @@ as much as it can.
9696
This iterates through the weak references for *object* and calls callbacks
9797
for those references which have one. It returns when all callbacks have
9898
been attempted.
99+
100+
101+
.. c:function:: void PyUnstable_Object_ClearWeakRefsNoCallbacks(PyObject *object)
102+
103+
Clears the weakrefs for *object* without calling the callbacks.
104+
105+
This function is called by the :c:member:`~PyTypeObject.tp_dealloc` handler
106+
for types with finalizers (i.e., :meth:`~object.__del__`). The handler for
107+
those objects first calls :c:func:`PyObject_ClearWeakRefs` to clear weakrefs
108+
and call their callbacks, then the finalizer, and finally this function to
109+
clear any weakrefs that may have been created by the finalizer.
110+
111+
In most circumstances, it's more appropriate to use
112+
:c:func:`PyObject_ClearWeakRefs` to clear weakrefs instead of this function.
113+
114+
.. versionadded:: 3.13

Doc/data/stable_abi.dat

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

Doc/glossary.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,9 @@ Glossary
694694

695695
CPython does not consistently apply the requirement that an iterator
696696
define :meth:`~iterator.__iter__`.
697+
And also please note that the free-threading CPython does not guarantee
698+
the thread-safety of iterator operations.
699+
697700

698701
key function
699702
A key function or collation function is a callable that returns a value

Doc/howto/enum.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _enum-howto:
2+
13
==========
24
Enum HOWTO
35
==========
@@ -1150,6 +1152,14 @@ the following are true:
11501152
>>> (Color.RED | Color.GREEN).name
11511153
'RED|GREEN'
11521154

1155+
>>> class Perm(IntFlag):
1156+
... R = 4
1157+
... W = 2
1158+
... X = 1
1159+
...
1160+
>>> (Perm.R & Perm.W).name is None # effectively Perm(0)
1161+
True
1162+
11531163
- multi-bit flags, aka aliases, can be returned from operations::
11541164

11551165
>>> Color.RED | Color.BLUE

0 commit comments

Comments
 (0)