mirror of
https://github.com/cool-RR/PySnooper.git
synced 2026-07-18 00:45:19 +00:00
Merge 70f69a5cb7 into 4be5c156ff
This commit is contained in:
commit
575e78426f
6 changed files with 187 additions and 1 deletions
BIN
.DS_Store
vendored
Normal file
BIN
.DS_Store
vendored
Normal file
Binary file not shown.
|
|
@ -435,10 +435,13 @@ class Tracer:
|
|||
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:
|
||||
|
|
|
|||
0
tests/test_depth_inf/__init__.py
Normal file
0
tests/test_depth_inf/__init__.py
Normal file
13
tests/test_depth_inf/factorial.py
Normal file
13
tests/test_depth_inf/factorial.py
Normal file
|
|
@ -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
tests/test_depth_inf/test_depth_inf.py
Normal file
60
tests/test_depth_inf/test_depth_inf.py
Normal file
|
|
@ -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()
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
110
tests/test_infinite_depth_support.py
Normal file
110
tests/test_infinite_depth_support.py
Normal file
|
|
@ -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,
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue