diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..835a6b7 Binary files /dev/null and b/.DS_Store differ diff --git a/pysnooper/tracer.py b/pysnooper/tracer.py index 0f35500..64e2d4b 100644 --- a/pysnooper/tracer.py +++ b/pysnooper/tracer.py @@ -403,10 +403,13 @@ def trace(self, frame, event, arg): return None else: _frame_candidate = frame - for i in range(1, self.depth): + depth_iterator = itertools.count(1) if (self.depth == float('inf')) else range(1, self.depth) + for i in depth_iterator: _frame_candidate = _frame_candidate.f_back if _frame_candidate is None: return None + elif self._is_internal_frame(_frame_candidate): + return None elif _frame_candidate.f_code in self.target_codes or _frame_candidate in self.target_frames: break else: @@ -467,7 +470,7 @@ def trace(self, frame, event, arg): "thread_info") current_thread = threading.current_thread() thread_info = "{ident}-{name} ".format( - ident=current_thread.ident, name=current_thread.getName()) + ident=current_thread.ident, name=current_thread.name) thread_info = self.set_thread_info_padding(thread_info) ### Reporting newish and modified variables: ########################## diff --git a/tests/test_depth_inf/__init__.py b/tests/test_depth_inf/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_depth_inf/factorial.py b/tests/test_depth_inf/factorial.py new file mode 100644 index 0000000..e7539d3 --- /dev/null +++ b/tests/test_depth_inf/factorial.py @@ -0,0 +1,13 @@ +#!/usr/bin/python3 +import pysnooper +# test recursion +@pysnooper.snoop(depth=float("inf"), color = False) +def factorial(x): + """This is a recursive function + to find the factorial of an integer""" + + if x == 1: + return 1 + else: + return (x * factorial(x-1)) + diff --git a/tests/test_depth_inf/test_depth_inf.py b/tests/test_depth_inf/test_depth_inf.py new file mode 100644 index 0000000..7f30d2a --- /dev/null +++ b/tests/test_depth_inf/test_depth_inf.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- +# Copyright 2023 Elijah Qi and Liuqing Yang. +# This program is distributed under the MIT license. + +import io +import textwrap +import threading +import types +import os +import sys + +from pysnooper.utils import truncate +import pytest + +import pysnooper +from pysnooper.variables import needs_parentheses +from ..utils import (assert_output, assert_sample_output, VariableEntry, + CallEntry, LineEntry, ReturnEntry, OpcodeEntry, + ReturnValueEntry, ExceptionEntry, ExceptionValueEntry, + SourcePathEntry, CallEndedByExceptionEntry, + ElapsedTimeEntry) +from .. import mini_toolbox +from . import factorial + + +def test_multiple_files(): + with mini_toolbox.OutputCapturer(stdout=False, + stderr=True) as output_capturer: + result = factorial.factorial(3) + assert result == 6 + output = output_capturer.string_io.getvalue() + assert_output( + output, + ( + SourcePathEntry(source_path_regex=r'.*factorial\.py$'), + VariableEntry('x', '3'), + CallEntry(), + LineEntry(), + LineEntry(), + VariableEntry('x', '2'), + CallEntry(), + LineEntry(), + LineEntry(), + VariableEntry('x', '1'), + CallEntry(), + LineEntry(), + LineEntry(), + ReturnEntry(), + ReturnValueEntry('1'), + ElapsedTimeEntry(), + ReturnEntry(), + ReturnValueEntry('2'), + ElapsedTimeEntry(), + ReturnEntry(), + ReturnValueEntry('6'), + ElapsedTimeEntry() + ) + ) + + diff --git a/tests/test_infinite_depth_support.py b/tests/test_infinite_depth_support.py new file mode 100644 index 0000000..b5e0148 --- /dev/null +++ b/tests/test_infinite_depth_support.py @@ -0,0 +1,110 @@ +# -*- coding: utf-8 -*- +# Copyright 2023 Elijah Qi and Liuqing Yang. +# This program is distributed under the MIT license. + +import io +import textwrap +import threading +import types +import sys + +from pysnooper.utils import truncate +import pytest + +import pysnooper +from pysnooper import pycompat +from pysnooper.variables import needs_parentheses +from .utils import (assert_output, assert_sample_output, VariableEntry, + CallEntry, LineEntry, ReturnEntry, OpcodeEntry, + ReturnValueEntry, ExceptionEntry, ExceptionValueEntry, + SourcePathEntry, CallEndedByExceptionEntry, + ElapsedTimeEntry) +from . import mini_toolbox + +@pytest.mark.parametrize("normalize", (True, False)) +def test_infinite_depth_support(normalize): + string_io = io.StringIO() + + def func1(x): + return x + + def func(x): + func1(x) + + def foo(x): + func(x) + + def recursive_function(x): + if x == 0: + return 1 + + foo(x) + + recursive_function(x - 1) + + with pysnooper.snoop(string_io, depth=float('inf'), normalize=normalize, color=False): + recursive_function(1) + + output = string_io.getvalue() + + assert_output( + output, + ( + SourcePathEntry(), + VariableEntry(), + VariableEntry(), + VariableEntry(), + VariableEntry(), + VariableEntry(), + VariableEntry(), + LineEntry("recursive_function(1)"), + VariableEntry("x", "1"), + VariableEntry("foo"), + VariableEntry("recursive_function"), + + CallEntry("def recursive_function(x):"), + LineEntry("if x == 0:"), + LineEntry("foo(x)"), + + VariableEntry("x", "1"), + VariableEntry("func"), + CallEntry("def foo(x):"), + LineEntry("func(x)"), + + VariableEntry("x", "1"), + VariableEntry("func1"), + CallEntry("def func(x):"), + LineEntry("func1(x)"), + + VariableEntry("x", "1"), + CallEntry("def func1(x):"), + LineEntry("return x"), + ReturnEntry(), + ReturnValueEntry("1"), + + ReturnEntry(), + ReturnValueEntry("None"), + + ReturnEntry(), + ReturnValueEntry("None"), + + LineEntry("recursive_function(x - 1)"), + + VariableEntry("x", "0"), + VariableEntry("foo"), + VariableEntry("recursive_function"), + + CallEntry("def recursive_function(x):"), + LineEntry("if x == 0:"), + LineEntry("return 1"), + ReturnEntry(), + ReturnValueEntry("1"), + + ReturnEntry(), + ReturnValueEntry("None"), + + LineEntry("with pysnooper.snoop(string_io, depth=float('inf'), normalize=normalize, color=False):"), + ElapsedTimeEntry(), + ), + normalize=normalize, + )