@@ -49,40 +49,42 @@ def is_user_frame(self):
4949 module = self .module or ""
5050 return "tripy" not in module .split ("." )
5151
52+ def fetch_source_code (self ):
53+ if self .code is not None :
54+ return
55+
56+ # Note that in some cases, e.g. when code is being provided via the interactive shell, we may not be able to retrieve it.
57+ # In that case we just leave it empty.
58+ try :
59+ lines = open (self .file , "r" ).readlines ()
60+ except OSError :
61+ self .code = ""
62+ else :
63+ self .code = lines [self .line - 1 ].rstrip ()
64+
5265
5366class StackInfo (list ):
5467 def __init__ (self , lst , include_code_index : Optional [int ] = None ):
5568 super ().__init__ (lst )
5669 self .include_code_index = include_code_index
57- self .code_fetched = False
70+ self ._code_fetched = False
5871
5972 def fetch_source_code (self ):
60- if self .code_fetched :
73+ if self ._code_fetched :
6174 return
6275
6376 first_user_frame_found = False
6477 for index , source_info in enumerate (self ):
65-
66- def add_code ():
67- # Note that in some cases, e.g. when code is being provided via the interactive shell, we may not be able to retrieve it.
68- # In that case we just leave it empty.
69- try :
70- lines = open (source_info .file , "r" ).readlines ()
71- except OSError :
72- return
73-
74- source_info .code = lines [source_info .line - 1 ].rstrip ()
75-
7678 if not first_user_frame_found :
7779 if source_info .is_user_frame ():
78- add_code ()
80+ source_info . fetch_source_code ()
7981 first_user_frame_found = True
8082 elif self .include_code_index is not None and index >= self .include_code_index :
81- add_code ()
83+ source_info . fetch_source_code ()
8284
83- self .code_fetched = True
85+ self ._code_fetched = True
8486
85- def get_first_user_frame_index (self ) -> int :
87+ def get_first_user_frame_index (self ) -> Optional [ int ] :
8688 for index , source_info in enumerate (self ):
8789 if source_info .is_user_frame ():
8890 return index
0 commit comments