| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | /* AST pre-processing */ | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  | #include "Python.h"
 | 
					
						
							| 
									
										
										
										
											2021-03-17 23:50:50 +01:00
										 |  |  | #include "pycore_ast.h"           // _PyAST_GetDocString()
 | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  | #include "pycore_c_array.h"       // _Py_CArray_EnsureCapacity()
 | 
					
						
							| 
									
										
										
										
											2021-05-08 22:33:10 +03:00
										 |  |  | #include "pycore_format.h"        // F_LJUST
 | 
					
						
							| 
									
										
										
										
											2025-03-17 12:32:43 +01:00
										 |  |  | #include "pycore_runtime.h"       // _Py_STR()
 | 
					
						
							|  |  |  | #include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString()
 | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  | /* See PEP 765 */ | 
					
						
							| 
									
										
										
										
											2023-05-31 20:21:46 +01:00
										 |  |  | typedef struct { | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |     bool in_finally; | 
					
						
							|  |  |  |     bool in_funcdef; | 
					
						
							|  |  |  |     bool in_loop; | 
					
						
							|  |  |  | } ControlFlowInFinallyContext; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct { | 
					
						
							|  |  |  |     PyObject *filename; | 
					
						
							| 
									
										
										
										
											2023-05-31 20:21:46 +01:00
										 |  |  |     int optimize; | 
					
						
							|  |  |  |     int ff_features; | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |     int syntax_check_only; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-31 16:29:23 +03:00
										 |  |  |     _Py_c_array_t cf_finally;       /* context for PEP 765 check */ | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |     int cf_finally_used; | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | } _PyASTPreprocessState; | 
					
						
							| 
									
										
										
										
											2023-05-31 20:21:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-25 09:24:48 +00:00
										 |  |  | #define ENTER_RECURSIVE() \
 | 
					
						
							|  |  |  | if (Py_EnterRecursiveCall(" during compilation")) { \ | 
					
						
							|  |  |  |     return 0; \ | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-08-03 12:45:45 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-25 09:24:48 +00:00
										 |  |  | #define LEAVE_RECURSIVE() Py_LeaveRecursiveCall();
 | 
					
						
							| 
									
										
										
										
											2023-05-31 20:21:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  | static ControlFlowInFinallyContext* | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | get_cf_finally_top(_PyASTPreprocessState *state) | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     int idx = state->cf_finally_used; | 
					
						
							|  |  |  |     return ((ControlFlowInFinallyContext*)state->cf_finally.array) + idx; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | push_cf_context(_PyASTPreprocessState *state, stmt_ty node, bool finally, bool funcdef, bool loop) | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     if (_Py_CArray_EnsureCapacity(&state->cf_finally, state->cf_finally_used+1) < 0) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     state->cf_finally_used++; | 
					
						
							|  |  |  |     ControlFlowInFinallyContext *ctx = get_cf_finally_top(state); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx->in_finally = finally; | 
					
						
							|  |  |  |     ctx->in_funcdef = funcdef; | 
					
						
							|  |  |  |     ctx->in_loop = loop; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | pop_cf_context(_PyASTPreprocessState *state) | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     assert(state->cf_finally_used > 0); | 
					
						
							|  |  |  |     state->cf_finally_used--; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | control_flow_in_finally_warning(const char *kw, stmt_ty n, _PyASTPreprocessState *state) | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     PyObject *msg = PyUnicode_FromFormat("'%s' in a 'finally' block", kw); | 
					
						
							|  |  |  |     if (msg == NULL) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     int ret = _PyErr_EmitSyntaxWarning(msg, state->filename, n->lineno, | 
					
						
							|  |  |  |                                        n->col_offset + 1, n->end_lineno, | 
					
						
							|  |  |  |                                        n->end_col_offset + 1); | 
					
						
							|  |  |  |     Py_DECREF(msg); | 
					
						
							|  |  |  |     return ret < 0 ? 0 : 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | before_return(_PyASTPreprocessState *state, stmt_ty node_) | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     if (state->cf_finally_used > 0) { | 
					
						
							|  |  |  |         ControlFlowInFinallyContext *ctx = get_cf_finally_top(state); | 
					
						
							|  |  |  |         if (ctx->in_finally && ! ctx->in_funcdef) { | 
					
						
							|  |  |  |             if (!control_flow_in_finally_warning("return", node_, state)) { | 
					
						
							|  |  |  |                 return 0; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | before_loop_exit(_PyASTPreprocessState *state, stmt_ty node_, const char *kw) | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  | { | 
					
						
							|  |  |  |     if (state->cf_finally_used > 0) { | 
					
						
							|  |  |  |         ControlFlowInFinallyContext *ctx = get_cf_finally_top(state); | 
					
						
							|  |  |  |         if (ctx->in_finally && ! ctx->in_loop) { | 
					
						
							|  |  |  |             if (!control_flow_in_finally_warning(kw, node_, state)) { | 
					
						
							|  |  |  |                 return 0; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define PUSH_CONTEXT(S, N, FINALLY, FUNCDEF, LOOP) \
 | 
					
						
							|  |  |  |     if (!push_cf_context((S), (N), (FINALLY), (FUNCDEF), (LOOP))) { \ | 
					
						
							|  |  |  |         return 0; \ | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define POP_CONTEXT(S) pop_cf_context(S)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define BEFORE_FINALLY(S, N)    PUSH_CONTEXT((S), (N), true, false, false)
 | 
					
						
							|  |  |  | #define AFTER_FINALLY(S)        POP_CONTEXT(S)
 | 
					
						
							|  |  |  | #define BEFORE_FUNC_BODY(S, N)  PUSH_CONTEXT((S), (N), false, true, false)
 | 
					
						
							|  |  |  | #define AFTER_FUNC_BODY(S)      POP_CONTEXT(S)
 | 
					
						
							|  |  |  | #define BEFORE_LOOP_BODY(S, N)  PUSH_CONTEXT((S), (N), false, false, true)
 | 
					
						
							|  |  |  | #define AFTER_LOOP_BODY(S)      POP_CONTEXT(S)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define BEFORE_RETURN(S, N) \
 | 
					
						
							|  |  |  |     if (!before_return((S), (N))) { \ | 
					
						
							|  |  |  |         return 0; \ | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define BEFORE_LOOP_EXIT(S, N, KW) \
 | 
					
						
							|  |  |  |     if (!before_loop_exit((S), (N), (KW))) { \ | 
					
						
							|  |  |  |         return 0; \ | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  | static int | 
					
						
							|  |  |  | make_const(expr_ty node, PyObject *val, PyArena *arena) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-11-07 22:35:17 +10:00
										 |  |  |     // Even if no new value was calculated, make_const may still
 | 
					
						
							|  |  |  |     // need to clear an error (e.g. for division by zero)
 | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |     if (val == NULL) { | 
					
						
							|  |  |  |         if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) { | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         PyErr_Clear(); | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-24 02:23:01 +01:00
										 |  |  |     if (_PyArena_AddPyObject(arena, val) < 0) { | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         Py_DECREF(val); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     node->kind = Constant_kind; | 
					
						
							| 
									
										
										
										
											2020-04-14 21:40:41 +01:00
										 |  |  |     node->v.Constant.kind = NULL; | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |     node->v.Constant.value = val; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define COPY_NODE(TO, FROM) (memcpy((TO), (FROM), sizeof(struct _expr)))
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-23 19:06:48 +03:00
										 |  |  | static int | 
					
						
							|  |  |  | has_starred(asdl_expr_seq *elts) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_ssize_t n = asdl_seq_LEN(elts); | 
					
						
							|  |  |  |     for (Py_ssize_t i = 0; i < n; i++) { | 
					
						
							|  |  |  |         expr_ty e = (expr_ty)asdl_seq_GET(elts, i); | 
					
						
							|  |  |  |         if (e->kind == Starred_kind) { | 
					
						
							|  |  |  |             return 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-08 22:33:10 +03:00
										 |  |  | static expr_ty | 
					
						
							|  |  |  | parse_literal(PyObject *fmt, Py_ssize_t *ppos, PyArena *arena) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     const void *data = PyUnicode_DATA(fmt); | 
					
						
							|  |  |  |     int kind = PyUnicode_KIND(fmt); | 
					
						
							|  |  |  |     Py_ssize_t size = PyUnicode_GET_LENGTH(fmt); | 
					
						
							|  |  |  |     Py_ssize_t start, pos; | 
					
						
							|  |  |  |     int has_percents = 0; | 
					
						
							|  |  |  |     start = pos = *ppos; | 
					
						
							|  |  |  |     while (pos < size) { | 
					
						
							|  |  |  |         if (PyUnicode_READ(kind, data, pos) != '%') { | 
					
						
							|  |  |  |             pos++; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (pos+1 < size && PyUnicode_READ(kind, data, pos+1) == '%') { | 
					
						
							|  |  |  |             has_percents = 1; | 
					
						
							|  |  |  |             pos += 2; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     *ppos = pos; | 
					
						
							|  |  |  |     if (pos == start) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     PyObject *str = PyUnicode_Substring(fmt, start, pos); | 
					
						
							|  |  |  |     /* str = str.replace('%%', '%') */ | 
					
						
							|  |  |  |     if (str && has_percents) { | 
					
						
							| 
									
										
										
										
											2022-02-14 17:36:51 -07:00
										 |  |  |         _Py_DECLARE_STR(dbl_percent, "%%"); | 
					
						
							| 
									
										
										
										
											2022-02-08 13:39:07 -07:00
										 |  |  |         Py_SETREF(str, PyUnicode_Replace(str, &_Py_STR(dbl_percent), | 
					
						
							| 
									
										
										
										
											2024-06-21 17:19:31 +02:00
										 |  |  |                                          _Py_LATIN1_CHR('%'), -1)); | 
					
						
							| 
									
										
										
										
											2021-05-08 22:33:10 +03:00
										 |  |  |     } | 
					
						
							|  |  |  |     if (!str) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (_PyArena_AddPyObject(arena, str) < 0) { | 
					
						
							|  |  |  |         Py_DECREF(str); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return _PyAST_Constant(str, NULL, -1, -1, -1, -1, arena); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define MAXDIGITS 3
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | simple_format_arg_parse(PyObject *fmt, Py_ssize_t *ppos, | 
					
						
							|  |  |  |                         int *spec, int *flags, int *width, int *prec) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_ssize_t pos = *ppos, len = PyUnicode_GET_LENGTH(fmt); | 
					
						
							|  |  |  |     Py_UCS4 ch; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define NEXTC do {                      \
 | 
					
						
							|  |  |  |     if (pos >= len) {                   \ | 
					
						
							|  |  |  |         return 0;                       \ | 
					
						
							|  |  |  |     }                                   \ | 
					
						
							|  |  |  |     ch = PyUnicode_READ_CHAR(fmt, pos); \ | 
					
						
							|  |  |  |     pos++;                              \ | 
					
						
							|  |  |  | } while (0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     *flags = 0; | 
					
						
							|  |  |  |     while (1) { | 
					
						
							|  |  |  |         NEXTC; | 
					
						
							|  |  |  |         switch (ch) { | 
					
						
							|  |  |  |             case '-': *flags |= F_LJUST; continue; | 
					
						
							|  |  |  |             case '+': *flags |= F_SIGN; continue; | 
					
						
							|  |  |  |             case ' ': *flags |= F_BLANK; continue; | 
					
						
							|  |  |  |             case '#': *flags |= F_ALT; continue; | 
					
						
							|  |  |  |             case '0': *flags |= F_ZERO; continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if ('0' <= ch && ch <= '9') { | 
					
						
							|  |  |  |         *width = 0; | 
					
						
							|  |  |  |         int digits = 0; | 
					
						
							|  |  |  |         while ('0' <= ch && ch <= '9') { | 
					
						
							|  |  |  |             *width = *width * 10 + (ch - '0'); | 
					
						
							|  |  |  |             NEXTC; | 
					
						
							|  |  |  |             if (++digits >= MAXDIGITS) { | 
					
						
							|  |  |  |                 return 0; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ch == '.') { | 
					
						
							|  |  |  |         NEXTC; | 
					
						
							| 
									
										
										
										
											2021-05-23 19:06:48 +03:00
										 |  |  |         *prec = 0; | 
					
						
							| 
									
										
										
										
											2021-05-08 22:33:10 +03:00
										 |  |  |         if ('0' <= ch && ch <= '9') { | 
					
						
							|  |  |  |             int digits = 0; | 
					
						
							|  |  |  |             while ('0' <= ch && ch <= '9') { | 
					
						
							|  |  |  |                 *prec = *prec * 10 + (ch - '0'); | 
					
						
							|  |  |  |                 NEXTC; | 
					
						
							|  |  |  |                 if (++digits >= MAXDIGITS) { | 
					
						
							|  |  |  |                     return 0; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     *spec = ch; | 
					
						
							|  |  |  |     *ppos = pos; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #undef NEXTC
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static expr_ty | 
					
						
							|  |  |  | parse_format(PyObject *fmt, Py_ssize_t *ppos, expr_ty arg, PyArena *arena) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int spec, flags, width = -1, prec = -1; | 
					
						
							|  |  |  |     if (!simple_format_arg_parse(fmt, ppos, &spec, &flags, &width, &prec)) { | 
					
						
							|  |  |  |         // Unsupported format.
 | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (spec == 's' || spec == 'r' || spec == 'a') { | 
					
						
							|  |  |  |         char buf[1 + MAXDIGITS + 1 + MAXDIGITS + 1], *p = buf; | 
					
						
							|  |  |  |         if (!(flags & F_LJUST) && width > 0) { | 
					
						
							|  |  |  |             *p++ = '>'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (width >= 0) { | 
					
						
							|  |  |  |             p += snprintf(p, MAXDIGITS + 1, "%d", width); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (prec >= 0) { | 
					
						
							|  |  |  |             p += snprintf(p, MAXDIGITS + 2, ".%d", prec); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         expr_ty format_spec = NULL; | 
					
						
							|  |  |  |         if (p != buf) { | 
					
						
							|  |  |  |             PyObject *str = PyUnicode_FromString(buf); | 
					
						
							|  |  |  |             if (str == NULL) { | 
					
						
							|  |  |  |                 return NULL; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (_PyArena_AddPyObject(arena, str) < 0) { | 
					
						
							|  |  |  |                 Py_DECREF(str); | 
					
						
							|  |  |  |                 return NULL; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             format_spec = _PyAST_Constant(str, NULL, -1, -1, -1, -1, arena); | 
					
						
							|  |  |  |             if (format_spec == NULL) { | 
					
						
							|  |  |  |                 return NULL; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return _PyAST_FormattedValue(arg, spec, format_spec, | 
					
						
							|  |  |  |                                      arg->lineno, arg->col_offset, | 
					
						
							|  |  |  |                                      arg->end_lineno, arg->end_col_offset, | 
					
						
							|  |  |  |                                      arena); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // Unsupported format.
 | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | optimize_format(expr_ty node, PyObject *fmt, asdl_expr_seq *elts, PyArena *arena) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_ssize_t pos = 0; | 
					
						
							|  |  |  |     Py_ssize_t cnt = 0; | 
					
						
							|  |  |  |     asdl_expr_seq *seq = _Py_asdl_expr_seq_new(asdl_seq_LEN(elts) * 2 + 1, arena); | 
					
						
							|  |  |  |     if (!seq) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     seq->size = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (1) { | 
					
						
							|  |  |  |         expr_ty lit = parse_literal(fmt, &pos, arena); | 
					
						
							|  |  |  |         if (lit) { | 
					
						
							|  |  |  |             asdl_seq_SET(seq, seq->size++, lit); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (PyErr_Occurred()) { | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (pos >= PyUnicode_GET_LENGTH(fmt)) { | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (cnt >= asdl_seq_LEN(elts)) { | 
					
						
							|  |  |  |             // More format units than items.
 | 
					
						
							|  |  |  |             return 1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         assert(PyUnicode_READ_CHAR(fmt, pos) == '%'); | 
					
						
							|  |  |  |         pos++; | 
					
						
							|  |  |  |         expr_ty expr = parse_format(fmt, &pos, asdl_seq_GET(elts, cnt), arena); | 
					
						
							|  |  |  |         cnt++; | 
					
						
							|  |  |  |         if (!expr) { | 
					
						
							|  |  |  |             return !PyErr_Occurred(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         asdl_seq_SET(seq, seq->size++, expr); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (cnt < asdl_seq_LEN(elts)) { | 
					
						
							|  |  |  |         // More items than format units.
 | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     expr_ty res = _PyAST_JoinedStr(seq, | 
					
						
							|  |  |  |                                    node->lineno, node->col_offset, | 
					
						
							|  |  |  |                                    node->end_lineno, node->end_col_offset, | 
					
						
							|  |  |  |                                    arena); | 
					
						
							|  |  |  |     if (!res) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     COPY_NODE(node, res); | 
					
						
							|  |  |  | //     PySys_FormatStderr("format = %R\n", fmt);
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | fold_binop(expr_ty node, PyArena *arena, _PyASTPreprocessState *state) | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |     if (state->syntax_check_only) { | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |     expr_ty lhs, rhs; | 
					
						
							|  |  |  |     lhs = node->v.BinOp.left; | 
					
						
							|  |  |  |     rhs = node->v.BinOp.right; | 
					
						
							| 
									
										
										
										
											2021-05-08 22:33:10 +03:00
										 |  |  |     if (lhs->kind != Constant_kind) { | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-09-27 17:42:37 +03:00
										 |  |  |     PyObject *lv = lhs->v.Constant.value; | 
					
						
							| 
									
										
										
										
											2021-05-08 22:33:10 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (node->v.BinOp.op == Mod && | 
					
						
							|  |  |  |         rhs->kind == Tuple_kind && | 
					
						
							| 
									
										
										
										
											2021-05-23 19:06:48 +03:00
										 |  |  |         PyUnicode_Check(lv) && | 
					
						
							|  |  |  |         !has_starred(rhs->v.Tuple.elts)) | 
					
						
							| 
									
										
										
										
											2021-05-08 22:33:10 +03:00
										 |  |  |     { | 
					
						
							|  |  |  |         return optimize_format(node, lv, rhs->v.Tuple.elts, arena); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-21 18:54:22 +01:00
										 |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | static int astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTPreprocessState *state); | 
					
						
							|  |  |  | static int astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTPreprocessState *state); | 
					
						
							|  |  |  | static int astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTPreprocessState *state); | 
					
						
							|  |  |  | static int astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTPreprocessState *state); | 
					
						
							|  |  |  | static int astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTPreprocessState *state); | 
					
						
							|  |  |  | static int astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTPreprocessState *state); | 
					
						
							|  |  |  | static int astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTPreprocessState *state); | 
					
						
							|  |  |  | static int astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTPreprocessState *state); | 
					
						
							|  |  |  | static int astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTPreprocessState *state); | 
					
						
							|  |  |  | static int astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTPreprocessState *state); | 
					
						
							|  |  |  | static int astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTPreprocessState *state); | 
					
						
							|  |  |  | static int astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTPreprocessState *state); | 
					
						
							| 
									
										
										
										
											2021-02-26 14:51:55 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  | #define CALL(FUNC, TYPE, ARG) \
 | 
					
						
							| 
									
										
										
										
											2020-03-18 23:02:09 +00:00
										 |  |  |     if (!FUNC((ARG), ctx_, state)) \ | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define CALL_OPT(FUNC, TYPE, ARG) \
 | 
					
						
							| 
									
										
										
										
											2020-03-18 23:02:09 +00:00
										 |  |  |     if ((ARG) != NULL && !FUNC((ARG), ctx_, state)) \ | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define CALL_SEQ(FUNC, TYPE, ARG) { \
 | 
					
						
							| 
									
										
										
										
											2024-06-24 17:11:47 +01:00
										 |  |  |     Py_ssize_t i; \ | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |     asdl_ ## TYPE ## _seq *seq = (ARG); /* avoid variable capture */ \ | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |     for (i = 0; i < asdl_seq_LEN(seq); i++) { \ | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ | 
					
						
							| 
									
										
										
										
											2020-03-18 23:02:09 +00:00
										 |  |  |         if (elt != NULL && !FUNC(elt, ctx_, state)) \ | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |             return 0; \ | 
					
						
							|  |  |  |     } \ | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-11 18:02:28 +01:00
										 |  |  | static int | 
					
						
							|  |  |  | stmt_seq_remove_item(asdl_stmt_seq *stmts, Py_ssize_t idx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (idx >= asdl_seq_LEN(stmts)) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     for (Py_ssize_t i = idx; i < asdl_seq_LEN(stmts) - 1; i++) { | 
					
						
							|  |  |  |         stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, i+1); | 
					
						
							|  |  |  |         asdl_seq_SET(stmts, i, st); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     stmts->size--; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-08 17:04:10 +02:00
										 |  |  | static int | 
					
						
							|  |  |  | remove_docstring(asdl_stmt_seq *stmts, Py_ssize_t idx, PyArena *ctx_) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     assert(_PyAST_GetDocString(stmts) != NULL); | 
					
						
							|  |  |  |     // In case there's just the docstring in the body, replace it with `pass`
 | 
					
						
							|  |  |  |     // keyword, so body won't be empty.
 | 
					
						
							|  |  |  |     if (asdl_seq_LEN(stmts) == 1) { | 
					
						
							|  |  |  |         stmt_ty docstring = (stmt_ty)asdl_seq_GET(stmts, 0); | 
					
						
							|  |  |  |         stmt_ty pass = _PyAST_Pass( | 
					
						
							|  |  |  |             docstring->lineno, docstring->col_offset, | 
					
						
							|  |  |  |             // we know that `pass` always takes 4 chars and a single line,
 | 
					
						
							|  |  |  |             // while docstring can span on multiple lines
 | 
					
						
							|  |  |  |             docstring->lineno, docstring->col_offset + 4, | 
					
						
							|  |  |  |             ctx_ | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         if (pass == NULL) { | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         asdl_seq_SET(stmts, 0, pass); | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     // In case there are more than 1 body items, just remove the docstring.
 | 
					
						
							|  |  |  |     return stmt_seq_remove_item(stmts, idx); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-29 12:04:55 +03:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | astfold_body(asdl_stmt_seq *stmts, PyArena *ctx_, _PyASTPreprocessState *state) | 
					
						
							| 
									
										
										
										
											2018-05-29 12:04:55 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-30 10:56:16 +03:00
										 |  |  |     int docstring = _PyAST_GetDocString(stmts) != NULL; | 
					
						
							| 
									
										
										
										
											2024-09-11 18:02:28 +01:00
										 |  |  |     if (docstring && (state->optimize >= 2)) { | 
					
						
							|  |  |  |         /* remove the docstring */ | 
					
						
							| 
									
										
										
										
											2025-08-08 17:04:10 +02:00
										 |  |  |         if (!remove_docstring(stmts, 0, ctx_)) { | 
					
						
							| 
									
										
										
										
											2024-09-11 18:02:28 +01:00
										 |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         docstring = 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |     CALL_SEQ(astfold_stmt, stmt, stmts); | 
					
						
							| 
									
										
										
										
											2018-05-30 10:56:16 +03:00
										 |  |  |     if (!docstring && _PyAST_GetDocString(stmts) != NULL) { | 
					
						
							|  |  |  |         stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, 0); | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         asdl_expr_seq *values = _Py_asdl_expr_seq_new(1, ctx_); | 
					
						
							| 
									
										
										
										
											2018-05-29 12:04:55 +03:00
										 |  |  |         if (!values) { | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         asdl_seq_SET(values, 0, st->v.Expr.value); | 
					
						
							| 
									
										
										
										
											2021-04-07 21:34:22 +02:00
										 |  |  |         expr_ty expr = _PyAST_JoinedStr(values, st->lineno, st->col_offset, | 
					
						
							|  |  |  |                                         st->end_lineno, st->end_col_offset, | 
					
						
							|  |  |  |                                         ctx_); | 
					
						
							| 
									
										
										
										
											2018-05-29 12:04:55 +03:00
										 |  |  |         if (!expr) { | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         st->v.Expr.value = expr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTPreprocessState *state) | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  | { | 
					
						
							|  |  |  |     switch (node_->kind) { | 
					
						
							|  |  |  |     case Module_kind: | 
					
						
							| 
									
										
										
										
											2018-05-29 12:04:55 +03:00
										 |  |  |         CALL(astfold_body, asdl_seq, node_->v.Module.body); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case Interactive_kind: | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_stmt, stmt, node_->v.Interactive.body); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case Expression_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.Expression.body); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2020-11-07 22:35:17 +10:00
										 |  |  |     // The following top level nodes don't participate in constant folding
 | 
					
						
							|  |  |  |     case FunctionType_kind: | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2020-11-07 22:35:17 +10:00
										 |  |  |     // No default case, so the compiler will emit a warning if new top level
 | 
					
						
							|  |  |  |     // compilation nodes are added without being handled here
 | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |     } | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTPreprocessState *state) | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-02-25 09:24:48 +00:00
										 |  |  |     ENTER_RECURSIVE(); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |     switch (node_->kind) { | 
					
						
							|  |  |  |     case BoolOp_kind: | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_expr, expr, node_->v.BoolOp.values); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case BinOp_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.BinOp.left); | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.BinOp.right); | 
					
						
							|  |  |  |         CALL(fold_binop, expr_ty, node_); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case UnaryOp_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.UnaryOp.operand); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case Lambda_kind: | 
					
						
							|  |  |  |         CALL(astfold_arguments, arguments_ty, node_->v.Lambda.args); | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.Lambda.body); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case IfExp_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.IfExp.test); | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.IfExp.body); | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.IfExp.orelse); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case Dict_kind: | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_expr, expr, node_->v.Dict.keys); | 
					
						
							|  |  |  |         CALL_SEQ(astfold_expr, expr, node_->v.Dict.values); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case Set_kind: | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_expr, expr, node_->v.Set.elts); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case ListComp_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.ListComp.elt); | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_comprehension, comprehension, node_->v.ListComp.generators); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case SetComp_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.SetComp.elt); | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_comprehension, comprehension, node_->v.SetComp.generators); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case DictComp_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.DictComp.key); | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.DictComp.value); | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_comprehension, comprehension, node_->v.DictComp.generators); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case GeneratorExp_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.GeneratorExp.elt); | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_comprehension, comprehension, node_->v.GeneratorExp.generators); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case Await_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.Await.value); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case Yield_kind: | 
					
						
							|  |  |  |         CALL_OPT(astfold_expr, expr_ty, node_->v.Yield.value); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case YieldFrom_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.YieldFrom.value); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case Compare_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.Compare.left); | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_expr, expr, node_->v.Compare.comparators); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case Call_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.Call.func); | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_expr, expr, node_->v.Call.args); | 
					
						
							|  |  |  |         CALL_SEQ(astfold_keyword, keyword, node_->v.Call.keywords); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case FormattedValue_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.FormattedValue.value); | 
					
						
							|  |  |  |         CALL_OPT(astfold_expr, expr_ty, node_->v.FormattedValue.format_spec); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2025-04-30 11:46:41 +02:00
										 |  |  |     case Interpolation_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.Interpolation.value); | 
					
						
							|  |  |  |         CALL_OPT(astfold_expr, expr_ty, node_->v.Interpolation.format_spec); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |     case JoinedStr_kind: | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_expr, expr, node_->v.JoinedStr.values); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2025-04-30 11:46:41 +02:00
										 |  |  |     case TemplateStr_kind: | 
					
						
							|  |  |  |         CALL_SEQ(astfold_expr, expr, node_->v.TemplateStr.values); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |     case Attribute_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.Attribute.value); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case Subscript_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.Subscript.value); | 
					
						
							| 
									
										
										
										
											2020-03-10 18:52:34 +02:00
										 |  |  |         CALL(astfold_expr, expr_ty, node_->v.Subscript.slice); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case Starred_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.Starred.value); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2020-03-10 18:52:34 +02:00
										 |  |  |     case Slice_kind: | 
					
						
							|  |  |  |         CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.lower); | 
					
						
							|  |  |  |         CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.upper); | 
					
						
							|  |  |  |         CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.step); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |     case List_kind: | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_expr, expr, node_->v.List.elts); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case Tuple_kind: | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_expr, expr, node_->v.Tuple.elts); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2017-12-25 12:47:50 +02:00
										 |  |  |     case Name_kind: | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |         if (state->syntax_check_only) { | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-04-22 23:29:27 +01:00
										 |  |  |         if (node_->v.Name.ctx == Load && | 
					
						
							|  |  |  |                 _PyUnicode_EqualToASCIIString(node_->v.Name.id, "__debug__")) { | 
					
						
							| 
									
										
										
										
											2025-02-25 09:24:48 +00:00
										 |  |  |             LEAVE_RECURSIVE(); | 
					
						
							| 
									
										
										
										
											2020-03-18 23:02:09 +00:00
										 |  |  |             return make_const(node_, PyBool_FromLong(!state->optimize), ctx_); | 
					
						
							| 
									
										
										
										
											2017-12-25 12:47:50 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2020-11-07 22:35:17 +10:00
										 |  |  |     case NamedExpr_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.NamedExpr.value); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case Constant_kind: | 
					
						
							|  |  |  |         // Already a constant, nothing further to do
 | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2020-11-07 22:35:17 +10:00
										 |  |  |     // No default case, so the compiler will emit a warning if new expression
 | 
					
						
							|  |  |  |     // kinds are added without being handled here
 | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-02-25 09:24:48 +00:00
										 |  |  |     LEAVE_RECURSIVE(); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTPreprocessState *state) | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  | { | 
					
						
							|  |  |  |     CALL(astfold_expr, expr_ty, node_->value); | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTPreprocessState *state) | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  | { | 
					
						
							|  |  |  |     CALL(astfold_expr, expr_ty, node_->target); | 
					
						
							|  |  |  |     CALL(astfold_expr, expr_ty, node_->iter); | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |     CALL_SEQ(astfold_expr, expr, node_->ifs); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTPreprocessState *state) | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-04-21 12:41:19 +01:00
										 |  |  |     CALL_SEQ(astfold_arg, arg, node_->posonlyargs); | 
					
						
							|  |  |  |     CALL_SEQ(astfold_arg, arg, node_->args); | 
					
						
							|  |  |  |     CALL_OPT(astfold_arg, arg_ty, node_->vararg); | 
					
						
							|  |  |  |     CALL_SEQ(astfold_arg, arg, node_->kwonlyargs); | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |     CALL_SEQ(astfold_expr, expr, node_->kw_defaults); | 
					
						
							| 
									
										
										
										
											2021-04-21 12:41:19 +01:00
										 |  |  |     CALL_OPT(astfold_arg, arg_ty, node_->kwarg); | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |     CALL_SEQ(astfold_expr, expr, node_->defaults); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-21 12:41:19 +01:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTPreprocessState *state) | 
					
						
							| 
									
										
										
										
											2021-04-21 12:41:19 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) { | 
					
						
							|  |  |  |         CALL_OPT(astfold_expr, expr_ty, node_->annotation); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTPreprocessState *state) | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-02-25 09:24:48 +00:00
										 |  |  |     ENTER_RECURSIVE(); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |     switch (node_->kind) { | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |     case FunctionDef_kind: { | 
					
						
							| 
									
										
										
										
											2023-05-21 21:25:09 -07:00
										 |  |  |         CALL_SEQ(astfold_type_param, type_param, node_->v.FunctionDef.type_params); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         CALL(astfold_arguments, arguments_ty, node_->v.FunctionDef.args); | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |         BEFORE_FUNC_BODY(state, node_); | 
					
						
							| 
									
										
										
										
											2018-05-29 12:04:55 +03:00
										 |  |  |         CALL(astfold_body, asdl_seq, node_->v.FunctionDef.body); | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |         AFTER_FUNC_BODY(state); | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_expr, expr, node_->v.FunctionDef.decorator_list); | 
					
						
							| 
									
										
										
										
											2021-04-21 12:41:19 +01:00
										 |  |  |         if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) { | 
					
						
							|  |  |  |             CALL_OPT(astfold_expr, expr_ty, node_->v.FunctionDef.returns); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     case AsyncFunctionDef_kind: { | 
					
						
							| 
									
										
										
										
											2023-05-21 21:25:09 -07:00
										 |  |  |         CALL_SEQ(astfold_type_param, type_param, node_->v.AsyncFunctionDef.type_params); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         CALL(astfold_arguments, arguments_ty, node_->v.AsyncFunctionDef.args); | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |         BEFORE_FUNC_BODY(state, node_); | 
					
						
							| 
									
										
										
										
											2018-05-29 12:04:55 +03:00
										 |  |  |         CALL(astfold_body, asdl_seq, node_->v.AsyncFunctionDef.body); | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |         AFTER_FUNC_BODY(state); | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_expr, expr, node_->v.AsyncFunctionDef.decorator_list); | 
					
						
							| 
									
										
										
										
											2021-04-21 12:41:19 +01:00
										 |  |  |         if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) { | 
					
						
							|  |  |  |             CALL_OPT(astfold_expr, expr_ty, node_->v.AsyncFunctionDef.returns); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |     case ClassDef_kind: | 
					
						
							| 
									
										
										
										
											2023-05-21 21:25:09 -07:00
										 |  |  |         CALL_SEQ(astfold_type_param, type_param, node_->v.ClassDef.type_params); | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_expr, expr, node_->v.ClassDef.bases); | 
					
						
							|  |  |  |         CALL_SEQ(astfold_keyword, keyword, node_->v.ClassDef.keywords); | 
					
						
							| 
									
										
										
										
											2018-05-29 12:04:55 +03:00
										 |  |  |         CALL(astfold_body, asdl_seq, node_->v.ClassDef.body); | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_expr, expr, node_->v.ClassDef.decorator_list); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case Return_kind: | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |         BEFORE_RETURN(state, node_); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         CALL_OPT(astfold_expr, expr_ty, node_->v.Return.value); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case Delete_kind: | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_expr, expr, node_->v.Delete.targets); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case Assign_kind: | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_expr, expr, node_->v.Assign.targets); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         CALL(astfold_expr, expr_ty, node_->v.Assign.value); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case AugAssign_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.AugAssign.target); | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.AugAssign.value); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case AnnAssign_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.AnnAssign.target); | 
					
						
							| 
									
										
										
										
											2021-04-21 12:41:19 +01:00
										 |  |  |         if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) { | 
					
						
							|  |  |  |             CALL(astfold_expr, expr_ty, node_->v.AnnAssign.annotation); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         CALL_OPT(astfold_expr, expr_ty, node_->v.AnnAssign.value); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2023-05-15 20:36:23 -07:00
										 |  |  |     case TypeAlias_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.TypeAlias.name); | 
					
						
							| 
									
										
										
										
											2023-05-21 21:25:09 -07:00
										 |  |  |         CALL_SEQ(astfold_type_param, type_param, node_->v.TypeAlias.type_params); | 
					
						
							| 
									
										
										
										
											2023-05-15 20:36:23 -07:00
										 |  |  |         CALL(astfold_expr, expr_ty, node_->v.TypeAlias.value); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |     case For_kind: { | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         CALL(astfold_expr, expr_ty, node_->v.For.target); | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.For.iter); | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |         BEFORE_LOOP_BODY(state, node_); | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_stmt, stmt, node_->v.For.body); | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |         AFTER_LOOP_BODY(state); | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_stmt, stmt, node_->v.For.orelse); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     case AsyncFor_kind: { | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         CALL(astfold_expr, expr_ty, node_->v.AsyncFor.target); | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.AsyncFor.iter); | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |         BEFORE_LOOP_BODY(state, node_); | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_stmt, stmt, node_->v.AsyncFor.body); | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |         AFTER_LOOP_BODY(state); | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_stmt, stmt, node_->v.AsyncFor.orelse); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     case While_kind: { | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         CALL(astfold_expr, expr_ty, node_->v.While.test); | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |         BEFORE_LOOP_BODY(state, node_); | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_stmt, stmt, node_->v.While.body); | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |         AFTER_LOOP_BODY(state); | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_stmt, stmt, node_->v.While.orelse); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |     case If_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.If.test); | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_stmt, stmt, node_->v.If.body); | 
					
						
							|  |  |  |         CALL_SEQ(astfold_stmt, stmt, node_->v.If.orelse); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case With_kind: | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_withitem, withitem, node_->v.With.items); | 
					
						
							|  |  |  |         CALL_SEQ(astfold_stmt, stmt, node_->v.With.body); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case AsyncWith_kind: | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_withitem, withitem, node_->v.AsyncWith.items); | 
					
						
							|  |  |  |         CALL_SEQ(astfold_stmt, stmt, node_->v.AsyncWith.body); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							|  |  |  |     case Raise_kind: | 
					
						
							|  |  |  |         CALL_OPT(astfold_expr, expr_ty, node_->v.Raise.exc); | 
					
						
							|  |  |  |         CALL_OPT(astfold_expr, expr_ty, node_->v.Raise.cause); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |     case Try_kind: { | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_stmt, stmt, node_->v.Try.body); | 
					
						
							|  |  |  |         CALL_SEQ(astfold_excepthandler, excepthandler, node_->v.Try.handlers); | 
					
						
							|  |  |  |         CALL_SEQ(astfold_stmt, stmt, node_->v.Try.orelse); | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |         BEFORE_FINALLY(state, node_); | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_stmt, stmt, node_->v.Try.finalbody); | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |         AFTER_FINALLY(state); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     case TryStar_kind: { | 
					
						
							| 
									
										
										
										
											2021-12-14 16:48:15 +00:00
										 |  |  |         CALL_SEQ(astfold_stmt, stmt, node_->v.TryStar.body); | 
					
						
							|  |  |  |         CALL_SEQ(astfold_excepthandler, excepthandler, node_->v.TryStar.handlers); | 
					
						
							|  |  |  |         CALL_SEQ(astfold_stmt, stmt, node_->v.TryStar.orelse); | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |         BEFORE_FINALLY(state, node_); | 
					
						
							| 
									
										
										
										
											2021-12-14 16:48:15 +00:00
										 |  |  |         CALL_SEQ(astfold_stmt, stmt, node_->v.TryStar.finalbody); | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |         AFTER_FINALLY(state); | 
					
						
							| 
									
										
										
										
											2021-12-14 16:48:15 +00:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |     case Assert_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.Assert.test); | 
					
						
							|  |  |  |         CALL_OPT(astfold_expr, expr_ty, node_->v.Assert.msg); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case Expr_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.Expr.value); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2021-02-26 14:51:55 -08:00
										 |  |  |     case Match_kind: | 
					
						
							|  |  |  |         CALL(astfold_expr, expr_ty, node_->v.Match.subject); | 
					
						
							|  |  |  |         CALL_SEQ(astfold_match_case, match_case, node_->v.Match.cases); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |     case Break_kind: | 
					
						
							|  |  |  |         BEFORE_LOOP_EXIT(state, node_, "break"); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case Continue_kind: | 
					
						
							|  |  |  |         BEFORE_LOOP_EXIT(state, node_, "continue"); | 
					
						
							|  |  |  |         break; | 
					
						
							| 
									
										
										
										
											2020-11-07 22:35:17 +10:00
										 |  |  |     // The following statements don't contain any subexpressions to be folded
 | 
					
						
							|  |  |  |     case Import_kind: | 
					
						
							|  |  |  |     case ImportFrom_kind: | 
					
						
							|  |  |  |     case Global_kind: | 
					
						
							|  |  |  |     case Nonlocal_kind: | 
					
						
							|  |  |  |     case Pass_kind: | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     // No default case, so the compiler will emit a warning if new statement
 | 
					
						
							|  |  |  |     // kinds are added without being handled here
 | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-02-25 09:24:48 +00:00
										 |  |  |     LEAVE_RECURSIVE(); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTPreprocessState *state) | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  | { | 
					
						
							|  |  |  |     switch (node_->kind) { | 
					
						
							|  |  |  |     case ExceptHandler_kind: | 
					
						
							|  |  |  |         CALL_OPT(astfold_expr, expr_ty, node_->v.ExceptHandler.type); | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |         CALL_SEQ(astfold_stmt, stmt, node_->v.ExceptHandler.body); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |         break; | 
					
						
							| 
									
										
										
										
											2020-11-07 22:35:17 +10:00
										 |  |  |     // No default case, so the compiler will emit a warning if new handler
 | 
					
						
							|  |  |  |     // kinds are added without being handled here
 | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |     } | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTPreprocessState *state) | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  | { | 
					
						
							|  |  |  |     CALL(astfold_expr, expr_ty, node_->context_expr); | 
					
						
							|  |  |  |     CALL_OPT(astfold_expr, expr_ty, node_->optional_vars); | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-21 18:54:22 +01:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | fold_const_match_patterns(expr_ty node, PyArena *ctx_, _PyASTPreprocessState *state) | 
					
						
							| 
									
										
										
										
											2025-02-21 18:54:22 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-03-23 13:50:14 +00:00
										 |  |  |     if (state->syntax_check_only) { | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-02-21 18:54:22 +01:00
										 |  |  |     switch (node->kind) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         case UnaryOp_kind: | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (node->v.UnaryOp.op == USub && | 
					
						
							|  |  |  |                 node->v.UnaryOp.operand->kind == Constant_kind) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 PyObject *operand = node->v.UnaryOp.operand->v.Constant.value; | 
					
						
							|  |  |  |                 PyObject *folded = PyNumber_Negative(operand); | 
					
						
							|  |  |  |                 return make_const(node, folded, ctx_); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         case BinOp_kind: | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             operator_ty op = node->v.BinOp.op; | 
					
						
							|  |  |  |             if ((op == Add || op == Sub) && | 
					
						
							|  |  |  |                 node->v.BinOp.right->kind == Constant_kind) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 CALL(fold_const_match_patterns, expr_ty, node->v.BinOp.left); | 
					
						
							|  |  |  |                 if (node->v.BinOp.left->kind == Constant_kind) { | 
					
						
							|  |  |  |                     PyObject *left = node->v.BinOp.left->v.Constant.value; | 
					
						
							|  |  |  |                     PyObject *right = node->v.BinOp.right->v.Constant.value; | 
					
						
							|  |  |  |                     PyObject *folded = op == Add ? PyNumber_Add(left, right) : PyNumber_Subtract(left, right); | 
					
						
							|  |  |  |                     return make_const(node, folded, ctx_); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-26 14:51:55 -08:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTPreprocessState *state) | 
					
						
							| 
									
										
										
										
											2021-02-26 14:51:55 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-04-29 15:58:44 +10:00
										 |  |  |     // Currently, this is really only used to form complex/negative numeric
 | 
					
						
							|  |  |  |     // constants in MatchValue and MatchMapping nodes
 | 
					
						
							|  |  |  |     // We still recurse into all subexpressions and subpatterns anyway
 | 
					
						
							| 
									
										
										
										
											2025-02-25 09:24:48 +00:00
										 |  |  |     ENTER_RECURSIVE(); | 
					
						
							| 
									
										
										
										
											2021-02-26 14:51:55 -08:00
										 |  |  |     switch (node_->kind) { | 
					
						
							| 
									
										
										
										
											2021-04-29 15:58:44 +10:00
										 |  |  |         case MatchValue_kind: | 
					
						
							| 
									
										
										
										
											2025-02-21 18:54:22 +01:00
										 |  |  |             CALL(fold_const_match_patterns, expr_ty, node_->v.MatchValue.value); | 
					
						
							| 
									
										
										
										
											2021-02-26 14:51:55 -08:00
										 |  |  |             break; | 
					
						
							| 
									
										
										
										
											2021-04-29 15:58:44 +10:00
										 |  |  |         case MatchSingleton_kind: | 
					
						
							| 
									
										
										
										
											2021-02-26 14:51:55 -08:00
										 |  |  |             break; | 
					
						
							| 
									
										
										
										
											2021-04-29 15:58:44 +10:00
										 |  |  |         case MatchSequence_kind: | 
					
						
							|  |  |  |             CALL_SEQ(astfold_pattern, pattern, node_->v.MatchSequence.patterns); | 
					
						
							| 
									
										
										
										
											2021-02-26 14:51:55 -08:00
										 |  |  |             break; | 
					
						
							| 
									
										
										
										
											2021-04-29 15:58:44 +10:00
										 |  |  |         case MatchMapping_kind: | 
					
						
							| 
									
										
										
										
											2025-02-21 18:54:22 +01:00
										 |  |  |             CALL_SEQ(fold_const_match_patterns, expr, node_->v.MatchMapping.keys); | 
					
						
							| 
									
										
										
										
											2021-04-29 15:58:44 +10:00
										 |  |  |             CALL_SEQ(astfold_pattern, pattern, node_->v.MatchMapping.patterns); | 
					
						
							| 
									
										
										
										
											2021-02-26 14:51:55 -08:00
										 |  |  |             break; | 
					
						
							| 
									
										
										
										
											2021-04-29 15:58:44 +10:00
										 |  |  |         case MatchClass_kind: | 
					
						
							|  |  |  |             CALL(astfold_expr, expr_ty, node_->v.MatchClass.cls); | 
					
						
							|  |  |  |             CALL_SEQ(astfold_pattern, pattern, node_->v.MatchClass.patterns); | 
					
						
							|  |  |  |             CALL_SEQ(astfold_pattern, pattern, node_->v.MatchClass.kwd_patterns); | 
					
						
							| 
									
										
										
										
											2021-02-26 14:51:55 -08:00
										 |  |  |             break; | 
					
						
							| 
									
										
										
										
											2021-04-29 15:58:44 +10:00
										 |  |  |         case MatchStar_kind: | 
					
						
							| 
									
										
										
										
											2021-02-26 14:51:55 -08:00
										 |  |  |             break; | 
					
						
							|  |  |  |         case MatchAs_kind: | 
					
						
							| 
									
										
										
										
											2021-04-29 15:58:44 +10:00
										 |  |  |             if (node_->v.MatchAs.pattern) { | 
					
						
							| 
									
										
										
										
											2021-04-29 17:19:28 -07:00
										 |  |  |                 CALL(astfold_pattern, pattern_ty, node_->v.MatchAs.pattern); | 
					
						
							| 
									
										
										
										
											2021-04-29 15:58:44 +10:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-02-26 14:51:55 -08:00
										 |  |  |             break; | 
					
						
							|  |  |  |         case MatchOr_kind: | 
					
						
							| 
									
										
										
										
											2021-04-29 15:58:44 +10:00
										 |  |  |             CALL_SEQ(astfold_pattern, pattern, node_->v.MatchOr.patterns); | 
					
						
							| 
									
										
										
										
											2021-02-26 14:51:55 -08:00
										 |  |  |             break; | 
					
						
							| 
									
										
										
										
											2021-04-29 15:58:44 +10:00
										 |  |  |     // No default case, so the compiler will emit a warning if new pattern
 | 
					
						
							|  |  |  |     // kinds are added without being handled here
 | 
					
						
							| 
									
										
										
										
											2021-02-26 14:51:55 -08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-02-25 09:24:48 +00:00
										 |  |  |     LEAVE_RECURSIVE(); | 
					
						
							| 
									
										
										
										
											2021-02-26 14:51:55 -08:00
										 |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTPreprocessState *state) | 
					
						
							| 
									
										
										
										
											2021-02-26 14:51:55 -08:00
										 |  |  | { | 
					
						
							|  |  |  |     CALL(astfold_pattern, expr_ty, node_->pattern); | 
					
						
							|  |  |  |     CALL_OPT(astfold_expr, expr_ty, node_->guard); | 
					
						
							|  |  |  |     CALL_SEQ(astfold_stmt, stmt, node_->body); | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-15 20:36:23 -07:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTPreprocessState *state) | 
					
						
							| 
									
										
										
										
											2023-05-15 20:36:23 -07:00
										 |  |  | { | 
					
						
							|  |  |  |     switch (node_->kind) { | 
					
						
							|  |  |  |         case TypeVar_kind: | 
					
						
							|  |  |  |             CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVar.bound); | 
					
						
							| 
									
										
										
										
											2024-08-28 16:38:56 +03:00
										 |  |  |             CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVar.default_value); | 
					
						
							| 
									
										
										
										
											2023-05-15 20:36:23 -07:00
										 |  |  |             break; | 
					
						
							|  |  |  |         case ParamSpec_kind: | 
					
						
							| 
									
										
										
										
											2024-08-28 16:38:56 +03:00
										 |  |  |             CALL_OPT(astfold_expr, expr_ty, node_->v.ParamSpec.default_value); | 
					
						
							| 
									
										
										
										
											2023-05-15 20:36:23 -07:00
										 |  |  |             break; | 
					
						
							|  |  |  |         case TypeVarTuple_kind: | 
					
						
							| 
									
										
										
										
											2024-08-28 16:38:56 +03:00
										 |  |  |             CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVarTuple.default_value); | 
					
						
							| 
									
										
										
										
											2023-05-15 20:36:23 -07:00
										 |  |  |             break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  | #undef CALL
 | 
					
						
							|  |  |  | #undef CALL_OPT
 | 
					
						
							|  |  |  | #undef CALL_SEQ
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  | _PyAST_Preprocess(mod_ty mod, PyArena *arena, PyObject *filename, int optimize, | 
					
						
							|  |  |  |                   int ff_features, int syntax_check_only) | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-05-04 20:07:35 +02:00
										 |  |  |     _PyASTPreprocessState state; | 
					
						
							|  |  |  |     memset(&state, 0, sizeof(_PyASTPreprocessState)); | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |     state.filename = filename; | 
					
						
							| 
									
										
										
										
											2023-05-31 20:21:46 +01:00
										 |  |  |     state.optimize = optimize; | 
					
						
							|  |  |  |     state.ff_features = ff_features; | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |     state.syntax_check_only = syntax_check_only; | 
					
						
							|  |  |  |     if (_Py_CArray_Init(&state.cf_finally, sizeof(ControlFlowInFinallyContext), 20) < 0) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-05-31 20:21:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     int ret = astfold_mod(mod, arena, &state); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |     assert(ret || PyErr_Occurred()); | 
					
						
							| 
									
										
										
										
											2021-04-25 13:38:00 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-17 20:48:54 +00:00
										 |  |  |     _Py_CArray_Fini(&state.cf_finally); | 
					
						
							| 
									
										
										
										
											2017-12-14 16:47:20 +09:00
										 |  |  |     return ret; | 
					
						
							|  |  |  | } |