Skip to content

Commit 2c2dc61

Browse files
authored
gh-104240: make _PyCompile_CodeGen support different compilation modes (#104241)
1 parent 1b19bd1 commit 2c2dc61

9 files changed

+43
-15
lines changed

Include/internal/pycore_compile.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ PyAPI_FUNC(PyObject*) _PyCompile_CodeGen(
9797
PyObject *ast,
9898
PyObject *filename,
9999
PyCompilerFlags *flags,
100-
int optimize);
100+
int optimize,
101+
int compile_mode);
101102

102103
PyAPI_FUNC(PyObject*) _PyCompile_OptimizeCfg(
103104
PyObject *instructions,

Include/internal/pycore_global_objects_fini_generated.h

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_global_strings.h

+1
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ struct _Py_global_strings {
335335
STRUCT_FOR_ID(code)
336336
STRUCT_FOR_ID(command)
337337
STRUCT_FOR_ID(comment_factory)
338+
STRUCT_FOR_ID(compile_mode)
338339
STRUCT_FOR_ID(consts)
339340
STRUCT_FOR_ID(context)
340341
STRUCT_FOR_ID(cookie)

Include/internal/pycore_runtime_init_generated.h

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_unicodeobject_generated.h

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_compiler_codegen.py

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def test_if_expression(self):
2525
('LOAD_CONST', 2, 1),
2626
exit_lbl,
2727
('POP_TOP', None),
28+
('LOAD_CONST', 3),
29+
('RETURN_VALUE', None),
2830
]
2931
self.codegen_test(snippet, expected)
3032

@@ -46,5 +48,7 @@ def test_for_loop(self):
4648
('JUMP', loop_lbl),
4749
exit_lbl,
4850
('END_FOR', None),
51+
('LOAD_CONST', 0),
52+
('RETURN_VALUE', None),
4953
]
5054
self.codegen_test(snippet, expected)

Modules/_testinternalcapi.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -593,17 +593,19 @@ _testinternalcapi.compiler_codegen -> object
593593
ast: object
594594
filename: object
595595
optimize: int
596+
compile_mode: int = 0
596597
597598
Apply compiler code generation to an AST.
598599
[clinic start generated code]*/
599600

600601
static PyObject *
601602
_testinternalcapi_compiler_codegen_impl(PyObject *module, PyObject *ast,
602-
PyObject *filename, int optimize)
603-
/*[clinic end generated code: output=fbbbbfb34700c804 input=e9fbe6562f7f75e4]*/
603+
PyObject *filename, int optimize,
604+
int compile_mode)
605+
/*[clinic end generated code: output=40a68f6e13951cc8 input=a0e00784f1517cd7]*/
604606
{
605607
PyCompilerFlags *flags = NULL;
606-
return _PyCompile_CodeGen(ast, filename, flags, optimize);
608+
return _PyCompile_CodeGen(ast, filename, flags, optimize, compile_mode);
607609
}
608610

609611

Modules/clinic/_testinternalcapi.c.h

+20-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/compile.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -7258,7 +7258,7 @@ cfg_to_instructions(cfg_builder *g)
72587258

72597259
PyObject *
72607260
_PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
7261-
int optimize)
7261+
int optimize, int compile_mode)
72627262
{
72637263
PyObject *res = NULL;
72647264

@@ -7272,7 +7272,7 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
72727272
return NULL;
72737273
}
72747274

7275-
mod_ty mod = PyAST_obj2mod(ast, arena, 0 /* exec */);
7275+
mod_ty mod = PyAST_obj2mod(ast, arena, compile_mode);
72767276
if (mod == NULL || !_PyAST_Validate(mod)) {
72777277
_PyArena_Free(arena);
72787278
return NULL;
@@ -7287,6 +7287,10 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
72877287
if (compiler_codegen(c, mod) < 0) {
72887288
goto finally;
72897289
}
7290+
int addNone = mod->kind != Expression_kind;
7291+
if (add_return_at_end(c, addNone) < 0) {
7292+
return NULL;
7293+
}
72907294

72917295
res = instr_sequence_to_instructions(INSTR_SEQUENCE(c));
72927296

0 commit comments

Comments
 (0)