diff options
author | Pablo Galindo Salgado <Pablogsal@gmail.com> | 2025-02-13 01:07:37 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-02-13 01:07:37 (GMT) |
commit | 56eda256336310a08d4beb75b998488cb359444b (patch) | |
tree | 1ed5b4e667140de01766ce1213eeb52d28e1ccee /Lib/test/test_string_literals.py | |
parent | 49b11033bd87fb26eb4b74ba2451ed30b1af9780 (diff) | |
download | cpython-56eda256336310a08d4beb75b998488cb359444b.zip cpython-56eda256336310a08d4beb75b998488cb359444b.tar.gz cpython-56eda256336310a08d4beb75b998488cb359444b.tar.bz2 |
gh-116042: Fix location for SyntaxErrors of invalid escapes in the tokenizer (#116049)
Diffstat (limited to 'Lib/test/test_string_literals.py')
-rw-r--r-- | Lib/test/test_string_literals.py | 39 |
1 files changed, 31 insertions, 8 deletions
diff --git a/Lib/test/test_string_literals.py b/Lib/test/test_string_literals.py index f56195c..9d57233 100644 --- a/Lib/test/test_string_literals.py +++ b/Lib/test/test_string_literals.py @@ -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') |