Skip to content

Commit 480ac8e

Browse files
committed
always run ast optimizer
1 parent 51cdf5a commit 480ac8e

6 files changed

Lines changed: 49 additions & 43 deletions

File tree

Include/internal/pycore_compile.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@ extern int _PyCompile_AstOptimize(
3131
PyObject *filename,
3232
PyCompilerFlags *flags,
3333
int optimize,
34-
struct _arena *arena);
34+
struct _arena *arena,
35+
int syntax_check_only);
3536

3637
extern int _PyAST_Optimize(
3738
struct _mod *,
3839
struct _arena *arena,
3940
PyObject *filename,
4041
int optimize,
41-
int ff_features);
42+
int ff_features,
43+
int syntax_check_only);
4244

4345

4446
typedef struct {

Lib/test/test___all__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def check_all(self, modname):
3737
(".* (module|package)", DeprecationWarning),
3838
(".* (module|package)", PendingDeprecationWarning),
3939
("", ResourceWarning),
40+
("", SyntaxWarning),
4041
quiet=True):
4142
try:
4243
exec("import %s" % modname, names)

Python/ast_opt.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ typedef struct {
1818
PyObject *filename;
1919
int optimize;
2020
int ff_features;
21+
int syntax_check_only;
2122

2223
int recursion_depth; /* current recursion depth */
2324
int recursion_limit; /* recursion limit */
@@ -165,6 +166,9 @@ unary_not(PyObject *v)
165166
static int
166167
fold_unaryop(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
167168
{
169+
if (state->syntax_check_only) {
170+
return 1;
171+
}
168172
expr_ty arg = node->v.UnaryOp.operand;
169173

170174
if (arg->kind != Constant_kind) {
@@ -548,6 +552,9 @@ optimize_format(expr_ty node, PyObject *fmt, asdl_expr_seq *elts, PyArena *arena
548552
static int
549553
fold_binop(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
550554
{
555+
if (state->syntax_check_only) {
556+
return 1;
557+
}
551558
expr_ty lhs, rhs;
552559
lhs = node->v.BinOp.left;
553560
rhs = node->v.BinOp.right;
@@ -644,6 +651,9 @@ make_const_tuple(asdl_expr_seq *elts)
644651
static int
645652
fold_tuple(expr_ty node, PyArena *arena, _PyASTOptimizeState *state)
646653
{
654+
if (state->syntax_check_only) {
655+
return 1;
656+
}
647657
PyObject *newval;
648658

649659
if (node->v.Tuple.ctx != Load)
@@ -849,6 +859,9 @@ astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
849859
CALL(fold_tuple, expr_ty, node_);
850860
break;
851861
case Name_kind:
862+
if (state->syntax_check_only) {
863+
break;
864+
}
852865
if (node_->v.Name.ctx == Load &&
853866
_PyUnicode_EqualToASCIIString(node_->v.Name.id, "__debug__")) {
854867
LEAVE_RECURSIVE(state);
@@ -1158,7 +1171,7 @@ astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTOptimizeState *stat
11581171

11591172
int
11601173
_PyAST_Optimize(mod_ty mod, PyArena *arena, PyObject *filename, int optimize,
1161-
int ff_features)
1174+
int ff_features, int syntax_check_only)
11621175
{
11631176
PyThreadState *tstate;
11641177
int starting_recursion_depth;
@@ -1168,6 +1181,7 @@ _PyAST_Optimize(mod_ty mod, PyArena *arena, PyObject *filename, int optimize,
11681181
state.filename = filename;
11691182
state.optimize = optimize;
11701183
state.ff_features = ff_features;
1184+
state.syntax_check_only = syntax_check_only;
11711185

11721186
/* Setup recursion depth check counters */
11731187
tstate = _PyThreadState_GET();

Python/bltinmodule.c

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -834,45 +834,35 @@ builtin_compile_impl(PyObject *module, PyObject *source, PyObject *filename,
834834
if (is_ast == -1)
835835
goto error;
836836
if (is_ast) {
837-
if ((flags & PyCF_OPTIMIZED_AST) == PyCF_ONLY_AST) {
838-
if (PyAst_CheckMode(source, compile_mode) < 0) {
839-
goto error;
840-
}
841-
// return an un-optimized AST
842-
result = Py_NewRef(source);
837+
PyArena *arena = _PyArena_New();
838+
if (arena == NULL) {
839+
goto error;
843840
}
844-
else {
845-
// Return an optimized AST or code object
846841

847-
PyArena *arena = _PyArena_New();
848-
if (arena == NULL) {
842+
if (flags & PyCF_ONLY_AST) {
843+
mod_ty mod = PyAST_obj2mod(source, arena, compile_mode);
844+
if (mod == NULL || !_PyAST_Validate(mod)) {
845+
_PyArena_Free(arena);
849846
goto error;
850847
}
851-
852-
if (flags & PyCF_ONLY_AST) {
853-
mod_ty mod = PyAST_obj2mod(source, arena, compile_mode);
854-
if (mod == NULL || !_PyAST_Validate(mod)) {
855-
_PyArena_Free(arena);
856-
goto error;
857-
}
858-
if (_PyCompile_AstOptimize(mod, filename, &cf, optimize,
859-
arena) < 0) {
860-
_PyArena_Free(arena);
861-
goto error;
862-
}
863-
result = PyAST_mod2obj(mod);
848+
int syntax_check_only = ((flags & PyCF_OPTIMIZED_AST) == PyCF_ONLY_AST); /* unoptiomized AST */
849+
if (_PyCompile_AstOptimize(mod, filename, &cf, optimize,
850+
arena, syntax_check_only) < 0) {
851+
_PyArena_Free(arena);
852+
goto error;
864853
}
865-
else {
866-
mod_ty mod = PyAST_obj2mod(source, arena, compile_mode);
867-
if (mod == NULL || !_PyAST_Validate(mod)) {
868-
_PyArena_Free(arena);
869-
goto error;
870-
}
871-
result = (PyObject*)_PyAST_Compile(mod, filename,
872-
&cf, optimize, arena);
854+
result = PyAST_mod2obj(mod);
855+
}
856+
else {
857+
mod_ty mod = PyAST_obj2mod(source, arena, compile_mode);
858+
if (mod == NULL || !_PyAST_Validate(mod)) {
859+
_PyArena_Free(arena);
860+
goto error;
873861
}
874-
_PyArena_Free(arena);
862+
result = (PyObject*)_PyAST_Compile(mod, filename,
863+
&cf, optimize, arena);
875864
}
865+
_PyArena_Free(arena);
876866
goto finally;
877867
}
878868

Python/compile.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ compiler_setup(compiler *c, mod_ty mod, PyObject *filename,
126126
c->c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
127127
c->c_save_nested_seqs = false;
128128

129-
if (!_PyAST_Optimize(mod, arena, filename, c->c_optimize, merged)) {
129+
if (!_PyAST_Optimize(mod, arena, filename, c->c_optimize, merged, 0)) {
130130
return ERROR;
131131
}
132132
c->c_st = _PySymtable_Build(mod, filename, &c->c_future);
@@ -1387,7 +1387,7 @@ _PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *pflags,
13871387

13881388
int
13891389
_PyCompile_AstOptimize(mod_ty mod, PyObject *filename, PyCompilerFlags *cf,
1390-
int optimize, PyArena *arena)
1390+
int optimize, PyArena *arena, int no_const_folding)
13911391
{
13921392
_PyFutureFeatures future;
13931393
if (!_PyFuture_FromAST(mod, filename, &future)) {
@@ -1397,7 +1397,7 @@ _PyCompile_AstOptimize(mod_ty mod, PyObject *filename, PyCompilerFlags *cf,
13971397
if (optimize == -1) {
13981398
optimize = _Py_GetConfig()->optimization_level;
13991399
}
1400-
if (!_PyAST_Optimize(mod, arena, filename, optimize, flags)) {
1400+
if (!_PyAST_Optimize(mod, arena, filename, optimize, flags, no_const_folding)) {
14011401
return -1;
14021402
}
14031403
return 0;

Python/pythonrun.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,11 +1450,10 @@ Py_CompileStringObject(const char *str, PyObject *filename, int start,
14501450
return NULL;
14511451
}
14521452
if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
1453-
if ((flags->cf_flags & PyCF_OPTIMIZED_AST) == PyCF_OPTIMIZED_AST) {
1454-
if (_PyCompile_AstOptimize(mod, filename, flags, optimize, arena) < 0) {
1455-
_PyArena_Free(arena);
1456-
return NULL;
1457-
}
1453+
int syntax_check_only = ((flags->cf_flags & PyCF_OPTIMIZED_AST) == PyCF_ONLY_AST); /* unoptiomized AST */
1454+
if (_PyCompile_AstOptimize(mod, filename, flags, optimize, arena, syntax_check_only) < 0) {
1455+
_PyArena_Free(arena);
1456+
return NULL;
14581457
}
14591458
PyObject *result = PyAST_mod2obj(mod);
14601459
_PyArena_Free(arena);

0 commit comments

Comments
 (0)