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
31 changes: 31 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,37 @@ 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_with_changed_code_argcount(self):
class C:
def __init__(self, value):
self.value = False

def varargs_init(self, *args):
self.value = isinstance(args, tuple)

def testfunc(n):
for _ in range(n):
result = C(42).value
return result

self.assertFalse(testfunc(100))
C.__init__.__code__ = C.varargs_init.__code__
self.assertTrue(testfunc(TIER2_THRESHOLD * 3))

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
20 changes: 15 additions & 5 deletions Python/optimizer_symbols.c
Original file line number Diff line number Diff line change
Expand Up @@ -1388,17 +1388,27 @@ _Py_uop_frame_new(
}

// Initialize with the initial state of all local variables
arg_len = Py_MIN(arg_len, co->co_argcount);
for (int i = 0; i < arg_len; i++) {
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