Skip to content

Commit 0f6373c

Browse files
Issue python#28739: f-string expressions no longer accepted as docstrings and
by ast.literal_eval() even if they do not include subexpressions.
2 parents 26817a8 + 4cc30ae commit 0f6373c

4 files changed

Lines changed: 20 additions & 17 deletions

File tree

Lib/test/test_fstring.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,18 @@ def __call__(self):
7070
# Make sure x was called.
7171
self.assertTrue(x.called)
7272

73-
def test_literal_eval(self):
74-
# With no expressions, an f-string is okay.
75-
self.assertEqual(ast.literal_eval("f'x'"), 'x')
76-
self.assertEqual(ast.literal_eval("f'x' 'y'"), 'xy')
77-
78-
# But this should raise an error.
79-
with self.assertRaisesRegex(ValueError, 'malformed node or string'):
80-
ast.literal_eval("f'x{3}'")
73+
def test_docstring(self):
74+
def f():
75+
f'''Not a docstring'''
76+
self.assertIsNone(f.__doc__)
77+
def g():
78+
'''Not a docstring''' \
79+
f''
80+
self.assertIsNone(g.__doc__)
8181

82-
# As should this, which uses a different ast node
82+
def test_literal_eval(self):
8383
with self.assertRaisesRegex(ValueError, 'malformed node or string'):
84-
ast.literal_eval("f'{3}'")
84+
ast.literal_eval("f'x'")
8585

8686
def test_ast_compile_time_concat(self):
8787
x = ['']

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #28739: f-string expressions no longer accepted as docstrings and
14+
by ast.literal_eval() even if they do not include expressions.
15+
1316
- Issue #28512: Fixed setting the offset attribute of SyntaxError by
1417
PyErr_SyntaxLocationEx() and PyErr_SyntaxLocationObject().
1518

Python/ast.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4784,6 +4784,7 @@ ExprList_Finish(ExprList *l, PyArena *arena)
47844784
typedef struct {
47854785
PyObject *last_str;
47864786
ExprList expr_list;
4787+
int fmode;
47874788
} FstringParser;
47884789

47894790
#ifdef NDEBUG
@@ -4802,6 +4803,7 @@ static void
48024803
FstringParser_Init(FstringParser *state)
48034804
{
48044805
state->last_str = NULL;
4806+
state->fmode = 0;
48054807
ExprList_Init(&state->expr_list);
48064808
FstringParser_check_invariants(state);
48074809
}
@@ -4864,6 +4866,7 @@ FstringParser_ConcatFstring(FstringParser *state, const char **str,
48644866
struct compiling *c, const node *n)
48654867
{
48664868
FstringParser_check_invariants(state);
4869+
state->fmode = 1;
48674870

48684871
/* Parse the f-string. */
48694872
while (1) {
@@ -4955,7 +4958,8 @@ FstringParser_Finish(FstringParser *state, struct compiling *c,
49554958

49564959
/* If we're just a constant string with no expressions, return
49574960
that. */
4958-
if(state->expr_list.size == 0) {
4961+
if (!state->fmode) {
4962+
assert(!state->expr_list.size);
49594963
if (!state->last_str) {
49604964
/* Create a zero length string. */
49614965
state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
@@ -4979,11 +4983,6 @@ FstringParser_Finish(FstringParser *state, struct compiling *c,
49794983
if (!seq)
49804984
goto error;
49814985

4982-
/* If there's only one expression, return it. Otherwise, we need
4983-
to join them together. */
4984-
if (seq->size == 1)
4985-
return seq->elements[0];
4986-
49874986
return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
49884987

49894988
error:

Python/compile.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3412,7 +3412,8 @@ static int
34123412
compiler_joined_str(struct compiler *c, expr_ty e)
34133413
{
34143414
VISIT_SEQ(c, expr, e->v.JoinedStr.values);
3415-
ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
3415+
if (asdl_seq_LEN(e->v.JoinedStr.values) != 1)
3416+
ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
34163417
return 1;
34173418
}
34183419

0 commit comments

Comments
 (0)