@@ -110,8 +110,8 @@ def print_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None, \
110110 value , tb = _parse_value_tb (exc , value , tb )
111111 if file is None :
112112 file = sys .stderr
113- for line in TracebackException (
114- type ( value ), value , tb , limit = limit ) .format (chain = chain ):
113+ te = TracebackException (type ( value ), value , tb , limit = limit , compact = True )
114+ for line in te .format (chain = chain ):
115115 print (line , file = file , end = "" )
116116
117117
@@ -126,8 +126,8 @@ def format_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None, \
126126 printed as does print_exception().
127127 """
128128 value , tb = _parse_value_tb (exc , value , tb )
129- return list ( TracebackException (
130- type ( value ), value , tb , limit = limit ) .format (chain = chain ))
129+ te = TracebackException (type ( value ), value , tb , limit = limit , compact = True )
130+ return list ( te .format (chain = chain ))
131131
132132
133133def format_exception_only (exc , / , value = _sentinel ):
@@ -146,8 +146,8 @@ def format_exception_only(exc, /, value=_sentinel):
146146 """
147147 if value is _sentinel :
148148 value = exc
149- return list ( TracebackException (
150- type ( value ), value , None ) .format_exception_only ())
149+ te = TracebackException (type ( value ), value , None , compact = True )
150+ return list ( te .format_exception_only ())
151151
152152
153153# -- not official API but folk probably use these two functions.
@@ -476,7 +476,8 @@ class TracebackException:
476476 """
477477
478478 def __init__ (self , exc_type , exc_value , exc_traceback , * , limit = None ,
479- lookup_lines = True , capture_locals = False , _seen = None ):
479+ lookup_lines = True , capture_locals = False , compact = False ,
480+ _seen = None ):
480481 # NB: we need to accept exc_traceback, exc_value, exc_traceback to
481482 # permit backwards compat with the existing API, otherwise we
482483 # need stub thunk objects just to glue it together.
@@ -485,6 +486,7 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
485486 if _seen is None :
486487 _seen = set ()
487488 _seen .add (id (exc_value ))
489+
488490 # TODO: locals.
489491 self .stack = StackSummary .extract (
490492 walk_tb (exc_traceback ), limit = limit , lookup_lines = lookup_lines ,
@@ -504,7 +506,7 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
504506 if lookup_lines :
505507 self ._load_lines ()
506508 self .__suppress_context__ = \
507- exc_value .__suppress_context__ if exc_value else False
509+ exc_value .__suppress_context__ if exc_value is not None else False
508510
509511 # Convert __cause__ and __context__ to `TracebackExceptions`s, use a
510512 # queue to avoid recursion (only the top-level call gets _seen == None)
@@ -524,8 +526,13 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
524526 _seen = _seen )
525527 else :
526528 cause = None
529+
530+ if compact :
531+ need_context = cause is None and not e .__suppress_context__
532+ else :
533+ need_context = True
527534 if (e and e .__context__ is not None
528- and id (e .__context__ ) not in _seen ):
535+ and need_context and id (e .__context__ ) not in _seen ):
529536 context = TracebackException (
530537 type (e .__context__ ),
531538 e .__context__ ,
0 commit comments