| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | #include "Python.h"
 | 
					
						
							| 
									
										
										
										
											2021-03-23 20:47:40 +01:00
										 |  |  | #include "pycore_ast.h"           // expr_ty
 | 
					
						
							|  |  |  | #include <float.h>                // DBL_MAX_10_EXP
 | 
					
						
							|  |  |  | #include <stdbool.h>
 | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-29 15:58:44 +10:00
										 |  |  | /* This limited unparser is used to convert annotations back to strings
 | 
					
						
							|  |  |  |  * during compilation rather than being a full AST unparser. | 
					
						
							|  |  |  |  * See ast.unparse for a full unparser (written in Python) | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | static PyObject *_str_open_br; | 
					
						
							|  |  |  | static PyObject *_str_dbl_open_br; | 
					
						
							|  |  |  | static PyObject *_str_close_br; | 
					
						
							|  |  |  | static PyObject *_str_dbl_close_br; | 
					
						
							| 
									
										
										
										
											2020-04-14 01:51:31 +03:00
										 |  |  | static PyObject *_str_inf; | 
					
						
							|  |  |  | static PyObject *_str_replace_inf; | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Forward declarations for recursion via helper functions. */ | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  | expr_as_unicode(expr_ty e, int level); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  | append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | static int | 
					
						
							|  |  |  | append_joinedstr(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec); | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2020-03-08 11:53:59 -05:00
										 |  |  | append_formattedvalue(_PyUnicodeWriter *writer, expr_ty e); | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2020-03-10 18:52:34 +02:00
										 |  |  | append_ast_slice(_PyUnicodeWriter *writer, expr_ty e); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | append_charp(_PyUnicodeWriter *writer, const char *charp) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-02-01 17:59:27 +01:00
										 |  |  |     return _PyUnicodeWriter_WriteASCIIString(writer, charp, -1); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  | #define APPEND_STR_FINISH(str)  do { \
 | 
					
						
							|  |  |  |         return append_charp(writer, (str)); \ | 
					
						
							|  |  |  |     } while (0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define APPEND_STR(str)  do { \
 | 
					
						
							|  |  |  |         if (-1 == append_charp(writer, (str))) { \ | 
					
						
							|  |  |  |             return -1; \ | 
					
						
							|  |  |  |         } \ | 
					
						
							|  |  |  |     } while (0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define APPEND_STR_IF(cond, str)  do { \
 | 
					
						
							|  |  |  |         if ((cond) && -1 == append_charp(writer, (str))) { \ | 
					
						
							|  |  |  |             return -1; \ | 
					
						
							|  |  |  |         } \ | 
					
						
							|  |  |  |     } while (0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define APPEND_STR_IF_NOT_FIRST(str)  do { \
 | 
					
						
							|  |  |  |         APPEND_STR_IF(!first, (str)); \ | 
					
						
							|  |  |  |         first = false; \ | 
					
						
							|  |  |  |     } while (0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define APPEND_EXPR(expr, pr)  do { \
 | 
					
						
							|  |  |  |         if (-1 == append_ast_expr(writer, (expr), (pr))) { \ | 
					
						
							|  |  |  |             return -1; \ | 
					
						
							|  |  |  |         } \ | 
					
						
							|  |  |  |     } while (0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define APPEND(type, value)  do { \
 | 
					
						
							|  |  |  |         if (-1 == append_ast_ ## type(writer, (value))) { \ | 
					
						
							|  |  |  |             return -1; \ | 
					
						
							|  |  |  |         } \ | 
					
						
							|  |  |  |     } while (0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | static int | 
					
						
							|  |  |  | append_repr(_PyUnicodeWriter *writer, PyObject *obj) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-04-14 01:51:31 +03:00
										 |  |  |     PyObject *repr = PyObject_Repr(obj); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     if (!repr) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-14 01:51:31 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if ((PyFloat_CheckExact(obj) && Py_IS_INFINITY(PyFloat_AS_DOUBLE(obj))) || | 
					
						
							|  |  |  |        PyComplex_CheckExact(obj)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         PyObject *new_repr = PyUnicode_Replace( | 
					
						
							|  |  |  |             repr, | 
					
						
							|  |  |  |             _str_inf, | 
					
						
							|  |  |  |             _str_replace_inf, | 
					
						
							|  |  |  |             -1 | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         Py_DECREF(repr); | 
					
						
							|  |  |  |         if (!new_repr) { | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         repr = new_repr; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     int ret = _PyUnicodeWriter_WriteStr(writer, repr); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     Py_DECREF(repr); | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  | /* Priority levels */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | enum { | 
					
						
							|  |  |  |     PR_TUPLE, | 
					
						
							|  |  |  |     PR_TEST,            /* 'if'-'else', 'lambda' */ | 
					
						
							|  |  |  |     PR_OR,              /* 'or' */ | 
					
						
							|  |  |  |     PR_AND,             /* 'and' */ | 
					
						
							|  |  |  |     PR_NOT,             /* 'not' */ | 
					
						
							|  |  |  |     PR_CMP,             /* '<', '>', '==', '>=', '<=', '!=',
 | 
					
						
							|  |  |  |                            'in', 'not in', 'is', 'is not' */ | 
					
						
							|  |  |  |     PR_EXPR, | 
					
						
							|  |  |  |     PR_BOR = PR_EXPR,   /* '|' */ | 
					
						
							|  |  |  |     PR_BXOR,            /* '^' */ | 
					
						
							|  |  |  |     PR_BAND,            /* '&' */ | 
					
						
							|  |  |  |     PR_SHIFT,           /* '<<', '>>' */ | 
					
						
							|  |  |  |     PR_ARITH,           /* '+', '-' */ | 
					
						
							|  |  |  |     PR_TERM,            /* '*', '@', '/', '%', '//' */ | 
					
						
							|  |  |  |     PR_FACTOR,          /* unary '+', '-', '~' */ | 
					
						
							|  |  |  |     PR_POWER,           /* '**' */ | 
					
						
							|  |  |  |     PR_AWAIT,           /* 'await' */ | 
					
						
							|  |  |  |     PR_ATOM, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | append_ast_boolop(_PyUnicodeWriter *writer, expr_ty e, int level) | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | { | 
					
						
							|  |  |  |     Py_ssize_t i, value_count; | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |     asdl_expr_seq *values; | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     const char *op = (e->v.BoolOp.op == And) ? " and " : " or "; | 
					
						
							|  |  |  |     int pr = (e->v.BoolOp.op == And) ? PR_AND : PR_OR; | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR_IF(level > pr, "("); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     values = e->v.BoolOp.values; | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     value_count = asdl_seq_LEN(values); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     for (i = 0; i < value_count; ++i) { | 
					
						
							|  |  |  |         APPEND_STR_IF(i > 0, op); | 
					
						
							|  |  |  |         APPEND_EXPR((expr_ty)asdl_seq_GET(values, i), pr + 1); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR_IF(level > pr, ")"); | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  | append_ast_binop(_PyUnicodeWriter *writer, expr_ty e, int level) | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | { | 
					
						
							|  |  |  |     const char *op; | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     int pr; | 
					
						
							|  |  |  |     bool rassoc = false;  /* is right-associative? */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch (e->v.BinOp.op) { | 
					
						
							|  |  |  |     case Add: op = " + "; pr = PR_ARITH; break; | 
					
						
							|  |  |  |     case Sub: op = " - "; pr = PR_ARITH; break; | 
					
						
							|  |  |  |     case Mult: op = " * "; pr = PR_TERM; break; | 
					
						
							|  |  |  |     case MatMult: op = " @ "; pr = PR_TERM; break; | 
					
						
							|  |  |  |     case Div: op = " / "; pr = PR_TERM; break; | 
					
						
							|  |  |  |     case Mod: op = " % "; pr = PR_TERM; break; | 
					
						
							|  |  |  |     case LShift: op = " << "; pr = PR_SHIFT; break; | 
					
						
							|  |  |  |     case RShift: op = " >> "; pr = PR_SHIFT; break; | 
					
						
							|  |  |  |     case BitOr: op = " | "; pr = PR_BOR; break; | 
					
						
							|  |  |  |     case BitXor: op = " ^ "; pr = PR_BXOR; break; | 
					
						
							|  |  |  |     case BitAnd: op = " & "; pr = PR_BAND; break; | 
					
						
							|  |  |  |     case FloorDiv: op = " // "; pr = PR_TERM; break; | 
					
						
							|  |  |  |     case Pow: op = " ** "; pr = PR_POWER; rassoc = true; break; | 
					
						
							| 
									
										
										
										
											2018-02-01 17:59:27 +01:00
										 |  |  |     default: | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         PyErr_SetString(PyExc_SystemError, | 
					
						
							|  |  |  |                         "unknown binary operator"); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR_IF(level > pr, "("); | 
					
						
							|  |  |  |     APPEND_EXPR(e->v.BinOp.left, pr + rassoc); | 
					
						
							|  |  |  |     APPEND_STR(op); | 
					
						
							|  |  |  |     APPEND_EXPR(e->v.BinOp.right, pr + !rassoc); | 
					
						
							|  |  |  |     APPEND_STR_IF(level > pr, ")"); | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  | append_ast_unaryop(_PyUnicodeWriter *writer, expr_ty e, int level) | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | { | 
					
						
							|  |  |  |     const char *op; | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     int pr; | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     switch (e->v.UnaryOp.op) { | 
					
						
							|  |  |  |     case Invert: op = "~"; pr = PR_FACTOR; break; | 
					
						
							|  |  |  |     case Not: op = "not "; pr = PR_NOT; break; | 
					
						
							|  |  |  |     case UAdd: op = "+"; pr = PR_FACTOR; break; | 
					
						
							|  |  |  |     case USub: op = "-"; pr = PR_FACTOR; break; | 
					
						
							| 
									
										
										
										
											2018-02-01 17:59:27 +01:00
										 |  |  |     default: | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         PyErr_SetString(PyExc_SystemError, | 
					
						
							|  |  |  |                         "unknown unary operator"); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR_IF(level > pr, "("); | 
					
						
							|  |  |  |     APPEND_STR(op); | 
					
						
							|  |  |  |     APPEND_EXPR(e->v.UnaryOp.operand, pr); | 
					
						
							|  |  |  |     APPEND_STR_IF(level > pr, ")"); | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | append_ast_arg(_PyUnicodeWriter *writer, arg_ty arg) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (-1 == _PyUnicodeWriter_WriteStr(writer, arg->arg)) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (arg->annotation) { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         APPEND_STR(": "); | 
					
						
							|  |  |  |         APPEND_EXPR(arg->annotation, PR_TEST); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | append_ast_args(_PyUnicodeWriter *writer, arguments_ty args) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     bool first; | 
					
						
							| 
									
										
										
										
											2019-05-18 23:40:22 +01:00
										 |  |  |     Py_ssize_t i, di, arg_count, posonlyarg_count, default_count; | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     first = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-18 23:40:22 +01:00
										 |  |  |     /* positional-only and positional arguments with defaults */ | 
					
						
							|  |  |  |     posonlyarg_count = asdl_seq_LEN(args->posonlyargs); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     arg_count = asdl_seq_LEN(args->args); | 
					
						
							|  |  |  |     default_count = asdl_seq_LEN(args->defaults); | 
					
						
							| 
									
										
										
										
											2019-05-18 23:40:22 +01:00
										 |  |  |     for (i = 0; i < posonlyarg_count + arg_count; i++) { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         APPEND_STR_IF_NOT_FIRST(", "); | 
					
						
							| 
									
										
										
										
											2019-05-18 23:40:22 +01:00
										 |  |  |         if (i < posonlyarg_count){ | 
					
						
							|  |  |  |             APPEND(arg, (arg_ty)asdl_seq_GET(args->posonlyargs, i)); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             APPEND(arg, (arg_ty)asdl_seq_GET(args->args, i-posonlyarg_count)); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-18 23:40:22 +01:00
										 |  |  |         di = i - posonlyarg_count - arg_count + default_count; | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |         if (di >= 0) { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |             APPEND_STR("="); | 
					
						
							|  |  |  |             APPEND_EXPR((expr_ty)asdl_seq_GET(args->defaults, di), PR_TEST); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-05-18 23:40:22 +01:00
										 |  |  |         if (posonlyarg_count && i + 1 == posonlyarg_count) { | 
					
						
							|  |  |  |             APPEND_STR(", /"); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* vararg, or bare '*' if no varargs but keyword-only arguments present */ | 
					
						
							| 
									
										
										
										
											2018-09-30 21:07:05 +03:00
										 |  |  |     if (args->vararg || asdl_seq_LEN(args->kwonlyargs)) { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         APPEND_STR_IF_NOT_FIRST(", "); | 
					
						
							|  |  |  |         APPEND_STR("*"); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |         if (args->vararg) { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |             APPEND(arg, args->vararg); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* keyword-only arguments */ | 
					
						
							|  |  |  |     arg_count = asdl_seq_LEN(args->kwonlyargs); | 
					
						
							|  |  |  |     default_count = asdl_seq_LEN(args->kw_defaults); | 
					
						
							|  |  |  |     for (i = 0; i < arg_count; i++) { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         APPEND_STR_IF_NOT_FIRST(", "); | 
					
						
							|  |  |  |         APPEND(arg, (arg_ty)asdl_seq_GET(args->kwonlyargs, i)); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         di = i - arg_count + default_count; | 
					
						
							|  |  |  |         if (di >= 0) { | 
					
						
							| 
									
										
										
										
											2018-09-30 21:07:05 +03:00
										 |  |  |             expr_ty default_ = (expr_ty)asdl_seq_GET(args->kw_defaults, di); | 
					
						
							|  |  |  |             if (default_) { | 
					
						
							|  |  |  |                 APPEND_STR("="); | 
					
						
							|  |  |  |                 APPEND_EXPR(default_, PR_TEST); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* **kwargs */ | 
					
						
							|  |  |  |     if (args->kwarg) { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         APPEND_STR_IF_NOT_FIRST(", "); | 
					
						
							|  |  |  |         APPEND_STR("**"); | 
					
						
							|  |  |  |         APPEND(arg, args->kwarg); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  | append_ast_lambda(_PyUnicodeWriter *writer, expr_ty e, int level) | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR_IF(level > PR_TEST, "("); | 
					
						
							| 
									
										
										
										
											2019-05-18 23:40:22 +01:00
										 |  |  |     Py_ssize_t n_positional = (asdl_seq_LEN(e->v.Lambda.args->args) + | 
					
						
							|  |  |  |                                asdl_seq_LEN(e->v.Lambda.args->posonlyargs)); | 
					
						
							|  |  |  |     APPEND_STR(n_positional ? "lambda " : "lambda"); | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND(args, e->v.Lambda.args); | 
					
						
							|  |  |  |     APPEND_STR(": "); | 
					
						
							|  |  |  |     APPEND_EXPR(e->v.Lambda.body, PR_TEST); | 
					
						
							|  |  |  |     APPEND_STR_IF(level > PR_TEST, ")"); | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  | append_ast_ifexp(_PyUnicodeWriter *writer, expr_ty e, int level) | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR_IF(level > PR_TEST, "("); | 
					
						
							|  |  |  |     APPEND_EXPR(e->v.IfExp.body, PR_TEST + 1); | 
					
						
							|  |  |  |     APPEND_STR(" if "); | 
					
						
							|  |  |  |     APPEND_EXPR(e->v.IfExp.test, PR_TEST + 1); | 
					
						
							|  |  |  |     APPEND_STR(" else "); | 
					
						
							|  |  |  |     APPEND_EXPR(e->v.IfExp.orelse, PR_TEST); | 
					
						
							|  |  |  |     APPEND_STR_IF(level > PR_TEST, ")"); | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | append_ast_dict(_PyUnicodeWriter *writer, expr_ty e) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_ssize_t i, value_count; | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     expr_ty key_node; | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR("{"); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     value_count = asdl_seq_LEN(e->v.Dict.values); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (i = 0; i < value_count; i++) { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         APPEND_STR_IF(i > 0, ", "); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |         key_node = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i); | 
					
						
							|  |  |  |         if (key_node != NULL) { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |             APPEND_EXPR(key_node, PR_TEST); | 
					
						
							|  |  |  |             APPEND_STR(": "); | 
					
						
							|  |  |  |             APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Dict.values, i), PR_TEST); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         else { | 
					
						
							|  |  |  |             APPEND_STR("**"); | 
					
						
							|  |  |  |             APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Dict.values, i), PR_EXPR); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR_FINISH("}"); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | append_ast_set(_PyUnicodeWriter *writer, expr_ty e) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_ssize_t i, elem_count; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR("{"); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     elem_count = asdl_seq_LEN(e->v.Set.elts); | 
					
						
							|  |  |  |     for (i = 0; i < elem_count; i++) { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         APPEND_STR_IF(i > 0, ", "); | 
					
						
							|  |  |  |         APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Set.elts, i), PR_TEST); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR_FINISH("}"); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | append_ast_list(_PyUnicodeWriter *writer, expr_ty e) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_ssize_t i, elem_count; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR("["); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     elem_count = asdl_seq_LEN(e->v.List.elts); | 
					
						
							|  |  |  |     for (i = 0; i < elem_count; i++) { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         APPEND_STR_IF(i > 0, ", "); | 
					
						
							|  |  |  |         APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.List.elts, i), PR_TEST); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR_FINISH("]"); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  | append_ast_tuple(_PyUnicodeWriter *writer, expr_ty e, int level) | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | { | 
					
						
							|  |  |  |     Py_ssize_t i, elem_count; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     elem_count = asdl_seq_LEN(e->v.Tuple.elts); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     if (elem_count == 0) { | 
					
						
							|  |  |  |         APPEND_STR_FINISH("()"); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR_IF(level > PR_TUPLE, "("); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     for (i = 0; i < elem_count; i++) { | 
					
						
							|  |  |  |         APPEND_STR_IF(i > 0, ", "); | 
					
						
							|  |  |  |         APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Tuple.elts, i), PR_TEST); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR_IF(elem_count == 1, ","); | 
					
						
							|  |  |  |     APPEND_STR_IF(level > PR_TUPLE, ")"); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | append_ast_comprehension(_PyUnicodeWriter *writer, comprehension_ty gen) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Py_ssize_t i, if_count; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR(gen->is_async ? " async for " : " for "); | 
					
						
							|  |  |  |     APPEND_EXPR(gen->target, PR_TUPLE); | 
					
						
							|  |  |  |     APPEND_STR(" in "); | 
					
						
							|  |  |  |     APPEND_EXPR(gen->iter, PR_TEST + 1); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if_count = asdl_seq_LEN(gen->ifs); | 
					
						
							|  |  |  |     for (i = 0; i < if_count; i++) { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         APPEND_STR(" if "); | 
					
						
							|  |  |  |         APPEND_EXPR((expr_ty)asdl_seq_GET(gen->ifs, i), PR_TEST + 1); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  | append_ast_comprehensions(_PyUnicodeWriter *writer, asdl_comprehension_seq *comprehensions) | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | { | 
					
						
							|  |  |  |     Py_ssize_t i, gen_count; | 
					
						
							|  |  |  |     gen_count = asdl_seq_LEN(comprehensions); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (i = 0; i < gen_count; i++) { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         APPEND(comprehension, (comprehension_ty)asdl_seq_GET(comprehensions, i)); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  | append_ast_genexp(_PyUnicodeWriter *writer, expr_ty e) | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR("("); | 
					
						
							|  |  |  |     APPEND_EXPR(e->v.GeneratorExp.elt, PR_TEST); | 
					
						
							|  |  |  |     APPEND(comprehensions, e->v.GeneratorExp.generators); | 
					
						
							|  |  |  |     APPEND_STR_FINISH(")"); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | append_ast_listcomp(_PyUnicodeWriter *writer, expr_ty e) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR("["); | 
					
						
							|  |  |  |     APPEND_EXPR(e->v.ListComp.elt, PR_TEST); | 
					
						
							|  |  |  |     APPEND(comprehensions, e->v.ListComp.generators); | 
					
						
							|  |  |  |     APPEND_STR_FINISH("]"); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | append_ast_setcomp(_PyUnicodeWriter *writer, expr_ty e) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR("{"); | 
					
						
							|  |  |  |     APPEND_EXPR(e->v.SetComp.elt, PR_TEST); | 
					
						
							|  |  |  |     APPEND(comprehensions, e->v.SetComp.generators); | 
					
						
							|  |  |  |     APPEND_STR_FINISH("}"); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | append_ast_dictcomp(_PyUnicodeWriter *writer, expr_ty e) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR("{"); | 
					
						
							|  |  |  |     APPEND_EXPR(e->v.DictComp.key, PR_TEST); | 
					
						
							|  |  |  |     APPEND_STR(": "); | 
					
						
							|  |  |  |     APPEND_EXPR(e->v.DictComp.value, PR_TEST); | 
					
						
							|  |  |  |     APPEND(comprehensions, e->v.DictComp.generators); | 
					
						
							|  |  |  |     APPEND_STR_FINISH("}"); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  | append_ast_compare(_PyUnicodeWriter *writer, expr_ty e, int level) | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | { | 
					
						
							|  |  |  |     const char *op; | 
					
						
							|  |  |  |     Py_ssize_t i, comparator_count; | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  |     asdl_expr_seq *comparators; | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     asdl_int_seq *ops; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR_IF(level > PR_CMP, "("); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     comparators = e->v.Compare.comparators; | 
					
						
							|  |  |  |     ops = e->v.Compare.ops; | 
					
						
							|  |  |  |     comparator_count = asdl_seq_LEN(comparators); | 
					
						
							|  |  |  |     assert(comparator_count > 0); | 
					
						
							|  |  |  |     assert(comparator_count == asdl_seq_LEN(ops)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_EXPR(e->v.Compare.left, PR_CMP + 1); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (i = 0; i < comparator_count; i++) { | 
					
						
							|  |  |  |         switch ((cmpop_ty)asdl_seq_GET(ops, i)) { | 
					
						
							|  |  |  |         case Eq: | 
					
						
							|  |  |  |             op = " == "; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case NotEq: | 
					
						
							|  |  |  |             op = " != "; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Lt: | 
					
						
							|  |  |  |             op = " < "; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case LtE: | 
					
						
							|  |  |  |             op = " <= "; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Gt: | 
					
						
							|  |  |  |             op = " > "; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case GtE: | 
					
						
							|  |  |  |             op = " >= "; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case Is: | 
					
						
							|  |  |  |             op = " is "; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case IsNot: | 
					
						
							|  |  |  |             op = " is not "; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case In: | 
					
						
							|  |  |  |             op = " in "; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         case NotIn: | 
					
						
							|  |  |  |             op = " not in "; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             PyErr_SetString(PyExc_SystemError, | 
					
						
							|  |  |  |                             "unexpected comparison kind"); | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         APPEND_STR(op); | 
					
						
							|  |  |  |         APPEND_EXPR((expr_ty)asdl_seq_GET(comparators, i), PR_CMP + 1); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR_IF(level > PR_CMP, ")"); | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | append_ast_keyword(_PyUnicodeWriter *writer, keyword_ty kw) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (kw->arg == NULL) { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         APPEND_STR("**"); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         if (-1 == _PyUnicodeWriter_WriteStr(writer, kw->arg)) { | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         APPEND_STR("="); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_EXPR(kw->value, PR_TEST); | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | append_ast_call(_PyUnicodeWriter *writer, expr_ty e) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     bool first; | 
					
						
							|  |  |  |     Py_ssize_t i, arg_count, kw_count; | 
					
						
							|  |  |  |     expr_ty expr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_EXPR(e->v.Call.func, PR_ATOM); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     arg_count = asdl_seq_LEN(e->v.Call.args); | 
					
						
							|  |  |  |     kw_count = asdl_seq_LEN(e->v.Call.keywords); | 
					
						
							|  |  |  |     if (arg_count == 1 && kw_count == 0) { | 
					
						
							|  |  |  |         expr = (expr_ty)asdl_seq_GET(e->v.Call.args, 0); | 
					
						
							|  |  |  |         if (expr->kind == GeneratorExp_kind) { | 
					
						
							|  |  |  |             /* Special case: a single generator expression. */ | 
					
						
							|  |  |  |             return append_ast_genexp(writer, expr); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR("("); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     first = true; | 
					
						
							|  |  |  |     for (i = 0; i < arg_count; i++) { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         APPEND_STR_IF_NOT_FIRST(", "); | 
					
						
							|  |  |  |         APPEND_EXPR((expr_ty)asdl_seq_GET(e->v.Call.args, i), PR_TEST); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (i = 0; i < kw_count; i++) { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         APPEND_STR_IF_NOT_FIRST(", "); | 
					
						
							|  |  |  |         APPEND(keyword, (keyword_ty)asdl_seq_GET(e->v.Call.keywords, i)); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR_FINISH(")"); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | escape_braces(PyObject *orig) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyObject *temp; | 
					
						
							|  |  |  |     PyObject *result; | 
					
						
							|  |  |  |     temp = PyUnicode_Replace(orig, _str_open_br, _str_dbl_open_br, -1); | 
					
						
							|  |  |  |     if (!temp) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     result = PyUnicode_Replace(temp, _str_close_br, _str_dbl_close_br, -1); | 
					
						
							|  |  |  |     Py_DECREF(temp); | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | append_fstring_unicode(_PyUnicodeWriter *writer, PyObject *unicode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     PyObject *escaped; | 
					
						
							|  |  |  |     int result = -1; | 
					
						
							|  |  |  |     escaped = escape_braces(unicode); | 
					
						
							|  |  |  |     if (escaped) { | 
					
						
							|  |  |  |         result = _PyUnicodeWriter_WriteStr(writer, escaped); | 
					
						
							|  |  |  |         Py_DECREF(escaped); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | append_fstring_element(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     switch (e->kind) { | 
					
						
							|  |  |  |     case Constant_kind: | 
					
						
							|  |  |  |         return append_fstring_unicode(writer, e->v.Constant.value); | 
					
						
							|  |  |  |     case JoinedStr_kind: | 
					
						
							|  |  |  |         return append_joinedstr(writer, e, is_format_spec); | 
					
						
							|  |  |  |     case FormattedValue_kind: | 
					
						
							| 
									
										
										
										
											2020-03-08 11:53:59 -05:00
										 |  |  |         return append_formattedvalue(writer, e); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     default: | 
					
						
							|  |  |  |         PyErr_SetString(PyExc_SystemError, | 
					
						
							|  |  |  |                         "unknown expression kind inside f-string"); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Build body separately to enable wrapping the entire stream of Strs,
 | 
					
						
							|  |  |  |    Constants and FormattedValues in one opening and one closing quote. */ | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2020-09-16 19:42:00 +01:00
										 |  |  | build_fstring_body(asdl_expr_seq *values, bool is_format_spec) | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | { | 
					
						
							|  |  |  |     Py_ssize_t i, value_count; | 
					
						
							|  |  |  |     _PyUnicodeWriter body_writer; | 
					
						
							|  |  |  |     _PyUnicodeWriter_Init(&body_writer); | 
					
						
							|  |  |  |     body_writer.min_length = 256; | 
					
						
							|  |  |  |     body_writer.overallocate = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     value_count = asdl_seq_LEN(values); | 
					
						
							|  |  |  |     for (i = 0; i < value_count; ++i) { | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |         if (-1 == append_fstring_element(&body_writer, | 
					
						
							|  |  |  |                                          (expr_ty)asdl_seq_GET(values, i), | 
					
						
							|  |  |  |                                          is_format_spec | 
					
						
							|  |  |  |                                          )) { | 
					
						
							|  |  |  |             _PyUnicodeWriter_Dealloc(&body_writer); | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return _PyUnicodeWriter_Finish(&body_writer); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | append_joinedstr(_PyUnicodeWriter *writer, expr_ty e, bool is_format_spec) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int result = -1; | 
					
						
							|  |  |  |     PyObject *body = build_fstring_body(e->v.JoinedStr.values, is_format_spec); | 
					
						
							|  |  |  |     if (!body) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!is_format_spec) { | 
					
						
							|  |  |  |         if (-1 != append_charp(writer, "f") && | 
					
						
							|  |  |  |             -1 != append_repr(writer, body)) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             result = 0; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         result = _PyUnicodeWriter_WriteStr(writer, body); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     Py_DECREF(body); | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2020-03-08 11:53:59 -05:00
										 |  |  | append_formattedvalue(_PyUnicodeWriter *writer, expr_ty e) | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     const char *conversion; | 
					
						
							|  |  |  |     const char *outer_brace = "{"; | 
					
						
							|  |  |  |     /* Grammar allows PR_TUPLE, but use >PR_TEST for adding parenthesis
 | 
					
						
							|  |  |  |        around a lambda with ':' */ | 
					
						
							|  |  |  |     PyObject *temp_fv_str = expr_as_unicode(e->v.FormattedValue.value, PR_TEST + 1); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     if (!temp_fv_str) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (PyUnicode_Find(temp_fv_str, _str_open_br, 0, 1, 1) == 0) { | 
					
						
							|  |  |  |         /* Expression starts with a brace, split it with a space from the outer
 | 
					
						
							|  |  |  |            one. */ | 
					
						
							|  |  |  |         outer_brace = "{ "; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (-1 == append_charp(writer, outer_brace)) { | 
					
						
							|  |  |  |         Py_DECREF(temp_fv_str); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (-1 == _PyUnicodeWriter_WriteStr(writer, temp_fv_str)) { | 
					
						
							|  |  |  |         Py_DECREF(temp_fv_str); | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     Py_DECREF(temp_fv_str); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (e->v.FormattedValue.conversion > 0) { | 
					
						
							|  |  |  |         switch (e->v.FormattedValue.conversion) { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         case 'a': | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |             conversion = "!a"; | 
					
						
							|  |  |  |             break; | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         case 'r': | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |             conversion = "!r"; | 
					
						
							|  |  |  |             break; | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         case 's': | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |             conversion = "!s"; | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         default: | 
					
						
							|  |  |  |             PyErr_SetString(PyExc_SystemError, | 
					
						
							|  |  |  |                             "unknown f-value conversion kind"); | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         APPEND_STR(conversion); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-02-01 17:59:27 +01:00
										 |  |  |     if (e->v.FormattedValue.format_spec) { | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |         if (-1 == _PyUnicodeWriter_WriteASCIIString(writer, ":", 1) || | 
					
						
							|  |  |  |             -1 == append_fstring_element(writer, | 
					
						
							|  |  |  |                                          e->v.FormattedValue.format_spec, | 
					
						
							|  |  |  |                                          true | 
					
						
							|  |  |  |                                         )) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     APPEND_STR_FINISH("}"); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-14 01:51:31 +03:00
										 |  |  | static int | 
					
						
							|  |  |  | append_ast_constant(_PyUnicodeWriter *writer, PyObject *constant) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (PyTuple_CheckExact(constant)) { | 
					
						
							|  |  |  |         Py_ssize_t i, elem_count; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         elem_count = PyTuple_GET_SIZE(constant); | 
					
						
							|  |  |  |         APPEND_STR("("); | 
					
						
							|  |  |  |         for (i = 0; i < elem_count; i++) { | 
					
						
							|  |  |  |             APPEND_STR_IF(i > 0, ", "); | 
					
						
							|  |  |  |             if (append_ast_constant(writer, PyTuple_GET_ITEM(constant, i)) < 0) { | 
					
						
							|  |  |  |                 return -1; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         APPEND_STR_IF(elem_count == 1, ","); | 
					
						
							|  |  |  |         APPEND_STR(")"); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return append_repr(writer, constant); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | static int | 
					
						
							|  |  |  | append_ast_attribute(_PyUnicodeWriter *writer, expr_ty e) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     const char *period; | 
					
						
							| 
									
										
										
										
											2018-09-27 17:42:37 +03:00
										 |  |  |     expr_ty v = e->v.Attribute.value; | 
					
						
							|  |  |  |     APPEND_EXPR(v, PR_ATOM); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Special case: integers require a space for attribute access to be
 | 
					
						
							| 
									
										
										
										
											2018-09-27 17:42:37 +03:00
										 |  |  |        unambiguous. */ | 
					
						
							|  |  |  |     if (v->kind == Constant_kind && PyLong_CheckExact(v->v.Constant.value)) { | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |         period = " ."; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         period = "."; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR(period); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return _PyUnicodeWriter_WriteStr(writer, e->v.Attribute.attr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2020-03-10 18:52:34 +02:00
										 |  |  | append_ast_slice(_PyUnicodeWriter *writer, expr_ty e) | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-10 18:52:34 +02:00
										 |  |  |     if (e->v.Slice.lower) { | 
					
						
							|  |  |  |         APPEND_EXPR(e->v.Slice.lower, PR_TEST); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR(":"); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-10 18:52:34 +02:00
										 |  |  |     if (e->v.Slice.upper) { | 
					
						
							|  |  |  |         APPEND_EXPR(e->v.Slice.upper, PR_TEST); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-10 18:52:34 +02:00
										 |  |  |     if (e->v.Slice.step) { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         APPEND_STR(":"); | 
					
						
							| 
									
										
										
										
											2020-03-10 18:52:34 +02:00
										 |  |  |         APPEND_EXPR(e->v.Slice.step, PR_TEST); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | append_ast_subscript(_PyUnicodeWriter *writer, expr_ty e) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_EXPR(e->v.Subscript.value, PR_ATOM); | 
					
						
							| 
									
										
										
										
											2020-05-18 21:23:48 +03:00
										 |  |  |     int level = PR_TUPLE; | 
					
						
							|  |  |  |     expr_ty slice = e->v.Subscript.slice; | 
					
						
							|  |  |  |     if (slice->kind == Tuple_kind) { | 
					
						
							|  |  |  |         for (Py_ssize_t i = 0; i < asdl_seq_LEN(slice->v.Tuple.elts); i++) { | 
					
						
							|  |  |  |             expr_ty element = asdl_seq_GET(slice->v.Tuple.elts, i); | 
					
						
							|  |  |  |             if (element->kind == Starred_kind) { | 
					
						
							|  |  |  |                 ++level; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR("["); | 
					
						
							| 
									
										
										
										
											2020-05-18 21:23:48 +03:00
										 |  |  |     APPEND_EXPR(e->v.Subscript.slice, level); | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR_FINISH("]"); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | append_ast_starred(_PyUnicodeWriter *writer, expr_ty e) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR("*"); | 
					
						
							|  |  |  |     APPEND_EXPR(e->v.Starred.value, PR_EXPR); | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  | append_ast_yield(_PyUnicodeWriter *writer, expr_ty e) | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     if (!e->v.Yield.value) { | 
					
						
							|  |  |  |         APPEND_STR_FINISH("(yield)"); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR("(yield "); | 
					
						
							|  |  |  |     APPEND_EXPR(e->v.Yield.value, PR_TEST); | 
					
						
							|  |  |  |     APPEND_STR_FINISH(")"); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  | append_ast_yield_from(_PyUnicodeWriter *writer, expr_ty e) | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR("(yield from "); | 
					
						
							|  |  |  |     APPEND_EXPR(e->v.YieldFrom.value, PR_TEST); | 
					
						
							|  |  |  |     APPEND_STR_FINISH(")"); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  | append_ast_await(_PyUnicodeWriter *writer, expr_ty e, int level) | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     APPEND_STR_IF(level > PR_AWAIT, "("); | 
					
						
							|  |  |  |     APPEND_STR("await "); | 
					
						
							|  |  |  |     APPEND_EXPR(e->v.Await.value, PR_ATOM); | 
					
						
							|  |  |  |     APPEND_STR_IF(level > PR_AWAIT, ")"); | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-19 01:10:20 +03:00
										 |  |  | static int | 
					
						
							|  |  |  | append_named_expr(_PyUnicodeWriter *writer, expr_ty e, int level) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     APPEND_STR_IF(level > PR_TUPLE, "("); | 
					
						
							|  |  |  |     APPEND_EXPR(e->v.NamedExpr.target, PR_ATOM); | 
					
						
							| 
									
										
										
										
											2020-04-18 19:17:19 +03:00
										 |  |  |     APPEND_STR(" := "); | 
					
						
							| 
									
										
										
										
											2019-05-19 01:10:20 +03:00
										 |  |  |     APPEND_EXPR(e->v.NamedExpr.value, PR_ATOM); | 
					
						
							|  |  |  |     APPEND_STR_IF(level > PR_TUPLE, ")"); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  | append_ast_expr(_PyUnicodeWriter *writer, expr_ty e, int level) | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | { | 
					
						
							|  |  |  |     switch (e->kind) { | 
					
						
							|  |  |  |     case BoolOp_kind: | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         return append_ast_boolop(writer, e, level); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     case BinOp_kind: | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         return append_ast_binop(writer, e, level); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     case UnaryOp_kind: | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         return append_ast_unaryop(writer, e, level); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     case Lambda_kind: | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         return append_ast_lambda(writer, e, level); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     case IfExp_kind: | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         return append_ast_ifexp(writer, e, level); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     case Dict_kind: | 
					
						
							|  |  |  |         return append_ast_dict(writer, e); | 
					
						
							|  |  |  |     case Set_kind: | 
					
						
							|  |  |  |         return append_ast_set(writer, e); | 
					
						
							|  |  |  |     case GeneratorExp_kind: | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         return append_ast_genexp(writer, e); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     case ListComp_kind: | 
					
						
							|  |  |  |         return append_ast_listcomp(writer, e); | 
					
						
							|  |  |  |     case SetComp_kind: | 
					
						
							|  |  |  |         return append_ast_setcomp(writer, e); | 
					
						
							|  |  |  |     case DictComp_kind: | 
					
						
							|  |  |  |         return append_ast_dictcomp(writer, e); | 
					
						
							|  |  |  |     case Yield_kind: | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         return append_ast_yield(writer, e); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     case YieldFrom_kind: | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         return append_ast_yield_from(writer, e); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     case Await_kind: | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         return append_ast_await(writer, e, level); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     case Compare_kind: | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         return append_ast_compare(writer, e, level); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     case Call_kind: | 
					
						
							|  |  |  |         return append_ast_call(writer, e); | 
					
						
							|  |  |  |     case Constant_kind: | 
					
						
							| 
									
										
										
										
											2018-09-27 17:42:37 +03:00
										 |  |  |         if (e->v.Constant.value == Py_Ellipsis) { | 
					
						
							|  |  |  |             APPEND_STR_FINISH("..."); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-04-14 23:21:22 +03:00
										 |  |  |         if (e->v.Constant.kind != NULL | 
					
						
							|  |  |  |             && -1 == _PyUnicodeWriter_WriteStr(writer, e->v.Constant.kind)) { | 
					
						
							|  |  |  |             return -1; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-04-14 01:51:31 +03:00
										 |  |  |         return append_ast_constant(writer, e->v.Constant.value); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     case JoinedStr_kind: | 
					
						
							|  |  |  |         return append_joinedstr(writer, e, false); | 
					
						
							|  |  |  |     case FormattedValue_kind: | 
					
						
							| 
									
										
										
										
											2020-03-08 11:53:59 -05:00
										 |  |  |         return append_formattedvalue(writer, e); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     /* The following exprs can be assignment targets. */ | 
					
						
							|  |  |  |     case Attribute_kind: | 
					
						
							|  |  |  |         return append_ast_attribute(writer, e); | 
					
						
							|  |  |  |     case Subscript_kind: | 
					
						
							|  |  |  |         return append_ast_subscript(writer, e); | 
					
						
							|  |  |  |     case Starred_kind: | 
					
						
							|  |  |  |         return append_ast_starred(writer, e); | 
					
						
							| 
									
										
										
										
											2020-03-10 18:52:34 +02:00
										 |  |  |     case Slice_kind: | 
					
						
							|  |  |  |         return append_ast_slice(writer, e); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     case Name_kind: | 
					
						
							|  |  |  |         return _PyUnicodeWriter_WriteStr(writer, e->v.Name.id); | 
					
						
							|  |  |  |     case List_kind: | 
					
						
							|  |  |  |         return append_ast_list(writer, e); | 
					
						
							|  |  |  |     case Tuple_kind: | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         return append_ast_tuple(writer, e, level); | 
					
						
							| 
									
										
										
										
											2019-05-19 01:10:20 +03:00
										 |  |  |     case NamedExpr_kind: | 
					
						
							|  |  |  |         return append_named_expr(writer, e, level); | 
					
						
							| 
									
										
										
										
											2021-04-29 15:58:44 +10:00
										 |  |  |     // No default so compiler emits a warning for unhandled cases
 | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-04-29 15:58:44 +10:00
										 |  |  |     PyErr_SetString(PyExc_SystemError, | 
					
						
							|  |  |  |                     "unknown expression kind"); | 
					
						
							|  |  |  |     return -1; | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int | 
					
						
							| 
									
										
										
										
											2018-02-01 17:59:27 +01:00
										 |  |  | maybe_init_static_strings(void) | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!_str_open_br && | 
					
						
							|  |  |  |         !(_str_open_br = PyUnicode_InternFromString("{"))) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!_str_dbl_open_br && | 
					
						
							|  |  |  |         !(_str_dbl_open_br = PyUnicode_InternFromString("{{"))) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!_str_close_br && | 
					
						
							|  |  |  |         !(_str_close_br = PyUnicode_InternFromString("}"))) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!_str_dbl_close_br && | 
					
						
							|  |  |  |         !(_str_dbl_close_br = PyUnicode_InternFromString("}}"))) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-04-14 01:51:31 +03:00
										 |  |  |     if (!_str_inf && | 
					
						
							|  |  |  |         !(_str_inf = PyUnicode_FromString("inf"))) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!_str_replace_inf && | 
					
						
							|  |  |  |         !(_str_replace_inf = PyUnicode_FromFormat("1e%d", 1 + DBL_MAX_10_EXP))) { | 
					
						
							|  |  |  |         return -1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  | expr_as_unicode(expr_ty e, int level) | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | { | 
					
						
							|  |  |  |     _PyUnicodeWriter writer; | 
					
						
							|  |  |  |     _PyUnicodeWriter_Init(&writer); | 
					
						
							|  |  |  |     writer.min_length = 256; | 
					
						
							|  |  |  |     writer.overallocate = 1; | 
					
						
							|  |  |  |     if (-1 == maybe_init_static_strings() || | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |         -1 == append_ast_expr(&writer, e, level)) | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  |     { | 
					
						
							|  |  |  |         _PyUnicodeWriter_Dealloc(&writer); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return _PyUnicodeWriter_Finish(&writer); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  | _PyAST_ExprAsUnicode(expr_ty e) | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-05-17 06:17:48 +03:00
										 |  |  |     return expr_as_unicode(e, PR_TEST); | 
					
						
							| 
									
										
										
										
											2018-01-26 08:20:18 -08:00
										 |  |  | } |