@@ -200,12 +200,30 @@ def __print_traceback__(typ, value, tb):
200
200
print (msg , file = stderr , end = "" )
201
201
return
202
202
try :
203
+ # CPython's C traceback printer diverges from traceback.print_exception in some details marked as (*)
204
+ import sys
203
205
import traceback
204
- lines = traceback .format_exception (typ , value , tb )
205
- # CPython's C traceback printer diverges from traceback.print_exception in this small detail.
206
- # We'd like to contribute to CPython to fix the divergence, but for now we do just
207
- # a string substitution to pass the tests
206
+ no_traceback = False
207
+ limit = getattr (sys , 'tracebacklimit' , None )
208
+ if isinstance (limit , int ):
209
+ if limit <= 0 :
210
+ # (*) the C traceback printer does nothing if the limit is <= 0,
211
+ # but the exception message is still printed
212
+ limit = 0
213
+ no_traceback = True
214
+ else :
215
+ # (*) in the C printer limit is interpreted as -limit in format_exception
216
+ limit = - limit
217
+ else :
218
+ # (*) non integer values of limit are interpreted as the default limit
219
+ limit = None
220
+ lines = traceback .format_exception (typ , value , tb , limit = limit )
221
+ # (*) if the exception cannot be printed, then the message differs between the C driver and format_exception
222
+ # We'd like to contribute to CPython to fix the divergence, but for now we do just a string substitution
223
+ # to pass the tests
208
224
lines [- 1 ] = lines [- 1 ].replace (f'<unprintable { typ .__name__ } object>' , f'<exception str() failed>' )
225
+ if no_traceback :
226
+ lines = lines [- 1 :]
209
227
for line in lines :
210
228
print (line , file = stderr , end = "" )
211
229
except BaseException as exc :
0 commit comments