diff options
author | Batuhan Taskaya <batuhanosmantaskaya@gmail.com> | 2020-05-01 13:13:43 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-01 13:13:43 (GMT) |
commit | 76c1b4d5c5a610c09943e1ee7ae18f1957804730 (patch) | |
tree | eeaf4c2eb7b712817866ba49d04a5c57a3508795 /Lib | |
parent | 719e14d2837520c18398a3e22a36f20c1fe76edf (diff) | |
download | cpython-76c1b4d5c5a610c09943e1ee7ae18f1957804730.zip cpython-76c1b4d5c5a610c09943e1ee7ae18f1957804730.tar.gz cpython-76c1b4d5c5a610c09943e1ee7ae18f1957804730.tar.bz2 |
bpo-40334: Improve column offsets for thrown syntax errors by Pegen (GH-19782)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_cmd_line_script.py | 6 | ||||
-rw-r--r-- | Lib/test/test_exceptions.py | 42 |
2 files changed, 24 insertions, 24 deletions
diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py index f0130e3..1fc9500 100644 --- a/Lib/test/test_cmd_line_script.py +++ b/Lib/test/test_cmd_line_script.py @@ -599,7 +599,7 @@ class CmdLineTest(unittest.TestCase): exitcode, stdout, stderr = assert_python_failure(script_name) text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read() # Confirm that the caret is located under the first 1 character - self.assertIn("\n 1 + 1 = 2\n ^", text) + self.assertIn("\n 1 + 1 = 2\n ^", text) def test_syntaxerror_indented_caret_position(self): script = textwrap.dedent("""\ @@ -611,7 +611,7 @@ class CmdLineTest(unittest.TestCase): exitcode, stdout, stderr = assert_python_failure(script_name) text = io.TextIOWrapper(io.BytesIO(stderr), 'ascii').read() # Confirm that the caret is located under the first 1 character - self.assertIn("\n 1 + 1 = 2\n ^", text) + self.assertIn("\n 1 + 1 = 2\n ^", text) # Try the same with a form feed at the start of the indented line script = ( @@ -622,7 +622,7 @@ class CmdLineTest(unittest.TestCase): exitcode, stdout, stderr = assert_python_failure(script_name) text = io.TextIOWrapper(io.BytesIO(stderr), "ascii").read() self.assertNotIn("\f", text) - self.assertIn("\n 1 + 1 = 2\n ^", text) + self.assertIn("\n 1 + 1 = 2\n ^", text) def test_syntaxerror_multi_line_fstring(self): script = 'foo = f"""{}\nfoo"""\n' diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py index a207fb4..354b3f4 100644 --- a/Lib/test/test_exceptions.py +++ b/Lib/test/test_exceptions.py @@ -178,19 +178,19 @@ class ExceptionTests(unittest.TestCase): s = '''if True:\n print()\n\texec "mixed tabs and spaces"''' ckmsg(s, "inconsistent use of tabs and spaces in indentation", TabError) - @support.skip_if_new_parser("Pegen column offsets might be different") - def testSyntaxErrorOffset(self): - def check(src, lineno, offset, encoding='utf-8'): - with self.assertRaises(SyntaxError) as cm: - compile(src, '<fragment>', 'exec') - self.assertEqual(cm.exception.lineno, lineno) - self.assertEqual(cm.exception.offset, offset) - if cm.exception.text is not None: - if not isinstance(src, str): - src = src.decode(encoding, 'replace') - line = src.split('\n')[lineno-1] - self.assertIn(line, cm.exception.text) + def check(self, src, lineno, offset, encoding='utf-8'): + with self.assertRaises(SyntaxError) as cm: + compile(src, '<fragment>', 'exec') + self.assertEqual(cm.exception.lineno, lineno) + self.assertEqual(cm.exception.offset, offset) + if cm.exception.text is not None: + if not isinstance(src, str): + src = src.decode(encoding, 'replace') + line = src.split('\n')[lineno-1] + self.assertIn(line, cm.exception.text) + def testSyntaxErrorOffset(self): + check = self.check check('def fact(x):\n\treturn x!\n', 2, 10) check('1 +\n', 1, 4) check('def spam():\n print(1)\n print(2)', 3, 10) @@ -238,20 +238,20 @@ class ExceptionTests(unittest.TestCase): check('nonlocal x', 1, 1) check('def f():\n global x\n nonlocal x', 2, 3) - # Errors thrown by ast.c - check('for 1 in []: pass', 1, 5) - check('def f(*):\n pass', 1, 7) - check('[*x for x in xs]', 1, 2) - check('def f():\n x, y: int', 2, 3) - check('(yield i) = 2', 1, 1) - check('foo(x for x in range(10), 100)', 1, 5) - check('foo(1=2)', 1, 5) - # Errors thrown by future.c check('from __future__ import doesnt_exist', 1, 1) check('from __future__ import braces', 1, 1) check('x=1\nfrom __future__ import division', 2, 1) + @support.skip_if_new_parser("Pegen column offsets might be different") + def testSyntaxErrorOffsetCustom(self): + self.check('for 1 in []: pass', 1, 5) + self.check('def f(*):\n pass', 1, 7) + self.check('[*x for x in xs]', 1, 2) + self.check('def f():\n x, y: int', 2, 3) + self.check('(yield i) = 2', 1, 1) + self.check('foo(x for x in range(10), 100)', 1, 5) + self.check('foo(1=2)', 1, 5) @cpython_only def testSettingException(self): |