diff options
author | Lysandros Nikolaou <lisandrosnik@gmail.com> | 2020-05-26 04:24:31 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-26 02:24:31 +0100 |
commit | 791a46ed58f74d673cf2c0d81deec57155bf7583 (patch) | |
tree | 26b3037aba7bcaafdb3a3f83ec73ebdc835511df | |
parent | 6cb0ad20396116b5076a58b05b55286d6d5e0c94 (diff) | |
download | cpython-git-791a46ed58f74d673cf2c0d81deec57155bf7583.tar.gz |
[3.9] bpo-38964: Print correct filename on a SyntaxError in an fstring (GH-20399) (GH-20404)
When a `SyntaxError` in the expression part of a fstring is found,
the filename attribute of the `SyntaxError` is always `<fstring>`.
With this commit, it gets changed to always have the name of the file
the fstring resides in.
Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>.
(cherry picked from commit f7b1e461567e5e3fa3ba46f589d9edc1b45b2dd0)
-rw-r--r-- | Lib/test/test_fstring.py | 14 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Core and Builtins/2020-05-25-21-49-11.bpo-38964.lrml90.rst | 1 | ||||
-rw-r--r-- | Parser/pegen/parse_string.c | 7 |
3 files changed, 16 insertions, 6 deletions
diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py index e423b52cd5..3237b5804d 100644 --- a/Lib/test/test_fstring.py +++ b/Lib/test/test_fstring.py @@ -8,10 +8,12 @@ # Unicode identifiers in tests is allowed by PEP 3131. import ast +import os import types import decimal import unittest -from test.support import use_old_parser +from test.support import temp_cwd, use_old_parser +from test.support.script_helper import assert_python_failure a_global = 'global variable' @@ -1050,6 +1052,16 @@ non-important content r"f'{1000:j}'", ]) + @unittest.skipIf(use_old_parser(), "The old parser only supports <fstring> as the filename") + def test_filename_in_syntaxerror(self): + # see issue 38964 + with temp_cwd() as cwd: + file_path = os.path.join(cwd, 't.py') + with open(file_path, 'w') as f: + f.write('f"{a b}"') # This generates a SyntaxError + _, _, stderr = assert_python_failure(file_path) + self.assertIn(file_path, stderr.decode('utf-8')) + def test_loop(self): for i in range(1000): self.assertEqual(f'i:{i}', 'i:' + str(i)) diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-05-25-21-49-11.bpo-38964.lrml90.rst b/Misc/NEWS.d/next/Core and Builtins/2020-05-25-21-49-11.bpo-38964.lrml90.rst new file mode 100644 index 0000000000..1200764306 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2020-05-25-21-49-11.bpo-38964.lrml90.rst @@ -0,0 +1 @@ +When there's a :exc:`SyntaxError` in the expression part of an fstring, the filename attribute of the :exc:`SyntaxError` gets correctly set to the name of the file the fstring resides in.
\ No newline at end of file diff --git a/Parser/pegen/parse_string.c b/Parser/pegen/parse_string.c index ca4b733c15..a0ec698fa5 100644 --- a/Parser/pegen/parse_string.c +++ b/Parser/pegen/parse_string.c @@ -606,11 +606,8 @@ fstring_compile_expr(Parser *p, const char *expr_start, const char *expr_end, if (tok == NULL) { return NULL; } - tok->filename = PyUnicode_FromString("<fstring>"); - if (!tok->filename) { - PyTokenizer_Free(tok); - return NULL; - } + Py_INCREF(p->tok->filename); + tok->filename = p->tok->filename; Parser *p2 = _PyPegen_Parser_New(tok, Py_fstring_input, p->flags, p->feature_version, NULL, p->arena); |