summaryrefslogtreecommitdiffstats
path: root/Parser
diff options
context:
space:
mode:
authorLysandros Nikolaou <lisandrosnik@gmail.com>2020-06-26 11:24:05 (GMT)
committerGitHub <noreply@github.com>2020-06-26 11:24:05 (GMT)
commit2e0a920e9eb540654c0bb2298143b00637dc5961 (patch)
tree25c0806ff15b04016ea802dfa84a042532e9e97f /Parser
parentef19bad7d6da99575d66c1f5dc8fd6ac57e92f6e (diff)
downloadcpython-2e0a920e9eb540654c0bb2298143b00637dc5961.zip
cpython-2e0a920e9eb540654c0bb2298143b00637dc5961.tar.gz
cpython-2e0a920e9eb540654c0bb2298143b00637dc5961.tar.bz2
bpo-41084: Adjust message when an f-string expression causes a SyntaxError (GH-21084)
Prefix the error message with `fstring: `, when parsing an f-string expression throws a `SyntaxError`.
Diffstat (limited to 'Parser')
-rw-r--r--Parser/pegen.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/Parser/pegen.c b/Parser/pegen.c
index 594754c..79fcd2f 100644
--- a/Parser/pegen.c
+++ b/Parser/pegen.c
@@ -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
+ 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;
@@ -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;
}