@@ -83,7 +83,11 @@ class SourceCodeInfo:
8383 def __init__ (self , frame ):
8484 self .__filename = frame .filename
8585 self .__lineno = frame .lineno
86- self .__line = frame .code_context [frame .index ]
86+ if frame .code_context :
87+ self .__line = frame .code_context [frame .index ]
88+ else :
89+ self .__line = None
90+
8791 if sys .version_info >= (3 , 11 ):
8892 self .__pos = frame .positions
8993 else :
@@ -117,18 +121,20 @@ def lines(self):
117121
118122 def __str__ (self ):
119123 prefix = '| '
120- ret = f'{ prefix } { self .filename } :{ self .lineno } \n { prefix } { self .line } '
121- if self .__pos is not None :
122- # Add precise column information
123- l_start , l_end = self .lines
124- c_start , c_end = self .columns
125- if l_start != l_end :
126- # If the code segment is multiline, the columns are not
127- # precise. So we use the full line as span.
128- c_end = len (self .line ) - 1
129-
130- span = c_end - c_start
131- ret += f'{ prefix } { " " * c_start } ^{ "~" * (span - 1 )} '
124+ ret = f'{ prefix } { self .filename } :{ self .lineno } '
125+ if self .line :
126+ ret += f'\n { prefix } { self .line } '
127+ if self .__pos is not None :
128+ # Add precise column information
129+ l_start , l_end = self .lines
130+ c_start , c_end = self .columns
131+ if l_start != l_end :
132+ # If the code segment is multiline, the columns are not
133+ # precise. So we use the full line as span.
134+ c_end = len (self .line ) - 1
135+
136+ span = c_end - c_start
137+ ret += f'{ prefix } { " " * c_start } ^{ "~" * (span - 1 )} '
132138
133139 return ret
134140
0 commit comments