Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for infinite depth tracing and fix pytest warning #235

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
7 changes: 5 additions & 2 deletions pysnooper/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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: ##########################
Expand Down
Empty file.
13 changes: 13 additions & 0 deletions tests/test_depth_inf/factorial.py
Original file line number Diff line number Diff line change
@@ -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))

60 changes: 60 additions & 0 deletions tests/test_depth_inf/test_depth_inf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# -*- coding: utf-8 -*-
# Copyright 2023 Elijah Qi and Liuqing Yang.

This comment was marked as resolved.

# This program is distributed under the MIT license.

import io

This comment was marked as off-topic.

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()
)
)


110 changes: 110 additions & 0 deletions tests/test_infinite_depth_support.py
Original file line number Diff line number Diff line change
@@ -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,
)