Skip to content

Commit 384f7be

Browse files
bpo-35798: Add test.support.check_syntax_warning().
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.
1 parent 4583525 commit 384f7be

File tree

4 files changed

+43
-52
lines changed

4 files changed

+43
-52
lines changed

Lib/test/support/__init__.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,7 @@ def make_bad_fd():
11131113
file.close()
11141114
unlink(TESTFN)
11151115

1116+
11161117
def check_syntax_error(testcase, statement, errtext='', *, lineno=None, offset=None):
11171118
with testcase.assertRaisesRegex(SyntaxError, errtext) as cm:
11181119
compile(statement, '<test string>', 'exec')
@@ -1124,6 +1125,33 @@ def check_syntax_error(testcase, statement, errtext='', *, lineno=None, offset=N
11241125
if offset is not None:
11251126
testcase.assertEqual(err.offset, offset)
11261127

1128+
def check_syntax_warning(testcase, statement, errtext='', *, lineno=1, offset=None):
1129+
# Test also that a warning is emitted only once.
1130+
with warnings.catch_warnings(record=True) as warns:
1131+
warnings.simplefilter('always', SyntaxWarning)
1132+
compile(statement, '<testcase>', 'exec')
1133+
testcase.assertEqual(len(warns), 1, warns)
1134+
1135+
warn, = warns
1136+
testcase.assertTrue(issubclass(warn.category, SyntaxWarning), warn.category)
1137+
if errtext:
1138+
testcase.assertRegex(str(warn.message), errtext)
1139+
testcase.assertEqual(warn.filename, '<testcase>')
1140+
testcase.assertIsNotNone(warn.lineno)
1141+
if lineno is not None:
1142+
testcase.assertEqual(warn.lineno, lineno)
1143+
1144+
# SyntaxWarning should be converted to SyntaxError when raised,
1145+
# since the latter contains more information and provides better
1146+
# error report.
1147+
with warnings.catch_warnings(record=True) as warns:
1148+
warnings.simplefilter('error', SyntaxWarning)
1149+
check_syntax_error(testcase, statement, errtext,
1150+
lineno=lineno, offset=offset)
1151+
# No warnings are leaked when a SyntaxError is raised.
1152+
testcase.assertEqual(warns, [])
1153+
1154+
11271155
def open_urlresource(url, *args, **kw):
11281156
import urllib.request, urllib.parse
11291157

Lib/test/test_grammar.py

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Python test set -- part 1, grammar.
22
# This just tests whether the parser accepts them all.
33

4-
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
@@ -101,7 +101,7 @@
101101

102102
class TokenTests(unittest.TestCase):
103103

104-
check_syntax_error = check_syntax_error
104+
from test.support import check_syntax_error
105105

106106
def test_backslash(self):
107107
# Backslash means line continuation:
@@ -276,7 +276,7 @@ def __getitem__(self, item):
276276

277277
class GrammarTests(unittest.TestCase):
278278

279-
check_syntax_error = check_syntax_error
279+
from test.support import check_syntax_error, check_syntax_warning
280280

281281
# 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'")
11111111

1112-
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')
11191117

11201118

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

12441242
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)
12521245

12531246
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')
12581251

12591252
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')
12651258

12661259
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)
12741262

12751263
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] [...]]')
13431331

13441332
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')

Lib/test/test_string_literals.py

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ def byte(i):
6363

6464
class TestLiterals(unittest.TestCase):
6565

66+
from test.support import check_syntax_warning
67+
6668
def setUp(self):
6769
self.save_path = sys.path[:]
6870
self.tmpdir = tempfile.mkdtemp()
@@ -112,21 +114,7 @@ def test_eval_str_invalid_escape(self):
112114
with self.assertWarns(SyntaxWarning):
113115
self.assertEqual(eval(r"'\%c'" % b), '\\' + chr(b))
114116

115-
with warnings.catch_warnings(record=True) as w:
116-
warnings.simplefilter('always', category=SyntaxWarning)
117-
eval("'''\n\\z'''")
118-
self.assertEqual(len(w), 1)
119-
self.assertEqual(w[0].filename, '<string>')
120-
self.assertEqual(w[0].lineno, 1)
121-
122-
with warnings.catch_warnings(record=True) as w:
123-
warnings.simplefilter('error', category=SyntaxWarning)
124-
with self.assertRaises(SyntaxError) as cm:
125-
eval("'''\n\\z'''")
126-
exc = cm.exception
127-
self.assertEqual(w, [])
128-
self.assertEqual(exc.filename, '<string>')
129-
self.assertEqual(exc.lineno, 1)
117+
self.check_syntax_warning("'''\n\\z'''")
130118

131119
def test_eval_str_raw(self):
132120
self.assertEqual(eval(""" r'x' """), 'x')
@@ -161,21 +149,7 @@ def test_eval_bytes_invalid_escape(self):
161149
with self.assertWarns(SyntaxWarning):
162150
self.assertEqual(eval(r"b'\%c'" % b), b'\\' + bytes([b]))
163151

164-
with warnings.catch_warnings(record=True) as w:
165-
warnings.simplefilter('always', category=SyntaxWarning)
166-
eval("b'''\n\\z'''")
167-
self.assertEqual(len(w), 1)
168-
self.assertEqual(w[0].filename, '<string>')
169-
self.assertEqual(w[0].lineno, 1)
170-
171-
with warnings.catch_warnings(record=True) as w:
172-
warnings.simplefilter('error', category=SyntaxWarning)
173-
with self.assertRaises(SyntaxError) as cm:
174-
eval("b'''\n\\z'''")
175-
exc = cm.exception
176-
self.assertEqual(w, [])
177-
self.assertEqual(exc.filename, '<string>')
178-
self.assertEqual(exc.lineno, 1)
152+
self.check_syntax_warning("b'''\n\\z'''")
179153

180154
def test_eval_bytes_raw(self):
181155
self.assertEqual(eval(""" br'x' """), b'x')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added :func:`test.support.check_syntax_warning`.

0 commit comments

Comments
 (0)