summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_fstring.py
diff options
context:
space:
mode:
authorLysandros Nikolaou <lisandrosnik@gmail.com>2020-05-26 00:32:18 (GMT)
committerGitHub <noreply@github.com>2020-05-26 00:32:18 (GMT)
commitf7b1e461567e5e3fa3ba46f589d9edc1b45b2dd0 (patch)
treeae35c88616222dd863201ea8549ac09c30d9b407 /Lib/test/test_fstring.py
parent2602d97a0ae92b2d320909024e901c202b003e14 (diff)
downloadcpython-f7b1e461567e5e3fa3ba46f589d9edc1b45b2dd0.zip
cpython-f7b1e461567e5e3fa3ba46f589d9edc1b45b2dd0.tar.gz
cpython-f7b1e461567e5e3fa3ba46f589d9edc1b45b2dd0.tar.bz2
bpo-38964: Print correct filename on a SyntaxError in an fstring (GH-20399)
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>
Diffstat (limited to 'Lib/test/test_fstring.py')
-rw-r--r--Lib/test/test_fstring.py13
1 files changed, 13 insertions, 0 deletions
diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py
index e0bb5b5..ea4e589 100644
--- a/Lib/test/test_fstring.py
+++ b/Lib/test/test_fstring.py
@@ -8,9 +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 temp_cwd, use_old_parser
+from test.support.script_helper import assert_python_failure
a_global = 'global variable'
@@ -1044,6 +1047,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))