diff --git a/CHANGELOG.md b/CHANGELOG.md index 40907151c87c1a510380d090635a5e4ef80f1310..9adf348dbf7f72c52da0300777572c1841026eeb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # v2.26.6 -* _TBA_ +* Traceback() may accept an Exception instance # v2.26.5 diff --git a/satella/__init__.py b/satella/__init__.py index 0e99c2295e2dcceca70ffa51fb6d9b9a50b7ae3b..75230b982b5e4bc4d22f7d79b0f6056e3f4953ec 100644 --- a/satella/__init__.py +++ b/satella/__init__.py @@ -1 +1 @@ -__version__ = '2.26.6a1' +__version__ = '2.26.6' diff --git a/satella/instrumentation/trace_back/trace_back.py b/satella/instrumentation/trace_back/trace_back.py index 758d5c8f7f95e951d862f2936c0967f5e537fa3a..f57498de1db0212e1082e3876e5ed8adfdcfa401 100644 --- a/satella/instrumentation/trace_back/trace_back.py +++ b/satella/instrumentation/trace_back/trace_back.py @@ -53,7 +53,7 @@ class Traceback(JSONAble): out.append(str(frame)) return ''.join(out) - def __init__(self, starting_frame: tp.Optional[types.FrameType] = None, + def __init__(self, starting_frame: tp.Optional[tp.Union[Exception, types.FrameType]] = None, policy=GenerationPolicy): if inspect.isclass(policy): value_pickling_policy = policy() @@ -69,7 +69,10 @@ class Traceback(JSONAble): else: f = inspect.currentframe() else: - f = starting_frame + if isinstance(starting_frame, Exception): + f = starting_frame.__traceback__.tb_frame + else: + f = starting_frame while f: self.frames.append(StackFrame(f, value_pickling_policy)) diff --git a/tests/test_instrumentation/test_trace_back.py b/tests/test_instrumentation/test_trace_back.py index 36bf8e57d0664a4880d7b32992c7171c21aa70f2..53d231bcfbb7b31a230fd505cd544e079577c865 100644 --- a/tests/test_instrumentation/test_trace_back.py +++ b/tests/test_instrumentation/test_trace_back.py @@ -7,6 +7,14 @@ from satella.instrumentation import Traceback, get_current_traceback class TestTraceback(unittest.TestCase): + + def test_traceback_from_exception(self): + try: + a = 1 / 0 + except ZeroDivisionError as e: + tb = Traceback(e) + self.assertIn('ZeroDivisionError', tb.pretty_format()) + def test_get_current_traceback(self): try: a = 1 / 0