diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2019-02-19 06:30:15 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-19 06:30:15 (GMT) |
commit | e7a4bb554edb72fc6619d23241d59162d06f249a (patch) | |
tree | ba74116b687307c63725d6bed28fce0e46cade90 /Lib/test/test_string_literals.py | |
parent | ee0f927bd8dba805a04963dbec1ad49fe830b842 (diff) | |
download | cpython-e7a4bb554edb72fc6619d23241d59162d06f249a.zip cpython-e7a4bb554edb72fc6619d23241d59162d06f249a.tar.gz cpython-e7a4bb554edb72fc6619d23241d59162d06f249a.tar.bz2 |
bpo-35798: Add test.support.check_syntax_warning(). (#11895)
It checks that a SyntaxWarning is raised when compile specified
statement, that it is raised only once, that it is converted to
a SyntaxError when raised as exception, and that both warning and
exception objects have corresponding attributes.
Diffstat (limited to 'Lib/test/test_string_literals.py')
-rw-r--r-- | Lib/test/test_string_literals.py | 34 |
1 files changed, 4 insertions, 30 deletions
diff --git a/Lib/test/test_string_literals.py b/Lib/test/test_string_literals.py index 635ba57..048f40d 100644 --- a/Lib/test/test_string_literals.py +++ b/Lib/test/test_string_literals.py @@ -63,6 +63,8 @@ def byte(i): class TestLiterals(unittest.TestCase): + from test.support import check_syntax_warning + def setUp(self): self.save_path = sys.path[:] self.tmpdir = tempfile.mkdtemp() @@ -112,21 +114,7 @@ class TestLiterals(unittest.TestCase): with self.assertWarns(SyntaxWarning): self.assertEqual(eval(r"'\%c'" % b), '\\' + chr(b)) - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('always', category=SyntaxWarning) - eval("'''\n\\z'''") - self.assertEqual(len(w), 1) - self.assertEqual(w[0].filename, '<string>') - self.assertEqual(w[0].lineno, 1) - - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('error', category=SyntaxWarning) - with self.assertRaises(SyntaxError) as cm: - eval("'''\n\\z'''") - exc = cm.exception - self.assertEqual(w, []) - self.assertEqual(exc.filename, '<string>') - self.assertEqual(exc.lineno, 1) + self.check_syntax_warning("'''\n\\z'''") def test_eval_str_raw(self): self.assertEqual(eval(""" r'x' """), 'x') @@ -161,21 +149,7 @@ class TestLiterals(unittest.TestCase): with self.assertWarns(SyntaxWarning): self.assertEqual(eval(r"b'\%c'" % b), b'\\' + bytes([b])) - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('always', category=SyntaxWarning) - eval("b'''\n\\z'''") - self.assertEqual(len(w), 1) - self.assertEqual(w[0].filename, '<string>') - self.assertEqual(w[0].lineno, 1) - - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter('error', category=SyntaxWarning) - with self.assertRaises(SyntaxError) as cm: - eval("b'''\n\\z'''") - exc = cm.exception - self.assertEqual(w, []) - self.assertEqual(exc.filename, '<string>') - self.assertEqual(exc.lineno, 1) + self.check_syntax_warning("b'''\n\\z'''") def test_eval_bytes_raw(self): self.assertEqual(eval(""" br'x' """), b'x') |