| 1 |
"Test run, coverage 54%." |
| 2 |
|
| 3 |
from idlelib import run |
| 4 |
import io |
| 5 |
import sys |
| 6 |
from test.support import captured_output, captured_stderr |
| 7 |
import unittest |
| 8 |
from unittest import mock |
| 9 |
import idlelib |
| 10 |
from idlelib.idle_test.mock_idle import Func |
| 11 |
from test.support import force_not_colorized |
| 12 |
|
| 13 |
idlelib.testing = True # Use {} for executing test user code. |
| 14 |
|
| 15 |
|
| 16 |
class ExceptionTest(unittest.TestCase): |
| 17 |
|
| 18 |
def test_print_exception_unhashable(self): |
| 19 |
class UnhashableException(Exception): |
| 20 |
def __eq__(self, other): |
| 21 |
return True |
| 22 |
|
| 23 |
ex1 = UnhashableException('ex1') |
| 24 |
ex2 = UnhashableException('ex2') |
| 25 |
try: |
| 26 |
raise ex2 from ex1 |
| 27 |
except UnhashableException: |
| 28 |
try: |
| 29 |
raise ex1 |
| 30 |
except UnhashableException: |
| 31 |
with captured_stderr() as output: |
| 32 |
with mock.patch.object(run, 'cleanup_traceback') as ct: |
| 33 |
ct.side_effect = lambda t, e: t |
| 34 |
run.print_exception() |
| 35 |
|
| 36 |
tb = output.getvalue().strip().splitlines() |
| 37 |
self.assertEqual(11, len(tb)) |
| 38 |
self.assertIn('UnhashableException: ex2', tb[3]) |
| 39 |
self.assertIn('UnhashableException: ex1', tb[10]) |
| 40 |
|
| 41 |
data = (('1/0', ZeroDivisionError, "division by zero\n"), |
| 42 |
('abc', NameError, "name 'abc' is not defined. " |
| 43 |
"Did you mean: 'abs'? " |
| 44 |
"Or did you forget to import 'abc'?\n"), |
| 45 |
('int.reel', AttributeError, |
| 46 |
"type object 'int' has no attribute 'reel'. " |
| 47 |
"Did you mean: 'real'?\n"), |
| 48 |
) |
| 49 |
|
| 50 |
@force_not_colorized |
| 51 |
def test_get_message(self): |
| 52 |
for code, exc, msg in self.data: |
| 53 |
with self.subTest(code=code): |
| 54 |
try: |
| 55 |
eval(compile(code, '', 'eval')) |
| 56 |
except exc: |
| 57 |
typ, val, tb = sys.exc_info() |
| 58 |
actual = run.get_message_lines(typ, val, tb)[0] |
| 59 |
expect = f'{exc.__name__}: {msg}' |
| 60 |
self.assertEqual(actual, expect) |
| 61 |
|
| 62 |
@force_not_colorized |
| 63 |
@mock.patch.object(run, 'cleanup_traceback', |
| 64 |
new_callable=lambda: (lambda t, e: None)) |
| 65 |
def test_get_multiple_message(self, mock): |
| 66 |
d = self.data |
| 67 |
data2 = ((d[0], d[1]), (d[1], d[2]), (d[2], d[0])) |
| 68 |
subtests = 0 |
| 69 |
for (code1, exc1, msg1), (code2, exc2, msg2) in data2: |
| 70 |
with self.subTest(codes=(code1,code2)): |
| 71 |
try: |
| 72 |
eval(compile(code1, '', 'eval')) |
| 73 |
except exc1: |
| 74 |
try: |
| 75 |
eval(compile(code2, '', 'eval')) |
| 76 |
except exc2: |
| 77 |
with captured_stderr() as output: |
| 78 |
run.print_exception() |
| 79 |
actual = output.getvalue() |
| 80 |
self.assertIn(msg1, actual) |
| 81 |
self.assertIn(msg2, actual) |
| 82 |
subtests += 1 |
| 83 |
self.assertEqual(subtests, len(data2)) # All subtests ran? |
| 84 |
|
| 85 |
# StdioFile tests. |
| 86 |
|
| 87 |
class S(str): |
| 88 |
def __str__(self): |
| 89 |
return '%s:str' % type(self).__name__ |
| 90 |
def __unicode__(self): |
| 91 |
return '%s:unicode' % type(self).__name__ |
| 92 |
def __len__(self): |
| 93 |
return 3 |
| 94 |
def __iter__(self): |
| 95 |
return iter('abc') |
| 96 |
def __getitem__(self, *args): |
| 97 |
return '%s:item' % type(self).__name__ |
| 98 |
def __getslice__(self, *args): |
| 99 |
return '%s:slice' % type(self).__name__ |
| 100 |
|
| 101 |
|
| 102 |
class MockShell: |
| 103 |
def __init__(self): |
| 104 |
self.reset() |
| 105 |
def write(self, *args): |
| 106 |
self.written.append(args) |
| 107 |
def readline(self): |
| 108 |
return self.lines.pop() |
| 109 |
def close(self): |
| 110 |
pass |
| 111 |
def reset(self): |
| 112 |
self.written = [] |
| 113 |
def push(self, lines): |
| 114 |
self.lines = list(lines)[::-1] |
| 115 |
|
| 116 |
|
| 117 |
class StdInputFilesTest(unittest.TestCase): |
| 118 |
|
| 119 |
def test_misc(self): |
| 120 |
shell = MockShell() |
| 121 |
f = run.StdInputFile(shell, 'stdin') |
| 122 |
self.assertIsInstance(f, io.TextIOBase) |
| 123 |
self.assertEqual(f.encoding, 'utf-8') |
| 124 |
self.assertEqual(f.errors, 'strict') |
| 125 |
self.assertIsNone(f.newlines) |
| 126 |
self.assertEqual(f.name, '<stdin>') |
| 127 |
self.assertFalse(f.closed) |
| 128 |
self.assertTrue(f.isatty()) |
| 129 |
self.assertTrue(f.readable()) |
| 130 |
self.assertFalse(f.writable()) |
| 131 |
self.assertFalse(f.seekable()) |
| 132 |
|
| 133 |
def test_unsupported(self): |
| 134 |
shell = MockShell() |
| 135 |
f = run.StdInputFile(shell, 'stdin') |
| 136 |
self.assertRaises(OSError, f.fileno) |
| 137 |
self.assertRaises(OSError, f.tell) |
| 138 |
self.assertRaises(OSError, f.seek, 0) |
| 139 |
self.assertRaises(OSError, f.write, 'x') |
| 140 |
self.assertRaises(OSError, f.writelines, ['x']) |
| 141 |
|
| 142 |
def test_read(self): |
| 143 |
shell = MockShell() |
| 144 |
f = run.StdInputFile(shell, 'stdin') |
| 145 |
shell.push(['one\n', 'two\n', '']) |
| 146 |
self.assertEqual(f.read(), 'one\ntwo\n') |
| 147 |
shell.push(['one\n', 'two\n', '']) |
| 148 |
self.assertEqual(f.read(-1), 'one\ntwo\n') |
| 149 |
shell.push(['one\n', 'two\n', '']) |
| 150 |
self.assertEqual(f.read(None), 'one\ntwo\n') |
| 151 |
shell.push(['one\n', 'two\n', 'three\n', '']) |
| 152 |
self.assertEqual(f.read(2), 'on') |
| 153 |
self.assertEqual(f.read(3), 'e\nt') |
| 154 |
self.assertEqual(f.read(10), 'wo\nthree\n') |
| 155 |
|
| 156 |
shell.push(['one\n', 'two\n']) |
| 157 |
self.assertEqual(f.read(0), '') |
| 158 |
self.assertRaises(TypeError, f.read, 1.5) |
| 159 |
self.assertRaises(TypeError, f.read, '1') |
| 160 |
self.assertRaises(TypeError, f.read, 1, 1) |
| 161 |
|
| 162 |
def test_readline(self): |
| 163 |
shell = MockShell() |
| 164 |
f = run.StdInputFile(shell, 'stdin') |
| 165 |
shell.push(['one\n', 'two\n', 'three\n', 'four\n']) |
| 166 |
self.assertEqual(f.readline(), 'one\n') |
| 167 |
self.assertEqual(f.readline(-1), 'two\n') |
| 168 |
self.assertEqual(f.readline(None), 'three\n') |
| 169 |
shell.push(['one\ntwo\n']) |
| 170 |
self.assertEqual(f.readline(), 'one\n') |
| 171 |
self.assertEqual(f.readline(), 'two\n') |
| 172 |
shell.push(['one', 'two', 'three']) |
| 173 |
self.assertEqual(f.readline(), 'one') |
| 174 |
self.assertEqual(f.readline(), 'two') |
| 175 |
shell.push(['one\n', 'two\n', 'three\n']) |
| 176 |
self.assertEqual(f.readline(2), 'on') |
| 177 |
self.assertEqual(f.readline(1), 'e') |
| 178 |
self.assertEqual(f.readline(1), '\n') |
| 179 |
self.assertEqual(f.readline(10), 'two\n') |
| 180 |
|
| 181 |
shell.push(['one\n', 'two\n']) |
| 182 |
self.assertEqual(f.readline(0), '') |
| 183 |
self.assertRaises(TypeError, f.readlines, 1.5) |
| 184 |
self.assertRaises(TypeError, f.readlines, '1') |
| 185 |
self.assertRaises(TypeError, f.readlines, 1, 1) |
| 186 |
|
| 187 |
def test_readlines(self): |
| 188 |
shell = MockShell() |
| 189 |
f = run.StdInputFile(shell, 'stdin') |
| 190 |
shell.push(['one\n', 'two\n', '']) |
| 191 |
self.assertEqual(f.readlines(), ['one\n', 'two\n']) |
| 192 |
shell.push(['one\n', 'two\n', '']) |
| 193 |
self.assertEqual(f.readlines(-1), ['one\n', 'two\n']) |
| 194 |
shell.push(['one\n', 'two\n', '']) |
| 195 |
self.assertEqual(f.readlines(None), ['one\n', 'two\n']) |
| 196 |
shell.push(['one\n', 'two\n', '']) |
| 197 |
self.assertEqual(f.readlines(0), ['one\n', 'two\n']) |
| 198 |
shell.push(['one\n', 'two\n', '']) |
| 199 |
self.assertEqual(f.readlines(3), ['one\n']) |
| 200 |
shell.push(['one\n', 'two\n', '']) |
| 201 |
self.assertEqual(f.readlines(4), ['one\n', 'two\n']) |
| 202 |
|
| 203 |
shell.push(['one\n', 'two\n', '']) |
| 204 |
self.assertRaises(TypeError, f.readlines, 1.5) |
| 205 |
self.assertRaises(TypeError, f.readlines, '1') |
| 206 |
self.assertRaises(TypeError, f.readlines, 1, 1) |
| 207 |
|
| 208 |
def test_close(self): |
| 209 |
shell = MockShell() |
| 210 |
f = run.StdInputFile(shell, 'stdin') |
| 211 |
shell.push(['one\n', 'two\n', '']) |
| 212 |
self.assertFalse(f.closed) |
| 213 |
self.assertEqual(f.readline(), 'one\n') |
| 214 |
f.close() |
| 215 |
self.assertFalse(f.closed) |
| 216 |
self.assertEqual(f.readline(), 'two\n') |
| 217 |
self.assertRaises(TypeError, f.close, 1) |
| 218 |
|
| 219 |
|
| 220 |
class StdOutputFilesTest(unittest.TestCase): |
| 221 |
|
| 222 |
def test_misc(self): |
| 223 |
shell = MockShell() |
| 224 |
f = run.StdOutputFile(shell, 'stdout') |
| 225 |
self.assertIsInstance(f, io.TextIOBase) |
| 226 |
self.assertEqual(f.encoding, 'utf-8') |
| 227 |
self.assertEqual(f.errors, 'strict') |
| 228 |
self.assertIsNone(f.newlines) |
| 229 |
self.assertEqual(f.name, '<stdout>') |
| 230 |
self.assertFalse(f.closed) |
| 231 |
self.assertTrue(f.isatty()) |
| 232 |
self.assertFalse(f.readable()) |
| 233 |
self.assertTrue(f.writable()) |
| 234 |
self.assertFalse(f.seekable()) |
| 235 |
|
| 236 |
def test_unsupported(self): |
| 237 |
shell = MockShell() |
| 238 |
f = run.StdOutputFile(shell, 'stdout') |
| 239 |
self.assertRaises(OSError, f.fileno) |
| 240 |
self.assertRaises(OSError, f.tell) |
| 241 |
self.assertRaises(OSError, f.seek, 0) |
| 242 |
self.assertRaises(OSError, f.read, 0) |
| 243 |
self.assertRaises(OSError, f.readline, 0) |
| 244 |
|
| 245 |
def test_write(self): |
| 246 |
shell = MockShell() |
| 247 |
f = run.StdOutputFile(shell, 'stdout') |
| 248 |
f.write('test') |
| 249 |
self.assertEqual(shell.written, [('test', 'stdout')]) |
| 250 |
shell.reset() |
| 251 |
f.write('t\xe8\u015b\U0001d599') |
| 252 |
self.assertEqual(shell.written, [('t\xe8\u015b\U0001d599', 'stdout')]) |
| 253 |
shell.reset() |
| 254 |
|
| 255 |
f.write(S('t\xe8\u015b\U0001d599')) |
| 256 |
self.assertEqual(shell.written, [('t\xe8\u015b\U0001d599', 'stdout')]) |
| 257 |
self.assertEqual(type(shell.written[0][0]), str) |
| 258 |
shell.reset() |
| 259 |
|
| 260 |
self.assertRaises(TypeError, f.write) |
| 261 |
self.assertEqual(shell.written, []) |
| 262 |
self.assertRaises(TypeError, f.write, b'test') |
| 263 |
self.assertRaises(TypeError, f.write, 123) |
| 264 |
self.assertEqual(shell.written, []) |
| 265 |
self.assertRaises(TypeError, f.write, 'test', 'spam') |
| 266 |
self.assertEqual(shell.written, []) |
| 267 |
|
| 268 |
def test_write_stderr_nonencodable(self): |
| 269 |
shell = MockShell() |
| 270 |
f = run.StdOutputFile(shell, 'stderr', 'iso-8859-15', 'backslashreplace') |
| 271 |
f.write('t\xe8\u015b\U0001d599\xa4') |
| 272 |
self.assertEqual(shell.written, [('t\xe8\\u015b\\U0001d599\\xa4', 'stderr')]) |
| 273 |
shell.reset() |
| 274 |
|
| 275 |
f.write(S('t\xe8\u015b\U0001d599\xa4')) |
| 276 |
self.assertEqual(shell.written, [('t\xe8\\u015b\\U0001d599\\xa4', 'stderr')]) |
| 277 |
self.assertEqual(type(shell.written[0][0]), str) |
| 278 |
shell.reset() |
| 279 |
|
| 280 |
self.assertRaises(TypeError, f.write) |
| 281 |
self.assertEqual(shell.written, []) |
| 282 |
self.assertRaises(TypeError, f.write, b'test') |
| 283 |
self.assertRaises(TypeError, f.write, 123) |
| 284 |
self.assertEqual(shell.written, []) |
| 285 |
self.assertRaises(TypeError, f.write, 'test', 'spam') |
| 286 |
self.assertEqual(shell.written, []) |
| 287 |
|
| 288 |
def test_writelines(self): |
| 289 |
shell = MockShell() |
| 290 |
f = run.StdOutputFile(shell, 'stdout') |
| 291 |
f.writelines([]) |
| 292 |
self.assertEqual(shell.written, []) |
| 293 |
shell.reset() |
| 294 |
f.writelines(['one\n', 'two']) |
| 295 |
self.assertEqual(shell.written, |
| 296 |
[('one\n', 'stdout'), ('two', 'stdout')]) |
| 297 |
shell.reset() |
| 298 |
f.writelines(['on\xe8\n', 'tw\xf2']) |
| 299 |
self.assertEqual(shell.written, |
| 300 |
[('on\xe8\n', 'stdout'), ('tw\xf2', 'stdout')]) |
| 301 |
shell.reset() |
| 302 |
|
| 303 |
f.writelines([S('t\xe8st')]) |
| 304 |
self.assertEqual(shell.written, [('t\xe8st', 'stdout')]) |
| 305 |
self.assertEqual(type(shell.written[0][0]), str) |
| 306 |
shell.reset() |
| 307 |
|
| 308 |
self.assertRaises(TypeError, f.writelines) |
| 309 |
self.assertEqual(shell.written, []) |
| 310 |
self.assertRaises(TypeError, f.writelines, 123) |
| 311 |
self.assertEqual(shell.written, []) |
| 312 |
self.assertRaises(TypeError, f.writelines, [b'test']) |
| 313 |
self.assertRaises(TypeError, f.writelines, [123]) |
| 314 |
self.assertEqual(shell.written, []) |
| 315 |
self.assertRaises(TypeError, f.writelines, [], []) |
| 316 |
self.assertEqual(shell.written, []) |
| 317 |
|
| 318 |
def test_close(self): |
| 319 |
shell = MockShell() |
| 320 |
f = run.StdOutputFile(shell, 'stdout') |
| 321 |
self.assertFalse(f.closed) |
| 322 |
f.write('test') |
| 323 |
f.close() |
| 324 |
self.assertTrue(f.closed) |
| 325 |
self.assertRaises(ValueError, f.write, 'x') |
| 326 |
self.assertEqual(shell.written, [('test', 'stdout')]) |
| 327 |
f.close() |
| 328 |
self.assertRaises(TypeError, f.close, 1) |
| 329 |
|
| 330 |
|
| 331 |
class RecursionLimitTest(unittest.TestCase): |
| 332 |
# Test (un)install_recursionlimit_wrappers and fixdoc. |
| 333 |
|
| 334 |
def test_bad_setrecursionlimit_calls(self): |
| 335 |
run.install_recursionlimit_wrappers() |
| 336 |
self.addCleanup(run.uninstall_recursionlimit_wrappers) |
| 337 |
f = sys.setrecursionlimit |
| 338 |
self.assertRaises(TypeError, f, limit=100) |
| 339 |
self.assertRaises(TypeError, f, 100, 1000) |
| 340 |
self.assertRaises(ValueError, f, 0) |
| 341 |
|
| 342 |
def test_roundtrip(self): |
| 343 |
run.install_recursionlimit_wrappers() |
| 344 |
self.addCleanup(run.uninstall_recursionlimit_wrappers) |
| 345 |
|
| 346 |
# Check that setting the recursion limit works. |
| 347 |
orig_reclimit = sys.getrecursionlimit() |
| 348 |
self.addCleanup(sys.setrecursionlimit, orig_reclimit) |
| 349 |
sys.setrecursionlimit(orig_reclimit + 3) |
| 350 |
|
| 351 |
# Check that the new limit is returned by sys.getrecursionlimit(). |
| 352 |
new_reclimit = sys.getrecursionlimit() |
| 353 |
self.assertEqual(new_reclimit, orig_reclimit + 3) |
| 354 |
|
| 355 |
def test_default_recursion_limit_preserved(self): |
| 356 |
orig_reclimit = sys.getrecursionlimit() |
| 357 |
run.install_recursionlimit_wrappers() |
| 358 |
self.addCleanup(run.uninstall_recursionlimit_wrappers) |
| 359 |
new_reclimit = sys.getrecursionlimit() |
| 360 |
self.assertEqual(new_reclimit, orig_reclimit) |
| 361 |
|
| 362 |
def test_fixdoc(self): |
| 363 |
# Put here until better place for miscellaneous test. |
| 364 |
def func(): "docstring" |
| 365 |
run.fixdoc(func, "more") |
| 366 |
self.assertEqual(func.__doc__, "docstring\n\nmore") |
| 367 |
func.__doc__ = None |
| 368 |
run.fixdoc(func, "more") |
| 369 |
self.assertEqual(func.__doc__, "more") |
| 370 |
|
| 371 |
|
| 372 |
class HandleErrorTest(unittest.TestCase): |
| 373 |
# Method of MyRPCServer |
| 374 |
def test_fatal_error(self): |
| 375 |
eq = self.assertEqual |
| 376 |
with captured_output('__stderr__') as err,\ |
| 377 |
mock.patch('idlelib.run.thread.interrupt_main', |
| 378 |
new_callable=Func) as func: |
| 379 |
try: |
| 380 |
raise EOFError |
| 381 |
except EOFError: |
| 382 |
run.MyRPCServer.handle_error(None, 'abc', '123') |
| 383 |
eq(run.exit_now, True) |
| 384 |
run.exit_now = False |
| 385 |
eq(err.getvalue(), '') |
| 386 |
|
| 387 |
try: |
| 388 |
raise IndexError |
| 389 |
except IndexError: |
| 390 |
run.MyRPCServer.handle_error(None, 'abc', '123') |
| 391 |
eq(run.quitting, True) |
| 392 |
run.quitting = False |
| 393 |
msg = err.getvalue() |
| 394 |
self.assertIn('abc', msg) |
| 395 |
self.assertIn('123', msg) |
| 396 |
self.assertIn('IndexError', msg) |
| 397 |
eq(func.called, 2) |
| 398 |
|
| 399 |
|
| 400 |
class ExecRuncodeTest(unittest.TestCase): |
| 401 |
|
| 402 |
@classmethod |
| 403 |
def setUpClass(cls): |
| 404 |
cls.addClassCleanup(setattr,run,'print_exception',run.print_exception) |
| 405 |
cls.prt = Func() # Need reference. |
| 406 |
run.print_exception = cls.prt |
| 407 |
mockrpc = mock.Mock() |
| 408 |
mockrpc.console.getvar = Func(result=False) |
| 409 |
cls.ex = run.Executive(mockrpc) |
| 410 |
|
| 411 |
@classmethod |
| 412 |
def tearDownClass(cls): |
| 413 |
assert sys.excepthook == sys.__excepthook__ |
| 414 |
|
| 415 |
def test_exceptions(self): |
| 416 |
ex = self.ex |
| 417 |
ex.runcode('1/0') |
| 418 |
self.assertIs(ex.user_exc_info[0], ZeroDivisionError) |
| 419 |
|
| 420 |
self.addCleanup(setattr, sys, 'excepthook', sys.__excepthook__) |
| 421 |
sys.excepthook = lambda t, e, tb: run.print_exception(t) |
| 422 |
ex.runcode('1/0') |
| 423 |
self.assertIs(self.prt.args[0], ZeroDivisionError) |
| 424 |
|
| 425 |
sys.excepthook = lambda: None |
| 426 |
ex.runcode('1/0') |
| 427 |
t, e, tb = ex.user_exc_info |
| 428 |
self.assertIs(t, TypeError) |
| 429 |
self.assertTrue(isinstance(e.__context__, ZeroDivisionError)) |
| 430 |
|
| 431 |
|
| 432 |
if __name__ == '__main__': |
| 433 |
unittest.main(verbosity=2) |