I have several context managers for the project I'm working on. He is about to send, and I am faced with what I'm starting to panic about.
I got the impression that you should not reraise the exceptions passed as arguments to the __exit__ method of the context manager class. However, I was preparing some tests, and it seemed that the context manager was suppressing the exception that throws it. When I changed the __exit__ methods to look like this:
def __exit__(self, type_, value, trace):
if trace is not None:
print('ERROR IN TRACEBACK: ' + str(value))
raise type_, value, trace
the errors seemed to go right.
My question is: how to handle exceptions in the __exit__ method correctly if type_, value and trace are not None? Is it wrong to raise (re-raise?) Such an exception? So should I do this?
The error I encountered may be caused by something else. Usually I went through and thoroughly tested all this, but my time seems very limited at the moment. I hope someone can explain the correct implementation of this function and
In the end: Can I safely leave the raise_ method, value, trace in my __exit__ context manager?
source
share