Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Feedback from code review
  • Loading branch information
brandtbucher committed Oct 20, 2022
commit dd52da542cc149a005a37ddde1030e08f1541227
78 changes: 72 additions & 6 deletions Lib/test/test_peepholer.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,10 +815,42 @@ def f():
self.assertInBytecode(f, 'LOAD_FAST_CHECK', "a73")
self.assertInBytecode(f, 'LOAD_FAST', "a73")

def test_setting_lineno_adds_check(self):
code = textwrap.dedent("""\
def test_setting_lineno_warns_and_assigns_none_0(self):
code = textwrap.dedent(f"""\
def f():
x = 2
x = y = 2
if not x:
return 4
for i in range(55):
x + 6
L = 7
L = 8
L = 9
L = 10
""")
ns = {}
exec(code, ns)
f = ns['f']
self.assertInBytecode(f, "LOAD_FAST")
self.assertNotInBytecode(f, "LOAD_FAST_CHECK")
co_code = f.__code__.co_code
def trace(frame, event, arg):
if event == 'line' and frame.f_lineno == 9:
frame.f_lineno = 3
sys.settrace(None)
return None
return trace
sys.settrace(trace)
result = f()
self.assertIsNone(result)
self.assertInBytecode(f, "LOAD_FAST")
self.assertNotInBytecode(f, "LOAD_FAST_CHECK")
self.assertEqual(f.__code__.co_code, co_code)

def test_setting_lineno_warns_and_assigns_none_1(self):
code = textwrap.dedent(f"""\
def f():
x = y = 2
if not x:
return 4
for i in range(55):
Expand All @@ -840,7 +872,41 @@ def trace(frame, event, arg):
sys.settrace(None)
return None
return trace
e = r"cannot leave local name 'x' unbound \(assigning None\)"
e = r"assigning None to 1 unbound local"
with self.assertWarnsRegex(RuntimeWarning, e):
sys.settrace(trace)
result = f()
self.assertEqual(result, 4)
self.assertInBytecode(f, "LOAD_FAST")
self.assertNotInBytecode(f, "LOAD_FAST_CHECK")
self.assertEqual(f.__code__.co_code, co_code)

def test_setting_lineno_warns_and_assigns_none_2(self):
code = textwrap.dedent(f"""\
def f():
x = y = 2
if not x:
return 4
for i in range(55):
x + 6
del x, y
L = 8
L = 9
L = 10
""")
ns = {}
exec(code, ns)
f = ns['f']
self.assertInBytecode(f, "LOAD_FAST")
self.assertNotInBytecode(f, "LOAD_FAST_CHECK")
co_code = f.__code__.co_code
def trace(frame, event, arg):
if event == 'line' and frame.f_lineno == 9:
frame.f_lineno = 3
sys.settrace(None)
return None
return trace
e = r"assigning None to 2 unbound locals"
with self.assertWarnsRegex(RuntimeWarning, e):
sys.settrace(trace)
result = f()
Expand All @@ -867,7 +933,7 @@ def f():
self.assertNotInBytecode(f, "LOAD_FAST_CHECK")
return f

def test_deleting_local_adds_check(self):
def test_deleting_local_warns_and_assigns_none(self):
f = self.make_function_with_no_checks()
co_code = f.__code__.co_code
def trace(frame, event, arg):
Expand All @@ -876,7 +942,7 @@ def trace(frame, event, arg):
sys.settrace(None)
return None
return trace
e = r"cannot leave local name 'x' unbound \(assigning None\)"
e = r"assigning None to unbound local 'x'"
with self.assertWarnsRegex(RuntimeWarning, e):
sys.settrace(trace)
f()
Expand Down
113 changes: 61 additions & 52 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -620,49 +620,6 @@ _PyFrame_GetState(PyFrameObject *frame)
Py_UNREACHABLE();
}

static bool
op_exists(PyCodeObject *co, int opcode, int oparg, int start, int stop)
{
// This only works when opcode is a non-quickened form:
assert(_PyOpcode_Deopt[opcode] == opcode);
int check_oparg = 0;
_Py_CODEUNIT *instructions = _PyCode_CODE(co);
while (start < stop) {
int check_opcode = _PyOpcode_Deopt[_Py_OPCODE(instructions[start])];
check_oparg |= _Py_OPARG(instructions[start]);
if (check_opcode == opcode && check_oparg == oparg) {
return true;
}
if (check_opcode == EXTENDED_ARG) {
check_oparg <<= 8;
}
else {
check_oparg = 0;
}
start += 1 + _PyOpcode_Caches[check_opcode];
}
return false;
}

static void
maybe_bind_fast_local(_PyInterpreterFrame *f, int i)
{
PyCodeObject *co = f->f_code;
if (f->localsplus[i] == NULL && op_exists(co, LOAD_FAST, i, 0, Py_SIZE(co)))
{
// We're being overly-cautious here (it's too expensive to run a full
// flow analysis), but at least one instruction expects i to be bound.
// Rather than crashing (or changing co_code), just bind None instead:
const char *e = "cannot leave local name %R unbound (assigning None)";
PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
if (PyErr_WarnFormat(PyExc_RuntimeWarning, 0, e, name)) {
// It's okay if frame_obj is NULL, just try anyways:
PyErr_WriteUnraisable((PyObject *)f->frame_obj);
}
f->localsplus[i] = Py_NewRef(Py_None);
}
}

Comment thread
brandtbucher marked this conversation as resolved.
/* Setter for f_lineno - you can set f_lineno from within a trace function in
* order to jump to a given line of code, subject to some restrictions. Most
* lines are OK to jump to because they don't make any assumptions about the
Expand Down Expand Up @@ -831,9 +788,30 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignore
}
start_stack = pop_value(start_stack);
}
// Populate any NULL locals that might be needed:
// Populate any NULL locals that the compiler might have "proven" to exist
// in the new location. Rather than crashing or changing co_code, just bind
// None instead:
int empty = 0;
for (int i = 0; i < f->f_frame->f_code->co_nlocalsplus; i++) {
maybe_bind_fast_local(f->f_frame, i);
// Counting every unbound local is overly-cautious, but a full flow
// analysis (like we do in the compiler) is probably too expensive:
empty += f->f_frame->localsplus[i] == NULL;
}
if (empty) {
const char *e = "assigning None to %d unbound local%s";
const char *s = (empty == 1) ? "" : "s";
if (PyErr_WarnFormat(PyExc_RuntimeWarning, 0, e, empty, s)) {
return -1;
}
// Do this in a second pass to avoid writing a bunch of Nones when
// warnings are being treated as errors and the previous bit raises:
for (int i = 0; i < f->f_frame->f_code->co_nlocalsplus; i++) {
if (f->f_frame->localsplus[i] == NULL) {
f->f_frame->localsplus[i] = Py_NewRef(Py_None);
empty--;
}
}
assert(empty == 0);
}
/* Finally set the new lasti and return OK. */
f->f_lineno = 0;
Expand Down Expand Up @@ -1117,6 +1095,31 @@ PyFrame_New(PyThreadState *tstate, PyCodeObject *code,
return f;
}

static int
_PyFrame_OpAlreadyRan(_PyInterpreterFrame *frame, int opcode, int oparg)
{
// This only works when opcode is a non-quickened form:
assert(_PyOpcode_Deopt[opcode] == opcode);
int check_oparg = 0;
for (_Py_CODEUNIT *instruction = _PyCode_CODE(frame->f_code);
instruction < frame->prev_instr; instruction++)
{
int check_opcode = _PyOpcode_Deopt[_Py_OPCODE(*instruction)];
check_oparg |= _Py_OPARG(*instruction);
if (check_opcode == opcode && check_oparg == oparg) {
return 1;
}
if (check_opcode == EXTENDED_ARG) {
check_oparg <<= 8;
}
else {
check_oparg = 0;
}
instruction += _PyOpcode_Caches[check_opcode];
}
return 0;
}

int
_PyFrame_FastToLocalsWithError(_PyInterpreterFrame *frame) {
/* Merge fast locals into f->f_locals */
Expand Down Expand Up @@ -1179,8 +1182,7 @@ _PyFrame_FastToLocalsWithError(_PyInterpreterFrame *frame) {
// run yet.
if (value != NULL) {
if (PyCell_Check(value) &&
op_exists(co, MAKE_CELL, i, 0, lasti))
{
_PyFrame_OpAlreadyRan(frame, MAKE_CELL, i)) {
// (likely) MAKE_CELL must have executed already.
value = PyCell_GET(value);
}
Expand Down Expand Up @@ -1254,7 +1256,6 @@ _PyFrame_LocalsToFast(_PyInterpreterFrame *frame, int clear)
}
fast = _PyFrame_GetLocalsArray(frame);
co = frame->f_code;
int lasti = _PyInterpreterFrame_LASTI(frame);

PyErr_Fetch(&error_type, &error_value, &error_traceback);
for (int i = 0; i < co->co_nlocalsplus; i++) {
Expand Down Expand Up @@ -1283,8 +1284,8 @@ _PyFrame_LocalsToFast(_PyInterpreterFrame *frame, int clear)
}
else if (kind & CO_FAST_CELL && oldvalue != NULL) {
/* Same test as in PyFrame_FastToLocals() above. */
if (PyCell_Check(oldvalue) && op_exists(co, MAKE_CELL, i, 0, lasti))
{
if (PyCell_Check(oldvalue) &&
_PyFrame_OpAlreadyRan(frame, MAKE_CELL, i)) {
// (likely) MAKE_CELL must have executed already.
cell = oldvalue;
}
Expand All @@ -1300,10 +1301,18 @@ _PyFrame_LocalsToFast(_PyInterpreterFrame *frame, int clear)
}
}
else if (value != oldvalue) {
if (value == NULL) {
// Probably can't delete this, since the compiler's flow
// analysis may have already "proved" that it exists here:
const char *e = "assigning None to unbound local %R";
if (PyErr_WarnFormat(PyExc_RuntimeWarning, 0, e, name)) {
// It's okay if frame_obj is NULL, just try anyways:
PyErr_WriteUnraisable((PyObject *)frame->frame_obj);
}
value = Py_NewRef(Py_None);
Comment thread
brandtbucher marked this conversation as resolved.
}
Py_XINCREF(value);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Py_XINCREF(value);
Py_INCREF(value);

Py_XSETREF(fast[i], value);
// Populate any NULL locals that might be needed:
maybe_bind_fast_local(frame, i);
}
Py_XDECREF(value);
}
Expand Down