Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1class TracebackEntry: 

2 """A traceback entry with file path, line number, function and source 

3 code. 

4 

5 """ 

6 

7 path: string 

8 line_number: i64 

9 function: string 

10 source_code: string 

11 

12class Traceback: 

13 """A traceback. 

14 

15 """ 

16 

17 stack: [TracebackEntry] 

18 

19 func __str__(self) -> string: 

20 lines = ["Traceback (most recent call last):"] 

21 

22 for entry in self.stack: 

23 lines.append(f" File: \"{entry.path}\", line {entry.line_number} " 

24 f"in {entry.function}") 

25 lines.append(f" {entry.source_code}") 

26 

27 return "\n".join(lines) 

28 

29func traceback() -> Traceback: 

30 """Returns the current traceback. 

31 

32 """ 

33 

34 stack: [TracebackEntry] = [] 

35 path: string? = None 

36 line_number: i64 = -1 

37 function: string? = None 

38 source_code: string? = None 

39 

40 c""" 

41 #if defined(MYS_TRACEBACK) 

42 

43 mys::TracebackEntry *item_p; 

44 mys::TracebackEntryInfo *entry_info_p; 

45 

46 item_p = mys::traceback_bottom_p->next_p; 

47 

48 while (item_p != mys::traceback_top_p) { 

49 path = String(item_p->info_p->path_p); 

50 entry_info_p = &item_p->info_p->entries_info_p[item_p->index]; 

51 line_number = entry_info_p->line_number; 

52 function = String(entry_info_p->name_p); 

53 source_code = String(entry_info_p->code_p); 

54 """ 

55 

56 stack.append(TracebackEntry(path, line_number, function, source_code)) 

57 

58 c""" 

59 item_p = item_p->next_p; 

60 } 

61 

62 #endif 

63 """ 

64 

65 return Traceback(stack) 

66 

67test traceback(): 

68 tb = traceback() 

69 

70 assert tb.stack.length() == 1 

71 

72 entry = tb.stack[0] 

73 assert entry.path == "./src/lib.mys" 

74 assert entry.line_number == 68 

75 assert entry.function == "traceback" 

76 assert entry.source_code == "tb = traceback()" 

77 

78 assert str(tb) == ( 

79 "Traceback (most recent call last):\n" 

80 " File: \"./src/lib.mys\", line 68 in traceback\n" 

81 " tb = traceback()")