@@ -106,72 +106,71 @@ def showsyntaxerror(self, filename=None, **kwargs):
106
106
The output is written by self.write(), below.
107
107
108
108
"""
109
- colorize = kwargs .pop ('colorize' , False )
110
- type , value , tb = sys .exc_info ()
111
- sys .last_exc = value
112
- sys .last_type = type
113
- sys .last_value = value
114
- sys .last_traceback = tb
115
- if filename and type is SyntaxError :
116
- # Work hard to stuff the correct filename in the exception
117
- try :
118
- msg , (dummy_filename , lineno , offset , line ) = value .args
119
- except ValueError :
120
- # Not the format we expect; leave it alone
121
- pass
122
- else :
123
- # Stuff in the right filename
124
- value = SyntaxError (msg , (filename , lineno , offset , line ))
125
- sys .last_exc = sys .last_value = value
126
- # Set the line of text that the exception refers to
127
- source = kwargs .pop ('source' , '' )
128
- lines = source .splitlines ()
129
- if (source and type is SyntaxError
130
- and not value .text and len (lines ) >= value .lineno ):
131
- value .text = lines [value .lineno - 1 ]
132
- if sys .excepthook is sys .__excepthook__ :
133
- lines = traceback .format_exception_only (type , value , colorize = colorize )
134
- self .write ('' .join (lines ))
135
- else :
136
- # If someone has set sys.excepthook, we let that take precedence
137
- # over self.write
138
- self ._call_excepthook (type , value , tb )
109
+ try :
110
+ typ , value , tb = sys .exc_info ()
111
+ if filename and typ is SyntaxError :
112
+ # Work hard to stuff the correct filename in the exception
113
+ try :
114
+ msg , (dummy_filename , lineno , offset , line ) = value .args
115
+ except ValueError :
116
+ # Not the format we expect; leave it alone
117
+ pass
118
+ else :
119
+ # Stuff in the right filename
120
+ value = SyntaxError (msg , (filename , lineno , offset , line ))
121
+ source = kwargs .pop ('source' , "" )
122
+ self ._showtraceback (typ , value , None , source )
123
+ finally :
124
+ typ = value = tb = None
139
125
140
- def showtraceback (self , ** kwargs ):
126
+ def showtraceback (self ):
141
127
"""Display the exception that just occurred.
142
128
143
129
We remove the first stack item because it is our own code.
144
130
145
131
The output is written by self.write(), below.
146
132
147
133
"""
148
- colorize = kwargs .pop ('colorize' , False )
149
- sys .last_type , sys .last_value , last_tb = ei = sys .exc_info ()
150
- sys .last_traceback = last_tb
151
- sys .last_exc = ei [1 ]
152
134
try :
153
- if sys .excepthook is sys .__excepthook__ :
154
- lines = traceback .format_exception (ei [0 ], ei [1 ], last_tb .tb_next , colorize = colorize )
155
- self .write ('' .join (lines ))
156
- else :
157
- # If someone has set sys.excepthook, we let that take precedence
158
- # over self.write
159
- self ._call_excepthook (ei [0 ], ei [1 ], last_tb )
135
+ typ , value , tb = sys .exc_info ()
136
+ self ._showtraceback (typ , value , tb .tb_next , "" )
160
137
finally :
161
- last_tb = ei = None
138
+ typ = value = tb = None
162
139
163
- def _call_excepthook (self , typ , value , tb ):
164
- try :
165
- sys .excepthook (typ , value , tb )
166
- except SystemExit :
167
- raise
168
- except BaseException as e :
169
- e .__context__ = None
170
- print ('Error in sys.excepthook:' , file = sys .stderr )
171
- sys .__excepthook__ (type (e ), e , e .__traceback__ .tb_next )
172
- print (file = sys .stderr )
173
- print ('Original exception was:' , file = sys .stderr )
174
- sys .__excepthook__ (typ , value , tb )
140
+
141
+ def _showtraceback (self , typ , value , tb , source ):
142
+ sys .last_type = typ
143
+ sys .last_traceback = tb
144
+ value = value .with_traceback (tb )
145
+ # Set the line of text that the exception refers to
146
+ lines = source .splitlines ()
147
+ if (source and typ is SyntaxError
148
+ and not value .text and len (lines ) >= value .lineno ):
149
+ value .text = lines [value .lineno - 1 ]
150
+ sys .last_exc = sys .last_value = value
151
+ if sys .excepthook is sys .__excepthook__ :
152
+ self ._excepthook (typ , value , tb )
153
+ else :
154
+ # If someone has set sys.excepthook, we let that take precedence
155
+ # over self.write
156
+ try :
157
+ sys .excepthook (typ , value , tb )
158
+ except SystemExit :
159
+ raise
160
+ except BaseException as e :
161
+ e .__context__ = None
162
+ e = e .with_traceback (e .__traceback__ .tb_next )
163
+ print ('Error in sys.excepthook:' , file = sys .stderr )
164
+ sys .__excepthook__ (type (e ), e , e .__traceback__ )
165
+ print (file = sys .stderr )
166
+ print ('Original exception was:' , file = sys .stderr )
167
+ sys .__excepthook__ (typ , value , tb )
168
+
169
+ def _excepthook (self , typ , value , tb ):
170
+ # This method is being overwritten in
171
+ # _pyrepl.console.InteractiveColoredConsole
172
+ lines = traceback .format_exception (typ , value , tb )
173
+ self .write ('' .join (lines ))
175
174
176
175
def write (self , data ):
177
176
"""Write a string.
0 commit comments