Skip to content

Commit 3a9a342

Browse files
authored
Merge branch 'main' into fix-127727
2 parents ef51005 + 62a45fa commit 3a9a342

80 files changed

Lines changed: 1515 additions & 495 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/threads.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,10 +736,10 @@ Low-level APIs
736736
.. c:function:: PyObject* PyThreadState_GetDict()
737737
738738
Return a dictionary in which extensions can store thread-specific state
739-
information. Each extension should use a unique key to use to store state in
739+
information. Each extension should use a unique key to store a state in
740740
the dictionary. It is okay to call this function when no :term:`thread state`
741741
is :term:`attached <attached thread state>`. If this function returns
742-
``NULL``, no exception has been raised and the caller should assume no
742+
``NULL`` and no exception has been raised, then the caller should assume no
743743
thread state is attached.
744744
745745

Doc/howto/free-threading-python.rst

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,132 @@ to false. If the flag is true then the :class:`warnings.catch_warnings`
165165
context manager uses a context variable for warning filters. If the flag is
166166
false then :class:`~warnings.catch_warnings` modifies the global filters list,
167167
which is not thread-safe. See the :mod:`warnings` module for more details.
168+
169+
170+
Increased memory usage
171+
----------------------
172+
173+
The free-threaded build will typically use more memory compared to the default
174+
build. There are multiple reasons for this, mostly due to design decisions.
175+
176+
177+
All interned strings are immortal
178+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
179+
180+
For modern Python versions (since version 2.3), interning a string (e.g. with
181+
:func:`sys.intern`) does not cause it to become immortal. Instead, if the last
182+
reference to that string disappears, it will be removed from the interned
183+
string table. This is not the case for the free-threaded build and any interned
184+
string will become immortal, surviving until interpreter shutdown.
185+
186+
187+
Non-GC objects have a larger object header
188+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
189+
190+
The free-threaded build uses a different :c:type:`PyObject` structure. Instead
191+
of having the GC related information allocated before the :c:type:`PyObject`
192+
structure, like in the default build, the GC related info is part of the normal
193+
object header. For example, on the AMD64 platform, ``None`` uses 32 bytes on
194+
the free-threaded build vs 16 bytes for the default build. GC objects (such as
195+
dicts and lists) are the same size for both builds since the free-threaded
196+
build does not use additional space for the GC info.
197+
198+
199+
QSBR can delay freeing of memory
200+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
201+
202+
In order to safely implement lock-free data structures, a safe memory
203+
reclamation (SMR) scheme is used, known as quiescent state-based reclamation
204+
(QSBR). This means that the memory backing data structures allowing lock-free
205+
access will use QSBR, which defers the free operation, rather than immediately
206+
freeing the memory. Two examples of these data structures are the list object
207+
and the dictionary keys object. See ``InternalDocs/qsbr.md`` in the CPython
208+
source tree for more details on how QSBR is implemented. Running
209+
:func:`gc.collect` should cause all memory being held by QSBR to be actually
210+
freed. Note that even when QSBR frees the memory, the underlying memory
211+
allocator may not immediately return that memory to the OS and so the resident
212+
set size (RSS) of the process might not decrease.
213+
214+
215+
mimalloc allocator vs pymalloc
216+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
217+
218+
The default build will normally use the "pymalloc" memory allocator for small
219+
allocations (512 bytes or smaller). The free-threaded build does not use
220+
pymalloc and allocates all Python objects using the "mimalloc" allocator. The
221+
pymalloc allocator has the following properties that help keep memory usage
222+
low: small per-allocated-block overhead, effective memory fragmentation
223+
prevention, and quick return of free memory to the operating system. The
224+
mimalloc allocator does quite well in these respects as well but can have some
225+
more overhead.
226+
227+
In the free-threaded build, mimalloc manages memory in a number of separate
228+
heaps (currently four). For example, all GC supporting objects are allocated
229+
from their own heap. Using separate heaps means that free memory in one heap
230+
cannot be used for an allocation that uses another heap. Also, some heaps are
231+
configured to use QSBR (quiescent-state based reclamation) when freeing the
232+
memory that backs up the heap (known as "pages" in mimalloc terminology). The
233+
use of QSBR creates a delay between all memory blocks for a page being freed
234+
and the memory page being released, either for new allocations or back to the
235+
OS.
236+
237+
The mimalloc allocator also defers returning freed memory back to the OS. You
238+
can reduce that delay by setting the environment variable
239+
:envvar:`!MIMALLOC_PURGE_DELAY` to ``0``. Note that this will likely reduce
240+
the performance of the allocator.
241+
242+
243+
Free-threaded reference counting can cause objects to live longer
244+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
245+
246+
In the default build, when an object's reference count reaches zero, it is
247+
normally deallocated. The free-threaded build uses "biased reference
248+
counting", with a fast-path for objects "owned" by the current thread and a
249+
slow path for other objects. See :pep:`703` for additional details. Any time
250+
an object's reference count ends up in a "queued" state, deallocation can be
251+
deferred. The queued state is cleared from the "eval breaker" section of the
252+
bytecode evaluator.
253+
254+
The free-threaded build also allows a different mode of reference counting,
255+
known as "deferred reference counting". This mode is enabled by setting a flag
256+
on a per-object basis. Deferred reference counting is enabled for the
257+
following types:
258+
259+
* module objects
260+
* module top-level functions
261+
* class methods defined in the class scope
262+
* descriptor objects
263+
* thread-local objects, created by :class:`threading.local`
264+
265+
When deferred reference counting is enabled, references from Python function
266+
stacks are not added to the reference count. This scheme reduces the overhead
267+
of reference counting, especially for objects used from multiple threads.
268+
Because the stack references are not counted, objects with deferred reference
269+
counting are not immediately freed when their internal reference count goes to
270+
zero. Instead, they are examined by the next GC run and, if no stack
271+
references to them are found, they are freed. This means these objects are
272+
freed by the GC and not when their reference count goes to zero, as is typical.
273+
274+
275+
Per-thread reference counting can delay freeing objects
276+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
277+
278+
To avoid contention on the reference count fields of frequently shared
279+
objects, the free-threaded build also uses "per-thread reference counting"
280+
for a few selected object types. Rather than updating a single shared
281+
reference count, each thread maintains its own local reference count array,
282+
indexed by a unique id assigned to the object. The true reference count is
283+
only computed by summing the per-thread counts when the object's local
284+
count drops to zero. Per-thread reference counting is currently used for:
285+
286+
* heap type objects (classes created in Python)
287+
* code objects
288+
* the ``__dict__`` of module objects
289+
290+
Because the per-thread counts must be merged back to the object before it
291+
can be deallocated, objects using per-thread reference counting are
292+
typically freed later than they would be in the default build. In
293+
particular, such an object is usually not freed until the thread that
294+
referenced it reaches a safe point (for example, in the "eval breaker"
295+
section of the bytecode evaluator) or exits. Running :func:`gc.collect`
296+
will merge the per-thread counts and allow these objects to be freed.

Doc/library/ctypes.rst

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ used to wrap these libraries in pure Python.
1414

1515
.. include:: ../includes/optional-module.rst
1616

17+
.. warning::
18+
19+
:mod:`!ctypes` provides low-level access to native libraries and the
20+
process's memory, bypassing Python's safety mechanisms and allowing
21+
execution of arbitrary native code.
22+
Incorrect use can corrupt data and objects, reveal sensitive information,
23+
cause crashes, or otherwise compromise the running process.
24+
1725

1826
.. _ctypes-ctypes-tutorial:
1927

@@ -198,10 +206,8 @@ argument values::
198206
OSError: exception: access violation reading 0x00000020
199207
>>>
200208

201-
There are, however, enough ways to crash Python with :mod:`!ctypes`, so you
202-
should be careful anyway. The :mod:`faulthandler` module can be helpful in
203-
debugging crashes (e.g. from segmentation faults produced by erroneous C library
204-
calls).
209+
The :mod:`faulthandler` module can help debug crashes,
210+
such as segmentation faults produced by erroneous C library calls.
205211

206212
``None``, integers, bytes objects and (unicode) strings are the only native
207213
Python objects that can directly be used as parameters in these function calls.

Doc/library/enum.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ Data types
222222
names of the members in *cls*::
223223

224224
>>> dir(Color)
225-
['BLUE', 'GREEN', 'RED', '__class__', '__contains__', '__doc__', '__getitem__', '__init_subclass__', '__iter__', '__len__', '__members__', '__module__', '__name__', '__qualname__']
225+
['BLUE', 'GREEN', 'RED', '__class__', '__contains__', '__doc__', '__getitem__', '__init_subclass__', '__iter__', '__len__', '__members__', '__module__', '__name__', '__qualname__', '_generate_next_value_', '_missing_']
226226

227227
.. method:: EnumType.__getitem__(cls, name)
228228

@@ -355,7 +355,7 @@ Data types
355355
... print(f'today is {cls(dt.date.today().isoweekday()).name}')
356356
...
357357
>>> dir(Weekday.SATURDAY)
358-
['__class__', '__doc__', '__eq__', '__hash__', '__module__', 'name', 'today', 'value']
358+
['__class__', '__doc__', '__eq__', '__hash__', '__module__', '_add_alias_', '_add_value_alias_', '_generate_next_value_', '_missing_', 'name', 'today', 'value']
359359

360360
.. method:: Enum._generate_next_value_(name, start, count, last_values)
361361

Doc/library/inspect.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,10 +1614,11 @@ properties, will be invoked and :meth:`~object.__getattr__` and
16141614
may be called.
16151615

16161616
For cases where you want passive introspection, like documentation tools, this
1617-
can be inconvenient. :func:`getattr_static` has the same signature as :func:`getattr`
1617+
can be inconvenient. :func:`getattr_static` has a similar signature as :func:`getattr`
16181618
but avoids executing code when it fetches attributes.
16191619

1620-
.. function:: getattr_static(obj, attr, default=None)
1620+
.. function:: getattr_static(obj, attr)
1621+
getattr_static(obj, attr, default)
16211622

16221623
Retrieve attributes without triggering dynamic lookup via the
16231624
descriptor protocol, :meth:`~object.__getattr__`

Doc/library/lzma.rst

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,12 +356,26 @@ options. Valid filter IDs are as follows:
356356

357357
* Branch-Call-Jump (BCJ) filters:
358358

359-
* :const:`FILTER_X86`
360-
* :const:`FILTER_IA64`
361-
* :const:`FILTER_ARM`
362-
* :const:`FILTER_ARMTHUMB`
363-
* :const:`FILTER_POWERPC`
364-
* :const:`FILTER_SPARC`
359+
* :const:`!FILTER_X86`
360+
* :const:`!FILTER_IA64`
361+
* :const:`!FILTER_ARM`
362+
* :const:`!FILTER_ARMTHUMB`
363+
* :const:`!FILTER_POWERPC`
364+
* :const:`!FILTER_SPARC`
365+
366+
The above work on all lzma runtime library versions.
367+
368+
* :const:`!FILTER_ARM64`
369+
370+
Only works if the lzma version is 5.4.0 or later.
371+
372+
.. versionadded:: next
373+
374+
* :const:`!FILTER_RISCV`
375+
376+
Only works if the lzma version is 5.6.0 or later.
377+
378+
.. versionadded:: next
365379

366380
A filter chain can consist of up to 4 filters, and cannot be empty. The last
367381
filter in the chain must be a compression filter, and any other filters must be

Doc/library/pydoc.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ will start a HTTP server on port 1234, allowing you to browse the
6868
documentation at ``http://localhost:1234/`` in your preferred web browser.
6969
Specifying ``0`` as the port number will select an arbitrary unused port.
7070

71+
.. warning::
72+
73+
The :mod:`!pydoc` HTTP server is intended for local use during
74+
development and is not suitable for production use.
75+
7176
:program:`python -m pydoc -n <hostname>` will start the server listening at the given
7277
hostname. By default the hostname is 'localhost' but if you want the server to
7378
be reached from other machines, you may want to change the host name that the

Doc/reference/compound_stmts.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ The match statement is used for pattern matching. Syntax:
620620
match_stmt: 'match' `subject_expr` ":" NEWLINE INDENT `case_block`+ DEDENT
621621
subject_expr: `flexible_expression` "," [`flexible_expression_list` [',']]
622622
: | `assignment_expression`
623-
case_block: 'case' `patterns` [`guard`] ":" `!block`
623+
case_block: 'case' `patterns` [`guard`] ":" `suite`
624624

625625
.. note::
626626
This section uses single quotes to denote

Doc/reference/datamodel.rst

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ subscript notation ``a[k]`` selects the item indexed by ``k`` from the mapping
496496
:keyword:`del` statements. The built-in function :func:`len` returns the number
497497
of items in a mapping.
498498

499-
There is currently a single intrinsic mapping type:
499+
There are two intrinsic mapping types:
500500

501501

502502
Dictionaries
@@ -535,6 +535,20 @@ module.
535535
an implementation detail at that time rather than a language guarantee.
536536

537537

538+
Frozen dictionaries
539+
^^^^^^^^^^^^^^^^^^^
540+
541+
.. index:: pair: object; frozendict
542+
543+
These represent an immutable dictionary. They are created by the built-in
544+
:func:`frozendict` constructor. A frozendict is :term:`hashable` if all of
545+
its keys and values are hashable, in which case it can be used as an element
546+
of a set, or as a key in another mapping. :class:`!frozendict` is not a
547+
subclass of :class:`dict`; it inherits directly from :class:`object`.
548+
549+
.. versionadded:: 3.15
550+
551+
538552
Callable types
539553
--------------
540554

0 commit comments

Comments
 (0)