Question: It seems that in Python (3.10) an exception that is raised inside a try contains a traceback that does not extend to the calling location of the try. This is somewhat surprising to me, and more importantly, not what I want. Here's a short program that illustrates the problem: # short_tb.py import traceback def caller(): somefunction() def somefunction(): try: raise ValueError("This is a value error") except ValueError as e: # in my actual code, e is passed to a function, and __traceback__ is pulled from it for logging at this other # location; the code below simply demonstrates the lack of frames in the traceback object print("".join(traceback.format_tb(e.__traceback__))) print("This will produce a traceback with only one frame, the one in somefunction()") caller() def caller2(): somefunction2() def somefunction2(): raise ValueError("This is a value error") print("This will produce a traceback with all relevant frames (the default behavior of the python interpreter)") caller2()  output: This will produce a traceback with only one frame, the one in somefunction() File ".../short_tb.py", line 10, in somefunction raise ValueError("This is a value error") This will produce a traceback with all relevant frames (the default behavior of the python interpreter) Traceback (most recent call last): File ".../short_tb.py", line 30, in <module> caller2() File ".../short_tb.py", line 22, in caller2 somefunction2() File ".../short_tb.py", line 26, in somefunction2 raise ValueError("This is a value error") ValueError: This is a value error  what I want is for __traceback__ to contain all the information in the second example. I'm happy to overwrite the variable and I can do so at the exception-handling location... but how do I get an object to use for that purpose? in this question there are many answers about tracebacks, but none of them seem to be about traceback objects: Catch and print full Python exception traceback without halting/exiting the program

Answer: A more correct handling is already suggested by @Mark Tolonen, but if it's really necessary for __traceback__ to contain a full stack:  Use tb_frame.f_back to access the previous frames. Reconstruct traceback objects using types.TracebackType. Link them with tb_next constructor argument.  import traceback import types def caller(full = False): somefunction(full) def somefunction(full): try: raise ValueError("This is a value error") except ValueError as e: if full: complete_traceback(e) print("".join(traceback.format_tb(e.__traceback__))) def complete_traceback(exception): t = exception.__traceback__ fb = t.tb_frame.f_back while fb: t = types.TracebackType(tb_next=t, tb_frame=fb, tb_lasti=fb.f_lasti, tb_lineno=fb.f_lineno) fb = t.tb_frame.f_back exception.__traceback__ = t print("This will produce a traceback with only one frame, the one in somefunction()") caller() print("This will produce a traceback with all relevant frames") caller(True)  Output: This will produce a traceback with only one frame, the one in somefunction() File ".../short_tb.py", line 10, in somefunction raise ValueError("This is a value error") This will produce a traceback with all relevant frames File ".../short_tb.py", line 32, in <module> caller(True) File ".../short_tb.py", line 6, in caller somefunction(full) File ".../short_tb.py", line 10, in somefunction raise ValueError("This is a value error")