Skip to content

Commit dcc5040

Browse files
committed
Merge remote-tracking branch 'origin/main' into c23-math-pifuns
2 parents d66a2c3 + 26696a6 commit dcc5040

60 files changed

Lines changed: 508 additions & 220 deletions

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/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/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/whatsnew/3.16.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ gzip
9393
which is passed on to the constructor of the :class:`~gzip.GzipFile` class.
9494
(Contributed by Marin Misur in :gh:`91372`.)
9595

96+
lzma
97+
----
98+
99+
* Add support of new BCJ filters ARM64 and RISC-V via
100+
:const:`!lzma.FILTER_ARM64` and :const:`!lzma.FILTER_RISCV`. Note that the
101+
new filters will work only if runtime library supports them. ARM64 filter
102+
requires ``lzma`` 5.4.0 or newer while RISC-V requires 5.6.0 or newer.
103+
(Contributed by Chien Wong in :gh:`115988`.)
104+
96105
math
97106
----
98107

Include/internal/pycore_interp_structs.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,15 @@ struct _import_state {
349349
int lazy_imports_mode;
350350
PyObject *lazy_imports_filter;
351351
PyObject *lazy_importing_modules;
352+
// The set stored in sys.lazy_modules if values that have been
353+
// lazily imported. This value is only for debugging/introspection
354+
// purposes and is not used by the runtime.
352355
PyObject *lazy_modules;
356+
// A dict mapping package names to a set of submodule names that
357+
// have been imported lazily from packages which have been imported
358+
// lazily. When the package is reified we need to add a
359+
// LazyImportObject which refers to the submodule on the module.
360+
PyObject *lazy_pending_submodules;
353361
#ifdef Py_GIL_DISABLED
354362
PyMutex lazy_mutex;
355363
#endif
@@ -1001,7 +1009,7 @@ struct _is {
10011009
struct ast_state ast;
10021010
struct types_state types;
10031011
struct callable_cache callable_cache;
1004-
PyObject *common_consts[NUM_COMMON_CONSTANTS];
1012+
_PyStackRef common_consts[NUM_COMMON_CONSTANTS];
10051013
bool jit;
10061014
bool compiling;
10071015

Include/internal/pycore_stackref.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,18 @@ _PyStackRef_DUP(_PyStackRef ref, const char *filename, int linenumber)
263263
}
264264
#define PyStackRef_DUP(REF) _PyStackRef_DUP(REF, __FILE__, __LINE__)
265265

266+
static inline _PyStackRef
267+
_PyStackRef_DupImmortal(_PyStackRef ref, const char *filename, int linenumber)
268+
{
269+
assert(!PyStackRef_IsError(ref));
270+
assert(!PyStackRef_IsTaggedInt(ref));
271+
assert(!PyStackRef_RefcountOnObject(ref));
272+
PyObject *obj = _Py_stackref_get_object(ref);
273+
assert(_Py_IsImmortal(obj));
274+
return _Py_stackref_create(obj, Py_TAG_REFCNT, filename, linenumber);
275+
}
276+
#define PyStackRef_DupImmortal(REF) _PyStackRef_DupImmortal((REF), __FILE__, __LINE__)
277+
266278
static inline void
267279
_PyStackRef_CLOSE_SPECIALIZED(_PyStackRef ref, destructor destruct, const char *filename, int linenumber)
268280
{
@@ -633,6 +645,15 @@ PyStackRef_DUP(_PyStackRef ref)
633645
}
634646
#endif
635647

648+
static inline _PyStackRef
649+
PyStackRef_DupImmortal(_PyStackRef ref)
650+
{
651+
assert(!PyStackRef_IsNull(ref));
652+
assert(!PyStackRef_RefcountOnObject(ref));
653+
assert(_Py_IsImmortal(BITS_TO_PTR_MASKED(ref)));
654+
return ref;
655+
}
656+
636657
static inline bool
637658
PyStackRef_IsHeapSafe(_PyStackRef ref)
638659
{

InternalDocs/compiler.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -359,19 +359,19 @@ in [Python/compile.c](../Python/compile.c) into a sequence of pseudo instruction
359359
These are similar to bytecode, but in some cases they are more abstract, and are
360360
resolved later into actual bytecode. The construction of this instruction sequence
361361
is handled by several functions that break the task down by various AST node types.
362-
The functions are all named `compiler_visit_{xx}` where *xx* is the name of the node
362+
The functions are all named `codegen_visit_{xx}` where *xx* is the name of the node
363363
type (such as `stmt`, `expr`, etc.). Each function receives a `struct compiler *`
364364
and `{xx}_ty` where *xx* is the AST node type. Typically these functions
365365
consist of a large 'switch' statement, branching based on the kind of
366366
node type passed to it. Simple things are handled inline in the
367367
'switch' statement with more complex transformations farmed out to other
368-
functions named `compiler_{xx}` with *xx* being a descriptive name of what is
368+
functions named `codegen_{xx}` with *xx* being a descriptive name of what is
369369
being handled.
370370

371371
When transforming an arbitrary AST node, use the `VISIT()` macro.
372-
The appropriate `compiler_visit_{xx}` function is called, based on the value
372+
The appropriate `codegen_visit_{xx}` function is called, based on the value
373373
passed in for <node type> (so `VISIT({c}, expr, {node})` calls
374-
`compiler_visit_expr({c}, {node})`). The `VISIT_SEQ()` macro is very similar,
374+
`codegen_visit_expr({c}, {node})`). The `VISIT_SEQ()` macro is very similar,
375375
but is called on AST node sequences (those values that were created as
376376
arguments to a node that used the '*' modifier).
377377

@@ -414,8 +414,8 @@ which is added at the end of a function is not associated with any
414414
line in the source code.
415415

416416
There are several helper functions that will emit pseudo-instructions
417-
and are named `compiler_{xx}()` where *xx* is what the function helps
418-
with (`list`, `boolop`, etc.). A rather useful one is `compiler_nameop()`.
417+
and are named `codegen_{xx}()` where *xx* is what the function helps
418+
with (`list`, `boolop`, etc.). A rather useful one is `codegen_nameop()`.
419419
This function looks up the scope of a variable and, based on the
420420
expression context, emits the proper opcode to load, store, or delete
421421
the variable.

InternalDocs/qsbr.md

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,39 @@ Periodically, a polling mechanism processes this deferred-free list:
101101

102102
To reduce memory contention from frequent updates to the global `wr_seq`, its
103103
advancement is sometimes deferred. Instead of incrementing `wr_seq` on every
104-
reclamation request, each thread tracks its number of deferrals locally. Once
105-
the deferral count reaches a limit (QSBR_DEFERRED_LIMIT, currently 10), the
106-
thread advances the global `wr_seq` and resets its local count.
107-
108-
When an object is added to the deferred-free list, its qsbr_goal is set to
109-
`wr_seq` + 2. By setting the goal to the next sequence value, we ensure it's safe
110-
to defer the global counter advancement. This optimization improves runtime
111-
speed but may increase peak memory usage by slightly delaying when memory can
112-
be reclaimed.
104+
reclamation request, the object's qsbr_goal is set to `wr_seq` + 2 (the value
105+
the counter *would* take on its next advance) without actually advancing the
106+
global counter. This is safe because the goal still corresponds to a future
107+
sequence value that no thread has yet observed as quiescent.
108+
109+
Whether to actually advance `wr_seq` is decided per request, based on how
110+
much memory and how many items the calling thread has already deferred since
111+
its last advance:
112+
113+
* For deferred object frees (`_PyMem_FreeDelayed`), the thread tracks both a
114+
count (`deferred_count`) and an estimate of the held memory
115+
(`deferred_memory`). The global `wr_seq` is advanced when the freed block
116+
is larger than `QSBR_FREE_MEM_LIMIT` (1 MiB), when the accumulated deferred
117+
memory exceeds that limit, or when the count exceeds `QSBR_DEFERRED_LIMIT`
118+
(127, sized so a chunk of work items is processed before it overflows).
119+
Crossing any of these thresholds also sets a per-thread `should_process`
120+
flag, signalling that the deferred-free list should be drained.
121+
122+
* For mimalloc pages held by QSBR, the thread tracks `deferred_page_memory`
123+
and advances `wr_seq` when either the individual page or the accumulated
124+
page memory exceeds `QSBR_PAGE_MEM_LIMIT` (4096 * 20 bytes). Advancing
125+
promptly here matters because a held page cannot be reused for a different
126+
size class or by a different thread.
127+
128+
Processing of the deferred-free list normally happens from the eval breaker
129+
(rather than from inside `_PyMem_FreeDelayed`), which gives the global
130+
`rd_seq` a better chance to have advanced far enough that items can actually
131+
be freed. `_PyMem_ProcessDelayed` is still called from the free path as a
132+
safety valve when a work-item chunk fills up.
133+
134+
This optimization improves runtime speed but may increase peak memory usage
135+
by slightly delaying when memory can be reclaimed; the size-based thresholds
136+
above bound that extra memory.
113137

114138

115139
## Limitations

0 commit comments

Comments
 (0)