diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2020-06-27 19:43:49 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-27 19:43:49 (GMT) |
commit | cb0dc52d37a5cc561ad39bc034afe7db9c461768 (patch) | |
tree | 30d1567517db082f620e6ef2b03738e7e4825ff4 /Parser/pegen | |
parent | 5193d0a665eb0944faae9fb20e2062cea0dc02e7 (diff) | |
download | cpython-cb0dc52d37a5cc561ad39bc034afe7db9c461768.zip cpython-cb0dc52d37a5cc561ad39bc034afe7db9c461768.tar.gz cpython-cb0dc52d37a5cc561ad39bc034afe7db9c461768.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`.
(cherry picked from commit 2e0a920e9eb540654c0bb2298143b00637dc5961)
Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Diffstat (limited to 'Parser/pegen')
-rw-r--r-- | Parser/pegen/pegen.c | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/Parser/pegen/pegen.c b/Parser/pegen/pegen.c index a1a59ae..53591d2 100644 --- a/Parser/pegen/pegen.c +++ b/Parser/pegen/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; } |