@@ -1872,7 +1872,10 @@ def parse(self, code_str, filename=None, mode="exec"):
1872
1872
self .lineno = err .lineno
1873
1873
self .col_offset = err .offset - 1
1874
1874
self .exception = f"syntax error { err } "
1875
- self .exception_long = self .format_exc (err , self .lineno , self .col_offset )
1875
+ if err .filename == self .filename :
1876
+ self .exception_long = self .format_exc (err , self .lineno , self .col_offset )
1877
+ else :
1878
+ self .exception_long = self .format_exc (err , 1 , self .col_offset , code_list = [err .text ])
1876
1879
return False
1877
1880
except asyncio .CancelledError :
1878
1881
raise
@@ -1884,15 +1887,17 @@ def parse(self, code_str, filename=None, mode="exec"):
1884
1887
self .exception_long = self .format_exc (err )
1885
1888
return False
1886
1889
1887
- def format_exc (self , exc , lineno = None , col_offset = None , short = False ):
1890
+ def format_exc (self , exc , lineno = None , col_offset = None , short = False , code_list = None ):
1888
1891
"""Format an multi-line exception message using lineno if available."""
1892
+ if code_list is None :
1893
+ code_list = self .code_list
1889
1894
if lineno is not None :
1890
1895
if short :
1891
1896
mesg = f"In <{ self .filename } > line { lineno } :\n "
1892
- mesg += " " + self . code_list [lineno - 1 ]
1897
+ mesg += " " + code_list [lineno - 1 ]
1893
1898
else :
1894
1899
mesg = f"Exception in <{ self .filename } > line { lineno } :\n "
1895
- mesg += " " + self . code_list [lineno - 1 ] + "\n "
1900
+ mesg += " " + code_list [lineno - 1 ] + "\n "
1896
1901
if col_offset is not None :
1897
1902
mesg += " " + " " * col_offset + "^\n "
1898
1903
mesg += f"{ type (exc ).__name__ } : { exc } "
0 commit comments