Skip to content

Commit 6d23daa

Browse files
authored
Merge pull request RustPython#5115 from NakanoMiku39/main
Update test_opcodes.py, test_codeop.py and codeop.py from CPython v3.12
2 parents fe61743 + ee9f6ae commit 6d23daa

14 files changed

Lines changed: 402 additions & 435 deletions

‎Lib/codeop.py‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ def _maybe_compile(compiler, source, filename, symbol):
7070
return None
7171
# fallthrough
7272

73-
return compiler(source, filename, symbol)
74-
73+
return compiler(source, filename, symbol, incomplete_input=False)
7574

7675
def _is_syntax_error(err1, err2):
7776
rep1 = repr(err1)
@@ -82,8 +81,13 @@ def _is_syntax_error(err1, err2):
8281
return True
8382
return False
8483

85-
def _compile(source, filename, symbol):
86-
return compile(source, filename, symbol, PyCF_DONT_IMPLY_DEDENT | PyCF_ALLOW_INCOMPLETE_INPUT)
84+
def _compile(source, filename, symbol, incomplete_input=True):
85+
flags = 0
86+
if incomplete_input:
87+
flags |= PyCF_ALLOW_INCOMPLETE_INPUT
88+
flags |= PyCF_DONT_IMPLY_DEDENT
89+
return compile(source, filename, symbol, flags)
90+
8791

8892
def compile_command(source, filename="<input>", symbol="single"):
8993
r"""Compile a command and determine whether it is incomplete.
@@ -114,8 +118,12 @@ class Compile:
114118
def __init__(self):
115119
self.flags = PyCF_DONT_IMPLY_DEDENT | PyCF_ALLOW_INCOMPLETE_INPUT
116120

117-
def __call__(self, source, filename, symbol):
118-
codeob = compile(source, filename, symbol, self.flags, True)
121+
def __call__(self, source, filename, symbol, **kwargs):
122+
flags = self.flags
123+
if kwargs.get('incomplete_input', True) is False:
124+
flags &= ~PyCF_DONT_IMPLY_DEDENT
125+
flags &= ~PyCF_ALLOW_INCOMPLETE_INPUT
126+
codeob = compile(source, filename, symbol, flags, True)
119127
for feature in _features:
120128
if codeob.co_flags & feature.compiler_flag:
121129
self.flags |= feature.compiler_flag

‎Lib/test/test_codeop.py‎

Lines changed: 77 additions & 73 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,25 @@ 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

6233
# TODO: RUSTPYTHON
63-
6434
@unittest.expectedFailure
6535
def test_valid(self):
6636
av = self.assertValid
6737

6838
# 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")
39+
self.assertEqual(compile_command(""),
40+
compile("pass", "<input>", 'single',
41+
PyCF_DONT_IMPLY_DEDENT))
42+
self.assertEqual(compile_command("\n"),
43+
compile("pass", "<input>", 'single',
44+
PyCF_DONT_IMPLY_DEDENT))
7945

8046
av("a = 1")
8147
av("\na = 1")
@@ -104,25 +70,25 @@ def test_valid(self):
10470
av("a=3\n\n")
10571
av("a = 9+ \\\n3")
10672

107-
av("3**3", "eval")
108-
av("(lambda z: \n z**3)", "eval")
73+
av("3**3","eval")
74+
av("(lambda z: \n z**3)","eval")
10975

110-
av("9+ \\\n3", "eval")
111-
av("9+ \\\n3\n", "eval")
76+
av("9+ \\\n3","eval")
77+
av("9+ \\\n3\n","eval")
11278

113-
av("\n\na**3", "eval")
114-
av("\n \na**3", "eval")
115-
av("#a\n#b\na**3", "eval")
79+
av("\n\na**3","eval")
80+
av("\n \na**3","eval")
81+
av("#a\n#b\na**3","eval")
11682

11783
av("\n\na = 1\n\n")
11884
av("\n\nif 1: a=1\n\n")
11985

12086
av("if 1:\n pass\n if 1:\n pass\n else:\n pass\n")
12187
av("#a\n\n \na=3\n\n")
12288

123-
av("\n\na**3", "eval")
124-
av("\n \na**3", "eval")
125-
av("#a\n#b\na**3", "eval")
89+
av("\n\na**3","eval")
90+
av("\n \na**3","eval")
91+
av("#a\n#b\na**3","eval")
12692

12793
av("def f():\n try: pass\n finally: [x for x in (1,2)]\n")
12894
av("def f():\n pass\n#foo\n")
@@ -141,6 +107,10 @@ def test_incomplete(self):
141107
ai("a = {")
142108
ai("b + {")
143109

110+
ai("print([1,\n2,")
111+
ai("print({1:1,\n2:3,")
112+
ai("print((1,\n2,")
113+
144114
ai("if 9==3:\n pass\nelse:")
145115
ai("if 9==3:\n pass\nelse:\n")
146116
ai("if 9==3:\n pass\nelse:\n pass")
@@ -163,13 +133,12 @@ def test_incomplete(self):
163133
ai("a = 'a\\")
164134
ai("a = '''xy")
165135

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")
136+
ai("","eval")
137+
ai("\n","eval")
138+
ai("(","eval")
139+
ai("(9+","eval")
140+
ai("9+ \\","eval")
141+
ai("lambda z: \\","eval")
173142

174143
ai("if True:\n if True:\n if True: \n")
175144

@@ -277,14 +246,13 @@ def test_invalid(self):
277246
ai("a = 'a\\ ")
278247
ai("a = 'a\\\n")
279248

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")
249+
ai("a = 1","eval")
250+
ai("]","eval")
251+
ai("())","eval")
252+
ai("[}","eval")
253+
ai("9+","eval")
254+
ai("lambda z:","eval")
255+
ai("a b","eval")
288256

289257
ai("return 2.3")
290258
ai("if (a == 1 and b = 2): pass")
@@ -314,11 +282,11 @@ def test_filename(self):
314282
# TODO: RUSTPYTHON
315283
@unittest.expectedFailure
316284
def test_warning(self):
317-
# Teswarnings_helper.check_warningsonly returned once.
285+
# Test that the warning is only returned once.
318286
with warnings_helper.check_warnings(
319-
(".*literal", SyntaxWarning),
320-
(".*invalid", DeprecationWarning),
321-
) as w:
287+
('"is" with \'str\' literal', SyntaxWarning),
288+
("invalid escape sequence", SyntaxWarning),
289+
) as w:
322290
compile_command(r"'\e' is 0")
323291
self.assertEqual(len(w.warnings), 2)
324292

@@ -327,6 +295,42 @@ def test_warning(self):
327295
warnings.simplefilter('error', SyntaxWarning)
328296
compile_command('1 is 1', symbol='exec')
329297

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

331335
if __name__ == "__main__":
332336
unittest.main()

0 commit comments

Comments
 (0)