Skip to content

Commit

Permalink
pythongh-104240: make _PyCompile_CodeGen support different compilatio…
Browse files Browse the repository at this point in the history
…n modes (python#104241)
  • Loading branch information
iritkatriel authored and jbower-fb committed May 8, 2023
1 parent 86ca8d2 commit c195a2c
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Include/internal/pycore_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ PyAPI_FUNC(PyObject*) _PyCompile_CodeGen(
PyObject *ast,
PyObject *filename,
PyCompilerFlags *flags,
int optimize);
int optimize,
int compile_mode);

PyAPI_FUNC(PyObject*) _PyCompile_OptimizeCfg(
PyObject *instructions,
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_global_objects_fini_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(code)
STRUCT_FOR_ID(command)
STRUCT_FOR_ID(comment_factory)
STRUCT_FOR_ID(compile_mode)
STRUCT_FOR_ID(consts)
STRUCT_FOR_ID(context)
STRUCT_FOR_ID(cookie)
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_runtime_init_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Include/internal/pycore_unicodeobject_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Lib/test/test_compiler_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def test_if_expression(self):
('LOAD_CONST', 2, 1),
exit_lbl,
('POP_TOP', None),
('LOAD_CONST', 3),
('RETURN_VALUE', None),
]
self.codegen_test(snippet, expected)

Expand All @@ -46,5 +48,7 @@ def test_for_loop(self):
('JUMP', loop_lbl),
exit_lbl,
('END_FOR', None),
('LOAD_CONST', 0),
('RETURN_VALUE', None),
]
self.codegen_test(snippet, expected)
8 changes: 5 additions & 3 deletions Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -593,17 +593,19 @@ _testinternalcapi.compiler_codegen -> object
ast: object
filename: object
optimize: int
compile_mode: int = 0
Apply compiler code generation to an AST.
[clinic start generated code]*/

static PyObject *
_testinternalcapi_compiler_codegen_impl(PyObject *module, PyObject *ast,
PyObject *filename, int optimize)
/*[clinic end generated code: output=fbbbbfb34700c804 input=e9fbe6562f7f75e4]*/
PyObject *filename, int optimize,
int compile_mode)
/*[clinic end generated code: output=40a68f6e13951cc8 input=a0e00784f1517cd7]*/
{
PyCompilerFlags *flags = NULL;
return _PyCompile_CodeGen(ast, filename, flags, optimize);
return _PyCompile_CodeGen(ast, filename, flags, optimize, compile_mode);
}


Expand Down
29 changes: 20 additions & 9 deletions Modules/clinic/_testinternalcapi.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -7258,7 +7258,7 @@ 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;

Expand All @@ -7272,7 +7272,7 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
return NULL;
}

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;
Expand All @@ -7287,6 +7287,10 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
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));

Expand Down

0 comments on commit c195a2c

Please sign in to comment.