bpo-35798: Add test.support.check_syntax_warning(). (#11895) · python/cpython@e7a4bb5
11# Python test set -- part 1, grammar.
22# This just tests whether the parser accepts them all.
334-from test.support import check_syntax_error
4+from test.support import check_syntax_error, check_syntax_warning
55import inspect
66import unittest
77import sys
101101102102class TokenTests(unittest.TestCase):
103103104-check_syntax_error = check_syntax_error
104+from test.support import check_syntax_error
105105106106def test_backslash(self):
107107# Backslash means line continuation:
@@ -276,7 +276,7 @@ def __getitem__(self, item):
276276277277class GrammarTests(unittest.TestCase):
278278279-check_syntax_error = check_syntax_error
279+from test.support import check_syntax_error, check_syntax_warning
280280281281# single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
282282# XXX can't test in a script -- this rule is only used when interactive
@@ -1109,12 +1109,10 @@ def testAssert2(self):
11091109else:
11101110self.fail("AssertionError not raised by 'assert False'")
111111111112-with self.assertWarnsRegex(SyntaxWarning, 'assertion is always true'):
1113-compile('assert(x, "msg")', '<testcase>', 'exec')
1112+self.check_syntax_warning('assert(x, "msg")',
1113+ 'assertion is always true')
11141114with warnings.catch_warnings():
1115-warnings.filterwarnings('error', category=SyntaxWarning)
1116-with self.assertRaisesRegex(SyntaxError, 'assertion is always true'):
1117-compile('assert(x, "msg")', '<testcase>', 'exec')
1115+warnings.simplefilter('error', SyntaxWarning)
11181116compile('assert x, "msg"', '<testcase>', 'exec')
1119111711201118@@ -1243,12 +1241,7 @@ def test_comparison(self):
1243124112441242def test_comparison_is_literal(self):
12451243def check(test, msg='"is" with a literal'):
1246-with self.assertWarnsRegex(SyntaxWarning, msg):
1247-compile(test, '<testcase>', 'exec')
1248-with warnings.catch_warnings():
1249-warnings.filterwarnings('error', category=SyntaxWarning)
1250-with self.assertRaisesRegex(SyntaxError, msg):
1251-compile(test, '<testcase>', 'exec')
1244+self.check_syntax_warning(test, msg)
1252124512531246check('x is 1')
12541247check('x is "thing"')
@@ -1257,20 +1250,15 @@ def check(test, msg='"is" with a literal'):
12571250check('x is not 1', '"is not" with a literal')
1258125112591252with warnings.catch_warnings():
1260-warnings.filterwarnings('error', category=SyntaxWarning)
1253+warnings.simplefilter('error', SyntaxWarning)
12611254compile('x is None', '<testcase>', 'exec')
12621255compile('x is False', '<testcase>', 'exec')
12631256compile('x is True', '<testcase>', 'exec')
12641257compile('x is ...', '<testcase>', 'exec')
1265125812661259def test_warn_missed_comma(self):
12671260def check(test):
1268-with self.assertWarnsRegex(SyntaxWarning, msg):
1269-compile(test, '<testcase>', 'exec')
1270-with warnings.catch_warnings():
1271-warnings.filterwarnings('error', category=SyntaxWarning)
1272-with self.assertRaisesRegex(SyntaxError, msg):
1273-compile(test, '<testcase>', 'exec')
1261+self.check_syntax_warning(test, msg)
1274126212751263msg=r'is not callable; perhaps you missed a comma\?'
12761264check('[(1, 2) (3, 4)]')
@@ -1342,7 +1330,7 @@ def check(test):
13421330check('[[1, 2] [...]]')
1343133113441332with warnings.catch_warnings():
1345-warnings.filterwarnings('error', category=SyntaxWarning)
1333+warnings.simplefilter('error', SyntaxWarning)
13461334compile('[(lambda x, y: x) (3, 4)]', '<testcase>', 'exec')
13471335compile('[[1, 2] [i]]', '<testcase>', 'exec')
13481336compile('[[1, 2] [0]]', '<testcase>', 'exec')