Skip to content

Commit 8ff0872

Browse files
committed
Update test_codeop.py from CPython v3.12
1 parent a96926c commit 8ff0872

1 file changed

Lines changed: 74 additions & 75 deletions

File tree

‎Lib/test/test_codeop.py‎

Lines changed: 74 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,19 @@
22
Test cases for codeop.py
33
Nick Mathewson
44
"""
5-
import sys
65
import unittest
76
import warnings
8-
from test import support
97
from test.support import warnings_helper
8+
from textwrap import dedent
109

1110
from codeop import compile_command, PyCF_DONT_IMPLY_DEDENT
12-
import io
13-
14-
if support.is_jython:
15-
16-
def unify_callables(d):
17-
for n, v in d.items():
18-
if hasattr(v, '__call__'):
19-
d[n] = True
20-
return d
21-
2211

2312
class CodeopTests(unittest.TestCase):
2413

2514
def assertValid(self, str, symbol='single'):
2615
'''succeed iff str is a valid piece of code'''
27-
if support.is_jython:
28-
code = compile_command(str, "<input>", symbol)
29-
self.assertTrue(code)
30-
if symbol == "single":
31-
d, r = {}, {}
32-
saved_stdout = sys.stdout
33-
sys.stdout = io.StringIO()
34-
try:
35-
exec(code, d)
36-
exec(compile(str, "<input>", "single"), r)
37-
finally:
38-
sys.stdout = saved_stdout
39-
elif symbol == 'eval':
40-
ctx = {'a': 2}
41-
d = {'value': eval(code, ctx)}
42-
r = {'value': eval(str, ctx)}
43-
self.assertEqual(unify_callables(r), unify_callables(d))
44-
else:
45-
expected = compile(str, "<input>", symbol, PyCF_DONT_IMPLY_DEDENT)
46-
self.assertEqual(compile_command(str, "<input>", symbol), expected)
16+
expected = compile(str, "<input>", symbol, PyCF_DONT_IMPLY_DEDENT)
17+
self.assertEqual(compile_command(str, "<input>", symbol), expected)
4718

4819
def assertIncomplete(self, str, symbol='single'):
4920
'''succeed iff str is the start of a valid piece of code'''
@@ -52,30 +23,23 @@ def assertIncomplete(self, str, symbol='single'):
5223
def assertInvalid(self, str, symbol='single', is_syntax=1):
5324
'''succeed iff str is the start of an invalid piece of code'''
5425
try:
55-
compile_command(str, symbol=symbol)
26+
compile_command(str,symbol=symbol)
5627
self.fail("No exception raised for invalid code")
5728
except SyntaxError:
5829
self.assertTrue(is_syntax)
5930
except OverflowError:
6031
self.assertTrue(not is_syntax)
6132

62-
# TODO: RUSTPYTHON
63-
64-
@unittest.expectedFailure
6533
def test_valid(self):
6634
av = self.assertValid
6735

6836
# special case
69-
if not support.is_jython:
70-
self.assertEqual(compile_command(""),
71-
compile("pass", "<input>", 'single',
72-
PyCF_DONT_IMPLY_DEDENT))
73-
self.assertEqual(compile_command("\n"),
74-
compile("pass", "<input>", 'single',
75-
PyCF_DONT_IMPLY_DEDENT))
76-
else:
77-
av("")
78-
av("\n")
37+
self.assertEqual(compile_command(""),
38+
compile("pass", "<input>", 'single',
39+
PyCF_DONT_IMPLY_DEDENT))
40+
self.assertEqual(compile_command("\n"),
41+
compile("pass", "<input>", 'single',
42+
PyCF_DONT_IMPLY_DEDENT))
7943

8044
av("a = 1")
8145
av("\na = 1")
@@ -104,25 +68,25 @@ def test_valid(self):
10468
av("a=3\n\n")
10569
av("a = 9+ \\\n3")
10670

107-
av("3**3", "eval")
108-
av("(lambda z: \n z**3)", "eval")
71+
av("3**3","eval")
72+
av("(lambda z: \n z**3)","eval")
10973

110-
av("9+ \\\n3", "eval")
111-
av("9+ \\\n3\n", "eval")
74+
av("9+ \\\n3","eval")
75+
av("9+ \\\n3\n","eval")
11276

113-
av("\n\na**3", "eval")
114-
av("\n \na**3", "eval")
115-
av("#a\n#b\na**3", "eval")
77+
av("\n\na**3","eval")
78+
av("\n \na**3","eval")
79+
av("#a\n#b\na**3","eval")
11680

11781
av("\n\na = 1\n\n")
11882
av("\n\nif 1: a=1\n\n")
11983

12084
av("if 1:\n pass\n if 1:\n pass\n else:\n pass\n")
12185
av("#a\n\n \na=3\n\n")
12286

123-
av("\n\na**3", "eval")
124-
av("\n \na**3", "eval")
125-
av("#a\n#b\na**3", "eval")
87+
av("\n\na**3","eval")
88+
av("\n \na**3","eval")
89+
av("#a\n#b\na**3","eval")
12690

12791
av("def f():\n try: pass\n finally: [x for x in (1,2)]\n")
12892
av("def f():\n pass\n#foo\n")
@@ -141,6 +105,10 @@ def test_incomplete(self):
141105
ai("a = {")
142106
ai("b + {")
143107

108+
ai("print([1,\n2,")
109+
ai("print({1:1,\n2:3,")
110+
ai("print((1,\n2,")
111+
144112
ai("if 9==3:\n pass\nelse:")
145113
ai("if 9==3:\n pass\nelse:\n")
146114
ai("if 9==3:\n pass\nelse:\n pass")
@@ -163,13 +131,12 @@ def test_incomplete(self):
163131
ai("a = 'a\\")
164132
ai("a = '''xy")
165133

166-
ai("", "eval")
167-
ai("\n", "eval")
168-
ai("(", "eval")
169-
ai("(\n\n\n", "eval")
170-
ai("(9+", "eval")
171-
ai("9+ \\", "eval")
172-
ai("lambda z: \\", "eval")
134+
ai("","eval")
135+
ai("\n","eval")
136+
ai("(","eval")
137+
ai("(9+","eval")
138+
ai("9+ \\","eval")
139+
ai("lambda z: \\","eval")
173140

174141
ai("if True:\n if True:\n if True: \n")
175142

@@ -277,14 +244,13 @@ def test_invalid(self):
277244
ai("a = 'a\\ ")
278245
ai("a = 'a\\\n")
279246

280-
ai("a = 1", "eval")
281-
ai("a = (", "eval")
282-
ai("]", "eval")
283-
ai("())", "eval")
284-
ai("[}", "eval")
285-
ai("9+", "eval")
286-
ai("lambda z:", "eval")
287-
ai("a b", "eval")
247+
ai("a = 1","eval")
248+
ai("]","eval")
249+
ai("())","eval")
250+
ai("[}","eval")
251+
ai("9+","eval")
252+
ai("lambda z:","eval")
253+
ai("a b","eval")
288254

289255
ai("return 2.3")
290256
ai("if (a == 1 and b = 2): pass")
@@ -314,11 +280,11 @@ def test_filename(self):
314280
# TODO: RUSTPYTHON
315281
@unittest.expectedFailure
316282
def test_warning(self):
317-
# Teswarnings_helper.check_warningsonly returned once.
283+
# Test that the warning is only returned once.
318284
with warnings_helper.check_warnings(
319-
(".*literal", SyntaxWarning),
320-
(".*invalid", DeprecationWarning),
321-
) as w:
285+
('"is" with \'str\' literal', SyntaxWarning),
286+
("invalid escape sequence", SyntaxWarning),
287+
) as w:
322288
compile_command(r"'\e' is 0")
323289
self.assertEqual(len(w.warnings), 2)
324290

@@ -327,6 +293,39 @@ def test_warning(self):
327293
warnings.simplefilter('error', SyntaxWarning)
328294
compile_command('1 is 1', symbol='exec')
329295

296+
# Check SyntaxWarning treated as an SyntaxError
297+
with warnings.catch_warnings(), self.assertRaises(SyntaxError):
298+
warnings.simplefilter('error', SyntaxWarning)
299+
compile_command(r"'\e'", symbol='exec')
300+
301+
def test_incomplete_warning(self):
302+
with warnings.catch_warnings(record=True) as w:
303+
warnings.simplefilter('always')
304+
self.assertIncomplete("'\\e' + (")
305+
self.assertEqual(w, [])
306+
307+
def test_invalid_warning(self):
308+
with warnings.catch_warnings(record=True) as w:
309+
warnings.simplefilter('always')
310+
self.assertInvalid("'\\e' 1")
311+
self.assertEqual(len(w), 1)
312+
self.assertEqual(w[0].category, SyntaxWarning)
313+
self.assertRegex(str(w[0].message), 'invalid escape sequence')
314+
self.assertEqual(w[0].filename, '<input>')
315+
316+
def assertSyntaxErrorMatches(self, code, message):
317+
with self.subTest(code):
318+
with self.assertRaisesRegex(SyntaxError, message):
319+
compile_command(code, symbol='exec')
320+
321+
def test_syntax_errors(self):
322+
self.assertSyntaxErrorMatches(
323+
dedent("""\
324+
def foo(x,x):
325+
pass
326+
"""), "duplicate argument 'x' in function definition")
327+
328+
330329

331330
if __name__ == "__main__":
332331
unittest.main()

0 commit comments

Comments
 (0)