Show More
@@ -8,10 +8,8 b' import os.path' | |||||
8 | from textwrap import dedent |
|
8 | from textwrap import dedent | |
9 | import traceback |
|
9 | import traceback | |
10 | import unittest |
|
10 | import unittest | |
11 | from unittest import mock |
|
|||
12 |
|
11 | |||
13 | import IPython.core.ultratb as ultratb |
|
12 | from IPython.core.ultratb import ColorTB, VerboseTB | |
14 | from IPython.core.ultratb import ColorTB, VerboseTB, find_recursion |
|
|||
15 |
|
13 | |||
16 |
|
14 | |||
17 | from IPython.testing import tools as tt |
|
15 | from IPython.testing import tools as tt | |
@@ -38,16 +36,12 b' def recursionlimit(frames):' | |||||
38 |
|
36 | |||
39 | def inner(test_function): |
|
37 | def inner(test_function): | |
40 | def wrapper(*args, **kwargs): |
|
38 | def wrapper(*args, **kwargs): | |
41 | _orig_rec_limit = ultratb._FRAME_RECURSION_LIMIT |
|
|||
42 | ultratb._FRAME_RECURSION_LIMIT = 50 |
|
|||
43 |
|
||||
44 | rl = sys.getrecursionlimit() |
|
39 | rl = sys.getrecursionlimit() | |
45 | sys.setrecursionlimit(frames) |
|
40 | sys.setrecursionlimit(frames) | |
46 | try: |
|
41 | try: | |
47 | return test_function(*args, **kwargs) |
|
42 | return test_function(*args, **kwargs) | |
48 | finally: |
|
43 | finally: | |
49 | sys.setrecursionlimit(rl) |
|
44 | sys.setrecursionlimit(rl) | |
50 | ultratb._FRAME_RECURSION_LIMIT = _orig_rec_limit |
|
|||
51 |
|
45 | |||
52 | return wrapper |
|
46 | return wrapper | |
53 |
|
47 | |||
@@ -350,45 +344,24 b' def r3o2():' | |||||
350 | ip.run_cell(self.DEFINITIONS) |
|
344 | ip.run_cell(self.DEFINITIONS) | |
351 |
|
345 | |||
352 | def test_no_recursion(self): |
|
346 | def test_no_recursion(self): | |
353 |
with tt.AssertNotPrints("frames |
|
347 | with tt.AssertNotPrints("skipping similar frames"): | |
354 | ip.run_cell("non_recurs()") |
|
348 | ip.run_cell("non_recurs()") | |
355 |
|
349 | |||
356 | @recursionlimit(150) |
|
350 | @recursionlimit(150) | |
357 | def test_recursion_one_frame(self): |
|
351 | def test_recursion_one_frame(self): | |
358 |
with tt.AssertPrints(" |
|
352 | with tt.AssertPrints("[... skipping similar frames: r1 at line 5 (95 times)]"): | |
359 | ip.run_cell("r1()") |
|
353 | ip.run_cell("r1()") | |
360 |
|
354 | |||
361 | @recursionlimit(150) |
|
355 | @recursionlimit(150) | |
362 | def test_recursion_three_frames(self): |
|
356 | def test_recursion_three_frames(self): | |
363 |
with tt.AssertPrints( |
|
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 | ip.run_cell("r3o2()") |
|
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