##// END OF EJS Templates
Fix test_ultratb (particularly recursion-related tests)
Alex Hall -
Show More
@@ -8,10 +8,8 b' import os.path'
8 8 from textwrap import dedent
9 9 import traceback
10 10 import unittest
11 from unittest import mock
12 11
13 import IPython.core.ultratb as ultratb
14 from IPython.core.ultratb import ColorTB, VerboseTB, find_recursion
12 from IPython.core.ultratb import ColorTB, VerboseTB
15 13
16 14
17 15 from IPython.testing import tools as tt
@@ -38,16 +36,12 b' def recursionlimit(frames):'
38 36
39 37 def inner(test_function):
40 38 def wrapper(*args, **kwargs):
41 _orig_rec_limit = ultratb._FRAME_RECURSION_LIMIT
42 ultratb._FRAME_RECURSION_LIMIT = 50
43
44 39 rl = sys.getrecursionlimit()
45 40 sys.setrecursionlimit(frames)
46 41 try:
47 42 return test_function(*args, **kwargs)
48 43 finally:
49 44 sys.setrecursionlimit(rl)
50 ultratb._FRAME_RECURSION_LIMIT = _orig_rec_limit
51 45
52 46 return wrapper
53 47
@@ -350,45 +344,24 b' def r3o2():'
350 344 ip.run_cell(self.DEFINITIONS)
351 345
352 346 def test_no_recursion(self):
353 with tt.AssertNotPrints("frames repeated"):
347 with tt.AssertNotPrints("skipping similar frames"):
354 348 ip.run_cell("non_recurs()")
355 349
356 350 @recursionlimit(150)
357 351 def test_recursion_one_frame(self):
358 with tt.AssertPrints("1 frames repeated"):
352 with tt.AssertPrints("[... skipping similar frames: r1 at line 5 (95 times)]"):
359 353 ip.run_cell("r1()")
360 354
361 355 @recursionlimit(150)
362 356 def test_recursion_three_frames(self):
363 with tt.AssertPrints("3 frames repeated"):
357 with tt.AssertPrints(
358 "[... skipping similar frames: "
359 "r3a at line 8 (29 times), "
360 "r3b at line 11 (29 times), "
361 "r3c at line 14 (29 times)]"
362 ):
364 363 ip.run_cell("r3o2()")
365 364
366 @recursionlimit(150)
367 def test_find_recursion(self):
368 captured = []
369 def capture_exc(*args, **kwargs):
370 captured.append(sys.exc_info())
371 with mock.patch.object(ip, 'showtraceback', capture_exc):
372 ip.run_cell("r3o2()")
373
374 self.assertEqual(len(captured), 1)
375 etype, evalue, tb = captured[0]
376 self.assertIn("recursion", str(evalue))
377
378 records = ip.InteractiveTB.get_records(tb, 3, ip.InteractiveTB.tb_offset)
379 for r in records[:10]:
380 print(r[1:4])
381
382 # The outermost frames should be:
383 # 0: the 'cell' that was running when the exception came up
384 # 1: r3o2()
385 # 2: r3o1()
386 # 3: r3a()
387 # Then repeating r3b, r3c, r3a
388 last_unique, repeat_length = find_recursion(etype, evalue, records)
389 self.assertEqual(last_unique, 2)
390 self.assertEqual(repeat_length, 3)
391
392 365
393 366 #----------------------------------------------------------------------------
394 367
General Comments 0
You need to be logged in to leave comments. Login now