Issue #28739: f-string expressions no longer accepted as docstrings and

by ast.literal_eval() even if they do not include subexpressions.
This commit is contained in:
Serhiy Storchaka 2016-12-11 19:37:19 +02:00
parent 8114f21668
commit 4cc30ae313
4 changed files with 20 additions and 17 deletions

View file

@ -4789,6 +4789,7 @@ ExprList_Finish(ExprList *l, PyArena *arena)
typedef struct {
PyObject *last_str;
ExprList expr_list;
int fmode;
} FstringParser;
#ifdef NDEBUG
@ -4807,6 +4808,7 @@ static void
FstringParser_Init(FstringParser *state)
{
state->last_str = NULL;
state->fmode = 0;
ExprList_Init(&state->expr_list);
FstringParser_check_invariants(state);
}
@ -4869,6 +4871,7 @@ FstringParser_ConcatFstring(FstringParser *state, const char **str,
struct compiling *c, const node *n)
{
FstringParser_check_invariants(state);
state->fmode = 1;
/* Parse the f-string. */
while (1) {
@ -4960,7 +4963,8 @@ FstringParser_Finish(FstringParser *state, struct compiling *c,
/* If we're just a constant string with no expressions, return
that. */
if(state->expr_list.size == 0) {
if (!state->fmode) {
assert(!state->expr_list.size);
if (!state->last_str) {
/* Create a zero length string. */
state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
@ -4984,11 +4988,6 @@ FstringParser_Finish(FstringParser *state, struct compiling *c,
if (!seq)
goto error;
/* If there's only one expression, return it. Otherwise, we need
to join them together. */
if (seq->size == 1)
return seq->elements[0];
return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
error: