@@ -15,6 +15,19 @@ typedef struct {
15
15
int recursion_limit ; /* recursion limit */
16
16
} _PyASTOptimizeState ;
17
17
18
+ #define ENTER_RECURSIVE (ST ) \
19
+ do { \
20
+ if (++(ST)->recursion_depth > (ST)->recursion_limit) { \
21
+ PyErr_SetString(PyExc_RecursionError, \
22
+ "maximum recursion depth exceeded during compilation"); \
23
+ return 0; \
24
+ } \
25
+ } while(0)
26
+
27
+ #define LEAVE_RECURSIVE (ST ) \
28
+ do { \
29
+ --(ST)->recursion_depth; \
30
+ } while(0)
18
31
19
32
static int
20
33
make_const (expr_ty node , PyObject * val , PyArena * arena )
@@ -708,11 +721,7 @@ astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
708
721
static int
709
722
astfold_expr (expr_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
710
723
{
711
- if (++ state -> recursion_depth > state -> recursion_limit ) {
712
- PyErr_SetString (PyExc_RecursionError ,
713
- "maximum recursion depth exceeded during compilation" );
714
- return 0 ;
715
- }
724
+ ENTER_RECURSIVE (state );
716
725
switch (node_ -> kind ) {
717
726
case BoolOp_kind :
718
727
CALL_SEQ (astfold_expr , expr , node_ -> v .BoolOp .values );
@@ -811,7 +820,7 @@ astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
811
820
case Name_kind :
812
821
if (node_ -> v .Name .ctx == Load &&
813
822
_PyUnicode_EqualToASCIIString (node_ -> v .Name .id , "__debug__" )) {
814
- state -> recursion_depth -- ;
823
+ LEAVE_RECURSIVE ( state ) ;
815
824
return make_const (node_ , PyBool_FromLong (!state -> optimize ), ctx_ );
816
825
}
817
826
break ;
@@ -824,7 +833,7 @@ astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
824
833
// No default case, so the compiler will emit a warning if new expression
825
834
// kinds are added without being handled here
826
835
}
827
- state -> recursion_depth -- ;
836
+ LEAVE_RECURSIVE ( state ); ;
828
837
return 1 ;
829
838
}
830
839
@@ -871,11 +880,7 @@ astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
871
880
static int
872
881
astfold_stmt (stmt_ty node_ , PyArena * ctx_ , _PyASTOptimizeState * state )
873
882
{
874
- if (++ state -> recursion_depth > state -> recursion_limit ) {
875
- PyErr_SetString (PyExc_RecursionError ,
876
- "maximum recursion depth exceeded during compilation" );
877
- return 0 ;
878
- }
883
+ ENTER_RECURSIVE (state );
879
884
switch (node_ -> kind ) {
880
885
case FunctionDef_kind :
881
886
CALL_SEQ (astfold_type_param , type_param , node_ -> v .FunctionDef .type_params );
@@ -999,7 +1004,7 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
999
1004
// No default case, so the compiler will emit a warning if new statement
1000
1005
// kinds are added without being handled here
1001
1006
}
1002
- state -> recursion_depth -- ;
1007
+ LEAVE_RECURSIVE ( state ) ;
1003
1008
return 1 ;
1004
1009
}
1005
1010
@@ -1031,11 +1036,7 @@ astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
1031
1036
// Currently, this is really only used to form complex/negative numeric
1032
1037
// constants in MatchValue and MatchMapping nodes
1033
1038
// We still recurse into all subexpressions and subpatterns anyway
1034
- if (++ state -> recursion_depth > state -> recursion_limit ) {
1035
- PyErr_SetString (PyExc_RecursionError ,
1036
- "maximum recursion depth exceeded during compilation" );
1037
- return 0 ;
1038
- }
1039
+ ENTER_RECURSIVE (state );
1039
1040
switch (node_ -> kind ) {
1040
1041
case MatchValue_kind :
1041
1042
CALL (astfold_expr , expr_ty , node_ -> v .MatchValue .value );
@@ -1067,7 +1068,7 @@ astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
1067
1068
// No default case, so the compiler will emit a warning if new pattern
1068
1069
// kinds are added without being handled here
1069
1070
}
1070
- state -> recursion_depth -- ;
1071
+ LEAVE_RECURSIVE ( state ) ;
1071
1072
return 1 ;
1072
1073
}
1073
1074
0 commit comments