diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2021-05-23 16:06:48 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-23 16:06:48 (GMT) |
commit | 8b010673185d36d13e69e5bf7d902a0b3fa63051 (patch) | |
tree | 0edd3dfd0d9ea213062c78f8eeb230f21af3c5c8 /Python/ast_opt.c | |
parent | bd7476dae337e905e7b1bbf33ddb96cc270fdc84 (diff) | |
download | cpython-8b010673185d36d13e69e5bf7d902a0b3fa63051.zip cpython-8b010673185d36d13e69e5bf7d902a0b3fa63051.tar.gz cpython-8b010673185d36d13e69e5bf7d902a0b3fa63051.tar.bz2 |
bpo-28307: Tests and fixes for optimization of C-style formatting (GH-26318)
Fix errors:
* "%10.s" should be equal to "%10.0s", not "%10s".
* Tuples with starred expressions caused a SyntaxError.
Diffstat (limited to 'Python/ast_opt.c')
-rw-r--r-- | Python/ast_opt.c | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/Python/ast_opt.c b/Python/ast_opt.c index 53e089c..f6506ce 100644 --- a/Python/ast_opt.c +++ b/Python/ast_opt.c @@ -30,6 +30,20 @@ make_const(expr_ty node, PyObject *val, PyArena *arena) #define COPY_NODE(TO, FROM) (memcpy((TO), (FROM), sizeof(struct _expr))) +static int +has_starred(asdl_expr_seq *elts) +{ + Py_ssize_t n = asdl_seq_LEN(elts); + for (Py_ssize_t i = 0; i < n; i++) { + expr_ty e = (expr_ty)asdl_seq_GET(elts, i); + if (e->kind == Starred_kind) { + return 1; + } + } + return 0; +} + + static PyObject* unary_not(PyObject *v) { @@ -318,8 +332,8 @@ simple_format_arg_parse(PyObject *fmt, Py_ssize_t *ppos, if (ch == '.') { NEXTC; + *prec = 0; if ('0' <= ch && ch <= '9') { - *prec = 0; int digits = 0; while ('0' <= ch && ch <= '9') { *prec = *prec * 10 + (ch - '0'); @@ -445,7 +459,8 @@ fold_binop(expr_ty node, PyArena *arena, _PyASTOptimizeState *state) if (node->v.BinOp.op == Mod && rhs->kind == Tuple_kind && - PyUnicode_Check(lv)) + PyUnicode_Check(lv) && + !has_starred(rhs->v.Tuple.elts)) { return optimize_format(node, lv, rhs->v.Tuple.elts, arena); } @@ -572,12 +587,8 @@ fold_iter(expr_ty arg, PyArena *arena, _PyASTOptimizeState *state) if (arg->kind == List_kind) { /* First change a list into tuple. */ asdl_expr_seq *elts = arg->v.List.elts; - Py_ssize_t n = asdl_seq_LEN(elts); - for (Py_ssize_t i = 0; i < n; i++) { - expr_ty e = (expr_ty)asdl_seq_GET(elts, i); - if (e->kind == Starred_kind) { - return 1; - } + if (has_starred(elts)) { + return 1; } expr_context_ty ctx = arg->v.List.ctx; arg->kind = Tuple_kind; |