Skip to content

Commit 73b9a45

Browse files
authored
Merge branch 'main' into int_int_inplace
2 parents 4d6288b + 62a6e89 commit 73b9a45

Some content is hidden

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

67 files changed

+554
-187
lines changed

.github/workflows/reusable-emscripten.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
build-emscripten-reusable:
1111
name: 'build and test'
1212
runs-on: ubuntu-24.04
13-
timeout-minutes: 60
13+
timeout-minutes: 40
1414
steps:
1515
- uses: actions/checkout@v6
1616
with:

Doc/data/threadsafety.dat

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,33 @@ PyByteArray_GET_SIZE:atomic:
123123

124124
# Raw data - no locking; mutating it is unsafe if the bytearray object is shared between threads
125125
PyByteArray_AsString:compatible:
126-
PyByteArray_AS_STRING:compatible:
126+
PyByteArray_AS_STRING:compatible:
127+
128+
# Capsule objects (Doc/c-api/capsule.rst)
129+
130+
# Type check - read ob_type pointer, always safe
131+
PyCapsule_CheckExact:atomic:
132+
133+
# Creation - pure allocation, no shared state
134+
PyCapsule_New:atomic:
135+
136+
# Validation - reads pointer and name fields; safe on distinct objects
137+
PyCapsule_IsValid:distinct:
138+
139+
# Getters - read struct fields; safe on distinct objects but
140+
# concurrent access to the same capsule requires external synchronization
141+
PyCapsule_GetPointer:distinct:
142+
PyCapsule_GetName:distinct:
143+
PyCapsule_GetDestructor:distinct:
144+
PyCapsule_GetContext:distinct:
145+
146+
# Setters - write struct fields; safe on distinct objects but
147+
# concurrent access to the same capsule requires external synchronization
148+
PyCapsule_SetPointer:distinct:
149+
PyCapsule_SetName:distinct:
150+
PyCapsule_SetDestructor:distinct:
151+
PyCapsule_SetContext:distinct:
152+
153+
# Import - looks up a capsule from a module attribute and
154+
# calls PyCapsule_GetPointer; may call arbitrary code
155+
PyCapsule_Import:compatible:

Doc/library/array.rst

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ defined:
4242
+-----------+--------------------+-------------------+-----------------------+-------+
4343
| ``'Q'`` | unsigned long long | int | 8 | |
4444
+-----------+--------------------+-------------------+-----------------------+-------+
45+
| ``'e'`` | _Float16 | float | 2 | \(3) |
46+
+-----------+--------------------+-------------------+-----------------------+-------+
4547
| ``'f'`` | float | float | 4 | |
4648
+-----------+--------------------+-------------------+-----------------------+-------+
4749
| ``'d'`` | double | float | 8 | |
4850
+-----------+--------------------+-------------------+-----------------------+-------+
49-
| ``'F'`` | float complex | complex | 8 | \(3) |
51+
| ``'F'`` | float complex | complex | 8 | \(4) |
5052
+-----------+--------------------+-------------------+-----------------------+-------+
51-
| ``'D'`` | double complex | complex | 16 | \(3) |
53+
| ``'D'`` | double complex | complex | 16 | \(4) |
5254
+-----------+--------------------+-------------------+-----------------------+-------+
5355

5456

@@ -69,6 +71,15 @@ Notes:
6971
.. versionadded:: 3.13
7072

7173
(3)
74+
The IEEE 754 binary16 "half precision" type was introduced in the 2008
75+
revision of the `IEEE 754 standard <ieee 754 standard_>`_.
76+
This type is not widely supported by C compilers. It's available
77+
as :c:expr:`_Float16` type, if the compiler supports the Annex H
78+
of the C23 standard.
79+
80+
.. versionadded:: next
81+
82+
(4)
7283
Complex types (``F`` and ``D``) are available unconditionally,
7384
regardless on support for complex types (the Annex G of the C11 standard)
7485
by the C compiler.
@@ -304,3 +315,5 @@ Examples::
304315

305316
`NumPy <https://numpy.org/>`_
306317
The NumPy package defines another array type.
318+
319+
.. _ieee 754 standard: https://en.wikipedia.org/wiki/IEEE_754-2008_revision

Doc/library/plistlib.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ and XML plist files.
1818

1919
The property list (``.plist``) file format is a simple serialization supporting
2020
basic object types, like dictionaries, lists, numbers and strings. Usually the
21-
top level object is a dictionary.
21+
top level object is a dictionary or a frozen dictionary.
2222

2323
To write out and to parse a plist file, use the :func:`dump` and
2424
:func:`load` functions.

Doc/library/stdtypes.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3735,12 +3735,13 @@ arbitrary binary data.
37353735
The separator to search for may be any :term:`bytes-like object`.
37363736

37373737

3738-
.. method:: bytes.replace(old, new, count=-1, /)
3739-
bytearray.replace(old, new, count=-1, /)
3738+
.. method:: bytes.replace(old, new, /, count=-1)
3739+
bytearray.replace(old, new, /, count=-1)
37403740

37413741
Return a copy of the sequence with all occurrences of subsequence *old*
3742-
replaced by *new*. If the optional argument *count* is given, only the
3743-
first *count* occurrences are replaced.
3742+
replaced by *new*. If *count* is given, only the first *count* occurrences
3743+
are replaced. If *count* is not specified or ``-1``, then all occurrences
3744+
are replaced.
37443745

37453746
The subsequence to search for and its replacement may be any
37463747
:term:`bytes-like object`.
@@ -3750,6 +3751,9 @@ arbitrary binary data.
37503751
The bytearray version of this method does *not* operate in place - it
37513752
always produces a new object, even if no changes were made.
37523753

3754+
.. versionchanged:: next
3755+
*count* is now supported as a keyword argument.
3756+
37533757

37543758
.. method:: bytes.rfind(sub[, start[, end]])
37553759
bytearray.rfind(sub[, start[, end]])

Doc/library/xml.etree.elementtree.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ Functions
691691
.. versionadded:: 3.2
692692

693693

694-
.. function:: SubElement(parent, tag, attrib={}, **extra)
694+
.. function:: SubElement(parent, tag, /, attrib={}, **extra)
695695

696696
Subelement factory. This function creates an element instance, and appends
697697
it to an existing element.
@@ -705,6 +705,9 @@ Functions
705705
.. versionchanged:: 3.15
706706
*attrib* can now be a :class:`frozendict`.
707707

708+
.. versionchanged:: next
709+
*parent* and *tag* are now positional-only parameters.
710+
708711

709712
.. function:: tostring(element, encoding="us-ascii", method="xml", *, \
710713
xml_declaration=None, default_namespace=None, \
@@ -880,7 +883,7 @@ Element Objects
880883
:noindex:
881884
:no-index:
882885

883-
.. class:: Element(tag, attrib={}, **extra)
886+
.. class:: Element(tag, /, attrib={}, **extra)
884887

885888
Element class. This class defines the Element interface, and provides a
886889
reference implementation of this interface.
@@ -893,6 +896,9 @@ Element Objects
893896
.. versionchanged:: 3.15
894897
*attrib* can now be a :class:`frozendict`.
895898

899+
.. versionchanged:: next
900+
*tag* is now a positional-only parameter.
901+
896902

897903
.. attribute:: tag
898904

Doc/whatsnew/3.15.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ For example::
217217

218218
The following standard library modules have been updated to accept
219219
:class:`!frozendict`: :mod:`copy`, :mod:`decimal`, :mod:`json`, :mod:`marshal`,
220-
:mod:`pickle`, :mod:`pprint` and :mod:`xml.etree.ElementTree`.
220+
:mod:`plistlib` (only for serialization), :mod:`pickle`, :mod:`pprint` and
221+
:mod:`xml.etree.ElementTree`.
221222

222223
:func:`eval` and :func:`exec` accept :class:`!frozendict` for *globals*, and
223224
:func:`type` and :meth:`str.maketrans` accept :class:`!frozendict` for *dict*.
@@ -604,6 +605,9 @@ Other language changes
604605
respectively.
605606
(Contributed by Sergey B Kirpichev in :gh:`146151`.)
606607

608+
* Allow the *count* argument of :meth:`bytes.replace` to be a keyword.
609+
(Contributed by Stan Ulbrych in :gh:`147856`.)
610+
607611

608612
New modules
609613
===========
@@ -642,6 +646,10 @@ array
642646
formatting characters ``'F'`` and ``'D'`` respectively.
643647
(Contributed by Sergey B Kirpichev in :gh:`146151`.)
644648

649+
* Support half-floats (16-bit IEEE 754 binary interchange format): formatting
650+
character ``'e'``.
651+
(Contributed by Sergey B Kirpichev in :gh:`146238`.)
652+
645653

646654
base64
647655
------

Include/internal/pycore_long.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,20 @@ _PyLong_IsPositive(const PyLongObject *op)
240240
return (op->long_value.lv_tag & SIGN_MASK) == 0;
241241
}
242242

243+
/* Return true if the argument is a small int */
244+
static inline bool
245+
_PyLong_IsSmallInt(const PyLongObject *op)
246+
{
247+
assert(PyLong_Check(op));
248+
bool is_small_int = (op->long_value.lv_tag & IMMORTALITY_BIT_MASK) != 0;
249+
assert(PyLong_CheckExact(op) || (!is_small_int));
250+
assert(_Py_IsImmortal(op) || (!is_small_int));
251+
assert((_PyLong_IsCompact(op)
252+
&& _PY_IS_SMALL_INT(_PyLong_CompactValue(op)))
253+
|| (!is_small_int));
254+
return is_small_int;
255+
}
256+
243257
static inline Py_ssize_t
244258
_PyLong_DigitCount(const PyLongObject *op)
245259
{
@@ -301,7 +315,9 @@ _PyLong_SetDigitCount(PyLongObject *op, Py_ssize_t size)
301315
#define NON_SIZE_MASK ~(uintptr_t)((1 << NON_SIZE_BITS) - 1)
302316

303317
static inline void
304-
_PyLong_FlipSign(PyLongObject *op) {
318+
_PyLong_FlipSign(PyLongObject *op)
319+
{
320+
assert(!_PyLong_IsSmallInt(op));
305321
unsigned int flipped_sign = 2 - (op->long_value.lv_tag & SIGN_MASK);
306322
op->long_value.lv_tag &= NON_SIZE_MASK;
307323
op->long_value.lv_tag |= flipped_sign;

InternalDocs/jit.md

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,54 @@ and this enables optimizations that span multiple instructions.
1111
Historically, the adaptive interpreter was referred to as `tier 1` and
1212
the JIT as `tier 2`. You will see remnants of this in the code.
1313

14-
## The Optimizer and Executors
14+
## The Trace Recorder and Executors
1515

16-
The program begins running on the adaptive interpreter, until a `JUMP_BACKWARD`
17-
instruction determines that it is "hot" because the counter in its
16+
There are two interpreters in this section:
17+
1. Adaptive interpreter (the default behavior)
18+
2. Trace recording interpreter (enabled on JIT builds)
19+
20+
The program begins running on the adaptive interpreter, until a `JUMP_BACKWARD` or
21+
`RESUME` instruction determines that it is "hot" because the counter in its
1822
[inline cache](interpreter.md#inline-cache-entries) indicates that it
1923
executed more than some threshold number of times (see
2024
[`backoff_counter_triggers`](../Include/internal/pycore_backoff.h)).
21-
It then calls the function `_PyOptimizer_Optimize()` in
25+
It then calls the function `_PyJit_TryInitializeTracing` in
2226
[`Python/optimizer.c`](../Python/optimizer.c), passing it the current
23-
[frame](frames.md) and instruction pointer. `_PyOptimizer_Optimize()`
24-
constructs an object of type
25-
[`_PyExecutorObject`](../Include/internal/pycore_optimizer.h) which implements
26-
an optimized version of the instruction trace beginning at this jump.
27-
28-
The optimizer determines where the trace ends, and the executor is set up
27+
[frame](frames.md), instruction pointer and state.
28+
The interpreter then switches into "tracing mode" via the macro
29+
`ENTER_TRACING()`. On platforms that support computed goto and tail-calling
30+
interpreters, the dispatch table is swapped out, while other platforms that do
31+
not support either use a single flag in the opcode.
32+
Execution between the normal interpreter and tracing interpreter are
33+
interleaved via this dispatch mechanism. This means that while logically
34+
there are two interpreters, the implementation appears to be a single
35+
interpreter.
36+
37+
During tracing mode, after each interpreter instruction's `DISPATCH()`,
38+
the interpreter jumps to the `TRACE_RECORD` instruction. This instruction
39+
records the previous instruction executed and also any live values of the next
40+
operation it may require. It then translates the previous instruction to
41+
a sequence of micro-ops using `_PyJit_translate_single_bytecode_to_trace`.
42+
To ensure that the adaptive interpreter instructions
43+
and cache entries are up-to-date, the trace recording interpreter always resets
44+
the adaptive counters of adaptive instructions it sees.
45+
This forces a re-specialization of any new instruction should an instruction
46+
deoptimize. Thus, feeding the trace recorder up-to-date information.
47+
Finally, the `TRACE_RECORD` instruction decides when to stop tracing
48+
using various heuristics.
49+
50+
Once trace recording concludes, `LEAVE_TRACING()` swaps out the dispatch
51+
table/the opcode flag set earlier by `ENTER_TRACING()` is unset.
52+
`stop_tracing_and_jit()` then calls `_PyOptimizer_Optimize()` which optimizes
53+
the trace and constructs an
54+
[`_PyExecutorObject`](../Include/internal/pycore_optimizer.h).
55+
56+
JIT execution is set up
2957
to either return to the adaptive interpreter and resume execution, or
3058
transfer control to another executor (see `_PyExitData` in
31-
Include/internal/pycore_optimizer.h).
59+
Include/internal/pycore_optimizer.h). When resuming to the adaptive interpreter,
60+
a "side exit", generated by an `EXIT_IF` may trigger recording of another trace.
61+
While a "deopt", generated by a `DEOPT_IF`, does not trigger recording.
3262

3363
The executor is stored on the [`code object`](code_objects.md) of the frame,
3464
in the `co_executors` field which is an array of executors. The start
@@ -40,12 +70,7 @@ executor in `co_executors`.
4070

4171
The micro-op (abbreviated `uop` to approximate `μop`) optimizer is defined in
4272
[`Python/optimizer.c`](../Python/optimizer.c) as `_PyOptimizer_Optimize`.
43-
It translates an instruction trace into a sequence of micro-ops by replacing
44-
each bytecode by an equivalent sequence of micro-ops (see
45-
`_PyOpcode_macro_expansion` in
46-
[pycore_opcode_metadata.h](../Include/internal/pycore_opcode_metadata.h)
47-
which is generated from [`Python/bytecodes.c`](../Python/bytecodes.c)).
48-
The micro-op sequence is then optimized by
73+
It takes a micro-op sequence from the trace recorder and optimizes with
4974
`_Py_uop_analyze_and_optimize` in
5075
[`Python/optimizer_analysis.c`](../Python/optimizer_analysis.c)
5176
and an instance of `_PyUOpExecutor_Type` is created to contain it.

Lib/collections/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,14 +328,14 @@ def __ior__(self, other):
328328
return self
329329

330330
def __or__(self, other):
331-
if not isinstance(other, dict):
331+
if not isinstance(other, (dict, frozendict)):
332332
return NotImplemented
333333
new = self.__class__(self)
334334
new.update(other)
335335
return new
336336

337337
def __ror__(self, other):
338-
if not isinstance(other, dict):
338+
if not isinstance(other, (dict, frozendict)):
339339
return NotImplemented
340340
new = self.__class__(other)
341341
new.update(self)
@@ -1217,14 +1217,14 @@ def __repr__(self):
12171217
def __or__(self, other):
12181218
if isinstance(other, UserDict):
12191219
return self.__class__(self.data | other.data)
1220-
if isinstance(other, dict):
1220+
if isinstance(other, (dict, frozendict)):
12211221
return self.__class__(self.data | other)
12221222
return NotImplemented
12231223

12241224
def __ror__(self, other):
12251225
if isinstance(other, UserDict):
12261226
return self.__class__(other.data | self.data)
1227-
if isinstance(other, dict):
1227+
if isinstance(other, (dict, frozendict)):
12281228
return self.__class__(other | self.data)
12291229
return NotImplemented
12301230

0 commit comments

Comments
 (0)