@@ -64,7 +64,7 @@ def runsource(self, source, filename="<input>", symbol="single"):
6464 code = self .compile (source , filename , symbol )
6565 except (OverflowError , SyntaxError , ValueError ):
6666 # Case 1
67- self .showsyntaxerror (filename )
67+ self .showsyntaxerror (filename , source = source )
6868 return False
6969
7070 if code is None :
@@ -94,7 +94,7 @@ def runcode(self, code):
9494 except :
9595 self .showtraceback ()
9696
97- def showsyntaxerror (self , filename = None ):
97+ def showsyntaxerror (self , filename = None , ** kwargs ):
9898 """Display the syntax error that just occurred.
9999
100100 This doesn't display a stack trace because there isn't one.
@@ -118,7 +118,8 @@ def showsyntaxerror(self, filename=None):
118118 else :
119119 # Stuff in the right filename
120120 value = SyntaxError (msg , (filename , lineno , offset , line ))
121- self ._showtraceback (typ , value , None )
121+ source = kwargs .pop ('source' , "" )
122+ self ._showtraceback (typ , value , None , source )
122123 finally :
123124 typ = value = tb = None
124125
@@ -132,14 +133,20 @@ def showtraceback(self):
132133 """
133134 try :
134135 typ , value , tb = sys .exc_info ()
135- self ._showtraceback (typ , value , tb .tb_next )
136+ self ._showtraceback (typ , value , tb .tb_next , "" )
136137 finally :
137138 typ = value = tb = None
138139
139- def _showtraceback (self , typ , value , tb ):
140+ def _showtraceback (self , typ , value , tb , source ):
140141 sys .last_type = typ
141142 sys .last_traceback = tb
142- sys .last_exc = sys .last_value = value = value .with_traceback (tb )
143+ value = value .with_traceback (tb )
144+ # Set the line of text that the exception refers to
145+ lines = source .splitlines ()
146+ if (source and typ is SyntaxError
147+ and not value .text and len (lines ) >= value .lineno ):
148+ value .text = lines [value .lineno - 1 ]
149+ sys .last_exc = sys .last_value = value
143150 if sys .excepthook is sys .__excepthook__ :
144151 self ._excepthook (typ , value , tb )
145152 else :
0 commit comments