gh-116042: Fix location for SyntaxErrors of invalid escapes in the tokenizer (#116049)

This commit is contained in:
Pablo Galindo Salgado 2025-02-13 01:07:37 +00:00 committed by GitHub
parent 49b11033bd
commit 56eda25633
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 80 additions and 17 deletions

View file

@ -120,7 +120,7 @@ class TestLiterals(unittest.TestCase):
r'Such sequences will not work in the future. '
r'Did you mean "\\z"? A raw string is also an option.')
self.assertEqual(w[0].filename, '<string>')
self.assertEqual(w[0].lineno, 1)
self.assertEqual(w[0].lineno, 2)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('error', category=SyntaxWarning)
@ -131,7 +131,7 @@ class TestLiterals(unittest.TestCase):
self.assertEqual(exc.msg, r'"\z" is an invalid escape sequence. '
r'Did you mean "\\z"? A raw string is also an option.')
self.assertEqual(exc.filename, '<string>')
self.assertEqual(exc.lineno, 1)
self.assertEqual(exc.lineno, 2)
self.assertEqual(exc.offset, 1)
# Check that the warning is raised only once if there are syntax errors
@ -160,7 +160,7 @@ class TestLiterals(unittest.TestCase):
r'Such sequences will not work in the future. '
r'Did you mean "\\407"? A raw string is also an option.')
self.assertEqual(w[0].filename, '<string>')
self.assertEqual(w[0].lineno, 1)
self.assertEqual(w[0].lineno, 2)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('error', category=SyntaxWarning)
@ -171,9 +171,32 @@ class TestLiterals(unittest.TestCase):
self.assertEqual(exc.msg, r'"\407" is an invalid octal escape sequence. '
r'Did you mean "\\407"? A raw string is also an option.')
self.assertEqual(exc.filename, '<string>')
self.assertEqual(exc.lineno, 1)
self.assertEqual(exc.lineno, 2)
self.assertEqual(exc.offset, 1)
def test_invalid_escape_locations_with_offset(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always', category=SyntaxWarning)
eval("\"'''''''''''''''''''''invalid\ Escape\"")
self.assertEqual(len(w), 1)
self.assertEqual(str(w[0].message),
r'"\ " is an invalid escape sequence. Such sequences '
r'will not work in the future. Did you mean "\\ "? '
r'A raw string is also an option.')
self.assertEqual(w[0].filename, '<string>')
self.assertEqual(w[0].lineno, 1)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always', category=SyntaxWarning)
eval("\"''Incorrect \ logic?\"")
self.assertEqual(len(w), 1)
self.assertEqual(str(w[0].message),
r'"\ " is an invalid escape sequence. Such sequences '
r'will not work in the future. Did you mean "\\ "? '
r'A raw string is also an option.')
self.assertEqual(w[0].filename, '<string>')
self.assertEqual(w[0].lineno, 1)
def test_eval_str_raw(self):
self.assertEqual(eval(""" r'x' """), 'x')
self.assertEqual(eval(r""" r'\x01' """), '\\' + 'x01')
@ -215,7 +238,7 @@ class TestLiterals(unittest.TestCase):
r'Such sequences will not work in the future. '
r'Did you mean "\\z"? A raw string is also an option.')
self.assertEqual(w[0].filename, '<string>')
self.assertEqual(w[0].lineno, 1)
self.assertEqual(w[0].lineno, 2)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('error', category=SyntaxWarning)
@ -226,7 +249,7 @@ class TestLiterals(unittest.TestCase):
self.assertEqual(exc.msg, r'"\z" is an invalid escape sequence. '
r'Did you mean "\\z"? A raw string is also an option.')
self.assertEqual(exc.filename, '<string>')
self.assertEqual(exc.lineno, 1)
self.assertEqual(exc.lineno, 2)
def test_eval_bytes_invalid_octal_escape(self):
for i in range(0o400, 0o1000):
@ -241,7 +264,7 @@ class TestLiterals(unittest.TestCase):
r'Such sequences will not work in the future. '
r'Did you mean "\\407"? A raw string is also an option.')
self.assertEqual(w[0].filename, '<string>')
self.assertEqual(w[0].lineno, 1)
self.assertEqual(w[0].lineno, 2)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('error', category=SyntaxWarning)
@ -252,7 +275,7 @@ class TestLiterals(unittest.TestCase):
self.assertEqual(exc.msg, r'"\407" is an invalid octal escape sequence. '
r'Did you mean "\\407"? A raw string is also an option.')
self.assertEqual(exc.filename, '<string>')
self.assertEqual(exc.lineno, 1)
self.assertEqual(exc.lineno, 2)
def test_eval_bytes_raw(self):
self.assertEqual(eval(""" br'x' """), b'x')