diff options
| author | Eric V. Smith <eric@trueblade.com> | 2016-10-31 09:22:08 -0400 |
|---|---|---|
| committer | Eric V. Smith <eric@trueblade.com> | 2016-10-31 09:22:08 -0400 |
| commit | 42454af094fd572a97a6302e8c1226be68c0efa9 (patch) | |
| tree | 29a7b5737fe214a0571af7c6daf53a4d85c6bae4 /Lib/test | |
| parent | a99cdb21a7d67ce5d1e3cc93b08eb3b6eecba60b (diff) | |
| download | cpython-git-42454af094fd572a97a6302e8c1226be68c0efa9.tar.gz | |
Issue 28128: Print out better error/warning messages for invalid string escapes.
Diffstat (limited to 'Lib/test')
| -rw-r--r-- | Lib/test/test_string_literals.py | 27 | ||||
| -rw-r--r-- | Lib/test/test_unicode.py | 7 |
2 files changed, 27 insertions, 7 deletions
diff --git a/Lib/test/test_string_literals.py b/Lib/test/test_string_literals.py index 37ace230f5..54f2be3598 100644 --- a/Lib/test/test_string_literals.py +++ b/Lib/test/test_string_literals.py @@ -31,6 +31,7 @@ import os import sys import shutil import tempfile +import warnings import unittest @@ -104,6 +105,19 @@ class TestLiterals(unittest.TestCase): self.assertRaises(SyntaxError, eval, r""" '\U000000' """) self.assertRaises(SyntaxError, eval, r""" '\U0000000' """) + def test_eval_str_invalid_escape(self): + for b in range(1, 128): + if b in b"""\n\r"'01234567NU\\abfnrtuvx""": + continue + with self.assertWarns(DeprecationWarning): + self.assertEqual(eval(r"'\%c'" % b), '\\' + chr(b)) + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter('always', category=DeprecationWarning) + eval("'''\n\\z'''") + self.assertEqual(len(w), 1) + self.assertEqual(w[0].filename, '<string>') + self.assertEqual(w[0].lineno, 2) + def test_eval_str_raw(self): self.assertEqual(eval(""" r'x' """), 'x') self.assertEqual(eval(r""" r'\x01' """), '\\' + 'x01') @@ -130,6 +144,19 @@ class TestLiterals(unittest.TestCase): self.assertRaises(SyntaxError, eval, r""" b'\x' """) self.assertRaises(SyntaxError, eval, r""" b'\x0' """) + def test_eval_bytes_invalid_escape(self): + for b in range(1, 128): + if b in b"""\n\r"'01234567\\abfnrtvx""": + continue + with self.assertWarns(DeprecationWarning): + self.assertEqual(eval(r"b'\%c'" % b), b'\\' + bytes([b])) + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter('always', category=DeprecationWarning) + eval("b'''\n\\z'''") + self.assertEqual(len(w), 1) + self.assertEqual(w[0].filename, '<string>') + self.assertEqual(w[0].lineno, 2) + def test_eval_bytes_raw(self): self.assertEqual(eval(""" br'x' """), b'x') self.assertEqual(eval(""" rb'x' """), b'x') diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index fe6cd28c63..0737140ccf 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -2413,13 +2413,6 @@ class UnicodeTest(string_tests.CommonTest, support.check_free_after_iterating(self, iter, str) support.check_free_after_iterating(self, reversed, str) - def test_invalid_sequences(self): - for letter in string.ascii_letters + "89": # 0-7 are octal escapes - if letter in "abfnrtuvxNU": - continue - with self.assertWarns(DeprecationWarning): - eval(r"'\%s'" % letter) - class CAPITest(unittest.TestCase): |
