diff options
author | shadchin <[email protected]> | 2022-02-10 16:44:30 +0300 |
---|---|---|
committer | Daniil Cherednik <[email protected]> | 2022-02-10 16:44:30 +0300 |
commit | 2598ef1d0aee359b4b6d5fdd1758916d5907d04f (patch) | |
tree | 012bb94d777798f1f56ac1cec429509766d05181 /contrib/tools/python3/src/Python/ast.c | |
parent | 6751af0b0c1b952fede40b19b71da8025b5d8bcf (diff) |
Restoring authorship annotation for <[email protected]>. Commit 1 of 2.
Diffstat (limited to 'contrib/tools/python3/src/Python/ast.c')
-rw-r--r-- | contrib/tools/python3/src/Python/ast.c | 2418 |
1 files changed, 1209 insertions, 1209 deletions
diff --git a/contrib/tools/python3/src/Python/ast.c b/contrib/tools/python3/src/Python/ast.c index 6dd70592631..cf9d61ccd20 100644 --- a/contrib/tools/python3/src/Python/ast.c +++ b/contrib/tools/python3/src/Python/ast.c @@ -13,8 +13,8 @@ #include <assert.h> #include <stdbool.h> -#define MAXLEVEL 200 /* Max parentheses level */ - +#define MAXLEVEL 200 /* Max parentheses level */ + static int validate_stmts(asdl_seq *); static int validate_exprs(asdl_seq *, expr_context_ty, int); static int validate_nonempty_seq(asdl_seq *, const char *, const char *); @@ -22,28 +22,28 @@ static int validate_stmt(stmt_ty); static int validate_expr(expr_ty, expr_context_ty); static int -validate_name(PyObject *name) -{ - assert(PyUnicode_Check(name)); - static const char * const forbidden[] = { - "None", - "True", - "False", - NULL - }; - for (int i = 0; forbidden[i] != NULL; i++) { - if (_PyUnicode_EqualToASCIIString(name, forbidden[i])) { - PyErr_Format(PyExc_ValueError, "Name node can't be used with '%s' constant", forbidden[i]); - return 0; - } - } - return 1; -} - -static int +validate_name(PyObject *name) +{ + assert(PyUnicode_Check(name)); + static const char * const forbidden[] = { + "None", + "True", + "False", + NULL + }; + for (int i = 0; forbidden[i] != NULL; i++) { + if (_PyUnicode_EqualToASCIIString(name, forbidden[i])) { + PyErr_Format(PyExc_ValueError, "Name node can't be used with '%s' constant", forbidden[i]); + return 0; + } + } + return 1; +} + +static int validate_comprehension(asdl_seq *gens) { - Py_ssize_t i; + Py_ssize_t i; if (!asdl_seq_LEN(gens)) { PyErr_SetString(PyExc_ValueError, "comprehension with no generators"); return 0; @@ -61,7 +61,7 @@ validate_comprehension(asdl_seq *gens) static int validate_keywords(asdl_seq *keywords) { - Py_ssize_t i; + Py_ssize_t i; for (i = 0; i < asdl_seq_LEN(keywords); i++) if (!validate_expr(((keyword_ty)asdl_seq_GET(keywords, i))->value, Load)) return 0; @@ -71,7 +71,7 @@ validate_keywords(asdl_seq *keywords) static int validate_args(asdl_seq *args) { - Py_ssize_t i; + Py_ssize_t i; for (i = 0; i < asdl_seq_LEN(args); i++) { arg_ty arg = asdl_seq_GET(args, i); if (arg->annotation && !validate_expr(arg->annotation, Load)) @@ -98,9 +98,9 @@ expr_context_name(expr_context_ty ctx) static int validate_arguments(arguments_ty args) { - if (!validate_args(args->posonlyargs) || !validate_args(args->args)) { + if (!validate_args(args->posonlyargs) || !validate_args(args->args)) { return 0; - } + } if (args->vararg && args->vararg->annotation && !validate_expr(args->vararg->annotation, Load)) { return 0; @@ -111,7 +111,7 @@ validate_arguments(arguments_ty args) && !validate_expr(args->kwarg->annotation, Load)) { return 0; } - if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->posonlyargs) + asdl_seq_LEN(args->args)) { + if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->posonlyargs) + asdl_seq_LEN(args->args)) { PyErr_SetString(PyExc_ValueError, "more positional defaults than args on arguments"); return 0; } @@ -166,11 +166,11 @@ validate_constant(PyObject *value) return 1; } - if (!PyErr_Occurred()) { - PyErr_Format(PyExc_TypeError, - "got an invalid type in Constant: %s", - _PyType_Name(Py_TYPE(value))); - } + if (!PyErr_Occurred()) { + PyErr_Format(PyExc_TypeError, + "got an invalid type in Constant: %s", + _PyType_Name(Py_TYPE(value))); + } return 0; } @@ -192,9 +192,9 @@ validate_expr(expr_ty exp, expr_context_ty ctx) actual_ctx = exp->v.Starred.ctx; break; case Name_kind: - if (!validate_name(exp->v.Name.id)) { - return 0; - } + if (!validate_name(exp->v.Name.id)) { + return 0; + } actual_ctx = exp->v.Name.ctx; break; case List_kind: @@ -302,26 +302,26 @@ validate_expr(expr_ty exp, expr_context_ty ctx) case Attribute_kind: return validate_expr(exp->v.Attribute.value, Load); case Subscript_kind: - return validate_expr(exp->v.Subscript.slice, Load) && + return validate_expr(exp->v.Subscript.slice, Load) && validate_expr(exp->v.Subscript.value, Load); case Starred_kind: return validate_expr(exp->v.Starred.value, ctx); - case Slice_kind: - return (!exp->v.Slice.lower || validate_expr(exp->v.Slice.lower, Load)) && - (!exp->v.Slice.upper || validate_expr(exp->v.Slice.upper, Load)) && - (!exp->v.Slice.step || validate_expr(exp->v.Slice.step, Load)); + case Slice_kind: + return (!exp->v.Slice.lower || validate_expr(exp->v.Slice.lower, Load)) && + (!exp->v.Slice.upper || validate_expr(exp->v.Slice.upper, Load)) && + (!exp->v.Slice.step || validate_expr(exp->v.Slice.step, Load)); case List_kind: return validate_exprs(exp->v.List.elts, ctx, 0); case Tuple_kind: return validate_exprs(exp->v.Tuple.elts, ctx, 0); - case NamedExpr_kind: - return validate_expr(exp->v.NamedExpr.value, Load); - /* This last case doesn't have any checking. */ + case NamedExpr_kind: + return validate_expr(exp->v.NamedExpr.value, Load); + /* This last case doesn't have any checking. */ case Name_kind: return 1; } - PyErr_SetString(PyExc_SystemError, "unexpected expression"); - return 0; + PyErr_SetString(PyExc_SystemError, "unexpected expression"); + return 0; } static int @@ -349,7 +349,7 @@ validate_body(asdl_seq *body, const char *owner) static int validate_stmt(stmt_ty stmt) { - Py_ssize_t i; + Py_ssize_t i; switch (stmt->kind) { case FunctionDef_kind: return validate_body(stmt->v.FunctionDef.body, "FunctionDef") && @@ -491,7 +491,7 @@ validate_stmt(stmt_ty stmt) static int validate_stmts(asdl_seq *seq) { - Py_ssize_t i; + Py_ssize_t i; for (i = 0; i < asdl_seq_LEN(seq); i++) { stmt_ty stmt = asdl_seq_GET(seq, i); if (stmt) { @@ -510,7 +510,7 @@ validate_stmts(asdl_seq *seq) static int validate_exprs(asdl_seq *exprs, expr_context_ty ctx, int null_ok) { - Py_ssize_t i; + Py_ssize_t i; for (i = 0; i < asdl_seq_LEN(exprs); i++) { expr_ty expr = asdl_seq_GET(exprs, i); if (expr) { @@ -560,7 +560,7 @@ struct compiling { PyArena *c_arena; /* Arena for allocating memory. */ PyObject *c_filename; /* filename */ PyObject *c_normalize; /* Normalization function from unicodedata. */ - int c_feature_version; /* Latest minor version of Python for allowed features */ + int c_feature_version; /* Latest minor version of Python for allowed features */ }; static asdl_seq *seq_for_testlist(struct compiling *, const node *); @@ -576,12 +576,12 @@ static stmt_ty ast_for_with_stmt(struct compiling *, const node *, bool); static stmt_ty ast_for_for_stmt(struct compiling *, const node *, bool); /* Note different signature for ast_for_call */ -static expr_ty ast_for_call(struct compiling *, const node *, expr_ty, - const node *, const node *, const node *); +static expr_ty ast_for_call(struct compiling *, const node *, expr_ty, + const node *, const node *, const node *); static PyObject *parsenumber(struct compiling *, const char *); static expr_ty parsestrplus(struct compiling *, const node *n); -static void get_last_end_pos(asdl_seq *, int *, int *); +static void get_last_end_pos(asdl_seq *, int *, int *); #define COMP_GENEXP 0 #define COMP_LISTCOMP 1 @@ -616,7 +616,7 @@ new_identifier(const char *n, struct compiling *c) Py_DECREF(id); return NULL; } - PyObject *form = PyUnicode_InternFromString("NFKC"); + PyObject *form = PyUnicode_InternFromString("NFKC"); if (form == NULL) { Py_DECREF(id); return NULL; @@ -624,14 +624,14 @@ new_identifier(const char *n, struct compiling *c) PyObject *args[2] = {form, id}; id2 = _PyObject_FastCall(c->c_normalize, args, 2); Py_DECREF(id); - Py_DECREF(form); + Py_DECREF(form); if (!id2) return NULL; if (!PyUnicode_Check(id2)) { PyErr_Format(PyExc_TypeError, "unicodedata.normalize() must return a string, not " "%.200s", - _PyType_Name(Py_TYPE(id2))); + _PyType_Name(Py_TYPE(id2))); Py_DECREF(id2); return NULL; } @@ -648,25 +648,25 @@ new_identifier(const char *n, struct compiling *c) #define NEW_IDENTIFIER(n) new_identifier(STR(n), c) static int -ast_error(struct compiling *c, const node *n, const char *errmsg, ...) +ast_error(struct compiling *c, const node *n, const char *errmsg, ...) { PyObject *value, *errstr, *loc, *tmp; - va_list va; - - va_start(va, errmsg); - errstr = PyUnicode_FromFormatV(errmsg, va); - va_end(va); - if (!errstr) { - return 0; - } + va_list va; + + va_start(va, errmsg); + errstr = PyUnicode_FromFormatV(errmsg, va); + va_end(va); + if (!errstr) { + return 0; + } loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n)); if (!loc) { Py_INCREF(Py_None); loc = Py_None; } - tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc); - if (!tmp) { - Py_DECREF(errstr); + tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset + 1, loc); + if (!tmp) { + Py_DECREF(errstr); return 0; } value = PyTuple_Pack(2, errstr, tmp); @@ -693,20 +693,20 @@ ast_error(struct compiling *c, const node *n, const char *errmsg, ...) small_stmt elements is returned. */ -static string -new_type_comment(const char *s, struct compiling *c) -{ - PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL); - if (res == NULL) - return NULL; - if (PyArena_AddPyObject(c->c_arena, res) < 0) { - Py_DECREF(res); - return NULL; - } - return res; -} -#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c) - +static string +new_type_comment(const char *s, struct compiling *c) +{ + PyObject *res = PyUnicode_DecodeUTF8(s, strlen(s), NULL); + if (res == NULL) + return NULL; + if (PyArena_AddPyObject(c->c_arena, res) < 0) { + Py_DECREF(res); + return NULL; + } + return res; +} +#define NEW_TYPE_COMMENT(n) new_type_comment(STR(n), c) + static int num_stmts(const node *n) { @@ -734,23 +734,23 @@ num_stmts(const node *n) case simple_stmt: return NCH(n) / 2; /* Divide by 2 to remove count of semi-colons */ case suite: - case func_body_suite: - /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */ - /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ + case func_body_suite: + /* func_body_suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */ + /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */ if (NCH(n) == 1) return num_stmts(CHILD(n, 0)); else { - i = 2; + i = 2; l = 0; - if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) - i += 2; - for (; i < (NCH(n) - 1); i++) + if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) + i += 2; + for (; i < (NCH(n) - 1); i++) l += num_stmts(CHILD(n, i)); return l; } default: { - _Py_FatalErrorFormat(__func__, "Non-statement found: %d %d", - TYPE(n), NCH(n)); + _Py_FatalErrorFormat(__func__, "Non-statement found: %d %d", + TYPE(n), NCH(n)); } } Py_UNREACHABLE(); @@ -765,20 +765,20 @@ PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags, { int i, j, k, num; asdl_seq *stmts = NULL; - asdl_seq *type_ignores = NULL; + asdl_seq *type_ignores = NULL; stmt_ty s; node *ch; struct compiling c; mod_ty res = NULL; - asdl_seq *argtypes = NULL; - expr_ty ret, arg; + asdl_seq *argtypes = NULL; + expr_ty ret, arg; c.c_arena = arena; /* borrowed reference */ c.c_filename = filename; c.c_normalize = NULL; - c.c_feature_version = flags && (flags->cf_flags & PyCF_ONLY_AST) ? - flags->cf_feature_version : PY_MINOR_VERSION; + c.c_feature_version = flags && (flags->cf_flags & PyCF_ONLY_AST) ? + flags->cf_feature_version : PY_MINOR_VERSION; if (TYPE(n) == encoding_decl) n = CHILD(n, 0); @@ -812,26 +812,26 @@ PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags, } } } - - /* Type ignores are stored under the ENDMARKER in file_input. */ - ch = CHILD(n, NCH(n) - 1); - REQ(ch, ENDMARKER); - num = NCH(ch); - type_ignores = _Py_asdl_seq_new(num, arena); - if (!type_ignores) - goto out; - - for (i = 0; i < num; i++) { - string type_comment = new_type_comment(STR(CHILD(ch, i)), &c); - if (!type_comment) - goto out; - type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), type_comment, arena); - if (!ti) - goto out; - asdl_seq_SET(type_ignores, i, ti); - } - - res = Module(stmts, type_ignores, arena); + + /* Type ignores are stored under the ENDMARKER in file_input. */ + ch = CHILD(n, NCH(n) - 1); + REQ(ch, ENDMARKER); + num = NCH(ch); + type_ignores = _Py_asdl_seq_new(num, arena); + if (!type_ignores) + goto out; + + for (i = 0; i < num; i++) { + string type_comment = new_type_comment(STR(CHILD(ch, i)), &c); + if (!type_comment) + goto out; + type_ignore_ty ti = TypeIgnore(LINENO(CHILD(ch, i)), type_comment, arena); + if (!ti) + goto out; + asdl_seq_SET(type_ignores, i, ti); + } + + res = Module(stmts, type_ignores, arena); break; case eval_input: { expr_ty testlist_ast; @@ -849,7 +849,7 @@ PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags, if (!stmts) goto out; asdl_seq_SET(stmts, 0, Pass(n->n_lineno, n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, + n->n_end_lineno, n->n_end_col_offset, arena)); if (!asdl_seq_GET(stmts, 0)) goto out; @@ -883,46 +883,46 @@ PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags, res = Interactive(stmts, arena); } break; - case func_type_input: - n = CHILD(n, 0); - REQ(n, func_type); - - if (TYPE(CHILD(n, 1)) == typelist) { - ch = CHILD(n, 1); - /* this is overly permissive -- we don't pay any attention to - * stars on the args -- just parse them into an ordered list */ - num = 0; - for (i = 0; i < NCH(ch); i++) { - if (TYPE(CHILD(ch, i)) == test) { - num++; - } - } - - argtypes = _Py_asdl_seq_new(num, arena); - if (!argtypes) - goto out; - - j = 0; - for (i = 0; i < NCH(ch); i++) { - if (TYPE(CHILD(ch, i)) == test) { - arg = ast_for_expr(&c, CHILD(ch, i)); - if (!arg) - goto out; - asdl_seq_SET(argtypes, j++, arg); - } - } - } - else { - argtypes = _Py_asdl_seq_new(0, arena); - if (!argtypes) - goto out; - } - - ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1)); - if (!ret) - goto out; - res = FunctionType(argtypes, ret, arena); - break; + case func_type_input: + n = CHILD(n, 0); + REQ(n, func_type); + + if (TYPE(CHILD(n, 1)) == typelist) { + ch = CHILD(n, 1); + /* this is overly permissive -- we don't pay any attention to + * stars on the args -- just parse them into an ordered list */ + num = 0; + for (i = 0; i < NCH(ch); i++) { + if (TYPE(CHILD(ch, i)) == test) { + num++; + } + } + + argtypes = _Py_asdl_seq_new(num, arena); + if (!argtypes) + goto out; + + j = 0; + for (i = 0; i < NCH(ch); i++) { + if (TYPE(CHILD(ch, i)) == test) { + arg = ast_for_expr(&c, CHILD(ch, i)); + if (!arg) + goto out; + asdl_seq_SET(argtypes, j++, arg); + } + } + } + else { + argtypes = _Py_asdl_seq_new(0, arena); + if (!argtypes) + goto out; + } + + ret = ast_for_expr(&c, CHILD(n, NCH(n) - 1)); + if (!ret) + goto out; + res = FunctionType(argtypes, ret, arena); + break; default: PyErr_Format(PyExc_SystemError, "invalid node %d for PyAST_FromNode", TYPE(n)); @@ -954,7 +954,7 @@ PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str, */ static operator_ty -get_operator(struct compiling *c, const node *n) +get_operator(struct compiling *c, const node *n) { switch (TYPE(n)) { case VBAR: @@ -974,11 +974,11 @@ get_operator(struct compiling *c, const node *n) case STAR: return Mult; case AT: - if (c->c_feature_version < 5) { - ast_error(c, n, - "The '@' operator is only supported in Python 3.5 and greater"); - return (operator_ty)0; - } + if (c->c_feature_version < 5) { + ast_error(c, n, + "The '@' operator is only supported in Python 3.5 and greater"); + return (operator_ty)0; + } return MatMult; case SLASH: return Div; @@ -995,7 +995,7 @@ static const char * const FORBIDDEN[] = { "None", "True", "False", - "__debug__", + "__debug__", NULL, }; @@ -1004,107 +1004,107 @@ forbidden_name(struct compiling *c, identifier name, const node *n, int full_checks) { assert(PyUnicode_Check(name)); - const char * const *p = FORBIDDEN; - if (!full_checks) { - /* In most cases, the parser will protect True, False, and None - from being assign to. */ - p += 3; - } - for (; *p; p++) { - if (_PyUnicode_EqualToASCIIString(name, *p)) { - ast_error(c, n, "cannot assign to %U", name); - return 1; + const char * const *p = FORBIDDEN; + if (!full_checks) { + /* In most cases, the parser will protect True, False, and None + from being assign to. */ + p += 3; + } + for (; *p; p++) { + if (_PyUnicode_EqualToASCIIString(name, *p)) { + ast_error(c, n, "cannot assign to %U", name); + return 1; } } return 0; } -static expr_ty -copy_location(expr_ty e, const node *n, const node *end) -{ - if (e) { - e->lineno = LINENO(n); - e->col_offset = n->n_col_offset; - e->end_lineno = end->n_end_lineno; - e->end_col_offset = end->n_end_col_offset; - } - return e; -} - -static const char * -get_expr_name(expr_ty e) -{ - switch (e->kind) { - case Attribute_kind: - return "attribute"; - case Subscript_kind: - return "subscript"; - case Starred_kind: - return "starred"; - case Name_kind: - return "name"; - case List_kind: - return "list"; - case Tuple_kind: - return "tuple"; - case Lambda_kind: - return "lambda"; - case Call_kind: - return "function call"; - case BoolOp_kind: - case BinOp_kind: - case UnaryOp_kind: - return "operator"; - case GeneratorExp_kind: - return "generator expression"; - case Yield_kind: - case YieldFrom_kind: - return "yield expression"; - case Await_kind: - return "await expression"; - case ListComp_kind: - return "list comprehension"; - case SetComp_kind: - return "set comprehension"; - case DictComp_kind: - return "dict comprehension"; - case Dict_kind: - return "dict display"; - case Set_kind: - return "set display"; - case JoinedStr_kind: - case FormattedValue_kind: - return "f-string expression"; - case Constant_kind: { - PyObject *value = e->v.Constant.value; - if (value == Py_None) { - return "None"; - } - if (value == Py_False) { - return "False"; - } - if (value == Py_True) { - return "True"; - } - if (value == Py_Ellipsis) { - return "Ellipsis"; - } - return "literal"; - } - case Compare_kind: - return "comparison"; - case IfExp_kind: - return "conditional expression"; - case NamedExpr_kind: - return "named expression"; - default: - PyErr_Format(PyExc_SystemError, - "unexpected expression in assignment %d (line %d)", - e->kind, e->lineno); - return NULL; - } -} - +static expr_ty +copy_location(expr_ty e, const node *n, const node *end) +{ + if (e) { + e->lineno = LINENO(n); + e->col_offset = n->n_col_offset; + e->end_lineno = end->n_end_lineno; + e->end_col_offset = end->n_end_col_offset; + } + return e; +} + +static const char * +get_expr_name(expr_ty e) +{ + switch (e->kind) { + case Attribute_kind: + return "attribute"; + case Subscript_kind: + return "subscript"; + case Starred_kind: + return "starred"; + case Name_kind: + return "name"; + case List_kind: + return "list"; + case Tuple_kind: + return "tuple"; + case Lambda_kind: + return "lambda"; + case Call_kind: + return "function call"; + case BoolOp_kind: + case BinOp_kind: + case UnaryOp_kind: + return "operator"; + case GeneratorExp_kind: + return "generator expression"; + case Yield_kind: + case YieldFrom_kind: + return "yield expression"; + case Await_kind: + return "await expression"; + case ListComp_kind: + return "list comprehension"; + case SetComp_kind: + return "set comprehension"; + case DictComp_kind: + return "dict comprehension"; + case Dict_kind: + return "dict display"; + case Set_kind: + return "set display"; + case JoinedStr_kind: + case FormattedValue_kind: + return "f-string expression"; + case Constant_kind: { + PyObject *value = e->v.Constant.value; + if (value == Py_None) { + return "None"; + } + if (value == Py_False) { + return "False"; + } + if (value == Py_True) { + return "True"; + } + if (value == Py_Ellipsis) { + return "Ellipsis"; + } + return "literal"; + } + case Compare_kind: + return "comparison"; + case IfExp_kind: + return "conditional expression"; + case NamedExpr_kind: + return "named expression"; + default: + PyErr_Format(PyExc_SystemError, + "unexpected expression in assignment %d (line %d)", + e->kind, e->lineno); + return NULL; + } +} + /* Set the context ctx for expr_ty e, recursively traversing e. Only sets context for expr kinds that "can appear in assignment context" @@ -1117,7 +1117,7 @@ set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n) { asdl_seq *s = NULL; - /* Expressions in an augmented assignment have a Store context. */ + /* Expressions in an augmented assignment have a Store context. */ switch (e->kind) { case Attribute_kind: @@ -1148,22 +1148,22 @@ set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n) e->v.Tuple.ctx = ctx; s = e->v.Tuple.elts; break; - default: { - const char *expr_name = get_expr_name(e); - if (expr_name != NULL) { - ast_error(c, n, "cannot %s %s", - ctx == Store ? "assign to" : "delete", - expr_name); - } + default: { + const char *expr_name = get_expr_name(e); + if (expr_name != NULL) { + ast_error(c, n, "cannot %s %s", + ctx == Store ? "assign to" : "delete", + expr_name); + } return 0; - } + } } /* If the LHS is a list or tuple, we need to set the assignment context for all the contained elements. */ if (s) { - Py_ssize_t i; + Py_ssize_t i; for (i = 0; i < asdl_seq_LEN(s); i++) { if (!set_context(c, (expr_ty)asdl_seq_GET(s, i), ctx, n)) @@ -1206,11 +1206,11 @@ ast_for_augassign(struct compiling *c, const node *n) else return Mult; case '@': - if (c->c_feature_version < 5) { - ast_error(c, n, - "The '@' operator is only supported in Python 3.5 and greater"); - return (operator_ty)0; - } + if (c->c_feature_version < 5) { + ast_error(c, n, + "The '@' operator is only supported in Python 3.5 and greater"); + return (operator_ty)0; + } return MatMult; default: PyErr_Format(PyExc_SystemError, "invalid augassign: %s", STR(n)); @@ -1289,7 +1289,7 @@ seq_for_testlist(struct compiling *c, const node *n) for (i = 0; i < NCH(n); i += 2) { const node *ch = CHILD(n, i); - assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test); + assert(TYPE(ch) == test || TYPE(ch) == test_nocond || TYPE(ch) == star_expr || TYPE(ch) == namedexpr_test); expression = ast_for_expr(c, ch); if (!expression) @@ -1323,8 +1323,8 @@ ast_for_arg(struct compiling *c, const node *n) return NULL; } - ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + ret = arg(name, annotation, NULL, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); if (!ret) return NULL; return ret; @@ -1343,7 +1343,7 @@ handle_keywordonly_args(struct compiling *c, const node *n, int start, PyObject *argname; node *ch; expr_ty expression, annotation; - arg_ty arg = NULL; + arg_ty arg = NULL; int i = start; int j = 0; /* index for kwdefaults and kwonlyargs */ @@ -1382,23 +1382,23 @@ handle_keywordonly_args(struct compiling *c, const node *n, int start, goto error; if (forbidden_name(c, argname, ch, 0)) goto error; - arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset, - ch->n_end_lineno, ch->n_end_col_offset, + arg = arg(argname, annotation, NULL, LINENO(ch), ch->n_col_offset, + ch->n_end_lineno, ch->n_end_col_offset, c->c_arena); if (!arg) goto error; asdl_seq_SET(kwonlyargs, j++, arg); - i += 1; /* the name */ - if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) - i += 1; /* the comma, if present */ - break; - case TYPE_COMMENT: - /* arg will be equal to the last argument processed */ - arg->type_comment = NEW_TYPE_COMMENT(ch); - if (!arg->type_comment) - goto error; - i += 1; + i += 1; /* the name */ + if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) + i += 1; /* the comma, if present */ break; + case TYPE_COMMENT: + /* arg will be equal to the last argument processed */ + arg->type_comment = NEW_TYPE_COMMENT(ch); + if (!arg->type_comment) + goto error; + i += 1; + break; case DOUBLESTAR: return i; default: @@ -1420,73 +1420,73 @@ ast_for_arguments(struct compiling *c, const node *n) and varargslist (lambda definition). parameters: '(' [typedargslist] ')' - - The following definition for typedarglist is equivalent to this set of rules: - - arguments = argument (',' [TYPE_COMMENT] argument)* - argument = tfpdef ['=' test] - kwargs = '**' tfpdef [','] [TYPE_COMMENT] - args = '*' [tfpdef] - kwonly_kwargs = (',' [TYPE_COMMENT] argument)* (TYPE_COMMENT | [',' - [TYPE_COMMENT] [kwargs]]) - args_kwonly_kwargs = args kwonly_kwargs | kwargs - poskeyword_args_kwonly_kwargs = arguments ( TYPE_COMMENT | [',' - [TYPE_COMMENT] [args_kwonly_kwargs]]) - typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs - typedarglist = (arguments ',' [TYPE_COMMENT] '/' [',' [[TYPE_COMMENT] - typedargslist_no_posonly]])|(typedargslist_no_posonly)" - - typedargslist: ( (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])* - ',' [TYPE_COMMENT] '/' [',' [ [TYPE_COMMENT] tfpdef ['=' test] ( ',' - [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*' - [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' - [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [','] - [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* - (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | - '**' tfpdef [','] [TYPE_COMMENT]]] ) | (tfpdef ['=' test] (',' - [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*' - [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' - [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [','] - [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* - (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | - '**' tfpdef [','] [TYPE_COMMENT])) - + + The following definition for typedarglist is equivalent to this set of rules: + + arguments = argument (',' [TYPE_COMMENT] argument)* + argument = tfpdef ['=' test] + kwargs = '**' tfpdef [','] [TYPE_COMMENT] + args = '*' [tfpdef] + kwonly_kwargs = (',' [TYPE_COMMENT] argument)* (TYPE_COMMENT | [',' + [TYPE_COMMENT] [kwargs]]) + args_kwonly_kwargs = args kwonly_kwargs | kwargs + poskeyword_args_kwonly_kwargs = arguments ( TYPE_COMMENT | [',' + [TYPE_COMMENT] [args_kwonly_kwargs]]) + typedargslist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs + typedarglist = (arguments ',' [TYPE_COMMENT] '/' [',' [[TYPE_COMMENT] + typedargslist_no_posonly]])|(typedargslist_no_posonly)" + + typedargslist: ( (tfpdef ['=' test] (',' [TYPE_COMMENT] tfpdef ['=' test])* + ',' [TYPE_COMMENT] '/' [',' [ [TYPE_COMMENT] tfpdef ['=' test] ( ',' + [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*' + [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' + [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [','] + [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* + (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | + '**' tfpdef [','] [TYPE_COMMENT]]] ) | (tfpdef ['=' test] (',' + [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' [TYPE_COMMENT] [ '*' + [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* (TYPE_COMMENT | [',' + [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | '**' tfpdef [','] + [TYPE_COMMENT]]]) | '*' [tfpdef] (',' [TYPE_COMMENT] tfpdef ['=' test])* + (TYPE_COMMENT | [',' [TYPE_COMMENT] ['**' tfpdef [','] [TYPE_COMMENT]]]) | + '**' tfpdef [','] [TYPE_COMMENT])) + tfpdef: NAME [':' test] - - The following definition for varargslist is equivalent to this set of rules: - - arguments = argument (',' argument )* - argument = vfpdef ['=' test] - kwargs = '**' vfpdef [','] - args = '*' [vfpdef] - kwonly_kwargs = (',' argument )* [',' [kwargs]] - args_kwonly_kwargs = args kwonly_kwargs | kwargs - poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]] - vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs - varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] | - (vararglist_no_posonly) - - varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ (vfpdef ['=' - test] (',' vfpdef ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' - ['**' vfpdef [',']]] | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* - [',' ['**' vfpdef [',']]] | '**' vfpdef [',']) ]] | (vfpdef ['=' test] (',' vfpdef - ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] - | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef - [',']]] | '**' vfpdef [',']) - + + The following definition for varargslist is equivalent to this set of rules: + + arguments = argument (',' argument )* + argument = vfpdef ['=' test] + kwargs = '**' vfpdef [','] + args = '*' [vfpdef] + kwonly_kwargs = (',' argument )* [',' [kwargs]] + args_kwonly_kwargs = args kwonly_kwargs | kwargs + poskeyword_args_kwonly_kwargs = arguments [',' [args_kwonly_kwargs]] + vararglist_no_posonly = poskeyword_args_kwonly_kwargs | args_kwonly_kwargs + varargslist = arguments ',' '/' [','[(vararglist_no_posonly)]] | + (vararglist_no_posonly) + + varargslist: vfpdef ['=' test ](',' vfpdef ['=' test])* ',' '/' [',' [ (vfpdef ['=' + test] (',' vfpdef ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' + ['**' vfpdef [',']]] | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* + [',' ['**' vfpdef [',']]] | '**' vfpdef [',']) ]] | (vfpdef ['=' test] (',' vfpdef + ['=' test])* [',' [ '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef [',']]] + | '**' vfpdef [',']]] | '*' [vfpdef] (',' vfpdef ['=' test])* [',' ['**' vfpdef + [',']]] | '**' vfpdef [',']) + vfpdef: NAME */ - int i, j, k, l, nposonlyargs=0, nposargs = 0, nkwonlyargs = 0; + int i, j, k, l, nposonlyargs=0, nposargs = 0, nkwonlyargs = 0; int nposdefaults = 0, found_default = 0; - asdl_seq *posonlyargs, *posargs, *posdefaults, *kwonlyargs, *kwdefaults; + asdl_seq *posonlyargs, *posargs, *posdefaults, *kwonlyargs, *kwdefaults; arg_ty vararg = NULL, kwarg = NULL; - arg_ty arg = NULL; + arg_ty arg = NULL; node *ch; if (TYPE(n) == parameters) { if (NCH(n) == 2) /* () as argument list */ - return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); + return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); n = CHILD(n, 1); } assert(TYPE(n) == typedargslist || TYPE(n) == varargslist); @@ -1510,10 +1510,10 @@ ast_for_arguments(struct compiling *c, const node *n) if (TYPE(ch) == DOUBLESTAR) break; if (TYPE(ch) == vfpdef || TYPE(ch) == tfpdef) nposargs++; if (TYPE(ch) == EQUAL) nposdefaults++; - if (TYPE(ch) == SLASH ) { - nposonlyargs = nposargs; - nposargs = 0; - } + if (TYPE(ch) == SLASH ) { + nposonlyargs = nposargs; + nposargs = 0; + } } /* count the number of keyword only args & defaults for keyword only args */ @@ -1522,10 +1522,10 @@ ast_for_arguments(struct compiling *c, const node *n) if (TYPE(ch) == DOUBLESTAR) break; if (TYPE(ch) == tfpdef || TYPE(ch) == vfpdef) nkwonlyargs++; } - posonlyargs = (nposonlyargs ? _Py_asdl_seq_new(nposonlyargs, c->c_arena) : NULL); - if (!posonlyargs && nposonlyargs) { - return NULL; - } + posonlyargs = (nposonlyargs ? _Py_asdl_seq_new(nposonlyargs, c->c_arena) : NULL); + if (!posonlyargs && nposonlyargs) { + return NULL; + } posargs = (nposargs ? _Py_asdl_seq_new(nposargs, c->c_arena) : NULL); if (!posargs && nposargs) return NULL; @@ -1551,7 +1551,7 @@ ast_for_arguments(struct compiling *c, const node *n) i = 0; j = 0; /* index for defaults */ k = 0; /* index for args */ - l = 0; /* index for posonlyargs */ + l = 0; /* index for posonlyargs */ while (i < NCH(n)) { ch = CHILD(n, i); switch (TYPE(ch)) { @@ -1571,48 +1571,48 @@ ast_for_arguments(struct compiling *c, const node *n) } else if (found_default) { ast_error(c, n, - "non-default argument follows default argument"); + "non-default argument follows default argument"); return NULL; } arg = ast_for_arg(c, ch); if (!arg) return NULL; - if (l < nposonlyargs) { - asdl_seq_SET(posonlyargs, l++, arg); - } else { - asdl_seq_SET(posargs, k++, arg); - } - i += 1; /* the name */ - if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) - i += 1; /* the comma, if present */ - break; - case SLASH: - /* Advance the slash and the comma. If there are more names - * after the slash there will be a comma so we are advancing - * the correct number of nodes. If the slash is the last item, - * we will be advancing an extra token but then * i > NCH(n) - * and the enclosing while will finish correctly. */ - i += 2; + if (l < nposonlyargs) { + asdl_seq_SET(posonlyargs, l++, arg); + } else { + asdl_seq_SET(posargs, k++, arg); + } + i += 1; /* the name */ + if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) + i += 1; /* the comma, if present */ break; + case SLASH: + /* Advance the slash and the comma. If there are more names + * after the slash there will be a comma so we are advancing + * the correct number of nodes. If the slash is the last item, + * we will be advancing an extra token but then * i > NCH(n) + * and the enclosing while will finish correctly. */ + i += 2; + break; case STAR: if (i+1 >= NCH(n) || - (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA - || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) { + (i+2 == NCH(n) && (TYPE(CHILD(n, i+1)) == COMMA + || TYPE(CHILD(n, i+1)) == TYPE_COMMENT))) { ast_error(c, CHILD(n, i), - "named arguments must follow bare *"); + "named arguments must follow bare *"); return NULL; } ch = CHILD(n, i+1); /* tfpdef or COMMA */ if (TYPE(ch) == COMMA) { int res = 0; i += 2; /* now follows keyword only arguments */ - - if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) { - ast_error(c, CHILD(n, i), - "bare * has associated type comment"); - return NULL; - } - + + if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) { + ast_error(c, CHILD(n, i), + "bare * has associated type comment"); + return NULL; + } + res = handle_keywordonly_args(c, n, i, kwonlyargs, kwdefaults); if (res == -1) return NULL; @@ -1623,17 +1623,17 @@ ast_for_arguments(struct compiling *c, const node *n) if (!vararg) return NULL; - i += 2; /* the star and the name */ - if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) - i += 1; /* the comma, if present */ - - if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) { - vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i)); - if (!vararg->type_comment) - return NULL; - i += 1; - } - + i += 2; /* the star and the name */ + if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) + i += 1; /* the comma, if present */ + + if (i < NCH(n) && TYPE(CHILD(n, i)) == TYPE_COMMENT) { + vararg->type_comment = NEW_TYPE_COMMENT(CHILD(n, i)); + if (!vararg->type_comment) + return NULL; + i += 1; + } + if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef || TYPE(CHILD(n, i)) == vfpdef)) { int res = 0; @@ -1650,22 +1650,22 @@ ast_for_arguments(struct compiling *c, const node *n) kwarg = ast_for_arg(c, ch); if (!kwarg) return NULL; - i += 2; /* the double star and the name */ - if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) - i += 1; /* the comma, if present */ - break; - case TYPE_COMMENT: - assert(i); - - if (kwarg) - arg = kwarg; - - /* arg will be equal to the last argument processed */ - arg->type_comment = NEW_TYPE_COMMENT(ch); - if (!arg->type_comment) - return NULL; - i += 1; + i += 2; /* the double star and the name */ + if (i < NCH(n) && TYPE(CHILD(n, i)) == COMMA) + i += 1; /* the comma, if present */ break; + case TYPE_COMMENT: + assert(i); + + if (kwarg) + arg = kwarg; + + /* arg will be equal to the last argument processed */ + arg->type_comment = NEW_TYPE_COMMENT(ch); + if (!arg->type_comment) + return NULL; + i += 1; + break; default: PyErr_Format(PyExc_SystemError, "unexpected node in varargslist: %d @ %d", @@ -1673,19 +1673,19 @@ ast_for_arguments(struct compiling *c, const node *n) return NULL; } } - return arguments(posonlyargs, posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena); + return arguments(posonlyargs, posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena); } static expr_ty ast_for_decorator(struct compiling *c, const node *n) { - /* decorator: '@' namedexpr_test NEWLINE */ + /* decorator: '@' namedexpr_test NEWLINE */ REQ(n, decorator); REQ(CHILD(n, 0), AT); - REQ(CHILD(n, 2), NEWLINE); + REQ(CHILD(n, 2), NEWLINE); - return ast_for_expr(c, CHILD(n, 1)); + return ast_for_expr(c, CHILD(n, 1)); } static asdl_seq* @@ -1713,23 +1713,23 @@ static stmt_ty ast_for_funcdef_impl(struct compiling *c, const node *n0, asdl_seq *decorator_seq, bool is_async) { - /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */ + /* funcdef: 'def' NAME parameters ['->' test] ':' [TYPE_COMMENT] suite */ const node * const n = is_async ? CHILD(n0, 1) : n0; identifier name; arguments_ty args; asdl_seq *body; expr_ty returns = NULL; int name_i = 1; - int end_lineno, end_col_offset; - node *tc; - string type_comment = NULL; - - if (is_async && c->c_feature_version < 5) { - ast_error(c, n, - "Async functions are only supported in Python 3.5 and greater"); - return NULL; - } - + int end_lineno, end_col_offset; + node *tc; + string type_comment = NULL; + + if (is_async && c->c_feature_version < 5) { + ast_error(c, n, + "Async functions are only supported in Python 3.5 and greater"); + return NULL; + } + REQ(n, funcdef); name = NEW_IDENTIFIER(CHILD(n, name_i)); @@ -1746,46 +1746,46 @@ ast_for_funcdef_impl(struct compiling *c, const node *n0, return NULL; name_i += 2; } - if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) { - type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3)); - if (!type_comment) - return NULL; - name_i += 1; - } + if (TYPE(CHILD(n, name_i + 3)) == TYPE_COMMENT) { + type_comment = NEW_TYPE_COMMENT(CHILD(n, name_i + 3)); + if (!type_comment) + return NULL; + name_i += 1; + } body = ast_for_suite(c, CHILD(n, name_i + 3)); if (!body) return NULL; - get_last_end_pos(body, &end_lineno, &end_col_offset); - - if (NCH(CHILD(n, name_i + 3)) > 1) { - /* Check if the suite has a type comment in it. */ - tc = CHILD(CHILD(n, name_i + 3), 1); - - if (TYPE(tc) == TYPE_COMMENT) { - if (type_comment != NULL) { - ast_error(c, n, "Cannot have two type comments on def"); - return NULL; - } - type_comment = NEW_TYPE_COMMENT(tc); - if (!type_comment) - return NULL; - } - } - + get_last_end_pos(body, &end_lineno, &end_col_offset); + + if (NCH(CHILD(n, name_i + 3)) > 1) { + /* Check if the suite has a type comment in it. */ + tc = CHILD(CHILD(n, name_i + 3), 1); + + if (TYPE(tc) == TYPE_COMMENT) { + if (type_comment != NULL) { + ast_error(c, n, "Cannot have two type comments on def"); + return NULL; + } + type_comment = NEW_TYPE_COMMENT(tc); + if (!type_comment) + return NULL; + } + } + if (is_async) - return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment, - LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena); + return AsyncFunctionDef(name, args, body, decorator_seq, returns, type_comment, + LINENO(n0), n0->n_col_offset, end_lineno, end_col_offset, c->c_arena); else - return FunctionDef(name, args, body, decorator_seq, returns, type_comment, - LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena); + return FunctionDef(name, args, body, decorator_seq, returns, type_comment, + LINENO(n), n->n_col_offset, end_lineno, end_col_offset, c->c_arena); } static stmt_ty ast_for_async_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) { - /* async_funcdef: ASYNC funcdef */ + /* async_funcdef: ASYNC funcdef */ REQ(n, async_funcdef); - REQ(CHILD(n, 0), ASYNC); + REQ(CHILD(n, 0), ASYNC); REQ(CHILD(n, 1), funcdef); return ast_for_funcdef_impl(c, n, decorator_seq, @@ -1804,9 +1804,9 @@ ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) static stmt_ty ast_for_async_stmt(struct compiling *c, const node *n) { - /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */ + /* async_stmt: ASYNC (funcdef | with_stmt | for_stmt) */ REQ(n, async_stmt); - REQ(CHILD(n, 0), ASYNC); + REQ(CHILD(n, 0), ASYNC); switch (TYPE(CHILD(n, 1))) { case funcdef: @@ -1856,41 +1856,41 @@ ast_for_decorated(struct compiling *c, const node *n) } static expr_ty -ast_for_namedexpr(struct compiling *c, const node *n) -{ - /* namedexpr_test: test [':=' test] - argument: ( test [comp_for] | - test ':=' test | - test '=' test | - '**' test | - '*' test ) - */ - expr_ty target, value; - - target = ast_for_expr(c, CHILD(n, 0)); - if (!target) - return NULL; - - value = ast_for_expr(c, CHILD(n, 2)); - if (!value) - return NULL; - - if (target->kind != Name_kind) { - const char *expr_name = get_expr_name(target); - if (expr_name != NULL) { - ast_error(c, n, "cannot use assignment expressions with %s", expr_name); - } - return NULL; - } - - if (!set_context(c, target, Store, n)) - return NULL; - - return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno, - n->n_end_col_offset, c->c_arena); -} - -static expr_ty +ast_for_namedexpr(struct compiling *c, const node *n) +{ + /* namedexpr_test: test [':=' test] + argument: ( test [comp_for] | + test ':=' test | + test '=' test | + '**' test | + '*' test ) + */ + expr_ty target, value; + + target = ast_for_expr(c, CHILD(n, 0)); + if (!target) + return NULL; + + value = ast_for_expr(c, CHILD(n, 2)); + if (!value) + return NULL; + + if (target->kind != Name_kind) { + const char *expr_name = get_expr_name(target); + if (expr_name != NULL) { + ast_error(c, n, "cannot use assignment expressions with %s", expr_name); + } + return NULL; + } + + if (!set_context(c, target, Store, n)) + return NULL; + + return NamedExpr(target, value, LINENO(n), n->n_col_offset, n->n_end_lineno, + n->n_end_col_offset, c->c_arena); +} + +static expr_ty ast_for_lambdef(struct compiling *c, const node *n) { /* lambdef: 'lambda' [varargslist] ':' test @@ -1899,7 +1899,7 @@ ast_for_lambdef(struct compiling *c, const node *n) expr_ty expression; if (NCH(n) == 3) { - args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); + args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena); if (!args) return NULL; expression = ast_for_expr(c, CHILD(n, 2)); @@ -1915,8 +1915,8 @@ ast_for_lambdef(struct compiling *c, const node *n) return NULL; } - return Lambda(args, expression, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Lambda(args, expression, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } static expr_ty @@ -1936,7 +1936,7 @@ ast_for_ifexpr(struct compiling *c, const node *n) if (!orelse) return NULL; return IfExp(expression, body, orelse, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } @@ -1955,7 +1955,7 @@ count_comp_fors(struct compiling *c, const node *n) n_fors++; REQ(n, comp_for); if (NCH(n) == 2) { - REQ(CHILD(n, 0), ASYNC); + REQ(CHILD(n, 0), ASYNC); n = CHILD(n, 1); } else if (NCH(n) == 1) { @@ -2040,7 +2040,7 @@ ast_for_comprehension(struct compiling *c, const node *n) if (NCH(n) == 2) { is_async = 1; - REQ(CHILD(n, 0), ASYNC); + REQ(CHILD(n, 0), ASYNC); sync_n = CHILD(n, 1); } else { @@ -2048,13 +2048,13 @@ ast_for_comprehension(struct compiling *c, const node *n) } REQ(sync_n, sync_comp_for); - /* Async comprehensions only allowed in Python 3.6 and greater */ - if (is_async && c->c_feature_version < 6) { - ast_error(c, n, - "Async comprehensions are only supported in Python 3.6 and greater"); - return NULL; - } - + /* Async comprehensions only allowed in Python 3.6 and greater */ + if (is_async && c->c_feature_version < 6) { + ast_error(c, n, + "Async comprehensions are only supported in Python 3.6 and greater"); + return NULL; + } + for_ch = CHILD(sync_n, 1); t = ast_for_exprlist(c, for_ch, Store); if (!t) @@ -2070,9 +2070,9 @@ ast_for_comprehension(struct compiling *c, const node *n) comp = comprehension(first, expression, NULL, is_async, c->c_arena); else - comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset, - for_ch->n_end_lineno, for_ch->n_end_col_offset, - c->c_arena), + comp = comprehension(Tuple(t, Store, first->lineno, first->col_offset, + for_ch->n_end_lineno, for_ch->n_end_col_offset, + c->c_arena), expression, NULL, is_async, c->c_arena); if (!comp) return NULL; @@ -2137,14 +2137,14 @@ ast_for_itercomp(struct compiling *c, const node *n, int type) return NULL; if (type == COMP_GENEXP) - return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return GeneratorExp(elt, comps, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); else if (type == COMP_LISTCOMP) - return ListComp(elt, comps, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return ListComp(elt, comps, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); else if (type == COMP_SETCOMP) - return SetComp(elt, comps, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return SetComp(elt, comps, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); else /* Should never happen */ return NULL; @@ -2206,8 +2206,8 @@ ast_for_dictcomp(struct compiling *c, const node *n) if (!comps) return NULL; - return DictComp(key, value, comps, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return DictComp(key, value, comps, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } static expr_ty @@ -2240,8 +2240,8 @@ ast_for_dictdisplay(struct compiling *c, const node *n) } keys->size = j; values->size = j; - return Dict(keys, values, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Dict(keys, values, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } static expr_ty @@ -2284,8 +2284,8 @@ ast_for_setdisplay(struct compiling *c, const node *n) return NULL; asdl_seq_SET(elts, i / 2, expression); } - return Set(elts, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Set(elts, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } static expr_ty @@ -2304,21 +2304,21 @@ ast_for_atom(struct compiling *c, const node *n) size_t len = strlen(s); if (len >= 4 && len <= 5) { if (!strcmp(s, "None")) - return Constant(Py_None, NULL, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Constant(Py_None, NULL, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); if (!strcmp(s, "True")) - return Constant(Py_True, NULL, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Constant(Py_True, NULL, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); if (!strcmp(s, "False")) - return Constant(Py_False, NULL, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Constant(Py_False, NULL, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } name = new_identifier(s, c); if (!name) return NULL; /* All names start in Load context, but may later be changed. */ - return Name(name, Load, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Name(name, Load, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } case STRING: { expr_ty str = parsestrplus(c, n); @@ -2332,13 +2332,13 @@ ast_for_atom(struct compiling *c, const node *n) PyObject *type, *value, *tback, *errstr; PyErr_Fetch(&type, &value, &tback); errstr = PyObject_Str(value); - if (errstr) { - ast_error(c, n, "(%s) %U", errtype, errstr); - Py_DECREF(errstr); - } - else { + if (errstr) { + ast_error(c, n, "(%s) %U", errtype, errstr); + Py_DECREF(errstr); + } + else { PyErr_Clear(); - ast_error(c, n, "(%s) unknown error", errtype); + ast_error(c, n, "(%s) unknown error", errtype); } Py_DECREF(type); Py_XDECREF(value); @@ -2349,15 +2349,15 @@ ast_for_atom(struct compiling *c, const node *n) return str; } case NUMBER: { - PyObject *pynum; - /* Underscores in numeric literals are only allowed in Python 3.6 or greater */ - /* Check for underscores here rather than in parse_number so we can report a line number on error */ - if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) { - ast_error(c, ch, - "Underscores in numeric literals are only supported in Python 3.6 and greater"); - return NULL; - } - pynum = parsenumber(c, STR(ch)); + PyObject *pynum; + /* Underscores in numeric literals are only allowed in Python 3.6 or greater */ + /* Check for underscores here rather than in parse_number so we can report a line number on error */ + if (c->c_feature_version < 6 && strchr(STR(ch), '_') != NULL) { + ast_error(c, ch, + "Underscores in numeric literals are only supported in Python 3.6 and greater"); + return NULL; + } + pynum = parsenumber(c, STR(ch)); if (!pynum) return NULL; @@ -2365,39 +2365,39 @@ ast_for_atom(struct compiling *c, const node *n) Py_DECREF(pynum); return NULL; } - return Constant(pynum, NULL, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Constant(pynum, NULL, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } case ELLIPSIS: /* Ellipsis */ - return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Constant(Py_Ellipsis, NULL, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); case LPAR: /* some parenthesized expressions */ ch = CHILD(n, 1); if (TYPE(ch) == RPAR) - return Tuple(NULL, Load, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Tuple(NULL, Load, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); if (TYPE(ch) == yield_expr) return ast_for_expr(c, ch); /* testlist_comp: test ( comp_for | (',' test)* [','] ) */ - if (NCH(ch) == 1) { - return ast_for_testlist(c, ch); - } - - if (TYPE(CHILD(ch, 1)) == comp_for) { - return copy_location(ast_for_genexp(c, ch), n, n); - } - else { - return copy_location(ast_for_testlist(c, ch), n, n); - } + if (NCH(ch) == 1) { + return ast_for_testlist(c, ch); + } + + if (TYPE(CHILD(ch, 1)) == comp_for) { + return copy_location(ast_for_genexp(c, ch), n, n); + } + else { + return copy_location(ast_for_testlist(c, ch), n, n); + } case LSQB: /* list (or list comprehension) */ ch = CHILD(n, 1); if (TYPE(ch) == RSQB) - return List(NULL, Load, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return List(NULL, Load, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); REQ(ch, testlist_comp); if (NCH(ch) == 1 || TYPE(CHILD(ch, 1)) == COMMA) { @@ -2405,12 +2405,12 @@ ast_for_atom(struct compiling *c, const node *n) if (!elts) return NULL; - return List(elts, Load, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); - } - else { - return copy_location(ast_for_listcomp(c, ch), n, n); + return List(elts, Load, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } + else { + return copy_location(ast_for_listcomp(c, ch), n, n); + } case LBRACE: { /* dictorsetmaker: ( ((test ':' test | '**' test) * (comp_for | (',' (test ':' test | '**' test))* [','])) | @@ -2420,8 +2420,8 @@ ast_for_atom(struct compiling *c, const node *n) ch = CHILD(n, 1); if (TYPE(ch) == RBRACE) { /* It's an empty dict. */ - return Dict(NULL, NULL, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Dict(NULL, NULL, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } else { int is_dict = (TYPE(CHILD(ch, 0)) == DOUBLESTAR); @@ -2440,8 +2440,8 @@ ast_for_atom(struct compiling *c, const node *n) TYPE(CHILD(ch, 3 - is_dict)) == comp_for) { /* It's a dictionary comprehension. */ if (is_dict) { - ast_error(c, n, - "dict unpacking cannot be used in dict comprehension"); + ast_error(c, n, + "dict unpacking cannot be used in dict comprehension"); return NULL; } res = ast_for_dictcomp(c, ch); @@ -2450,7 +2450,7 @@ ast_for_atom(struct compiling *c, const node *n) /* It's a dictionary display. */ res = ast_for_dictdisplay(c, ch); } - return copy_location(res, n, n); + return copy_location(res, n, n); } } default: @@ -2459,7 +2459,7 @@ ast_for_atom(struct compiling *c, const node *n) } } -static expr_ty +static expr_ty ast_for_slice(struct compiling *c, const node *n) { node *ch; @@ -2473,7 +2473,7 @@ ast_for_slice(struct compiling *c, const node *n) */ ch = CHILD(n, 0); if (NCH(n) == 1 && TYPE(ch) == test) { - return ast_for_expr(c, ch); + return ast_for_expr(c, ch); } if (TYPE(ch) == test) { @@ -2515,8 +2515,8 @@ ast_for_slice(struct compiling *c, const node *n) } } - return Slice(lower, upper, step, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Slice(lower, upper, step, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } static expr_ty @@ -2539,12 +2539,12 @@ ast_for_binop(struct compiling *c, const node *n) if (!expr2) return NULL; - newoperator = get_operator(c, CHILD(n, 1)); + newoperator = get_operator(c, CHILD(n, 1)); if (!newoperator) return NULL; result = BinOp(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, - CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset, + CHILD(n, 2)->n_end_lineno, CHILD(n, 2)->n_end_col_offset, c->c_arena); if (!result) return NULL; @@ -2554,7 +2554,7 @@ ast_for_binop(struct compiling *c, const node *n) expr_ty tmp_result, tmp; const node* next_oper = CHILD(n, i * 2 + 1); - newoperator = get_operator(c, next_oper); + newoperator = get_operator(c, next_oper); if (!newoperator) return NULL; @@ -2563,9 +2563,9 @@ ast_for_binop(struct compiling *c, const node *n) return NULL; tmp_result = BinOp(result, newoperator, tmp, - LINENO(n), n->n_col_offset, - CHILD(n, i * 2 + 2)->n_end_lineno, - CHILD(n, i * 2 + 2)->n_end_col_offset, + LINENO(n), n->n_col_offset, + CHILD(n, i * 2 + 2)->n_end_lineno, + CHILD(n, i * 2 + 2)->n_end_col_offset, c->c_arena); if (!tmp_result) return NULL; @@ -2575,64 +2575,64 @@ ast_for_binop(struct compiling *c, const node *n) } static expr_ty -ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr, const node *start) +ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr, const node *start) { /* trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME subscriptlist: subscript (',' subscript)* [','] subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop] */ - const node *n_copy = n; + const node *n_copy = n; REQ(n, trailer); if (TYPE(CHILD(n, 0)) == LPAR) { if (NCH(n) == 2) - return Call(left_expr, NULL, NULL, LINENO(start), start->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Call(left_expr, NULL, NULL, LINENO(start), start->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); else - return ast_for_call(c, CHILD(n, 1), left_expr, - start, CHILD(n, 0), CHILD(n, 2)); + return ast_for_call(c, CHILD(n, 1), left_expr, + start, CHILD(n, 0), CHILD(n, 2)); } else if (TYPE(CHILD(n, 0)) == DOT) { PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1)); if (!attr_id) return NULL; return Attribute(left_expr, attr_id, Load, - LINENO(start), start->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + LINENO(start), start->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } else { REQ(CHILD(n, 0), LSQB); REQ(CHILD(n, 2), RSQB); n = CHILD(n, 1); if (NCH(n) == 1) { - expr_ty slc = ast_for_slice(c, CHILD(n, 0)); + expr_ty slc = ast_for_slice(c, CHILD(n, 0)); if (!slc) return NULL; - return Subscript(left_expr, slc, Load, LINENO(start), start->n_col_offset, - n_copy->n_end_lineno, n_copy->n_end_col_offset, + return Subscript(left_expr, slc, Load, LINENO(start), start->n_col_offset, + n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena); } else { - int j; - expr_ty slc, e; - asdl_seq *elts; - elts = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); - if (!elts) + int j; + expr_ty slc, e; + asdl_seq *elts; + elts = _Py_asdl_seq_new((NCH(n) + 1) / 2, c->c_arena); + if (!elts) return NULL; for (j = 0; j < NCH(n); j += 2) { slc = ast_for_slice(c, CHILD(n, j)); if (!slc) return NULL; - asdl_seq_SET(elts, j / 2, slc); + asdl_seq_SET(elts, j / 2, slc); } - e = Tuple(elts, Load, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, - c->c_arena); + e = Tuple(elts, Load, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, + c->c_arena); if (!e) return NULL; - return Subscript(left_expr, e, - Load, LINENO(start), start->n_col_offset, - n_copy->n_end_lineno, n_copy->n_end_col_offset, - c->c_arena); + return Subscript(left_expr, e, + Load, LINENO(start), start->n_col_offset, + n_copy->n_end_lineno, n_copy->n_end_col_offset, + c->c_arena); } } } @@ -2649,16 +2649,16 @@ ast_for_factor(struct compiling *c, const node *n) switch (TYPE(CHILD(n, 0))) { case PLUS: return UnaryOp(UAdd, expression, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); case MINUS: return UnaryOp(USub, expression, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); case TILDE: - return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, - c->c_arena); + return UnaryOp(Invert, expression, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, + c->c_arena); } PyErr_Format(PyExc_SystemError, "unhandled factor: %d", TYPE(CHILD(n, 0))); @@ -2669,17 +2669,17 @@ static expr_ty ast_for_atom_expr(struct compiling *c, const node *n) { int i, nch, start = 0; - expr_ty e; + expr_ty e; REQ(n, atom_expr); nch = NCH(n); - if (TYPE(CHILD(n, 0)) == AWAIT) { - if (c->c_feature_version < 5) { - ast_error(c, n, - "Await expressions are only supported in Python 3.5 and greater"); - return NULL; - } + if (TYPE(CHILD(n, 0)) == AWAIT) { + if (c->c_feature_version < 5) { + ast_error(c, n, + "Await expressions are only supported in Python 3.5 and greater"); + return NULL; + } start = 1; assert(nch > 1); } @@ -2690,23 +2690,23 @@ ast_for_atom_expr(struct compiling *c, const node *n) if (nch == 1) return e; if (start && nch == 2) { - return Await(e, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Await(e, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } for (i = start + 1; i < nch; i++) { node *ch = CHILD(n, i); if (TYPE(ch) != trailer) break; - e = ast_for_trailer(c, ch, e, CHILD(n, start)); - if (!e) + e = ast_for_trailer(c, ch, e, CHILD(n, start)); + if (!e) return NULL; } if (start) { /* there was an 'await' */ - return Await(e, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Await(e, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } else { return e; @@ -2729,8 +2729,8 @@ ast_for_power(struct compiling *c, const node *n) expr_ty f = ast_for_expr(c, CHILD(n, NCH(n) - 1)); if (!f) return NULL; - e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + e = BinOp(e, Pow, f, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } return e; } @@ -2746,8 +2746,8 @@ ast_for_starred(struct compiling *c, const node *n) return NULL; /* The Load context is changed later. */ - return Starred(tmp, Load, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Starred(tmp, Load, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } @@ -2758,7 +2758,7 @@ static expr_ty ast_for_expr(struct compiling *c, const node *n) { /* handle the full range of simple expressions - namedexpr_test: test [':=' test] + namedexpr_test: test [':=' test] test: or_test ['if' or_test 'else' test] | lambdef test_nocond: or_test | lambdef_nocond or_test: and_test ('or' and_test)* @@ -2773,7 +2773,7 @@ ast_for_expr(struct compiling *c, const node *n) term: factor (('*'|'@'|'/'|'%'|'//') factor)* factor: ('+'|'-'|'~') factor | power power: atom_expr ['**' factor] - atom_expr: [AWAIT] atom trailer* + atom_expr: [AWAIT] atom trailer* yield_expr: 'yield' [yield_arg] */ @@ -2782,10 +2782,10 @@ ast_for_expr(struct compiling *c, const node *n) loop: switch (TYPE(n)) { - case namedexpr_test: - if (NCH(n) == 3) - return ast_for_namedexpr(c, n); - /* Fallthrough */ + case namedexpr_test: + if (NCH(n) == 3) + return ast_for_namedexpr(c, n); + /* Fallthrough */ case test: case test_nocond: if (TYPE(CHILD(n, 0)) == lambdef || @@ -2811,11 +2811,11 @@ ast_for_expr(struct compiling *c, const node *n) } if (!strcmp(STR(CHILD(n, 1)), "and")) return BoolOp(And, seq, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); assert(!strcmp(STR(CHILD(n, 1)), "or")); - return BoolOp(Or, seq, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return BoolOp(Or, seq, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); case not_test: if (NCH(n) == 1) { n = CHILD(n, 0); @@ -2827,7 +2827,7 @@ ast_for_expr(struct compiling *c, const node *n) return NULL; return UnaryOp(Not, expression, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } case comparison: @@ -2867,8 +2867,8 @@ ast_for_expr(struct compiling *c, const node *n) return NULL; } - return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Compare(expression, ops, cmps, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } case star_expr: @@ -2907,10 +2907,10 @@ ast_for_expr(struct compiling *c, const node *n) return NULL; } if (is_from) - return YieldFrom(exp, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); - return Yield(exp, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return YieldFrom(exp, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Yield(exp, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } case factor: if (NCH(n) == 1) { @@ -2929,8 +2929,8 @@ ast_for_expr(struct compiling *c, const node *n) } static expr_ty -ast_for_call(struct compiling *c, const node *n, expr_ty func, - const node *start, const node *maybegenbeg, const node *closepar) +ast_for_call(struct compiling *c, const node *n, expr_ty func, + const node *start, const node *maybegenbeg, const node *closepar) { /* arglist: argument (',' argument)* [','] @@ -2953,7 +2953,7 @@ ast_for_call(struct compiling *c, const node *n, expr_ty func, nargs++; else if (TYPE(CHILD(ch, 1)) == comp_for) { nargs++; - if (!maybegenbeg) { + if (!maybegenbeg) { ast_error(c, ch, "invalid syntax"); return NULL; } @@ -2964,9 +2964,9 @@ ast_for_call(struct compiling *c, const node *n, expr_ty func, } else if (TYPE(CHILD(ch, 0)) == STAR) nargs++; - else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) { - nargs++; - } + else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) { + nargs++; + } else /* TYPE(CHILD(ch, 0)) == DOUBLESTAR or keyword argument */ nkeywords++; @@ -2993,13 +2993,13 @@ ast_for_call(struct compiling *c, const node *n, expr_ty func, if (nkeywords) { if (ndoublestars) { ast_error(c, chch, - "positional argument follows " - "keyword argument unpacking"); + "positional argument follows " + "keyword argument unpacking"); } else { ast_error(c, chch, - "positional argument follows " - "keyword argument"); + "positional argument follows " + "keyword argument"); } return NULL; } @@ -3013,8 +3013,8 @@ ast_for_call(struct compiling *c, const node *n, expr_ty func, expr_ty starred; if (ndoublestars) { ast_error(c, chch, - "iterable argument unpacking follows " - "keyword argument unpacking"); + "iterable argument unpacking follows " + "keyword argument unpacking"); return NULL; } e = ast_for_expr(c, CHILD(ch, 1)); @@ -3022,7 +3022,7 @@ ast_for_call(struct compiling *c, const node *n, expr_ty func, return NULL; starred = Starred(e, Load, LINENO(chch), chch->n_col_offset, - e->end_lineno, e->end_col_offset, + e->end_lineno, e->end_col_offset, c->c_arena); if (!starred) return NULL; @@ -3036,91 +3036,91 @@ ast_for_call(struct compiling *c, const node *n, expr_ty func, e = ast_for_expr(c, CHILD(ch, 1)); if (!e) return NULL; - kw = keyword(NULL, e, chch->n_lineno, chch->n_col_offset, - e->end_lineno, e->end_col_offset, c->c_arena); + kw = keyword(NULL, e, chch->n_lineno, chch->n_col_offset, + e->end_lineno, e->end_col_offset, c->c_arena); asdl_seq_SET(keywords, nkeywords++, kw); ndoublestars++; } else if (TYPE(CHILD(ch, 1)) == comp_for) { /* the lone generator expression */ - e = copy_location(ast_for_genexp(c, ch), maybegenbeg, closepar); - if (!e) - return NULL; - asdl_seq_SET(args, nargs++, e); - } - else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) { - /* treat colon equal as positional argument */ - if (nkeywords) { - if (ndoublestars) { - ast_error(c, chch, - "positional argument follows " - "keyword argument unpacking"); - } - else { - ast_error(c, chch, - "positional argument follows " - "keyword argument"); - } - return NULL; - } - e = ast_for_namedexpr(c, ch); + e = copy_location(ast_for_genexp(c, ch), maybegenbeg, closepar); if (!e) return NULL; asdl_seq_SET(args, nargs++, e); } + else if (TYPE(CHILD(ch, 1)) == COLONEQUAL) { + /* treat colon equal as positional argument */ + if (nkeywords) { + if (ndoublestars) { + ast_error(c, chch, + "positional argument follows " + "keyword argument unpacking"); + } + else { + ast_error(c, chch, + "positional argument follows " + "keyword argument"); + } + return NULL; + } + e = ast_for_namedexpr(c, ch); + if (!e) + return NULL; + asdl_seq_SET(args, nargs++, e); + } else { /* a keyword argument */ keyword_ty kw; - identifier key; - - // To remain LL(1), the grammar accepts any test (basically, any - // expression) in the keyword slot of a call site. So, we need - // to manually enforce that the keyword is a NAME here. - static const int name_tree[] = { - test, - or_test, - and_test, - not_test, - comparison, - expr, - xor_expr, - and_expr, - shift_expr, - arith_expr, - term, - factor, - power, - atom_expr, - atom, - 0, - }; - node *expr_node = chch; - for (int i = 0; name_tree[i]; i++) { - if (TYPE(expr_node) != name_tree[i]) - break; - if (NCH(expr_node) != 1) - break; - expr_node = CHILD(expr_node, 0); - } - if (TYPE(expr_node) != NAME) { + identifier key; + + // To remain LL(1), the grammar accepts any test (basically, any + // expression) in the keyword slot of a call site. So, we need + // to manually enforce that the keyword is a NAME here. + static const int name_tree[] = { + test, + or_test, + and_test, + not_test, + comparison, + expr, + xor_expr, + and_expr, + shift_expr, + arith_expr, + term, + factor, + power, + atom_expr, + atom, + 0, + }; + node *expr_node = chch; + for (int i = 0; name_tree[i]; i++) { + if (TYPE(expr_node) != name_tree[i]) + break; + if (NCH(expr_node) != 1) + break; + expr_node = CHILD(expr_node, 0); + } + if (TYPE(expr_node) != NAME) { ast_error(c, chch, - "expression cannot contain assignment, " - "perhaps you meant \"==\"?"); + "expression cannot contain assignment, " + "perhaps you meant \"==\"?"); return NULL; } - key = new_identifier(STR(expr_node), c); - if (key == NULL) { + key = new_identifier(STR(expr_node), c); + if (key == NULL) { return NULL; } - if (forbidden_name(c, key, chch, 1)) { + if (forbidden_name(c, key, chch, 1)) { return NULL; } e = ast_for_expr(c, CHILD(ch, 2)); if (!e) return NULL; - kw = keyword(key, e, chch->n_lineno, chch->n_col_offset, - e->end_lineno, e->end_col_offset, c->c_arena); - + kw = keyword(key, e, chch->n_lineno, chch->n_col_offset, + e->end_lineno, e->end_col_offset, c->c_arena); + if (!kw) return NULL; asdl_seq_SET(keywords, nkeywords++, kw); @@ -3128,8 +3128,8 @@ ast_for_call(struct compiling *c, const node *n, expr_ty func, } } - return Call(func, args, keywords, LINENO(start), start->n_col_offset, - closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena); + return Call(func, args, keywords, LINENO(start), start->n_col_offset, + closepar->n_end_lineno, closepar->n_end_col_offset, c->c_arena); } static expr_ty @@ -3152,8 +3152,8 @@ ast_for_testlist(struct compiling *c, const node* n) asdl_seq *tmp = seq_for_testlist(c, n); if (!tmp) return NULL; - return Tuple(tmp, Load, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Tuple(tmp, Load, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } } @@ -3162,22 +3162,22 @@ ast_for_expr_stmt(struct compiling *c, const node *n) { REQ(n, expr_stmt); /* expr_stmt: testlist_star_expr (annassign | augassign (yield_expr|testlist) | - [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] ) - annassign: ':' test ['=' (yield_expr|testlist)] - testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] - augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | - '<<=' | '>>=' | '**=' | '//=') + [('=' (yield_expr|testlist_star_expr))+ [TYPE_COMMENT]] ) + annassign: ':' test ['=' (yield_expr|testlist)] + testlist_star_expr: (test|star_expr) (',' (test|star_expr))* [','] + augassign: ('+=' | '-=' | '*=' | '@=' | '/=' | '%=' | '&=' | '|=' | '^=' | + '<<=' | '>>=' | '**=' | '//=') test: ... here starts the operator precedence dance */ - int num = NCH(n); + int num = NCH(n); - if (num == 1) { + if (num == 1) { expr_ty e = ast_for_testlist(c, CHILD(n, 0)); if (!e) return NULL; - return Expr(e, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Expr(e, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } else if (TYPE(CHILD(n, 1)) == augassign) { expr_ty expr1, expr2; @@ -3187,7 +3187,7 @@ ast_for_expr_stmt(struct compiling *c, const node *n) expr1 = ast_for_testlist(c, ch); if (!expr1) return NULL; - /* Augmented assignments can only have a name, a subscript, or an + /* Augmented assignments can only have a name, a subscript, or an attribute on the left, though, so we have to explicitly check for those. */ switch (expr1->kind) { @@ -3196,16 +3196,16 @@ ast_for_expr_stmt(struct compiling *c, const node *n) case Subscript_kind: break; default: - ast_error(c, ch, "'%s' is an illegal expression for augmented assignment", - get_expr_name(expr1)); + ast_error(c, ch, "'%s' is an illegal expression for augmented assignment", + get_expr_name(expr1)); return NULL; } - /* set_context checks that most expressions are not the left side. */ - if(!set_context(c, expr1, Store, ch)) { - return NULL; - } - + /* set_context checks that most expressions are not the left side. */ + if(!set_context(c, expr1, Store, ch)) { + return NULL; + } + ch = CHILD(n, 2); if (TYPE(ch) == testlist) expr2 = ast_for_testlist(c, ch); @@ -3218,8 +3218,8 @@ ast_for_expr_stmt(struct compiling *c, const node *n) if (!newoperator) return NULL; - return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return AugAssign(expr1, newoperator, expr2, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } else if (TYPE(CHILD(n, 1)) == annassign) { expr_ty expr1, expr2, expr3; @@ -3227,13 +3227,13 @@ ast_for_expr_stmt(struct compiling *c, const node *n) node *deep, *ann = CHILD(n, 1); int simple = 1; - /* AnnAssigns are only allowed in Python 3.6 or greater */ - if (c->c_feature_version < 6) { - ast_error(c, ch, - "Variable annotation syntax is only supported in Python 3.6 and greater"); - return NULL; - } - + /* AnnAssigns are only allowed in Python 3.6 or greater */ + if (c->c_feature_version < 6) { + ast_error(c, ch, + "Variable annotation syntax is only supported in Python 3.6 and greater"); + return NULL; + } + /* we keep track of parens to qualify (x) as expression not name */ deep = ch; while (NCH(deep) == 1) { @@ -3286,42 +3286,42 @@ ast_for_expr_stmt(struct compiling *c, const node *n) } if (NCH(ann) == 2) { return AnnAssign(expr1, expr2, NULL, simple, - LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } else { ch = CHILD(ann, 3); - if (TYPE(ch) == testlist_star_expr) { - expr3 = ast_for_testlist(c, ch); - } - else { - expr3 = ast_for_expr(c, ch); - } + if (TYPE(ch) == testlist_star_expr) { + expr3 = ast_for_testlist(c, ch); + } + else { + expr3 = ast_for_expr(c, ch); + } if (!expr3) { return NULL; } return AnnAssign(expr1, expr2, expr3, simple, - LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } } else { - int i, nch_minus_type, has_type_comment; + int i, nch_minus_type, has_type_comment; asdl_seq *targets; node *value; expr_ty expression; - string type_comment; + string type_comment; /* a normal assignment */ REQ(CHILD(n, 1), EQUAL); - - has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT; - nch_minus_type = num - has_type_comment; - - targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena); + + has_type_comment = TYPE(CHILD(n, num - 1)) == TYPE_COMMENT; + nch_minus_type = num - has_type_comment; + + targets = _Py_asdl_seq_new(nch_minus_type / 2, c->c_arena); if (!targets) return NULL; - for (i = 0; i < nch_minus_type - 2; i += 2) { + for (i = 0; i < nch_minus_type - 2; i += 2) { expr_ty e; node *ch = CHILD(n, i); if (TYPE(ch) == yield_expr) { @@ -3338,22 +3338,22 @@ ast_for_expr_stmt(struct compiling *c, const node *n) asdl_seq_SET(targets, i / 2, e); } - value = CHILD(n, nch_minus_type - 1); + value = CHILD(n, nch_minus_type - 1); if (TYPE(value) == testlist_star_expr) expression = ast_for_testlist(c, value); else expression = ast_for_expr(c, value); if (!expression) return NULL; - if (has_type_comment) { - type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type)); - if (!type_comment) - return NULL; - } - else - type_comment = NULL; - return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + if (has_type_comment) { + type_comment = NEW_TYPE_COMMENT(CHILD(n, nch_minus_type)); + if (!type_comment) + return NULL; + } + else + type_comment = NULL; + return Assign(targets, expression, type_comment, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } } @@ -3392,8 +3392,8 @@ ast_for_del_stmt(struct compiling *c, const node *n) expr_list = ast_for_exprlist(c, CHILD(n, 1), Del); if (!expr_list) return NULL; - return Delete(expr_list, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Delete(expr_list, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } static stmt_ty @@ -3415,33 +3415,33 @@ ast_for_flow_stmt(struct compiling *c, const node *n) ch = CHILD(n, 0); switch (TYPE(ch)) { case break_stmt: - return Break(LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Break(LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); case continue_stmt: - return Continue(LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Continue(LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); case yield_stmt: { /* will reduce to yield_expr */ expr_ty exp = ast_for_expr(c, CHILD(ch, 0)); if (!exp) return NULL; - return Expr(exp, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Expr(exp, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } case return_stmt: if (NCH(ch) == 1) - return Return(NULL, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Return(NULL, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); else { expr_ty expression = ast_for_testlist(c, CHILD(ch, 1)); if (!expression) return NULL; - return Return(expression, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Return(expression, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } case raise_stmt: if (NCH(ch) == 1) - return Raise(NULL, NULL, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Raise(NULL, NULL, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); else if (NCH(ch) >= 2) { expr_ty cause = NULL; expr_ty expression = ast_for_expr(c, CHILD(ch, 1)); @@ -3452,8 +3452,8 @@ ast_for_flow_stmt(struct compiling *c, const node *n) if (!cause) return NULL; } - return Raise(expression, cause, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Raise(expression, cause, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } /* fall through */ default: @@ -3609,14 +3609,14 @@ ast_for_import_stmt(struct compiling *c, const node *n) return NULL; asdl_seq_SET(aliases, i / 2, import_alias); } - // Even though n is modified above, the end position is not changed - return Import(aliases, lineno, col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + // Even though n is modified above, the end position is not changed + return Import(aliases, lineno, col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } else if (TYPE(n) == import_from) { int n_children; int idx, ndots = 0; - const node *n_copy = n; + const node *n_copy = n; alias_ty mod = NULL; identifier modname = NULL; @@ -3655,9 +3655,9 @@ ast_for_import_stmt(struct compiling *c, const node *n) n = CHILD(n, idx); n_children = NCH(n); if (n_children % 2 == 0) { - ast_error(c, n, - "trailing comma not allowed without" - " surrounding parentheses"); + ast_error(c, n, + "trailing comma not allowed without" + " surrounding parentheses"); return NULL; } break; @@ -3688,7 +3688,7 @@ ast_for_import_stmt(struct compiling *c, const node *n) if (mod != NULL) modname = mod->name; return ImportFrom(modname, aliases, ndots, lineno, col_offset, - n_copy->n_end_lineno, n_copy->n_end_col_offset, + n_copy->n_end_lineno, n_copy->n_end_col_offset, c->c_arena); } PyErr_Format(PyExc_SystemError, @@ -3715,8 +3715,8 @@ ast_for_global_stmt(struct compiling *c, const node *n) return NULL; asdl_seq_SET(s, i / 2, name); } - return Global(s, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Global(s, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } static stmt_ty @@ -3737,8 +3737,8 @@ ast_for_nonlocal_stmt(struct compiling *c, const node *n) return NULL; asdl_seq_SET(s, i / 2, name); } - return Nonlocal(s, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Nonlocal(s, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } static stmt_ty @@ -3750,8 +3750,8 @@ ast_for_assert_stmt(struct compiling *c, const node *n) expr_ty expression = ast_for_expr(c, CHILD(n, 1)); if (!expression) return NULL; - return Assert(expression, NULL, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Assert(expression, NULL, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } else if (NCH(n) == 4) { expr_ty expr1, expr2; @@ -3763,8 +3763,8 @@ ast_for_assert_stmt(struct compiling *c, const node *n) if (!expr2) return NULL; - return Assert(expr1, expr2, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Assert(expr1, expr2, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } PyErr_Format(PyExc_SystemError, "improper number of parts to 'assert' statement: %d", @@ -3775,15 +3775,15 @@ ast_for_assert_stmt(struct compiling *c, const node *n) static asdl_seq * ast_for_suite(struct compiling *c, const node *n) { - /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */ + /* suite: simple_stmt | NEWLINE [TYPE_COMMENT NEWLINE] INDENT stmt+ DEDENT */ asdl_seq *seq; stmt_ty s; int i, total, num, end, pos = 0; node *ch; - if (TYPE(n) != func_body_suite) { - REQ(n, suite); - } + if (TYPE(n) != func_body_suite) { + REQ(n, suite); + } total = num_stmts(n); seq = _Py_asdl_seq_new(total, c->c_arena); @@ -3807,13 +3807,13 @@ ast_for_suite(struct compiling *c, const node *n) } } else { - i = 2; - if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) { - i += 2; - REQ(CHILD(n, 2), NEWLINE); - } - - for (; i < (NCH(n) - 1); i++) { + i = 2; + if (TYPE(CHILD(n, 1)) == TYPE_COMMENT) { + i += 2; + REQ(CHILD(n, 2), NEWLINE); + } + + for (; i < (NCH(n) - 1); i++) { ch = CHILD(n, i); REQ(ch, stmt); num = num_stmts(ch); @@ -3846,17 +3846,17 @@ ast_for_suite(struct compiling *c, const node *n) return seq; } -static void -get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset) -{ - Py_ssize_t tot = asdl_seq_LEN(s); - // There must be no empty suites. - assert(tot > 0); - stmt_ty last = asdl_seq_GET(s, tot - 1); - *end_lineno = last->end_lineno; - *end_col_offset = last->end_col_offset; -} - +static void +get_last_end_pos(asdl_seq *s, int *end_lineno, int *end_col_offset) +{ + Py_ssize_t tot = asdl_seq_LEN(s); + // There must be no empty suites. + assert(tot > 0); + stmt_ty last = asdl_seq_GET(s, tot - 1); + *end_lineno = last->end_lineno; + *end_col_offset = last->end_col_offset; +} + static stmt_ty ast_for_if_stmt(struct compiling *c, const node *n) { @@ -3864,7 +3864,7 @@ ast_for_if_stmt(struct compiling *c, const node *n) ['else' ':' suite] */ char *s; - int end_lineno, end_col_offset; + int end_lineno, end_col_offset; REQ(n, if_stmt); @@ -3878,10 +3878,10 @@ ast_for_if_stmt(struct compiling *c, const node *n) suite_seq = ast_for_suite(c, CHILD(n, 3)); if (!suite_seq) return NULL; - get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); + get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); return If(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, - end_lineno, end_col_offset, c->c_arena); + end_lineno, end_col_offset, c->c_arena); } s = STR(CHILD(n, 4)); @@ -3902,10 +3902,10 @@ ast_for_if_stmt(struct compiling *c, const node *n) seq2 = ast_for_suite(c, CHILD(n, 6)); if (!seq2) return NULL; - get_last_end_pos(seq2, &end_lineno, &end_col_offset); + get_last_end_pos(seq2, &end_lineno, &end_col_offset); return If(expression, seq1, seq2, LINENO(n), n->n_col_offset, - end_lineno, end_col_offset, c->c_arena); + end_lineno, end_col_offset, c->c_arena); } else if (s[2] == 'i') { int i, n_elif, has_else = 0; @@ -3937,13 +3937,13 @@ ast_for_if_stmt(struct compiling *c, const node *n) suite_seq2 = ast_for_suite(c, CHILD(n, NCH(n) - 1)); if (!suite_seq2) return NULL; - get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset); + get_last_end_pos(suite_seq2, &end_lineno, &end_col_offset); asdl_seq_SET(orelse, 0, If(expression, suite_seq, suite_seq2, - LINENO(CHILD(n, NCH(n) - 7)), - CHILD(n, NCH(n) - 7)->n_col_offset, - end_lineno, end_col_offset, c->c_arena)); + LINENO(CHILD(n, NCH(n) - 7)), + CHILD(n, NCH(n) - 7)->n_col_offset, + end_lineno, end_col_offset, c->c_arena)); /* the just-created orelse handled the last elif */ n_elif--; } @@ -3960,16 +3960,16 @@ ast_for_if_stmt(struct compiling *c, const node *n) if (!suite_seq) return NULL; - if (orelse != NULL) { - get_last_end_pos(orelse, &end_lineno, &end_col_offset); - } else { - get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); - } + if (orelse != NULL) { + get_last_end_pos(orelse, &end_lineno, &end_col_offset); + } else { + get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); + } asdl_seq_SET(newobj, 0, If(expression, suite_seq, orelse, - LINENO(CHILD(n, off - 1)), - CHILD(n, off - 1)->n_col_offset, - end_lineno, end_col_offset, c->c_arena)); + LINENO(CHILD(n, off - 1)), + CHILD(n, off - 1)->n_col_offset, + end_lineno, end_col_offset, c->c_arena)); orelse = newobj; } expression = ast_for_expr(c, CHILD(n, 1)); @@ -3978,10 +3978,10 @@ ast_for_if_stmt(struct compiling *c, const node *n) suite_seq = ast_for_suite(c, CHILD(n, 3)); if (!suite_seq) return NULL; - get_last_end_pos(orelse, &end_lineno, &end_col_offset); + get_last_end_pos(orelse, &end_lineno, &end_col_offset); return If(expression, suite_seq, orelse, - LINENO(n), n->n_col_offset, - end_lineno, end_col_offset, c->c_arena); + LINENO(n), n->n_col_offset, + end_lineno, end_col_offset, c->c_arena); } PyErr_Format(PyExc_SystemError, @@ -3994,7 +3994,7 @@ ast_for_while_stmt(struct compiling *c, const node *n) { /* while_stmt: 'while' test ':' suite ['else' ':' suite] */ REQ(n, while_stmt); - int end_lineno, end_col_offset; + int end_lineno, end_col_offset; if (NCH(n) == 4) { expr_ty expression; @@ -4006,9 +4006,9 @@ ast_for_while_stmt(struct compiling *c, const node *n) suite_seq = ast_for_suite(c, CHILD(n, 3)); if (!suite_seq) return NULL; - get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); - return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, - end_lineno, end_col_offset, c->c_arena); + get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); + return While(expression, suite_seq, NULL, LINENO(n), n->n_col_offset, + end_lineno, end_col_offset, c->c_arena); } else if (NCH(n) == 7) { expr_ty expression; @@ -4023,10 +4023,10 @@ ast_for_while_stmt(struct compiling *c, const node *n) seq2 = ast_for_suite(c, CHILD(n, 6)); if (!seq2) return NULL; - get_last_end_pos(seq2, &end_lineno, &end_col_offset); + get_last_end_pos(seq2, &end_lineno, &end_col_offset); - return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, - end_lineno, end_col_offset, c->c_arena); + return While(expression, seq1, seq2, LINENO(n), n->n_col_offset, + end_lineno, end_col_offset, c->c_arena); } PyErr_Format(PyExc_SystemError, @@ -4043,23 +4043,23 @@ ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async) expr_ty expression; expr_ty target, first; const node *node_target; - int end_lineno, end_col_offset; - int has_type_comment; - string type_comment; - - if (is_async && c->c_feature_version < 5) { - ast_error(c, n, - "Async for loops are only supported in Python 3.5 and greater"); - return NULL; - } - - /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */ + int end_lineno, end_col_offset; + int has_type_comment; + string type_comment; + + if (is_async && c->c_feature_version < 5) { + ast_error(c, n, + "Async for loops are only supported in Python 3.5 and greater"); + return NULL; + } + + /* for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite] */ REQ(n, for_stmt); - has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT; - - if (NCH(n) == 9 + has_type_comment) { - seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment)); + has_type_comment = TYPE(CHILD(n, 5)) == TYPE_COMMENT; + + if (NCH(n) == 9 + has_type_comment) { + seq = ast_for_suite(c, CHILD(n, 8 + has_type_comment)); if (!seq) return NULL; } @@ -4074,46 +4074,46 @@ ast_for_for_stmt(struct compiling *c, const node *n0, bool is_async) if (NCH(node_target) == 1) target = first; else - target = Tuple(_target, Store, first->lineno, first->col_offset, - node_target->n_end_lineno, node_target->n_end_col_offset, - c->c_arena); + target = Tuple(_target, Store, first->lineno, first->col_offset, + node_target->n_end_lineno, node_target->n_end_col_offset, + c->c_arena); expression = ast_for_testlist(c, CHILD(n, 3)); if (!expression) return NULL; - suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment)); + suite_seq = ast_for_suite(c, CHILD(n, 5 + has_type_comment)); if (!suite_seq) return NULL; - if (seq != NULL) { - get_last_end_pos(seq, &end_lineno, &end_col_offset); - } else { - get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); - } - - if (has_type_comment) { - type_comment = NEW_TYPE_COMMENT(CHILD(n, 5)); - if (!type_comment) - return NULL; - } - else - type_comment = NULL; - + if (seq != NULL) { + get_last_end_pos(seq, &end_lineno, &end_col_offset); + } else { + get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); + } + + if (has_type_comment) { + type_comment = NEW_TYPE_COMMENT(CHILD(n, 5)); + if (!type_comment) + return NULL; + } + else + type_comment = NULL; + if (is_async) - return AsyncFor(target, expression, suite_seq, seq, type_comment, + return AsyncFor(target, expression, suite_seq, seq, type_comment, LINENO(n0), n0->n_col_offset, - end_lineno, end_col_offset, c->c_arena); + end_lineno, end_col_offset, c->c_arena); else - return For(target, expression, suite_seq, seq, type_comment, + return For(target, expression, suite_seq, seq, type_comment, LINENO(n), n->n_col_offset, - end_lineno, end_col_offset, c->c_arena); + end_lineno, end_col_offset, c->c_arena); } static excepthandler_ty ast_for_except_clause(struct compiling *c, const node *exc, node *body) { /* except_clause: 'except' [test ['as' test]] */ - int end_lineno, end_col_offset; + int end_lineno, end_col_offset; REQ(exc, except_clause); REQ(body, suite); @@ -4121,11 +4121,11 @@ ast_for_except_clause(struct compiling *c, const node *exc, node *body) asdl_seq *suite_seq = ast_for_suite(c, body); if (!suite_seq) return NULL; - get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); + get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); return ExceptHandler(NULL, NULL, suite_seq, LINENO(exc), - exc->n_col_offset, - end_lineno, end_col_offset, c->c_arena); + exc->n_col_offset, + end_lineno, end_col_offset, c->c_arena); } else if (NCH(exc) == 2) { expr_ty expression; @@ -4137,11 +4137,11 @@ ast_for_except_clause(struct compiling *c, const node *exc, node *body) suite_seq = ast_for_suite(c, body); if (!suite_seq) return NULL; - get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); + get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); return ExceptHandler(expression, NULL, suite_seq, LINENO(exc), - exc->n_col_offset, - end_lineno, end_col_offset, c->c_arena); + exc->n_col_offset, + end_lineno, end_col_offset, c->c_arena); } else if (NCH(exc) == 4) { asdl_seq *suite_seq; @@ -4157,11 +4157,11 @@ ast_for_except_clause(struct compiling *c, const node *exc, node *body) suite_seq = ast_for_suite(c, body); if (!suite_seq) return NULL; - get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); + get_last_end_pos(suite_seq, &end_lineno, &end_col_offset); return ExceptHandler(expression, e, suite_seq, LINENO(exc), - exc->n_col_offset, - end_lineno, end_col_offset, c->c_arena); + exc->n_col_offset, + end_lineno, end_col_offset, c->c_arena); } PyErr_Format(PyExc_SystemError, @@ -4174,9 +4174,9 @@ static stmt_ty ast_for_try_stmt(struct compiling *c, const node *n) { const int nch = NCH(n); - int end_lineno, end_col_offset, n_except = (nch - 3)/3; + int end_lineno, end_col_offset, n_except = (nch - 3)/3; asdl_seq *body, *handlers = NULL, *orelse = NULL, *finally = NULL; - excepthandler_ty last_handler; + excepthandler_ty last_handler; REQ(n, try_stmt); @@ -4232,20 +4232,20 @@ ast_for_try_stmt(struct compiling *c, const node *n) } assert(finally != NULL || asdl_seq_LEN(handlers)); - if (finally != NULL) { - // finally is always last - get_last_end_pos(finally, &end_lineno, &end_col_offset); - } else if (orelse != NULL) { - // otherwise else is last - get_last_end_pos(orelse, &end_lineno, &end_col_offset); - } else { - // inline the get_last_end_pos logic due to layout mismatch - last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1); - end_lineno = last_handler->end_lineno; - end_col_offset = last_handler->end_col_offset; - } - return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, - end_lineno, end_col_offset, c->c_arena); + if (finally != NULL) { + // finally is always last + get_last_end_pos(finally, &end_lineno, &end_col_offset); + } else if (orelse != NULL) { + // otherwise else is last + get_last_end_pos(orelse, &end_lineno, &end_col_offset); + } else { + // inline the get_last_end_pos logic due to layout mismatch + last_handler = (excepthandler_ty) asdl_seq_GET(handlers, n_except - 1); + end_lineno = last_handler->end_lineno; + end_col_offset = last_handler->end_col_offset; + } + return Try(body, handlers, orelse, finally, LINENO(n), n->n_col_offset, + end_lineno, end_col_offset, c->c_arena); } /* with_item: test ['as' expr] */ @@ -4272,31 +4272,31 @@ ast_for_with_item(struct compiling *c, const node *n) return withitem(context_expr, optional_vars, c->c_arena); } -/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */ +/* with_stmt: 'with' with_item (',' with_item)* ':' [TYPE_COMMENT] suite */ static stmt_ty ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async) { const node * const n = is_async ? CHILD(n0, 1) : n0; - int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset; + int i, n_items, nch_minus_type, has_type_comment, end_lineno, end_col_offset; asdl_seq *items, *body; - string type_comment; - - if (is_async && c->c_feature_version < 5) { - ast_error(c, n, - "Async with statements are only supported in Python 3.5 and greater"); - return NULL; - } - + string type_comment; + + if (is_async && c->c_feature_version < 5) { + ast_error(c, n, + "Async with statements are only supported in Python 3.5 and greater"); + return NULL; + } + REQ(n, with_stmt); - has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT; - nch_minus_type = NCH(n) - has_type_comment; - - n_items = (nch_minus_type - 2) / 2; + has_type_comment = TYPE(CHILD(n, NCH(n) - 2)) == TYPE_COMMENT; + nch_minus_type = NCH(n) - has_type_comment; + + n_items = (nch_minus_type - 2) / 2; items = _Py_asdl_seq_new(n_items, c->c_arena); if (!items) return NULL; - for (i = 1; i < nch_minus_type - 2; i += 2) { + for (i = 1; i < nch_minus_type - 2; i += 2) { withitem_ty item = ast_for_with_item(c, CHILD(n, i)); if (!item) return NULL; @@ -4306,22 +4306,22 @@ ast_for_with_stmt(struct compiling *c, const node *n0, bool is_async) body = ast_for_suite(c, CHILD(n, NCH(n) - 1)); if (!body) return NULL; - get_last_end_pos(body, &end_lineno, &end_col_offset); - - if (has_type_comment) { - type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2)); - if (!type_comment) - return NULL; - } - else - type_comment = NULL; - + get_last_end_pos(body, &end_lineno, &end_col_offset); + + if (has_type_comment) { + type_comment = NEW_TYPE_COMMENT(CHILD(n, NCH(n) - 2)); + if (!type_comment) + return NULL; + } + else + type_comment = NULL; + if (is_async) - return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset, - end_lineno, end_col_offset, c->c_arena); + return AsyncWith(items, body, type_comment, LINENO(n0), n0->n_col_offset, + end_lineno, end_col_offset, c->c_arena); else - return With(items, body, type_comment, LINENO(n), n->n_col_offset, - end_lineno, end_col_offset, c->c_arena); + return With(items, body, type_comment, LINENO(n), n->n_col_offset, + end_lineno, end_col_offset, c->c_arena); } static stmt_ty @@ -4331,7 +4331,7 @@ ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) PyObject *classname; asdl_seq *s; expr_ty call; - int end_lineno, end_col_offset; + int end_lineno, end_col_offset; REQ(n, classdef); @@ -4339,32 +4339,32 @@ ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) s = ast_for_suite(c, CHILD(n, 3)); if (!s) return NULL; - get_last_end_pos(s, &end_lineno, &end_col_offset); - + get_last_end_pos(s, &end_lineno, &end_col_offset); + classname = NEW_IDENTIFIER(CHILD(n, 1)); if (!classname) return NULL; if (forbidden_name(c, classname, CHILD(n, 3), 0)) return NULL; return ClassDef(classname, NULL, NULL, s, decorator_seq, - LINENO(n), n->n_col_offset, - end_lineno, end_col_offset, c->c_arena); + LINENO(n), n->n_col_offset, + end_lineno, end_col_offset, c->c_arena); } if (TYPE(CHILD(n, 3)) == RPAR) { /* class NAME '(' ')' ':' suite */ s = ast_for_suite(c, CHILD(n, 5)); if (!s) return NULL; - get_last_end_pos(s, &end_lineno, &end_col_offset); - + get_last_end_pos(s, &end_lineno, &end_col_offset); + classname = NEW_IDENTIFIER(CHILD(n, 1)); if (!classname) return NULL; if (forbidden_name(c, classname, CHILD(n, 3), 0)) return NULL; return ClassDef(classname, NULL, NULL, s, decorator_seq, - LINENO(n), n->n_col_offset, - end_lineno, end_col_offset, c->c_arena); + LINENO(n), n->n_col_offset, + end_lineno, end_col_offset, c->c_arena); } /* class NAME '(' arglist ')' ':' suite */ @@ -4375,19 +4375,19 @@ ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) dummy_name = NEW_IDENTIFIER(CHILD(n, 1)); if (!dummy_name) return NULL; - dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, - CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset, - c->c_arena); - call = ast_for_call(c, CHILD(n, 3), dummy, - CHILD(n, 1), NULL, CHILD(n, 4)); + dummy = Name(dummy_name, Load, LINENO(n), n->n_col_offset, + CHILD(n, 1)->n_end_lineno, CHILD(n, 1)->n_end_col_offset, + c->c_arena); + call = ast_for_call(c, CHILD(n, 3), dummy, + CHILD(n, 1), NULL, CHILD(n, 4)); if (!call) return NULL; } s = ast_for_suite(c, CHILD(n, 6)); if (!s) return NULL; - get_last_end_pos(s, &end_lineno, &end_col_offset); - + get_last_end_pos(s, &end_lineno, &end_col_offset); + classname = NEW_IDENTIFIER(CHILD(n, 1)); if (!classname) return NULL; @@ -4395,8 +4395,8 @@ ast_for_classdef(struct compiling *c, const node *n, asdl_seq *decorator_seq) return NULL; return ClassDef(classname, call->v.Call.args, call->v.Call.keywords, s, - decorator_seq, LINENO(n), n->n_col_offset, - end_lineno, end_col_offset, c->c_arena); + decorator_seq, LINENO(n), n->n_col_offset, + end_lineno, end_col_offset, c->c_arena); } static stmt_ty @@ -4421,8 +4421,8 @@ ast_for_stmt(struct compiling *c, const node *n) case del_stmt: return ast_for_del_stmt(c, n); case pass_stmt: - return Pass(LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Pass(LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); case flow_stmt: return ast_for_flow_stmt(c, n); case import_stmt: @@ -4467,7 +4467,7 @@ ast_for_stmt(struct compiling *c, const node *n) return ast_for_async_stmt(c, ch); default: PyErr_Format(PyExc_SystemError, - "unhandled compound_stmt: TYPE=%d NCH=%d\n", + "unhandled compound_stmt: TYPE=%d NCH=%d\n", TYPE(n), NCH(n)); return NULL; } @@ -4573,7 +4573,7 @@ warn_invalid_escape_sequence(struct compiling *c, const node *n, /* Replace the DeprecationWarning exception with a SyntaxError to get a more accurate error report */ PyErr_Clear(); - ast_error(c, n, "%U", msg); + ast_error(c, n, "%U", msg); } Py_DECREF(msg); return -1; @@ -4614,7 +4614,7 @@ decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s, if (*s & 0x80) { /* XXX inefficient */ PyObject *w; int kind; - const void *data; + const void *data; Py_ssize_t len, i; w = decode_utf8(c, &s, end); if (w == NULL) { @@ -4640,7 +4640,7 @@ decode_unicode_with_escapes(struct compiling *c, const node *n, const char *s, s = buf; const char *first_invalid_escape; - v = _PyUnicode_DecodeUnicodeEscapeInternal(s, len, NULL, NULL, &first_invalid_escape); + v = _PyUnicode_DecodeUnicodeEscapeInternal(s, len, NULL, NULL, &first_invalid_escape); if (v != NULL && first_invalid_escape != NULL) { if (warn_invalid_escape_sequence(c, n, *first_invalid_escape) < 0) { @@ -4660,7 +4660,7 @@ decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s, size_t len) { const char *first_invalid_escape; - PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, + PyObject *result = _PyBytes_DecodeEscape(s, len, NULL, &first_invalid_escape); if (result == NULL) return NULL; @@ -4679,7 +4679,7 @@ decode_bytes_with_escapes(struct compiling *c, const node *n, const char *s, static void fstring_shift_node_locations(node *n, int lineno, int col_offset) { n->n_col_offset = n->n_col_offset + col_offset; - n->n_end_col_offset = n->n_end_col_offset + col_offset; + n->n_end_col_offset = n->n_end_col_offset + col_offset; for (int i = 0; i < NCH(n); ++i) { if (n->n_lineno && n->n_lineno < CHILD(n, i)->n_lineno) { /* Shifting column offsets unnecessary if there's been newlines. */ @@ -4688,7 +4688,7 @@ static void fstring_shift_node_locations(node *n, int lineno, int col_offset) fstring_shift_node_locations(CHILD(n, i), lineno, col_offset); } n->n_lineno = n->n_lineno + lineno; - n->n_end_lineno = n->n_end_lineno + lineno; + n->n_end_lineno = n->n_end_lineno + lineno; } /* Fix locations for the given node and its children. @@ -4716,14 +4716,14 @@ fstring_fix_node_location(const node *parent, node *n, char *expr_str) break; start--; } - cols += (int)(substr - start); - /* adjust the start based on the number of newlines encountered - before the f-string expression */ - for (char* p = parent->n_str; p < substr; p++) { - if (*p == '\n') { - lines++; - } - } + cols += (int)(substr - start); + /* adjust the start based on the number of newlines encountered + before the f-string expression */ + for (char* p = parent->n_str; p < substr; p++) { + if (*p == '\n') { + lines++; + } + } } } fstring_shift_node_locations(n, lines, cols); @@ -4744,8 +4744,8 @@ fstring_compile_expr(const char *expr_start, const char *expr_end, assert(expr_end >= expr_start); assert(*(expr_start-1) == '{'); - assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':' || - *expr_end == '='); + assert(*expr_end == '}' || *expr_end == '!' || *expr_end == ':' || + *expr_end == '='); /* If the substring is all whitespace, it's an error. We need to catch this here, and not when we call PyParser_SimpleParseStringFlagsFilename, @@ -4766,7 +4766,7 @@ fstring_compile_expr(const char *expr_start, const char *expr_end, len = expr_end - expr_start; /* Allocate 3 extra bytes: open paren, close paren, null byte. */ - str = PyMem_Malloc(len + 3); + str = PyMem_Malloc(len + 3); if (str == NULL) { PyErr_NoMemory(); return NULL; @@ -4777,12 +4777,12 @@ fstring_compile_expr(const char *expr_start, const char *expr_end, str[len+1] = ')'; str[len+2] = 0; - PyCompilerFlags cf = _PyCompilerFlags_INIT; + PyCompilerFlags cf = _PyCompilerFlags_INIT; cf.cf_flags = PyCF_ONLY_AST; mod_n = PyParser_SimpleParseStringFlagsFilename(str, "<fstring>", Py_eval_input, 0); if (!mod_n) { - PyMem_Free(str); + PyMem_Free(str); return NULL; } /* Reuse str to find the correct column offset. */ @@ -4790,7 +4790,7 @@ fstring_compile_expr(const char *expr_start, const char *expr_end, str[len+1] = '}'; fstring_fix_node_location(n, mod_n, str); mod = PyAST_FromNode(mod_n, &cf, "<fstring>", c->c_arena); - PyMem_Free(str); + PyMem_Free(str); PyNode_Free(mod_n); if (!mod) return NULL; @@ -4888,24 +4888,24 @@ fstring_parse(const char **str, const char *end, int raw, int recurse_lvl, struct compiling *c, const node *n); /* Parse the f-string at *str, ending at end. We know *str starts an - expression (so it must be a '{'). Returns the FormattedValue node, which - includes the expression, conversion character, format_spec expression, and - optionally the text of the expression (if = is used). + expression (so it must be a '{'). Returns the FormattedValue node, which + includes the expression, conversion character, format_spec expression, and + optionally the text of the expression (if = is used). Note that I don't do a perfect job here: I don't make sure that a closing brace doesn't match an opening paren, for example. It doesn't need to error on all invalid expressions, just correctly find the end of all valid ones. Any errors inside the expression - will be caught when we parse it later. - - *expression is set to the expression. For an '=' "debug" expression, - *expr_text is set to the debug text (the original text of the expression, - including the '=' and any whitespace around it, as a string object). If - not a debug expression, *expr_text set to NULL. */ + will be caught when we parse it later. + + *expression is set to the expression. For an '=' "debug" expression, + *expr_text is set to the debug text (the original text of the expression, + including the '=' and any whitespace around it, as a string object). If + not a debug expression, *expr_text set to NULL. */ static int fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl, - PyObject **expr_text, expr_ty *expression, - struct compiling *c, const node *n) + PyObject **expr_text, expr_ty *expression, + struct compiling *c, const node *n) { /* Return -1 on error, else 0. */ @@ -4913,9 +4913,9 @@ fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl, const char *expr_end; expr_ty simple_expression; expr_ty format_spec = NULL; /* Optional format specifier. */ - int conversion = -1; /* The conversion char. Use default if not - specified, or !r if using = and no format - spec. */ + int conversion = -1; /* The conversion char. Use default if not + specified, or !r if using = and no format + spec. */ /* 0 if we're not in a string, else the quote char we're trying to match (single or double quote). */ @@ -4927,14 +4927,14 @@ fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl, /* Keep track of nesting level for braces/parens/brackets in expressions. */ Py_ssize_t nested_depth = 0; - char parenstack[MAXLEVEL]; - - *expr_text = NULL; + char parenstack[MAXLEVEL]; + *expr_text = NULL; + /* Can only nest one level deep. */ if (recurse_lvl >= 2) { ast_error(c, n, "f-string: expressions nested too deeply"); - goto error; + goto error; } /* The first char must be a left brace, or we wouldn't have gotten @@ -4959,10 +4959,10 @@ fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl, if (ch == '\\') { /* Error: can't include a backslash character, inside parens or strings or not. */ - ast_error(c, n, - "f-string expression part " - "cannot include a backslash"); - goto error; + ast_error(c, n, + "f-string expression part " + "cannot include a backslash"); + goto error; } if (quote_char) { /* We're inside a string. See if we're at the end. */ @@ -5005,61 +5005,61 @@ fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl, /* Start looking for the end of the string. */ quote_char = ch; } else if (ch == '[' || ch == '{' || ch == '(') { - if (nested_depth >= MAXLEVEL) { - ast_error(c, n, "f-string: too many nested parenthesis"); - goto error; - } - parenstack[nested_depth] = ch; + if (nested_depth >= MAXLEVEL) { + ast_error(c, n, "f-string: too many nested parenthesis"); + goto error; + } + parenstack[nested_depth] = ch; nested_depth++; } else if (ch == '#') { /* Error: can't include a comment character, inside parens or not. */ ast_error(c, n, "f-string expression part cannot include '#'"); - goto error; + goto error; } else if (nested_depth == 0 && - (ch == '!' || ch == ':' || ch == '}' || - ch == '=' || ch == '>' || ch == '<')) { - /* See if there's a next character. */ - if (*str+1 < end) { - char next = *(*str+1); - - /* For "!=". since '=' is not an allowed conversion character, - nothing is lost in this test. */ - if ((ch == '!' && next == '=') || /* != */ - (ch == '=' && next == '=') || /* == */ - (ch == '<' && next == '=') || /* <= */ - (ch == '>' && next == '=') /* >= */ - ) { - *str += 1; - continue; - } - /* Don't get out of the loop for these, if they're single - chars (not part of 2-char tokens). If by themselves, they - don't end an expression (unlike say '!'). */ - if (ch == '>' || ch == '<') { - continue; - } + (ch == '!' || ch == ':' || ch == '}' || + ch == '=' || ch == '>' || ch == '<')) { + /* See if there's a next character. */ + if (*str+1 < end) { + char next = *(*str+1); + + /* For "!=". since '=' is not an allowed conversion character, + nothing is lost in this test. */ + if ((ch == '!' && next == '=') || /* != */ + (ch == '=' && next == '=') || /* == */ + (ch == '<' && next == '=') || /* <= */ + (ch == '>' && next == '=') /* >= */ + ) { + *str += 1; + continue; + } + /* Don't get out of the loop for these, if they're single + chars (not part of 2-char tokens). If by themselves, they + don't end an expression (unlike say '!'). */ + if (ch == '>' || ch == '<') { + continue; + } } - + /* Normal way out of this loop. */ break; - } else if (ch == ']' || ch == '}' || ch == ')') { - if (!nested_depth) { - ast_error(c, n, "f-string: unmatched '%c'", ch); - goto error; - } - nested_depth--; - int opening = parenstack[nested_depth]; - if (!((opening == '(' && ch == ')') || - (opening == '[' && ch == ']') || - (opening == '{' && ch == '}'))) - { - ast_error(c, n, - "f-string: closing parenthesis '%c' " - "does not match opening parenthesis '%c'", - ch, opening); - goto error; - } + } else if (ch == ']' || ch == '}' || ch == ')') { + if (!nested_depth) { + ast_error(c, n, "f-string: unmatched '%c'", ch); + goto error; + } + nested_depth--; + int opening = parenstack[nested_depth]; + if (!((opening == '(' && ch == ')') || + (opening == '[' && ch == ']') || + (opening == '{' && ch == '}'))) + { + ast_error(c, n, + "f-string: closing parenthesis '%c' " + "does not match opening parenthesis '%c'", + ch, opening); + goto error; + } } else { /* Just consume this char and loop around. */ } @@ -5071,12 +5071,12 @@ fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl, let's just do that.*/ if (quote_char) { ast_error(c, n, "f-string: unterminated string"); - goto error; + goto error; } if (nested_depth) { - int opening = parenstack[nested_depth - 1]; - ast_error(c, n, "f-string: unmatched '%c'", opening); - goto error; + int opening = parenstack[nested_depth - 1]; + ast_error(c, n, "f-string: unmatched '%c'", opening); + goto error; } if (*str >= end) @@ -5087,33 +5087,33 @@ fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl, conversion or format_spec. */ simple_expression = fstring_compile_expr(expr_start, expr_end, c, n); if (!simple_expression) - goto error; - - /* Check for =, which puts the text value of the expression in - expr_text. */ - if (**str == '=') { - if (c->c_feature_version < 8) { - ast_error(c, n, - "f-string: self documenting expressions are " - "only supported in Python 3.8 and greater"); - goto error; - } - *str += 1; - - /* Skip over ASCII whitespace. No need to test for end of string - here, since we know there's at least a trailing quote somewhere - ahead. */ - while (Py_ISSPACE(**str)) { - *str += 1; - } - - /* Set *expr_text to the text of the expression. */ - *expr_text = PyUnicode_FromStringAndSize(expr_start, *str-expr_start); - if (!*expr_text) { - goto error; - } - } - + goto error; + + /* Check for =, which puts the text value of the expression in + expr_text. */ + if (**str == '=') { + if (c->c_feature_version < 8) { + ast_error(c, n, + "f-string: self documenting expressions are " + "only supported in Python 3.8 and greater"); + goto error; + } + *str += 1; + + /* Skip over ASCII whitespace. No need to test for end of string + here, since we know there's at least a trailing quote somewhere + ahead. */ + while (Py_ISSPACE(**str)) { + *str += 1; + } + + /* Set *expr_text to the text of the expression. */ + *expr_text = PyUnicode_FromStringAndSize(expr_start, *str-expr_start); + if (!*expr_text) { + goto error; + } + } + /* Check for a conversion char, if present. */ if (**str == '!') { *str += 1; @@ -5124,13 +5124,13 @@ fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl, *str += 1; /* Validate the conversion. */ - if (!(conversion == 's' || conversion == 'r' || conversion == 'a')) { - ast_error(c, n, - "f-string: invalid conversion character: " - "expected 's', 'r', or 'a'"); - goto error; + if (!(conversion == 's' || conversion == 'r' || conversion == 'a')) { + ast_error(c, n, + "f-string: invalid conversion character: " + "expected 's', 'r', or 'a'"); + goto error; } - + } /* Check for the format spec, if present. */ @@ -5144,7 +5144,7 @@ fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl, /* Parse the format spec. */ format_spec = fstring_parse(str, end, raw, recurse_lvl+1, c, n); if (!format_spec) - goto error; + goto error; } if (*str >= end || **str != '}') @@ -5155,31 +5155,31 @@ fstring_find_expr(const char **str, const char *end, int raw, int recurse_lvl, assert(**str == '}'); *str += 1; - /* If we're in = mode (detected by non-NULL expr_text), and have no format - spec and no explicit conversion, set the conversion to 'r'. */ - if (*expr_text && format_spec == NULL && conversion == -1) { - conversion = 'r'; - } - + /* If we're in = mode (detected by non-NULL expr_text), and have no format + spec and no explicit conversion, set the conversion to 'r'. */ + if (*expr_text && format_spec == NULL && conversion == -1) { + conversion = 'r'; + } + /* And now create the FormattedValue node that represents this entire expression with the conversion and format spec. */ *expression = FormattedValue(simple_expression, conversion, - format_spec, LINENO(n), - n->n_col_offset, n->n_end_lineno, - n->n_end_col_offset, c->c_arena); + format_spec, LINENO(n), + n->n_col_offset, n->n_end_lineno, + n->n_end_col_offset, c->c_arena); if (!*expression) - goto error; + goto error; return 0; unexpected_end_of_string: ast_error(c, n, "f-string: expecting '}'"); - /* Falls through to error. */ - -error: - Py_XDECREF(*expr_text); + /* Falls through to error. */ + +error: + Py_XDECREF(*expr_text); return -1; - + } /* Return -1 on error. @@ -5208,7 +5208,7 @@ error: static int fstring_find_literal_and_expr(const char **str, const char *end, int raw, int recurse_lvl, PyObject **literal, - PyObject **expr_text, expr_ty *expression, + PyObject **expr_text, expr_ty *expression, struct compiling *c, const node *n) { int result; @@ -5236,8 +5236,8 @@ fstring_find_literal_and_expr(const char **str, const char *end, int raw, /* We must now be the start of an expression, on a '{'. */ assert(**str == '{'); - if (fstring_find_expr(str, end, raw, recurse_lvl, expr_text, - expression, c, n) < 0) + if (fstring_find_expr(str, end, raw, recurse_lvl, expr_text, + expression, c, n) < 0) goto error; return 0; @@ -5255,8 +5255,8 @@ typedef struct { expr_ty's, and then after that start dynamically allocating, doubling the number allocated each time. Note that the f-string f'{0}a{1}' contains 3 expr_ty's: 2 FormattedValue's, and one - Constant for the literal 'a'. So you add expr_ty's about twice as - fast as you add expressions in an f-string. */ + Constant for the literal 'a'. So you add expr_ty's about twice as + fast as you add expressions in an f-string. */ Py_ssize_t allocated; /* Number we've allocated. */ Py_ssize_t size; /* Number we've used. */ @@ -5306,7 +5306,7 @@ ExprList_Append(ExprList *l, expr_ty exp) Py_ssize_t i; /* We're still using the cached data. Switch to alloc-ing. */ - l->p = PyMem_Malloc(sizeof(expr_ty) * new_size); + l->p = PyMem_Malloc(sizeof(expr_ty) * new_size); if (!l->p) return -1; /* Copy the cached data into the new buffer. */ @@ -5314,9 +5314,9 @@ ExprList_Append(ExprList *l, expr_ty exp) l->p[i] = l->data[i]; } else { /* Just realloc. */ - expr_ty *tmp = PyMem_Realloc(l->p, sizeof(expr_ty) * new_size); + expr_ty *tmp = PyMem_Realloc(l->p, sizeof(expr_ty) * new_size); if (!tmp) { - PyMem_Free(l->p); + PyMem_Free(l->p); l->p = NULL; return -1; } @@ -5344,7 +5344,7 @@ ExprList_Dealloc(ExprList *l) /* Do nothing. */ } else { /* We have dynamically allocated. Free the memory. */ - PyMem_Free(l->p); + PyMem_Free(l->p); } l->p = NULL; l->size = -1; @@ -5407,58 +5407,58 @@ FstringParser_Dealloc(FstringParser *state) ExprList_Dealloc(&state->expr_list); } -/* Constants for the following */ -static PyObject *u_kind; - -/* Compute 'kind' field for string Constant (either 'u' or None) */ -static PyObject * -make_kind(struct compiling *c, const node *n) -{ - char *s = NULL; - PyObject *kind = NULL; - - /* Find the first string literal, if any */ - while (TYPE(n) != STRING) { - if (NCH(n) == 0) - return NULL; - n = CHILD(n, 0); - } - REQ(n, STRING); - - /* If it starts with 'u', return a PyUnicode "u" string */ - s = STR(n); - if (s && *s == 'u') { - if (!u_kind) { - u_kind = PyUnicode_InternFromString("u"); - if (!u_kind) - return NULL; - } - kind = u_kind; - if (PyArena_AddPyObject(c->c_arena, kind) < 0) { - return NULL; - } - Py_INCREF(kind); - } - return kind; -} - -/* Make a Constant node, but decref the PyUnicode object being added. */ +/* Constants for the following */ +static PyObject *u_kind; + +/* Compute 'kind' field for string Constant (either 'u' or None) */ +static PyObject * +make_kind(struct compiling *c, const node *n) +{ + char *s = NULL; + PyObject *kind = NULL; + + /* Find the first string literal, if any */ + while (TYPE(n) != STRING) { + if (NCH(n) == 0) + return NULL; + n = CHILD(n, 0); + } + REQ(n, STRING); + + /* If it starts with 'u', return a PyUnicode "u" string */ + s = STR(n); + if (s && *s == 'u') { + if (!u_kind) { + u_kind = PyUnicode_InternFromString("u"); + if (!u_kind) + return NULL; + } + kind = u_kind; + if (PyArena_AddPyObject(c->c_arena, kind) < 0) { + return NULL; + } + Py_INCREF(kind); + } + return kind; +} + +/* Make a Constant node, but decref the PyUnicode object being added. */ static expr_ty make_str_node_and_del(PyObject **str, struct compiling *c, const node* n) { PyObject *s = *str; - PyObject *kind = NULL; + PyObject *kind = NULL; *str = NULL; assert(PyUnicode_CheckExact(s)); if (PyArena_AddPyObject(c->c_arena, s) < 0) { Py_DECREF(s); return NULL; } - kind = make_kind(c, n); - if (kind == NULL && PyErr_Occurred()) - return NULL; - return Constant(s, kind, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + kind = make_kind(c, n); + if (kind == NULL && PyErr_Occurred()) + return NULL; + return Constant(s, kind, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } /* Add a non-f-string (that is, a regular literal string). str is @@ -5501,7 +5501,7 @@ FstringParser_ConcatFstring(FstringParser *state, const char **str, /* Parse the f-string. */ while (1) { PyObject *literal = NULL; - PyObject *expr_text = NULL; + PyObject *expr_text = NULL; expr_ty expression = NULL; /* If there's a zero length literal in front of the @@ -5509,23 +5509,23 @@ FstringParser_ConcatFstring(FstringParser *state, const char **str, the f-string, expression will be NULL (unless result == 1, see below). */ int result = fstring_find_literal_and_expr(str, end, raw, recurse_lvl, - &literal, &expr_text, - &expression, c, n); + &literal, &expr_text, + &expression, c, n); if (result < 0) return -1; /* Add the literal, if any. */ - if (literal && FstringParser_ConcatAndDel(state, literal) < 0) { - Py_XDECREF(expr_text); - return -1; - } - /* Add the expr_text, if any. */ - if (expr_text && FstringParser_ConcatAndDel(state, expr_text) < 0) { - return -1; + if (literal && FstringParser_ConcatAndDel(state, literal) < 0) { + Py_XDECREF(expr_text); + return -1; } + /* Add the expr_text, if any. */ + if (expr_text && FstringParser_ConcatAndDel(state, expr_text) < 0) { + return -1; + } - /* We've dealt with the literal and expr_text, their ownership has - been transferred to the state object. Don't look at them again. */ + /* We've dealt with the literal and expr_text, their ownership has + been transferred to the state object. Don't look at them again. */ /* See if we should just loop around to get the next literal and expression, while ignoring the expression this @@ -5539,11 +5539,11 @@ FstringParser_ConcatFstring(FstringParser *state, const char **str, break; /* We know we have an expression. Convert any existing string - to a Constant node. */ + to a Constant node. */ if (!state->last_str) { /* Do nothing. No previous literal. */ } else { - /* Convert the existing last_str literal to a Constant node. */ + /* Convert the existing last_str literal to a Constant node. */ expr_ty str = make_str_node_and_del(&state->last_str, c, n); if (!str || ExprList_Append(&state->expr_list, str) < 0) return -1; @@ -5570,7 +5570,7 @@ FstringParser_ConcatFstring(FstringParser *state, const char **str, } /* Convert the partial state reflected in last_str and expr_list to an - expr_ty. The expr_ty can be a Constant, or a JoinedStr. */ + expr_ty. The expr_ty can be a Constant, or a JoinedStr. */ static expr_ty FstringParser_Finish(FstringParser *state, struct compiling *c, const node *n) @@ -5592,7 +5592,7 @@ FstringParser_Finish(FstringParser *state, struct compiling *c, return make_str_node_and_del(&state->last_str, c, n); } - /* Create a Constant node out of last_str, if needed. It will be the + /* Create a Constant node out of last_str, if needed. It will be the last node in our expression list. */ if (state->last_str) { expr_ty str = make_str_node_and_del(&state->last_str, c, n); @@ -5606,8 +5606,8 @@ FstringParser_Finish(FstringParser *state, struct compiling *c, if (!seq) goto error; - return JoinedStr(seq, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return JoinedStr(seq, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); error: FstringParser_Dealloc(state); @@ -5674,13 +5674,13 @@ parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode, } } } - - /* fstrings are only allowed in Python 3.6 and greater */ - if (fmode && c->c_feature_version < 6) { - ast_error(c, n, "Format strings are only supported in Python 3.6 and greater"); - return -1; - } - + + /* fstrings are only allowed in Python 3.6 and greater */ + if (fmode && c->c_feature_version < 6) { + ast_error(c, n, "Format strings are only supported in Python 3.6 and greater"); + return -1; + } + if (fmode && *bytesmode) { PyErr_BadInternalCall(); return -1; @@ -5731,8 +5731,8 @@ parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode, const char *ch; for (ch = s; *ch; ch++) { if (Py_CHARMASK(*ch) >= 0x80) { - ast_error(c, n, - "bytes can only contain ASCII " + ast_error(c, n, + "bytes can only contain ASCII " "literal characters."); return -1; } @@ -5752,9 +5752,9 @@ parsestr(struct compiling *c, const node *n, int *bytesmode, int *rawmode, /* Accepts a STRING+ atom, and produces an expr_ty node. Run through each STRING atom, and process it as needed. For bytes, just - concatenate them together, and the result will be a Constant node. For + concatenate them together, and the result will be a Constant node. For normal strings and f-strings, concatenate them together. The result - will be a Constant node if there were no f-strings; a FormattedValue + will be a Constant node if there were no f-strings; a FormattedValue node if there's just an f-string (with no leading or trailing literals), or a JoinedStr node if there are multiple f-strings or any literals involved. */ @@ -5825,8 +5825,8 @@ parsestrplus(struct compiling *c, const node *n) /* Just return the bytes object and we're done. */ if (PyArena_AddPyObject(c->c_arena, bytes_str) < 0) goto error; - return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset, - n->n_end_lineno, n->n_end_col_offset, c->c_arena); + return Constant(bytes_str, NULL, LINENO(n), n->n_col_offset, + n->n_end_lineno, n->n_end_col_offset, c->c_arena); } /* We're not a bytes string, bytes_str should never have been set. */ @@ -5839,20 +5839,20 @@ error: FstringParser_Dealloc(&state); return NULL; } - -PyObject * -_PyAST_GetDocString(asdl_seq *body) -{ - if (!asdl_seq_LEN(body)) { - return NULL; - } - stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0); - if (st->kind != Expr_kind) { - return NULL; - } - expr_ty e = st->v.Expr.value; - if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) { - return e->v.Constant.value; - } - return NULL; -} + +PyObject * +_PyAST_GetDocString(asdl_seq *body) +{ + if (!asdl_seq_LEN(body)) { + return NULL; + } + stmt_ty st = (stmt_ty)asdl_seq_GET(body, 0); + if (st->kind != Expr_kind) { + return NULL; + } + expr_ty e = st->v.Expr.value; + if (e->kind == Constant_kind && PyUnicode_CheckExact(e->v.Constant.value)) { + return e->v.Constant.value; + } + return NULL; +} |