Skip to content

Commit d28df6d

Browse files
serhiy-storchakaasvetlov
authored andcommitted
bpo-46820: Refactor tests for ambiguous end of numerical literal (GH-31494)
1 parent 89d1e0e commit d28df6d

File tree

1 file changed

+25
-89
lines changed

1 file changed

+25
-89
lines changed

Lib/test/test_grammar.py

Lines changed: 25 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -214,99 +214,35 @@ def test_bad_numerical_literals(self):
214214
check("1e+", "invalid decimal literal")
215215

216216
def test_end_of_numerical_literals(self):
217-
def check(test):
218-
with self.assertWarns(DeprecationWarning):
219-
compile(test, "<testcase>", "eval")
220-
221-
def check_error(test):
222-
with warnings.catch_warnings(record=True) as w:
223-
with self.assertRaises(SyntaxError):
224-
compile(test, "<testcase>", "eval")
225-
self.assertEqual(w, [])
226-
227-
check_error("0xfand x")
228-
check("0o7and x")
229-
check("0b1and x")
230-
check("9and x")
231-
check("0and x")
232-
check("1.and x")
233-
check("1e3and x")
234-
check("1jand x")
235-
236-
check("0xfor x")
237-
check("0o7or x")
238-
check("0b1or x")
239-
check("9or x")
240-
check_error("0or x")
241-
check("1.or x")
242-
check("1e3or x")
243-
check("1jor x")
244-
245-
check("0xfin x")
246-
check("0o7in x")
247-
check("0b1in x")
248-
check("9in x")
249-
check("0in x")
250-
check("1.in x")
251-
check("1e3in x")
252-
check("1jin x")
253-
254-
check("0xfnot in x")
255-
check("0o7not in x")
256-
check("0b1not in x")
257-
check("9not in x")
258-
check("0not in x")
259-
check("1.not in x")
260-
check("1e3not in x")
261-
check("1jnot in x")
262-
263-
with warnings.catch_warnings():
264-
warnings.simplefilter('ignore', SyntaxWarning)
265-
check("0xfis x")
266-
check("0o7is x")
267-
check("0b1is x")
268-
check("9is x")
269-
check("0is x")
270-
check("1.is x")
271-
check("1e3is x")
272-
check("1jis x")
273-
274-
check("0xfif x else y")
275-
check("0o7if x else y")
276-
check("0b1if x else y")
277-
check("9if x else y")
278-
check("0if x else y")
279-
check("1.if x else y")
280-
check("1e3if x else y")
281-
check("1jif x else y")
282-
283-
check_error("x if 0xfelse y")
284-
check("x if 0o7else y")
285-
check("x if 0b1else y")
286-
check("x if 9else y")
287-
check("x if 0else y")
288-
check("x if 1.else y")
289-
check("x if 1e3else y")
290-
check("x if 1jelse y")
217+
def check(test, error=False):
218+
with self.subTest(expr=test):
219+
if error:
220+
with warnings.catch_warnings(record=True) as w:
221+
with self.assertRaises(SyntaxError):
222+
compile(test, "<testcase>", "eval")
223+
self.assertEqual(w, [])
224+
else:
225+
with self.assertWarns(DeprecationWarning):
226+
compile(test, "<testcase>", "eval")
227+
228+
for num in "0xf", "0o7", "0b1", "9", "0", "1.", "1e3", "1j":
229+
compile(num, "<testcase>", "eval")
230+
check(f"{num}and x", error=(num == "0xf"))
231+
check(f"{num}or x", error=(num == "0"))
232+
check(f"{num}in x")
233+
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+
check(f"{num}if x else y")
239+
check(f"x if {num}else y", error=(num == "0xf"))
240+
check(f"[{num}for x in ()]")
241+
check(f"{num}spam", error=True)
291242

292243
check("[0x1ffor x in ()]")
293244
check("[0x1for x in ()]")
294245
check("[0xfor x in ()]")
295-
check("[0o7for x in ()]")
296-
check("[0b1for x in ()]")
297-
check("[9for x in ()]")
298-
check("[1.for x in ()]")
299-
check("[1e3for x in ()]")
300-
check("[1jfor x in ()]")
301-
302-
check_error("0xfspam")
303-
check_error("0o7spam")
304-
check_error("0b1spam")
305-
check_error("9spam")
306-
check_error("0spam")
307-
check_error("1.spam")
308-
check_error("1e3spam")
309-
check_error("1jspam")
310246

311247
def test_string_literals(self):
312248
x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y)

0 commit comments

Comments
 (0)