Skip to content

Commit 0630b4e

Browse files
committed
move compiler_enter_scope out of compiler_codegen
1 parent f508800 commit 0630b4e

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

Python/compile.c

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,16 +1655,10 @@ compiler_body(struct compiler *c, location loc, asdl_stmt_seq *stmts)
16551655
static int
16561656
compiler_codegen(struct compiler *c, mod_ty mod)
16571657
{
1658-
_Py_DECLARE_STR(anon_module, "<module>");
1659-
RETURN_IF_ERROR(
1660-
compiler_enter_scope(c, &_Py_STR(anon_module), COMPILER_SCOPE_MODULE,
1661-
mod, 1));
1662-
16631658
location loc = LOCATION(1, 1, 0, 0);
16641659
switch (mod->kind) {
16651660
case Module_kind:
16661661
if (compiler_body(c, loc, mod->v.Module.body) < 0) {
1667-
compiler_exit_scope(c);
16681662
return ERROR;
16691663
}
16701664
break;
@@ -1691,6 +1685,11 @@ static PyCodeObject *
16911685
compiler_mod(struct compiler *c, mod_ty mod)
16921686
{
16931687
int addNone = mod->kind != Expression_kind;
1688+
_Py_DECLARE_STR(anon_module, "<module>");
1689+
if (compiler_enter_scope(c, &_Py_STR(anon_module), COMPILER_SCOPE_MODULE,
1690+
mod, 1) < 0) {
1691+
return NULL;
1692+
}
16941693
if (compiler_codegen(c, mod) < 0) {
16951694
return NULL;
16961695
}
@@ -7261,27 +7260,33 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
72617260
int optimize)
72627261
{
72637262
PyObject *res = NULL;
7263+
struct compiler *c = NULL;
7264+
PyArena *arena = NULL;
72647265

72657266
if (!PyAST_Check(ast)) {
72667267
PyErr_SetString(PyExc_TypeError, "expected an AST");
72677268
return NULL;
72687269
}
72697270

7270-
PyArena *arena = _PyArena_New();
7271+
arena = _PyArena_New();
72717272
if (arena == NULL) {
7272-
return NULL;
7273+
goto end;
72737274
}
72747275

72757276
mod_ty mod = PyAST_obj2mod(ast, arena, 0 /* exec */);
72767277
if (mod == NULL || !_PyAST_Validate(mod)) {
7277-
_PyArena_Free(arena);
7278-
return NULL;
7278+
goto end;
72797279
}
72807280

7281-
struct compiler *c = new_compiler(mod, filename, pflags, optimize, arena);
7281+
c = new_compiler(mod, filename, pflags, optimize, arena);
72827282
if (c == NULL) {
7283-
_PyArena_Free(arena);
7284-
return NULL;
7283+
goto end;
7284+
}
7285+
7286+
_Py_DECLARE_STR(anon_module, "<module>");
7287+
if (compiler_enter_scope(c, &_Py_STR(anon_module), COMPILER_SCOPE_MODULE,
7288+
mod, 1) < 0) {
7289+
goto end;
72857290
}
72867291

72877292
if (compiler_codegen(c, mod) < 0) {
@@ -7292,8 +7297,13 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
72927297

72937298
finally:
72947299
compiler_exit_scope(c);
7295-
compiler_free(c);
7296-
_PyArena_Free(arena);
7300+
end:
7301+
if (c != NULL) {
7302+
compiler_free(c);
7303+
}
7304+
if (arena != NULL) {
7305+
_PyArena_Free(arena);
7306+
}
72977307
return res;
72987308
}
72997309

0 commit comments

Comments
 (0)