|
104 | 104 | class TokenTests(unittest.TestCase):
|
105 | 105 |
|
106 | 106 | from test.support import check_syntax_error
|
| 107 | + from test.support.warnings_helper import check_syntax_warning |
107 | 108 |
|
108 | 109 | def test_backslash(self):
|
109 | 110 | # Backslash means line continuation:
|
@@ -178,7 +179,7 @@ def test_floats(self):
|
178 | 179 | def test_float_exponent_tokenization(self):
|
179 | 180 | # See issue 21642.
|
180 | 181 | with warnings.catch_warnings():
|
181 |
| - warnings.simplefilter('ignore', DeprecationWarning) |
| 182 | + warnings.simplefilter('ignore', SyntaxWarning) |
182 | 183 | self.assertEqual(eval("1 if 1else 0"), 1)
|
183 | 184 | self.assertEqual(eval("1 if 0else 0"), 0)
|
184 | 185 | self.assertRaises(SyntaxError, eval, "0 if 1Else 0")
|
@@ -218,28 +219,36 @@ def check(test, error=False):
|
218 | 219 | with self.subTest(expr=test):
|
219 | 220 | if error:
|
220 | 221 | with warnings.catch_warnings(record=True) as w:
|
221 |
| - with self.assertRaises(SyntaxError): |
| 222 | + with self.assertRaisesRegex(SyntaxError, |
| 223 | + r'invalid \w+ literal'): |
222 | 224 | compile(test, "<testcase>", "eval")
|
223 | 225 | self.assertEqual(w, [])
|
224 | 226 | else:
|
225 |
| - with self.assertWarns(DeprecationWarning): |
226 |
| - compile(test, "<testcase>", "eval") |
| 227 | + self.check_syntax_warning(test, |
| 228 | + errtext=r'invalid \w+ literal') |
227 | 229 |
|
228 | 230 | for num in "0xf", "0o7", "0b1", "9", "0", "1.", "1e3", "1j":
|
229 | 231 | compile(num, "<testcase>", "eval")
|
230 | 232 | check(f"{num}and x", error=(num == "0xf"))
|
231 | 233 | check(f"{num}or x", error=(num == "0"))
|
232 | 234 | check(f"{num}in x")
|
233 | 235 | check(f"{num}not in x")
|
234 |
| - with warnings.catch_warnings(): |
235 |
| - warnings.filterwarnings('ignore', '"is" with a literal', |
236 |
| - SyntaxWarning) |
237 |
| - check(f"{num}is x") |
238 | 236 | check(f"{num}if x else y")
|
239 | 237 | check(f"x if {num}else y", error=(num == "0xf"))
|
240 | 238 | check(f"[{num}for x in ()]")
|
241 | 239 | check(f"{num}spam", error=True)
|
242 | 240 |
|
| 241 | + with warnings.catch_warnings(): |
| 242 | + warnings.filterwarnings('ignore', '"is" with a literal', |
| 243 | + SyntaxWarning) |
| 244 | + with self.assertWarnsRegex(SyntaxWarning, |
| 245 | + r'invalid \w+ literal'): |
| 246 | + compile(f"{num}is x", "<testcase>", "eval") |
| 247 | + warnings.simplefilter('error', SyntaxWarning) |
| 248 | + with self.assertRaisesRegex(SyntaxError, |
| 249 | + r'invalid \w+ literal'): |
| 250 | + compile(f"{num}is x", "<testcase>", "eval") |
| 251 | + |
243 | 252 | check("[0x1ffor x in ()]")
|
244 | 253 | check("[0x1for x in ()]")
|
245 | 254 | check("[0xfor x in ()]")
|
|
0 commit comments