-
-
Notifications
You must be signed in to change notification settings - Fork 32.9k
GH-93143: Don't turn LOAD_FAST
into LOAD_FAST_CHECK
#99075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e0f2459
Just assign None instead of changing co_code
brandtbucher 6f68d32
blurb add
brandtbucher dd52da5
Feedback from code review
brandtbucher da07230
fixup
brandtbucher 555d23e
Perform assignments *before* adjusting the stack
brandtbucher 4e597b3
Address code review
brandtbucher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Core and Builtins/2022-10-19-23-54-43.gh-issue-93143.1wCYub.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Rather than changing :attr:`~types.CodeType.co_code`, the interpreter will | ||
now display a :exc:`RuntimeWarning` and assign :const:`None` to any fast | ||
locals that are incorrectly left unbound after jumps or :keyword:`del` | ||
|
||
statements executed while tracing. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -620,42 +620,6 @@ _PyFrame_GetState(PyFrameObject *frame) | |||||
Py_UNREACHABLE(); | ||||||
} | ||||||
|
||||||
static void | ||||||
add_load_fast_null_checks(PyCodeObject *co) | ||||||
{ | ||||||
int changed = 0; | ||||||
_Py_CODEUNIT *instructions = _PyCode_CODE(co); | ||||||
for (Py_ssize_t i = 0; i < Py_SIZE(co); i++) { | ||||||
int opcode = _Py_OPCODE(instructions[i]); | ||||||
switch (opcode) { | ||||||
case LOAD_FAST: | ||||||
case LOAD_FAST__LOAD_FAST: | ||||||
case LOAD_FAST__LOAD_CONST: | ||||||
changed = 1; | ||||||
_Py_SET_OPCODE(instructions[i], LOAD_FAST_CHECK); | ||||||
break; | ||||||
case LOAD_CONST__LOAD_FAST: | ||||||
changed = 1; | ||||||
_Py_SET_OPCODE(instructions[i], LOAD_CONST); | ||||||
break; | ||||||
case STORE_FAST__LOAD_FAST: | ||||||
changed = 1; | ||||||
_Py_SET_OPCODE(instructions[i], STORE_FAST); | ||||||
break; | ||||||
} | ||||||
i += _PyOpcode_Caches[_PyOpcode_Deopt[opcode]]; | ||||||
} | ||||||
if (changed && co->_co_cached != NULL) { | ||||||
// invalidate cached co_code object | ||||||
Py_CLEAR(co->_co_cached->_co_code); | ||||||
Py_CLEAR(co->_co_cached->_co_cellvars); | ||||||
Py_CLEAR(co->_co_cached->_co_freevars); | ||||||
Py_CLEAR(co->_co_cached->_co_varnames); | ||||||
PyMem_Free(co->_co_cached); | ||||||
co->_co_cached = NULL; | ||||||
} | ||||||
} | ||||||
|
||||||
brandtbucher marked this conversation as resolved.
Show resolved
Hide 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 | ||||||
|
@@ -745,8 +709,6 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignore | |||||
return -1; | ||||||
} | ||||||
|
||||||
add_load_fast_null_checks(f->f_frame->f_code); | ||||||
|
||||||
/* PyCode_NewWithPosOnlyArgs limits co_code to be under INT_MAX so this | ||||||
* should never overflow. */ | ||||||
int len = (int)Py_SIZE(f->f_frame->f_code); | ||||||
|
@@ -805,6 +767,31 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno, void *Py_UNUSED(ignore | |||||
PyErr_SetString(PyExc_ValueError, msg); | ||||||
return -1; | ||||||
} | ||||||
// 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 unbound = 0; | ||||||
for (int i = 0; i < f->f_frame->f_code->co_nlocalsplus; i++) { | ||||||
// Counting every unbound local is overly-cautious, but a full flow | ||||||
// analysis (like we do in the compiler) is probably too expensive: | ||||||
unbound += f->f_frame->localsplus[i] == NULL; | ||||||
} | ||||||
if (unbound) { | ||||||
const char *e = "assigning None to %d unbound local%s"; | ||||||
const char *s = (unbound == 1) ? "" : "s"; | ||||||
if (PyErr_WarnFormat(PyExc_RuntimeWarning, 0, e, unbound, 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); | ||||||
unbound--; | ||||||
} | ||||||
} | ||||||
assert(unbound == 0); | ||||||
} | ||||||
if (state == FRAME_SUSPENDED) { | ||||||
/* Account for value popped by yield */ | ||||||
start_stack = pop_value(start_stack); | ||||||
|
@@ -1269,7 +1256,6 @@ _PyFrame_LocalsToFast(_PyInterpreterFrame *frame, int clear) | |||||
} | ||||||
fast = _PyFrame_GetLocalsArray(frame); | ||||||
co = frame->f_code; | ||||||
bool added_null_checks = false; | ||||||
|
||||||
PyErr_Fetch(&error_type, &error_value, &error_traceback); | ||||||
for (int i = 0; i < co->co_nlocalsplus; i++) { | ||||||
|
@@ -1289,10 +1275,6 @@ _PyFrame_LocalsToFast(_PyInterpreterFrame *frame, int clear) | |||||
} | ||||||
} | ||||||
PyObject *oldvalue = fast[i]; | ||||||
if (!added_null_checks && oldvalue != NULL && value == NULL) { | ||||||
add_load_fast_null_checks(co); | ||||||
added_null_checks = true; | ||||||
} | ||||||
PyObject *cell = NULL; | ||||||
if (kind == CO_FAST_FREE) { | ||||||
// The cell was set when the frame was created from | ||||||
|
@@ -1319,6 +1301,16 @@ _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 "proven" 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); | ||||||
brandtbucher marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} | ||||||
Py_XINCREF(value); | ||||||
|
Py_XINCREF(value); | |
Py_INCREF(value); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.