summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_exceptions.py
diff options
context:
space:
mode:
authorBatuhan Taskaya <batuhanosmantaskaya@gmail.com>2020-05-01 13:13:43 (GMT)
committerGitHub <noreply@github.com>2020-05-01 13:13:43 (GMT)
commit76c1b4d5c5a610c09943e1ee7ae18f1957804730 (patch)
treeeeaf4c2eb7b712817866ba49d04a5c57a3508795 /Lib/test/test_exceptions.py
parent719e14d2837520c18398a3e22a36f20c1fe76edf (diff)
downloadcpython-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/test/test_exceptions.py')
-rw-r--r--Lib/test/test_exceptions.py42
1 files changed, 21 insertions, 21 deletions
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):