Exception with original traceback - 2.6-3.X compatible version
Say I have a context manager like this - which works in Python 2.X and
preserves traceback on exit.
class MyContextManager(object):
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
raise AssertionError("Failed :-/"), None, traceback
In Python 3, the raise is a syntax error, but I think you can just set the
__traceback__ parameter.
def __exit__(self, exc_type, exc_value, traceback):
e = AssertionError("Failed :-/")
e.__traceback__ = traceback
raise e
Is there a way to preserve traceback that's compatible with both Python 2
and Python 3 (i.e., doesn't generate syntax errors on either)? I'm
somewhat stuck at this point. It needs to work in 2.6, 2.7, 3.2 and 3.3.
The goal would be to make sure that the user still sees the earlier
traceback.
No comments:
Post a Comment