@@ -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)
165166static int
166167fold_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
548552static int
549553fold_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)
644651static int
645652fold_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
11591172int
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 ();
0 commit comments