There seems to be no straight-forward way to print the full stack-trace of the exception occurred when using Python context manager.
For example:
class SomeContext():
def __enter__(self):
pass
def __exit__(self, exc_type, exc_value, exc_traceback):
print(exc_type)
print(exc_value)
print(exc_traceback)
with SomeContext():
raise Exception('Oh no')
While the exc_type
and exc_value
are returned as expected, exc_traceback
only gives a vague value (e.g: <traceback object at 0x7ff05251d840>
).
When I try printing exc_traceback.__dict__
, it gives: AttributeError: 'traceback' object has no attribute '__dict__'
I know for sure, that I can wrap everything called within the created context with a try ... except ...
to print the stack-trace, but it wastes time and I don't want to do it every time I use this context manager. Besides, why Python even gives the exception value and type with __exit__
in the first place, but omits the real stack trace?
This question is not a duplicate of How to catch and print the full exception traceback without halting/exiting the program?, my question regards how to "bubble the traceback" within a created context.