From f1194be092cb224a4b1d43e12b9323230dc0cbe3 Mon Sep 17 00:00:00 2001 From: Yael Mintz Date: Sun, 13 Sep 2020 15:33:51 +0300 Subject: [PATCH] fix #195 Fix '_thread._local' object has no attribute 'depth' raised by snooper if there's exception upon calling snooped func by initializing thread_global dict with depth when entering the trace() func --- pysnooper/tracer.py | 2 +- tests/test_pysnooper.py | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/pysnooper/tracer.py b/pysnooper/tracer.py index b9d2300..56165d3 100644 --- a/pysnooper/tracer.py +++ b/pysnooper/tracer.py @@ -293,6 +293,7 @@ class Tracer: def __enter__(self): if DISABLED: return + thread_global.__dict__.setdefault('depth', -1) calling_frame = inspect.currentframe().f_back if not self._is_internal_frame(calling_frame): calling_frame.f_trace = self.trace @@ -362,7 +363,6 @@ class Tracer: else: return None - thread_global.__dict__.setdefault('depth', -1) if event == 'call': thread_global.depth += 1 indent = ' ' * 4 * thread_global.depth diff --git a/tests/test_pysnooper.py b/tests/test_pysnooper.py index fdf650c..882b5ea 100644 --- a/tests/test_pysnooper.py +++ b/tests/test_pysnooper.py @@ -1894,5 +1894,10 @@ def test_exception(): ) +def test_exception_on_entry(): + @pysnooper.snoop() + def f(x): + pass - + with pytest.raises(TypeError): + f()