Debian Codegraph @

lib-python/3/test/test_pyrepl/test_interact.py

raw symbols SCIP
1 import contextlib
2 import io
3 import sys
4 import unittest
5 from unittest.mock import patch
6 from textwrap import dedent
7
8 from test.support import force_not_colorized
9
10 from pyrepl.console import InteractiveColoredConsole
11 from pyrepl.simple_interact import _more_lines
12
13 class TestSimpleInteract(unittest.TestCase):
14 def test_multiple_statements(self):
15 namespace = {}
16 code = dedent("""\
17 class A:
18 def foo(self):
19
20
21 pass
22
23 class B:
24 def bar(self):
25 pass
26
27 a = 1
28 a
29 """)
30 console = InteractiveColoredConsole(namespace, filename="<stdin>")
31 f = io.StringIO()
32 with (
33 patch.object(InteractiveColoredConsole, "showsyntaxerror") as showsyntaxerror,
34 patch.object(InteractiveColoredConsole, "runsource", wraps=console.runsource) as runsource,
35 contextlib.redirect_stdout(f),
36 ):
37 more = console.push(code, filename="<stdin>", _symbol="single") # type: ignore[call-arg]
38 self.assertFalse(more)
39 showsyntaxerror.assert_not_called()
40
41
42 def test_multiple_statements_output(self):
43 namespace = {}
44 code = dedent("""\
45 b = 1
46 b
47 a = 1
48 a
49 """)
50 console = InteractiveColoredConsole(namespace, filename="<stdin>")
51 f = io.StringIO()
52 with contextlib.redirect_stdout(f):
53 more = console.push(code, filename="<stdin>", _symbol="single") # type: ignore[call-arg]
54 self.assertFalse(more)
55 self.assertEqual(f.getvalue(), "1\n")
56
57 def test_empty(self):
58 namespace = {}
59 code = ""
60 console = InteractiveColoredConsole(namespace, filename="<stdin>")
61 f = io.StringIO()
62 with contextlib.redirect_stdout(f):
63 more = console.push(code, filename="<stdin>", _symbol="single") # type: ignore[call-arg]
64 self.assertFalse(more)
65 self.assertEqual(f.getvalue(), "")
66
67 def test_runsource_compiles_and_runs_code(self):
68 console = InteractiveColoredConsole()
69 source = "print('Hello, world!')"
70 with patch.object(console, "runcode") as mock_runcode:
71 console.runsource(source)
72 mock_runcode.assert_called_once()
73
74 def test_runsource_returns_false_for_successful_compilation(self):
75 console = InteractiveColoredConsole()
76 source = "print('Hello, world!')"
77 f = io.StringIO()
78 with contextlib.redirect_stdout(f):
79 result = console.runsource(source)
80 self.assertFalse(result)
81
82 @force_not_colorized
83 def test_runsource_returns_false_for_failed_compilation(self):
84 console = InteractiveColoredConsole()
85 source = "print('Hello, world!'"
86 f = io.StringIO()
87 with contextlib.redirect_stderr(f):
88 result = console.runsource(source)
89 self.assertFalse(result)
90 self.assertIn('SyntaxError', f.getvalue())
91
92 @force_not_colorized
93 def test_runsource_show_syntax_error_location(self):
94 console = InteractiveColoredConsole()
95 source = "def f(x, x): ..."
96 f = io.StringIO()
97 with contextlib.redirect_stderr(f):
98 result = console.runsource(source)
99 self.assertFalse(result)
100 if sys.implementation.name == 'pypy':
101 r = """
102 SyntaxError: duplicate argument 'x' in function definition"""
103 else:
104 r = """
105 def f(x, x): ...
106 ^
107 SyntaxError: duplicate argument 'x' in function definition"""
108 self.assertIn(r, f.getvalue())
109
110 def test_runsource_shows_syntax_error_for_failed_compilation(self):
111 console = InteractiveColoredConsole()
112 source = "print('Hello, world!'"
113 with patch.object(console, "showsyntaxerror") as mock_showsyntaxerror:
114 console.runsource(source)
115 mock_showsyntaxerror.assert_called_once()
116 source = dedent("""\
117 match 1:
118 case {0: _, 0j: _}:
119 pass
120 """)
121 with patch.object(console, "showsyntaxerror") as mock_showsyntaxerror:
122 console.runsource(source)
123 mock_showsyntaxerror.assert_called_once()
124
125 def test_runsource_survives_null_bytes(self):
126 console = InteractiveColoredConsole()
127 source = "\x00\n"
128 f = io.StringIO()
129 with contextlib.redirect_stdout(f), contextlib.redirect_stderr(f):
130 result = console.runsource(source)
131 self.assertFalse(result)
132 self.assertIn("source code string cannot contain null bytes", f.getvalue())
133
134 def test_no_active_future(self):
135 console = InteractiveColoredConsole()
136 source = dedent("""\
137 x: int = 1
138 print(__annotations__)
139 """)
140 f = io.StringIO()
141 with contextlib.redirect_stdout(f):
142 result = console.runsource(source)
143 self.assertFalse(result)
144 self.assertEqual(f.getvalue(), "{'x': <class 'int'>}\n")
145
146 def test_future_annotations(self):
147 console = InteractiveColoredConsole()
148 source = dedent("""\
149 from __future__ import annotations
150 def g(x: int): ...
151 print(g.__annotations__)
152 """)
153 f = io.StringIO()
154 with contextlib.redirect_stdout(f):
155 result = console.runsource(source)
156 self.assertFalse(result)
157 self.assertEqual(f.getvalue(), "{'x': 'int'}\n")
158
159 def test_future_barry_as_flufl(self):
160 console = InteractiveColoredConsole()
161 f = io.StringIO()
162 with contextlib.redirect_stdout(f):
163 result = console.runsource("from __future__ import barry_as_FLUFL\n")
164 result = console.runsource("""print("black" != 'blue')\n""")
165 self.assertFalse(result)
166 self.assertEqual(f.getvalue(), "True\n")
167
168
169 class TestMoreLines(unittest.TestCase):
170 @unittest.skipIf(sys.implementation.name=='pypy', reason="differs")
171 def test_invalid_syntax_single_line(self):
172 namespace = {}
173 code = "if foo"
174 console = InteractiveColoredConsole(namespace, filename="<stdin>")
175 self.assertFalse(_more_lines(console, code))
176
177 def test_empty_line(self):
178 namespace = {}
179 code = ""
180 console = InteractiveColoredConsole(namespace, filename="<stdin>")
181 self.assertFalse(_more_lines(console, code))
182
183 def test_valid_single_statement(self):
184 namespace = {}
185 code = "foo = 1"
186 console = InteractiveColoredConsole(namespace, filename="<stdin>")
187 self.assertFalse(_more_lines(console, code))
188
189 def test_multiline_single_assignment(self):
190 namespace = {}
191 code = dedent("""\
192 foo = [
193 1,
194 2,
195 3,
196 ]""")
197 console = InteractiveColoredConsole(namespace, filename="<stdin>")
198 self.assertFalse(_more_lines(console, code))
199
200 def test_multiline_single_block(self):
201 namespace = {}
202 code = dedent("""\
203 def foo():
204 '''docs'''
205
206 return 1""")
207 console = InteractiveColoredConsole(namespace, filename="<stdin>")
208 self.assertTrue(_more_lines(console, code))
209
210 def test_multiple_statements_single_line(self):
211 namespace = {}
212 code = "foo = 1;bar = 2"
213 console = InteractiveColoredConsole(namespace, filename="<stdin>")
214 self.assertFalse(_more_lines(console, code))
215
216 def test_multiple_statements(self):
217 namespace = {}
218 code = dedent("""\
219 import time
220
221 foo = 1""")
222 console = InteractiveColoredConsole(namespace, filename="<stdin>")
223 self.assertTrue(_more_lines(console, code))
224
225 def test_multiple_blocks(self):
226 namespace = {}
227 code = dedent("""\
228 from dataclasses import dataclass
229
230 @dataclass
231 class Point:
232 x: float
233 y: float""")
234 console = InteractiveColoredConsole(namespace, filename="<stdin>")
235 self.assertTrue(_more_lines(console, code))
236
237 def test_multiple_blocks_empty_newline(self):
238 namespace = {}
239 code = dedent("""\
240 from dataclasses import dataclass
241
242 @dataclass
243 class Point:
244 x: float
245 y: float
246 """)
247 console = InteractiveColoredConsole(namespace, filename="<stdin>")
248 self.assertFalse(_more_lines(console, code))
249
250 def test_multiple_blocks_indented_newline(self):
251 namespace = {}
252 code = (
253 "from dataclasses import dataclass\n"
254 "\n"
255 "@dataclass\n"
256 "class Point:\n"
257 " x: float\n"
258 " y: float\n"
259 " "
260 )
261 console = InteractiveColoredConsole(namespace, filename="<stdin>")
262 self.assertFalse(_more_lines(console, code))
263
264 def test_incomplete_statement(self):
265 namespace = {}
266 code = "if foo:"
267 console = InteractiveColoredConsole(namespace, filename="<stdin>")
268 self.assertTrue(_more_lines(console, code))