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

55

import inspect

66

import unittest

77

import sys

101101102102

class TokenTests(unittest.TestCase):

103103104-

check_syntax_error = check_syntax_error

104+

from test.support import check_syntax_error

105105106106

def test_backslash(self):

107107

# Backslash means line continuation:

@@ -276,7 +276,7 @@ def __getitem__(self, item):

276276277277

class 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):

11091109

else:

11101110

self.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')

11141114

with 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)

11181116

compile('assert x, "msg"', '<testcase>', 'exec')

1119111711201118

@@ -1243,12 +1241,7 @@ def test_comparison(self):

1243124112441242

def test_comparison_is_literal(self):

12451243

def 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)

1252124512531246

check('x is 1')

12541247

check('x is "thing"')

@@ -1257,20 +1250,15 @@ def check(test, msg='"is" with a literal'):

12571250

check('x is not 1', '"is not" with a literal')

1258125112591252

with warnings.catch_warnings():

1260-

warnings.filterwarnings('error', category=SyntaxWarning)

1253+

warnings.simplefilter('error', SyntaxWarning)

12611254

compile('x is None', '<testcase>', 'exec')

12621255

compile('x is False', '<testcase>', 'exec')

12631256

compile('x is True', '<testcase>', 'exec')

12641257

compile('x is ...', '<testcase>', 'exec')

1265125812661259

def test_warn_missed_comma(self):

12671260

def 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)

1274126212751263

msg=r'is not callable; perhaps you missed a comma\?'

12761264

check('[(1, 2) (3, 4)]')

@@ -1342,7 +1330,7 @@ def check(test):

13421330

check('[[1, 2] [...]]')

1343133113441332

with warnings.catch_warnings():

1345-

warnings.filterwarnings('error', category=SyntaxWarning)

1333+

warnings.simplefilter('error', SyntaxWarning)

13461334

compile('[(lambda x, y: x) (3, 4)]', '<testcase>', 'exec')

13471335

compile('[[1, 2] [i]]', '<testcase>', 'exec')

13481336

compile('[[1, 2] [0]]', '<testcase>', 'exec')