-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
gh-104240: make _PyCompile_CodeGen support different compilation modes #104241
Changes from 3 commits
0630b4e
52f3a59
7f654d7
de8b2ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1655,16 +1655,10 @@ compiler_body(struct compiler *c, location loc, asdl_stmt_seq *stmts) | |
static int | ||
compiler_codegen(struct compiler *c, mod_ty mod) | ||
{ | ||
_Py_DECLARE_STR(anon_module, "<module>"); | ||
RETURN_IF_ERROR( | ||
compiler_enter_scope(c, &_Py_STR(anon_module), COMPILER_SCOPE_MODULE, | ||
mod, 1)); | ||
|
||
location loc = LOCATION(1, 1, 0, 0); | ||
switch (mod->kind) { | ||
case Module_kind: | ||
if (compiler_body(c, loc, mod->v.Module.body) < 0) { | ||
compiler_exit_scope(c); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit confused by this; how does this not leave us with one fewer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think currently we may have one too many - one exit here and then we return to the caller which exits again. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually I'm wrong in the case of compiler_mod, I'll fix it there. |
||
return ERROR; | ||
} | ||
break; | ||
|
@@ -1691,6 +1685,11 @@ static PyCodeObject * | |
compiler_mod(struct compiler *c, mod_ty mod) | ||
{ | ||
int addNone = mod->kind != Expression_kind; | ||
_Py_DECLARE_STR(anon_module, "<module>"); | ||
if (compiler_enter_scope(c, &_Py_STR(anon_module), COMPILER_SCOPE_MODULE, | ||
mod, 1) < 0) { | ||
return NULL; | ||
} | ||
if (compiler_codegen(c, mod) < 0) { | ||
return NULL; | ||
} | ||
|
@@ -7258,42 +7257,57 @@ cfg_to_instructions(cfg_builder *g) | |
|
||
PyObject * | ||
_PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags, | ||
int optimize) | ||
int optimize, int compile_mode) | ||
{ | ||
PyObject *res = NULL; | ||
struct compiler *c = NULL; | ||
PyArena *arena = NULL; | ||
|
||
if (!PyAST_Check(ast)) { | ||
PyErr_SetString(PyExc_TypeError, "expected an AST"); | ||
return NULL; | ||
} | ||
|
||
PyArena *arena = _PyArena_New(); | ||
arena = _PyArena_New(); | ||
if (arena == NULL) { | ||
return NULL; | ||
goto end; | ||
} | ||
|
||
mod_ty mod = PyAST_obj2mod(ast, arena, 0 /* exec */); | ||
mod_ty mod = PyAST_obj2mod(ast, arena, compile_mode); | ||
if (mod == NULL || !_PyAST_Validate(mod)) { | ||
_PyArena_Free(arena); | ||
return NULL; | ||
goto end; | ||
} | ||
|
||
struct compiler *c = new_compiler(mod, filename, pflags, optimize, arena); | ||
c = new_compiler(mod, filename, pflags, optimize, arena); | ||
if (c == NULL) { | ||
_PyArena_Free(arena); | ||
return NULL; | ||
goto end; | ||
} | ||
|
||
_Py_DECLARE_STR(anon_module, "<module>"); | ||
if (compiler_enter_scope(c, &_Py_STR(anon_module), COMPILER_SCOPE_MODULE, | ||
mod, 1) < 0) { | ||
goto end; | ||
} | ||
|
||
if (compiler_codegen(c, mod) < 0) { | ||
goto finally; | ||
} | ||
int addNone = mod->kind != Expression_kind; | ||
if (add_return_at_end(c, addNone) < 0) { | ||
return NULL; | ||
} | ||
|
||
res = instr_sequence_to_instructions(INSTR_SEQUENCE(c)); | ||
|
||
finally: | ||
compiler_exit_scope(c); | ||
compiler_free(c); | ||
_PyArena_Free(arena); | ||
end: | ||
if (c != NULL) { | ||
compiler_free(c); | ||
} | ||
if (arena != NULL) { | ||
_PyArena_Free(arena); | ||
} | ||
return res; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose of moving this from here out into the (only) two callers of this function? It duplicates this line of code, but it doesn't seem like it would make any other difference, AFAICT.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The purpose is to be able to make a distinction between compiler_enter_scope failed, in which case we don't need to exit the scope, and the case where compiler_enter_scope succeeded but compiler_codegen failed later, in which case we do need to exit the scope. I don't think this is currently correct in all cases in main.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense! Beyond adding the compilation modes option, I wasn't clear where this PR was aiming to maintain behavior vs fixing problems in existing behavior.
Looking at the definition of
compiler_exit_scope
, it seems like if we have one too many it would leave things in an observably broken state (like too smallc_nestlevel
?) Is there somewhere we can add asserts to show that things are broken today, but not after this fix?Also, it seems like it would be simplest to get the enter/exit logic correct if we do it just once, inside
compiler_codegen
, rather than in both callers?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm looking into it, I think it's a bigger mess than I previously thought. There are functions like compile_class which always exit the scope (which they did not enter). And VISIT_SEQ/VISIT_SEQ_IN_SCOPE also call compiler_exit_scope.
I don't think we can exit the scope in compiler_codegen, because we need it until after optimize_and_assemble.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to revert the commit with the enter/exit changes for now - I probably only needed it in a broken state when I was sending bad inputs to the unit tests.