Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4cc30ae

Browse files
Issue #28739: f-string expressions no longer accepted as docstrings and
by ast.literal_eval() even if they do not include subexpressions.
1 parent 8114f21 commit 4cc30ae

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.6.1 release candidate 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
@@ -4789,6 +4789,7 @@ ExprList_Finish(ExprList *l, PyArena *arena)
47894789
typedef struct {
47904790
PyObject *last_str;
47914791
ExprList expr_list;
4792+
int fmode;
47924793
} FstringParser;
47934794

47944795
#ifdef NDEBUG
@@ -4807,6 +4808,7 @@ static void
48074808
FstringParser_Init(FstringParser *state)
48084809
{
48094810
state->last_str = NULL;
4811+
state->fmode = 0;
48104812
ExprList_Init(&state->expr_list);
48114813
FstringParser_check_invariants(state);
48124814
}
@@ -4869,6 +4871,7 @@ FstringParser_ConcatFstring(FstringParser *state, const char **str,
48694871
struct compiling *c, const node *n)
48704872
{
48714873
FstringParser_check_invariants(state);
4874+
state->fmode = 1;
48724875

48734876
/* Parse the f-string. */
48744877
while (1) {
@@ -4960,7 +4963,8 @@ FstringParser_Finish(FstringParser *state, struct compiling *c,
49604963

49614964
/* If we're just a constant string with no expressions, return
49624965
that. */
4963-
if(state->expr_list.size == 0) {
4966+
if (!state->fmode) {
4967+
assert(!state->expr_list.size);
49644968
if (!state->last_str) {
49654969
/* Create a zero length string. */
49664970
state->last_str = PyUnicode_FromStringAndSize(NULL, 0);
@@ -4984,11 +4988,6 @@ FstringParser_Finish(FstringParser *state, struct compiling *c,
49844988
if (!seq)
49854989
goto error;
49864990

4987-
/* If there's only one expression, return it. Otherwise, we need
4988-
to join them together. */
4989-
if (seq->size == 1)
4990-
return seq->elements[0];
4991-
49924991
return JoinedStr(seq, LINENO(n), n->n_col_offset, c->c_arena);
49934992

49944993
error:

Python/compile.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3415,7 +3415,8 @@ static int
34153415
compiler_joined_str(struct compiler *c, expr_ty e)
34163416
{
34173417
VISIT_SEQ(c, expr, e->v.JoinedStr.values);
3418-
ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
3418+
if (asdl_seq_LEN(e->v.JoinedStr.values) != 1)
3419+
ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
34193420
return 1;
34203421
}
34213422

0 commit comments

Comments
 (0)