Skip to content

Commit 0cf0de2

Browse files
committed
Merge branch 'main' into gh-148871-immortal-stackref
2 parents e142095 + 441af3a commit 0cf0de2

18 files changed

Lines changed: 200 additions & 15 deletions

Include/internal/pycore_magic_number.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ Known values:
298298
Python 3.15a8 3665 (Add FOR_ITER_VIRTUAL and GET_ITER specializations)
299299
Python 3.15b1 3666 (Add SEND_VIRTUAL and SEND_ASYNC_GEN specializations)
300300
Python 3.16a0 3700 (Initial version)
301+
Python 3.16a0 3701 (Add CONSTANT_EMPTY_TUPLE to LOAD_COMMON_CONSTANT)
301302
302303
303304
Python 3.17 will start with 3750
@@ -311,7 +312,7 @@ PC/launcher.c must also be updated.
311312
312313
*/
313314

314-
#define PYC_MAGIC_NUMBER 3700
315+
#define PYC_MAGIC_NUMBER 3701
315316
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
316317
(little-endian) and then appending b'\r\n'. */
317318
#define PYC_MAGIC_NUMBER_TOKEN \

Include/internal/pycore_opcode_utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ extern "C" {
8181
#define CONSTANT_FALSE 10
8282
#define CONSTANT_MINUS_ONE 11
8383
#define CONSTANT_BUILTIN_FROZENSET 12
84-
#define NUM_COMMON_CONSTANTS 13
84+
#define CONSTANT_EMPTY_TUPLE 13
85+
#define NUM_COMMON_CONSTANTS 14
8586

8687
/* Values used in the oparg for RESUME */
8788
#define RESUME_AT_FUNC_START 0

Include/internal/pycore_optimizer.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ extern "C" {
3131
* 4. A push followed by a matching return is net-zero on frame-specific
3232
* fitness, excluding per-slot costs.
3333
*/
34-
#define MAX_TARGET_LENGTH (UOP_MAX_TRACE_LENGTH / 2)
3534
#define OPTIMIZER_EFFECTIVENESS 2
36-
#define FITNESS_INITIAL (MAX_TARGET_LENGTH * OPTIMIZER_EFFECTIVENESS)
35+
#define MAX_TARGET_LENGTH (FITNESS_INITIAL / OPTIMIZER_EFFECTIVENESS)
3736

3837
/* Exit quality thresholds: trace stops when fitness < exit_quality.
3938
* Higher = trace is more willing to stop here. */

Include/internal/pycore_uop.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@ typedef struct _PyUOpInstruction{
3636
#endif
3737
} _PyUOpInstruction;
3838

39-
// This is the length of the trace we translate initially.
39+
// Fitness is the target length of the trace we translate initially. The uop
40+
// buffer has a small amount of extra space for entry/loop-closing overhead.
4041
#if defined(Py_DEBUG) && defined(_Py_JIT)
4142
// With asserts, the stencils are a lot larger
42-
#define UOP_MAX_TRACE_LENGTH 1000
43+
#define FITNESS_INITIAL 1000
4344
#else
44-
#define UOP_MAX_TRACE_LENGTH 2500
45+
#define FITNESS_INITIAL 2500
4546
#endif
4647

48+
#define UOP_TRACE_BUFFER_OVERHEAD 10
49+
#define UOP_MAX_TRACE_LENGTH (FITNESS_INITIAL + UOP_TRACE_BUFFER_OVERHEAD)
50+
4751
/* Bloom filter with m = 256
4852
* https://en.wikipedia.org/wiki/Bloom_filter */
4953
#ifdef HAVE_GCC_UINT128_T

Lib/dis.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,8 @@ def _get_const_value(op, arg, co_consts):
698698
if op == LOAD_SMALL_INT:
699699
return arg
700700
if op == LOAD_COMMON_CONSTANT:
701-
# Opargs 0-6 are callables; 7-11 are literal values.
702-
if 7 <= arg <= 11:
701+
# Opargs 0-6 and 12 are callables; 7-11 and 13 are literal values.
702+
if 7 <= arg <= 11 or arg == 13:
703703
return _common_constants[arg]
704704
return UNKNOWN
705705
argval = UNKNOWN

Lib/opcode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
builtins.set,
4545
# Append-only — must match CONSTANT_* in
4646
# Include/internal/pycore_opcode_utils.h.
47-
None, "", True, False, -1, builtins.frozenset]
47+
None, "", True, False, -1, builtins.frozenset, ()]
4848
_nb_ops = _opcode.get_nb_ops()
4949

5050
hascompare = [opmap["COMPARE_OP"]]

Lib/test/test_capi/test_opt.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5924,6 +5924,44 @@ def __next__(self):
59245924
"""), PYTHON_JIT="1", PYTHON_JIT_STRESS="1")
59255925
self.assertEqual(result[0].rc, 0, result)
59265926

5927+
def test_149335_trace_buffer_guard(self):
5928+
# https://github.com/python/cpython/issues/149335
5929+
5930+
result = script_helper.run_python_until_end('-c', textwrap.dedent("""
5931+
import sys
5932+
5933+
def f1():
5934+
for i_3178 in 0, 2, 10:
5935+
mv162 = 162
5936+
5937+
mv3 = mv1 = mv_165 = mv16 = \
5938+
mv167 = mv168 = \
5939+
mv169 = \
5940+
mv_1403_170 = \
5941+
169
5942+
5943+
mv_1403_170
5944+
5945+
mv_172 = mv_3 = mv_4 = mv175 = mv176 = mv17 = mv178 = mv179 = mv0 = mv1 = mv182 = (
5946+
mv3
5947+
) = mv4 = mv185 = mv186 = mv187 = mv18 = mv189 = mv0 = mv1 = mv192 = mv3 = mv4 = (
5948+
mv195
5949+
) = mv196 = mv197 = mv_198 = mv19 = mv0 = mv1 = mv2 = mv3 = mv4 = mv05 = mv06 = (
5950+
mv07
5951+
) = mv08 = mv09 = mv0 = mv1 = mv2 = mv3 = mv4 = mv15 = mv16 = mv17 = mv18 = mv19 = (
5952+
mv0
5953+
) = mv1 = mv_2 = mv3 = mv4 = mv_25 = mv_26 = mv_27 = mv_28 = mv_29 = mv0 = mv1 = (
5954+
mv2
5955+
) = mv_1403 = mv4 = mv35 = mv36 = mv37 = mv38 = mv39 = mv0 = -sys.maxsize / 3
5956+
5957+
mv1 = mv_12 = mv3 = mv_14 = mv45 = sys.float_info.epsilon
5958+
mv46 = sys.float_info.epsilon
5959+
5960+
for i in range(15000):
5961+
f1()
5962+
"""), PYTHON_JIT="1")
5963+
self.assertEqual(result[0].rc, 0, result)
5964+
59275965
def test_144068_daemon_thread_jit_cleanup(self):
59285966
result = script_helper.run_python_until_end('-c', textwrap.dedent("""
59295967
import threading

Lib/test/test_crossinterp.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ def ignore_byteswarning():
157157
{},
158158
{1: 7, 2: 8, 3: 9},
159159
{1: [1], 2: (2,), 3: {3: 4}},
160+
# frozendict
161+
frozendict(),
162+
frozendict({1: 7, 2: 8, 3: 9}),
163+
frozendict({1: [1], 2: (2,), 3: {3: 4}, 4: frozendict({5: 6})}),
160164
# set
161165
set(),
162166
{1, 2, 3},

Lib/test/test_lazy_import/__init__.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,59 @@ def test_from_import_with_module_getattr(self):
9898
""")
9999
assert_python_ok("-c", code)
100100

101+
@support.requires_subprocess()
102+
def test_from_import_with_module_getattr_raising(self):
103+
"""Lazy from import should respect module-level __getattr__."""
104+
code = textwrap.dedent("""
105+
lazy from test.test_lazy_import.data.module_with_getattr import raising_attr
106+
107+
try:
108+
raising_attr
109+
except ValueError as exc:
110+
assert str(exc) == 'from_getattr', exc
111+
else:
112+
assert False, f'ValueError is not raised: {raising_attr}'
113+
""")
114+
assert_python_ok("-c", code)
115+
116+
@support.requires_subprocess()
117+
def test_from_import_with_module_getattr_missing(self):
118+
"""Lazy from import should respect module-level __getattr__."""
119+
for attr in ("missing_attr", "import_error_attr"):
120+
with self.subTest(attr=attr):
121+
code = textwrap.dedent(f"""
122+
lazy from test.test_lazy_import.data.module_with_getattr import {attr}
123+
124+
try:
125+
{attr}
126+
except ImportError as exc:
127+
assert '{attr}' in str(exc), exc
128+
assert exc.__cause__ is not None
129+
else:
130+
assert False, ('ImportError is not raised', {attr})
131+
""")
132+
assert_python_ok("-c", code)
133+
134+
@support.requires_subprocess()
135+
def test_from_import_with_module_getattr_warning(self):
136+
"""Lazy from import should respect module-level __getattr__."""
137+
code = textwrap.dedent("""
138+
import warnings
139+
140+
with warnings.catch_warnings(record=True) as log:
141+
lazy from test.test_lazy_import.data.module_with_getattr import warning_attr
142+
143+
assert log == []
144+
145+
with warnings.catch_warnings(record=True) as log:
146+
warning_attr
147+
assert warning_attr == 'from_warning_attr', warning_attr
148+
assert len(log) == 1, log
149+
assert isinstance(log[0].message, UserWarning), log
150+
assert str(log[0].message) == 'from_getattr', log
151+
""")
152+
assert_python_ok("-c", code)
153+
101154
@support.requires_subprocess()
102155
def test_from_import_with_imported_module_getattr(self):
103156
"""Lazy from import should not shadow an imported module's __getattr__."""
@@ -463,6 +516,59 @@ def test_lazy_import_pkg_cross_import(self):
463516
self.assertEqual(type(g["x"]), int)
464517
self.assertEqual(type(g["b"]), types.LazyImportType)
465518

519+
@support.requires_subprocess()
520+
def test_package_from_import_with_module_getattr_raising(self):
521+
"""Lazy from import should respect a package's __getattr__."""
522+
code = textwrap.dedent("""
523+
lazy from test.test_lazy_import.data.pkg import raising_attr
524+
525+
try:
526+
raising_attr
527+
except ValueError as exc:
528+
assert str(exc) == 'from_getattr', exc
529+
else:
530+
assert False, f'ValueError is not raised: {raising_attr}'
531+
""")
532+
assert_python_ok("-c", code)
533+
534+
@support.requires_subprocess()
535+
def test_package_from_import_with_module_getattr_missing(self):
536+
"""Lazy from import should respect package's __getattr__."""
537+
for attr in ("missing_attr", "import_error_attr"):
538+
with self.subTest(attr=attr):
539+
code = textwrap.dedent(f"""
540+
lazy from test.test_lazy_import.data.pkg import {attr}
541+
542+
try:
543+
{attr}
544+
except ImportError as exc:
545+
assert '{attr}' in str(exc), exc
546+
assert exc.__cause__ is not None
547+
else:
548+
assert False, ('ImportError is not raised', {attr})
549+
""")
550+
assert_python_ok("-c", code)
551+
552+
@support.requires_subprocess()
553+
def test_from_import_with_module_getattr_warning(self):
554+
"""Lazy from import should respect package's __getattr__."""
555+
code = textwrap.dedent("""
556+
import warnings
557+
558+
with warnings.catch_warnings(record=True) as log:
559+
lazy from test.test_lazy_import.data.pkg import warning_attr
560+
561+
assert log == []
562+
563+
with warnings.catch_warnings(record=True) as log:
564+
warning_attr
565+
assert warning_attr == 'from_warning_attr', warning_attr
566+
assert len(log) == 1, log
567+
assert isinstance(log[0].message, UserWarning), log
568+
assert str(log[0].message) == 'from_getattr', log
569+
""")
570+
assert_python_ok("-c", code)
571+
466572
@support.requires_subprocess()
467573
def test_package_from_import_with_module_getattr(self):
468574
"""Lazy from import should respect a package's __getattr__."""
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
def __getattr__(name):
22
if name == "dynamic_attr":
33
return "from_getattr"
4+
elif name == "raising_attr":
5+
raise ValueError("from_getattr")
6+
elif name == "import_error_attr":
7+
raise ImportError(name)
8+
elif name == "warning_attr":
9+
import warnings
10+
warnings.warn("from_getattr", category=UserWarning)
11+
return "from_warning_attr"
412
raise AttributeError(name)

0 commit comments

Comments
 (0)