3

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.


Update (2023.09.12):

My temporal solution is:

def __exit__(self, exc_type, exc_value, exc_traceback):
    if exc_type is not None:
        raise exc_type(exc_value).with_traceback(exc_traceback)

Sometimes I still can't get the full stack trace (e.g: with Pydantic ValidationError, so I have to replace exc_type with a native exception such as RuntimeError). But I'm working on it.

7
  • 1
    It's not a bot, I closed it. What exactly does "bubble the traceback" mean? Your current code is trying to print it, which both the linked posts do cover (please read the answers, not just the title). "why Python... omits the real stack trace" - it doesn't, the traceback object is the "real stack trace".
    – jonrsharpe
    Jan 16, 2023 at 10:34
  • 2
    @jonrsharpe it’s not a duplicate because it’s asking about using a context manager which none of the answers on that post mention.
    – Dadsdy
    Jun 18, 2023 at 3:21
  • @Dadsdy so what? It's not clear why you or the OP think that matters at all; the same approaches shown in the linked duplicates work just fine in __exit__. In fact, as the OP's __exit__ implementation doesn't return a truth-y value, the full traceback is already shown.
    – jonrsharpe
    Jun 18, 2023 at 8:40
  • @jonrsharpe Because I am trying to make a context manager that intercepts each of stdin, stdout, and stderr and writes everything writen to them to a file (for stdout and stderr) and reads everything read from them from a file (for stdin), and my prior approach (setting sys.stderr equal to something didn't work (it worked for stdin and stdout)), so using the __exit__ params seemed my only choice. However, I wasn't able to get the text of the traceback.
    – Dadsdy
    Jun 18, 2023 at 18:11
  • @Dadsdy the linked duplicates do show how to get the formatted traceback text from exactly the values that __exit__ receives, at which point you can send it wherever you like, so it's not clear what the problem is.
    – jonrsharpe
    Jun 18, 2023 at 20:21

0

Browse other questions tagged or ask your own question.