Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1812,6 +1812,20 @@ def testfunc(n):
# __init__ resolution allows promotion of range to constant
self.assertNotIn("_LOAD_GLOBAL_BUILTINS", uops)

# See https://github.com/python/cpython/issues/158072
def test_init_with_default_argument(self):
script_helper.assert_python_ok("-c", textwrap.dedent(f"""\
sentinel = object()

class WithDefault:
def __init__(self, value=sentinel):
if value is not sentinel:
pass

for _ in range({TIER2_THRESHOLD * 3}):
WithDefault()
"""), PYTHON_JIT="1")

def test_init_guards_removed(self):
class MyPoint:
def __init__(self, x, y):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a JIT crash when a class is instantiated with their default arguments
19 changes: 14 additions & 5 deletions Python/optimizer_symbols.c
Original file line number Diff line number Diff line change
Expand Up @@ -1392,13 +1392,22 @@ _Py_uop_frame_new(
frame->locals[i] = PyJitRef_RemoveUnique(args[i]);
}

// If the args are known, then it's safe to just initialize
// every other non-set local to null symbol.
bool default_null = args != NULL;
// When args is available, missing parameters get defaults or *args/**kwargs.
// Other locals start as NULL. When args is NULL, treat all locals as unknown.
int parameter_count = co->co_argcount + co->co_kwonlyargcount;
parameter_count += (co->co_flags & CO_VARARGS) != 0;
parameter_count += (co->co_flags & CO_VARKEYWORDS) != 0;

for (int i = arg_len; i < co->co_nlocalsplus; i++) {
JitOptRef local = default_null ? _Py_uop_sym_new_null(ctx) : _Py_uop_sym_new_unknown(ctx);
frame->locals[i] = local;
if (args == NULL) {
frame->locals[i] = _Py_uop_sym_new_unknown(ctx);
}
else if (i < parameter_count) {
frame->locals[i] = _Py_uop_sym_new_not_null(ctx);
}
else {
frame->locals[i] = _Py_uop_sym_new_null(ctx);
}
}

frame->callable = _Py_uop_sym_new_not_null(ctx);
Expand Down
Loading