From 34b2d7e0e39213222acd54c617eaa1491ca50690 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl>
Date: Sat, 7 Dec 2024 17:48:06 +0100
Subject: [PATCH] Traceback() may accept an Exception instance

---
 CHANGELOG.md                                     | 2 +-
 satella/__init__.py                              | 2 +-
 satella/instrumentation/trace_back/trace_back.py | 7 +++++--
 tests/test_instrumentation/test_trace_back.py    | 8 ++++++++
 4 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 40907151..9adf348d 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 0e99c229..75230b98 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 758d5c8f..f57498de 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 36bf8e57..53d231bc 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
-- 
GitLab