| 1 |
import contextlib |
| 2 |
import io |
| 3 |
import warnings |
| 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 |
@force_not_colorized |
| 58 |
def test_multiple_statements_fail_early(self): |
| 59 |
console = InteractiveColoredConsole() |
| 60 |
code = dedent("""\ |
| 61 |
raise Exception('foobar') |
| 62 |
print('spam', 'eggs', sep='&') |
| 63 |
""") |
| 64 |
f = io.StringIO() |
| 65 |
with contextlib.redirect_stderr(f): |
| 66 |
console.runsource(code) |
| 67 |
self.assertIn('Exception: foobar', f.getvalue()) |
| 68 |
self.assertNotIn('spam&eggs', f.getvalue()) |
| 69 |
|
| 70 |
def test_empty(self): |
| 71 |
namespace = {} |
| 72 |
code = "" |
| 73 |
console = InteractiveColoredConsole(namespace, filename="<stdin>") |
| 74 |
f = io.StringIO() |
| 75 |
with contextlib.redirect_stdout(f): |
| 76 |
more = console.push(code, filename="<stdin>", _symbol="single") # type: ignore[call-arg] |
| 77 |
self.assertFalse(more) |
| 78 |
self.assertEqual(f.getvalue(), "") |
| 79 |
|
| 80 |
def test_runsource_compiles_and_runs_code(self): |
| 81 |
console = InteractiveColoredConsole() |
| 82 |
source = "print('Hello, world!')" |
| 83 |
with patch.object(console, "runcode") as mock_runcode: |
| 84 |
console.runsource(source) |
| 85 |
mock_runcode.assert_called_once() |
| 86 |
|
| 87 |
def test_runsource_returns_false_for_successful_compilation(self): |
| 88 |
console = InteractiveColoredConsole() |
| 89 |
source = "print('Hello, world!')" |
| 90 |
f = io.StringIO() |
| 91 |
with contextlib.redirect_stdout(f): |
| 92 |
result = console.runsource(source) |
| 93 |
self.assertFalse(result) |
| 94 |
|
| 95 |
@force_not_colorized |
| 96 |
def test_runsource_returns_false_for_failed_compilation(self): |
| 97 |
console = InteractiveColoredConsole() |
| 98 |
source = "print('Hello, world!'" |
| 99 |
f = io.StringIO() |
| 100 |
with contextlib.redirect_stderr(f): |
| 101 |
result = console.runsource(source) |
| 102 |
self.assertFalse(result) |
| 103 |
self.assertIn('SyntaxError', f.getvalue()) |
| 104 |
|
| 105 |
@force_not_colorized |
| 106 |
def test_runsource_show_syntax_error_location(self): |
| 107 |
console = InteractiveColoredConsole() |
| 108 |
source = "def f(x, x): ..." |
| 109 |
f = io.StringIO() |
| 110 |
with contextlib.redirect_stderr(f): |
| 111 |
result = console.runsource(source) |
| 112 |
self.assertFalse(result) |
| 113 |
r = """ |
| 114 |
def f(x, x): ... |
| 115 |
^ |
| 116 |
SyntaxError: duplicate argument 'x' in function definition""" |
| 117 |
self.assertIn(r, f.getvalue()) |
| 118 |
|
| 119 |
def test_runsource_shows_syntax_error_for_failed_compilation(self): |
| 120 |
console = InteractiveColoredConsole() |
| 121 |
source = "print('Hello, world!'" |
| 122 |
with patch.object(console, "showsyntaxerror") as mock_showsyntaxerror: |
| 123 |
console.runsource(source) |
| 124 |
mock_showsyntaxerror.assert_called_once() |
| 125 |
source = dedent("""\ |
| 126 |
match 1: |
| 127 |
case {0: _, 0j: _}: |
| 128 |
pass |
| 129 |
""") |
| 130 |
with patch.object(console, "showsyntaxerror") as mock_showsyntaxerror: |
| 131 |
console.runsource(source) |
| 132 |
mock_showsyntaxerror.assert_called_once() |
| 133 |
|
| 134 |
def test_runsource_survives_null_bytes(self): |
| 135 |
console = InteractiveColoredConsole() |
| 136 |
source = "\x00\n" |
| 137 |
f = io.StringIO() |
| 138 |
with contextlib.redirect_stdout(f), contextlib.redirect_stderr(f): |
| 139 |
result = console.runsource(source) |
| 140 |
self.assertFalse(result) |
| 141 |
self.assertIn("source code string cannot contain null bytes", f.getvalue()) |
| 142 |
|
| 143 |
def test_no_active_future(self): |
| 144 |
console = InteractiveColoredConsole() |
| 145 |
source = dedent("""\ |
| 146 |
x: int = 1 |
| 147 |
print(__annotate__(1)) |
| 148 |
""") |
| 149 |
f = io.StringIO() |
| 150 |
with contextlib.redirect_stdout(f): |
| 151 |
result = console.runsource(source) |
| 152 |
self.assertFalse(result) |
| 153 |
self.assertEqual(f.getvalue(), "{'x': <class 'int'>}\n") |
| 154 |
|
| 155 |
def test_future_annotations(self): |
| 156 |
console = InteractiveColoredConsole() |
| 157 |
source = dedent("""\ |
| 158 |
from __future__ import annotations |
| 159 |
def g(x: int): ... |
| 160 |
print(g.__annotations__) |
| 161 |
""") |
| 162 |
f = io.StringIO() |
| 163 |
with contextlib.redirect_stdout(f): |
| 164 |
result = console.runsource(source) |
| 165 |
self.assertFalse(result) |
| 166 |
self.assertEqual(f.getvalue(), "{'x': 'int'}\n") |
| 167 |
|
| 168 |
def test_future_barry_as_flufl(self): |
| 169 |
console = InteractiveColoredConsole() |
| 170 |
f = io.StringIO() |
| 171 |
with contextlib.redirect_stdout(f): |
| 172 |
result = console.runsource("from __future__ import barry_as_FLUFL\n") |
| 173 |
result = console.runsource("""print("black" <> 'blue')\n""") |
| 174 |
self.assertFalse(result) |
| 175 |
self.assertEqual(f.getvalue(), "True\n") |
| 176 |
|
| 177 |
|
| 178 |
class TestMoreLines(unittest.TestCase): |
| 179 |
def test_invalid_syntax_single_line(self): |
| 180 |
namespace = {} |
| 181 |
code = "if foo" |
| 182 |
console = InteractiveColoredConsole(namespace, filename="<stdin>") |
| 183 |
self.assertFalse(_more_lines(console, code)) |
| 184 |
|
| 185 |
def test_empty_line(self): |
| 186 |
namespace = {} |
| 187 |
code = "" |
| 188 |
console = InteractiveColoredConsole(namespace, filename="<stdin>") |
| 189 |
self.assertFalse(_more_lines(console, code)) |
| 190 |
|
| 191 |
def test_valid_single_statement(self): |
| 192 |
namespace = {} |
| 193 |
code = "foo = 1" |
| 194 |
console = InteractiveColoredConsole(namespace, filename="<stdin>") |
| 195 |
self.assertFalse(_more_lines(console, code)) |
| 196 |
|
| 197 |
def test_multiline_single_assignment(self): |
| 198 |
namespace = {} |
| 199 |
code = dedent("""\ |
| 200 |
foo = [ |
| 201 |
1, |
| 202 |
2, |
| 203 |
3, |
| 204 |
]""") |
| 205 |
console = InteractiveColoredConsole(namespace, filename="<stdin>") |
| 206 |
self.assertFalse(_more_lines(console, code)) |
| 207 |
|
| 208 |
def test_multiline_single_block(self): |
| 209 |
namespace = {} |
| 210 |
code = dedent("""\ |
| 211 |
def foo(): |
| 212 |
'''docs''' |
| 213 |
|
| 214 |
return 1""") |
| 215 |
console = InteractiveColoredConsole(namespace, filename="<stdin>") |
| 216 |
self.assertTrue(_more_lines(console, code)) |
| 217 |
|
| 218 |
def test_multiple_statements_single_line(self): |
| 219 |
namespace = {} |
| 220 |
code = "foo = 1;bar = 2" |
| 221 |
console = InteractiveColoredConsole(namespace, filename="<stdin>") |
| 222 |
self.assertFalse(_more_lines(console, code)) |
| 223 |
|
| 224 |
def test_multiple_statements(self): |
| 225 |
namespace = {} |
| 226 |
code = dedent("""\ |
| 227 |
import time |
| 228 |
|
| 229 |
foo = 1""") |
| 230 |
console = InteractiveColoredConsole(namespace, filename="<stdin>") |
| 231 |
self.assertTrue(_more_lines(console, code)) |
| 232 |
|
| 233 |
def test_multiple_blocks(self): |
| 234 |
namespace = {} |
| 235 |
code = dedent("""\ |
| 236 |
from dataclasses import dataclass |
| 237 |
|
| 238 |
@dataclass |
| 239 |
class Point: |
| 240 |
x: float |
| 241 |
y: float""") |
| 242 |
console = InteractiveColoredConsole(namespace, filename="<stdin>") |
| 243 |
self.assertTrue(_more_lines(console, code)) |
| 244 |
|
| 245 |
def test_multiple_blocks_empty_newline(self): |
| 246 |
namespace = {} |
| 247 |
code = dedent("""\ |
| 248 |
from dataclasses import dataclass |
| 249 |
|
| 250 |
@dataclass |
| 251 |
class Point: |
| 252 |
x: float |
| 253 |
y: float |
| 254 |
""") |
| 255 |
console = InteractiveColoredConsole(namespace, filename="<stdin>") |
| 256 |
self.assertFalse(_more_lines(console, code)) |
| 257 |
|
| 258 |
def test_multiple_blocks_indented_newline(self): |
| 259 |
namespace = {} |
| 260 |
code = ( |
| 261 |
"from dataclasses import dataclass\n" |
| 262 |
"\n" |
| 263 |
"@dataclass\n" |
| 264 |
"class Point:\n" |
| 265 |
" x: float\n" |
| 266 |
" y: float\n" |
| 267 |
" " |
| 268 |
) |
| 269 |
console = InteractiveColoredConsole(namespace, filename="<stdin>") |
| 270 |
self.assertFalse(_more_lines(console, code)) |
| 271 |
|
| 272 |
def test_incomplete_statement(self): |
| 273 |
namespace = {} |
| 274 |
code = "if foo:" |
| 275 |
console = InteractiveColoredConsole(namespace, filename="<stdin>") |
| 276 |
self.assertTrue(_more_lines(console, code)) |
| 277 |
|
| 278 |
|
| 279 |
class TestWarnings(unittest.TestCase): |
| 280 |
def test_pep_765_warning(self): |
| 281 |
""" |
| 282 |
Test that a SyntaxWarning emitted from the |
| 283 |
AST optimizer is only shown once in the REPL. |
| 284 |
""" |
| 285 |
# gh-131927 |
| 286 |
console = InteractiveColoredConsole() |
| 287 |
code = dedent("""\ |
| 288 |
def f(): |
| 289 |
try: |
| 290 |
return 1 |
| 291 |
finally: |
| 292 |
return 2 |
| 293 |
""") |
| 294 |
|
| 295 |
with warnings.catch_warnings(record=True) as caught: |
| 296 |
warnings.simplefilter("always") |
| 297 |
console.runsource(code) |
| 298 |
|
| 299 |
count = sum("'return' in a 'finally' block" in str(w.message) |
| 300 |
for w in caught) |
| 301 |
self.assertEqual(count, 1) |