Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Lib/test/test_fstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ def test_format_specifier_expressions(self):
# This looks like a nested format spec.
])

self.assertAllRaise(SyntaxError, "invalid syntax",
self.assertAllRaise(SyntaxError, "f-string: invalid syntax",
[# Invalid syntax inside a nested spec.
"f'{4:{/5}}'",
])
Expand Down Expand Up @@ -598,7 +598,7 @@ def test_parens_in_expressions(self):
# are added around it. But we shouldn't go from an invalid
# expression to a valid one. The added parens are just
# supposed to allow whitespace (including newlines).
self.assertAllRaise(SyntaxError, 'invalid syntax',
self.assertAllRaise(SyntaxError, 'f-string: invalid syntax',
["f'{,}'",
"f'{,}'", # this is (,), which is an error
])
Expand Down Expand Up @@ -716,7 +716,7 @@ def test_lambda(self):

# lambda doesn't work without parens, because the colon
# makes the parser think it's a format_spec
self.assertAllRaise(SyntaxError, 'invalid syntax',
self.assertAllRaise(SyntaxError, 'f-string: invalid syntax',
["f'{lambda x:x}'",
])

Expand Down Expand Up @@ -1193,6 +1193,10 @@ def test_walrus(self):
self.assertEqual(f'{(x:=10)}', '10')
self.assertEqual(x, 10)

def test_invalid_syntax_error_message(self):
with self.assertRaisesRegex(SyntaxError, "f-string: invalid syntax"):
compile("f'{a $ b}'", "?", "exec")


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Prefix the error message with 'f-string: ', when parsing an f-string expression which throws a :exc:`SyntaxError`.
21 changes: 21 additions & 0 deletions Parser/pegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,21 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
PyObject *tmp = NULL;
p->error_indicator = 1;

if (p->start_rule == Py_fstring_input) {
const char *fstring_msg = "f-string: ";
Py_ssize_t len = strlen(fstring_msg) + strlen(errmsg);

char *new_errmsg = PyMem_RawMalloc(len + 1); // Lengths of both strings plus NULL character

@pablogsal pablogsal Jun 23, 2020

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be PyMem_Malloc no? Any reason why you need the raw allocator?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used PyMem_RawMalloc, because fstring_compile_expr uses it for str in string_parser.c. You know best what's more suitable here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to move them to PyMem_Malloc, but let's do that in a different PR

if (!new_errmsg) {
return (void *) PyErr_NoMemory();
}

// Copy both strings into new buffer
memcpy(new_errmsg, fstring_msg, strlen(fstring_msg));
memcpy(new_errmsg + strlen(fstring_msg), errmsg, strlen(errmsg));
new_errmsg[len] = 0;
errmsg = new_errmsg;
}
errstr = PyUnicode_FromFormatV(errmsg, va);
if (!errstr) {
goto error;
Expand Down Expand Up @@ -427,11 +442,17 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,

Py_DECREF(errstr);
Py_DECREF(value);
if (p->start_rule == Py_fstring_input) {
PyMem_RawFree((void *)errmsg);
}
return NULL;

error:
Py_XDECREF(errstr);
Py_XDECREF(error_line);
if (p->start_rule == Py_fstring_input) {
PyMem_RawFree((void *)errmsg);
}
return NULL;
}

Expand Down