Coverage for MC6809/utils/simple_debugger.py: 10%

44 statements  

« prev     ^ index     » next       coverage.py v7.2.1, created at 2023-03-06 19:50 +0100

1#!/usr/bin/env python 

2 

3""" 

4 borrowed from http://code.activestate.com/recipes/52215/ 

5 

6 usage: 

7 

8 try: 

9 # ...do something... 

10 except: 

11 print_exc_plus() 

12""" 

13 

14 

15import sys 

16import traceback 

17 

18import click 

19 

20 

21MAX_CHARS = 256 

22 

23 

24def print_exc_plus(): 

25 """ 

26 Print the usual traceback information, followed by a listing of all the 

27 local variables in each frame. 

28 """ 

29 sys.stderr.flush() # for eclipse 

30 sys.stdout.flush() # for eclipse 

31 

32 tb = sys.exc_info()[2] 

33 while True: 

34 if not tb.tb_next: 

35 break 

36 tb = tb.tb_next 

37 stack = [] 

38 f = tb.tb_frame 

39 while f: 

40 stack.append(f) 

41 f = f.f_back 

42 

43 txt = traceback.format_exc() 

44 txt_lines = txt.splitlines() 

45 first_line = txt_lines.pop(0) 

46 last_line = txt_lines.pop(-1) 

47 click.secho(first_line, fg='red') 

48 

49 for line in txt_lines: 

50 if line.strip().startswith("File"): 

51 print(line) 

52 else: 

53 click.secho(line, fg='white', bold=True) 

54 click.secho(line, fg="white", bold=True) 

55 click.secho(last_line, fg="red") 

56 

57 print() 

58 click.secho( 

59 "Locals by frame, most recent call first:", 

60 fg="blue", bold=True 

61 ) 

62 for frame in stack: 

63 click.secho(f'\n *** File "{frame.f_code.co_filename}", line {frame.f_lineno:d}, in {frame.f_code.co_name}', 

64 fg="white", 

65 bold=True 

66 ) 

67 

68 for key, value in list(frame.f_locals.items()): 

69 print(click.style("%30s = " % key, bold=True), end=' ') 

70 # We have to be careful not to cause a new error in our error 

71 # printer! Calling str() on an unknown object could cause an 

72 # error we don't want. 

73 if isinstance(value, int): 

74 value = f"${value:x} (decimal: {value:d})" 

75 else: 

76 value = repr(value) 

77 

78 if len(value) > MAX_CHARS: 

79 value = f"{value[:MAX_CHARS]}..." 

80 

81 try: 

82 print(value) 

83 except BaseException: 

84 print("<ERROR WHILE PRINTING VALUE>")