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

Feature request: support recursion, including with depth argument #10

Closed
zengxiaofei opened this issue Apr 22, 2019 · 2 comments
Closed

Comments

@zengxiaofei
Copy link

For example:

from __future__ import print_function
import pysnooper

@pysnooper.snoop()
def fact(n):
    if n == 1:
        return 1
    return n * fact(n - 1)

def main():
    print(fact(3))

if __name__ == '__main__':
    main()

Output:

$ python mytest2.py 
Starting var:.. n = 3
01:22:43.094346 call         4 @pysnooper.snoop()
01:22:43.095271 line         6     if n == 1:
01:22:43.095435 line         8     return n * fact(n - 1)
Starting var:.. n = 2
01:22:43.095591 call         4 @pysnooper.snoop()
01:22:43.095664 line         6     if n == 1:
01:22:43.095722 line         8     return n * fact(n - 1)
Starting var:.. n = 1
01:22:43.095830 call         4 @pysnooper.snoop()
01:22:43.095884 line         6     if n == 1:
01:22:43.095941 line         7         return 1
01:22:43.096022 return       7         return 1
01:22:43.096110 return       8     return n * fact(n - 1)
01:22:43.096175 return       8     return n * fact(n - 1)
6

When setting depth=2, it even raised an error:

Starting var:.. n = 3
01:26:47.652684 call         4 @pysnooper.snoop(depth=2)
01:26:47.653612 line         6     if n == 1:
01:26:47.653684 line         8     return n * fact(n - 1)
    Starting var:.. n = 2
Traceback (most recent call last):
  File "mytest2.py", line 14, in <module>
    main()
  File "mytest2.py", line 11, in main
    print(fact(3))
  File "</home/xiaofei/.local/lib/python2.7/site-packages/decorator.pyc:decorator-gen-2>", line 2, in fact
  File "/home/xiaofei/.local/lib/python2.7/site-packages/pysnooper/pysnooper.py", line 72, in decorate
    return function(*args, **kwargs)
  File "mytest2.py", line 8, in fact
    return n * fact(n - 1)
  File "</home/xiaofei/.local/lib/python2.7/site-packages/decorator.pyc:decorator-gen-2>", line 1, in fact
  File "/home/xiaofei/.local/lib/python2.7/site-packages/pysnooper/tracer.py", line 182, in trace
    source_line = get_source_from_frame(frame)[frame.f_lineno - 1]
  File "/home/xiaofei/.local/lib/python2.7/site-packages/pysnooper/tracer.py", line 75, in get_source_from_frame
    raise NotImplementedError
NotImplementedError
@cool-RR cool-RR changed the title "depth" cannot deal with recursive function Feature request: support recursion, including with depth argument Apr 22, 2019
@cool-RR
Copy link
Owner

cool-RR commented Apr 22, 2019

Good feature request. Feel free to submit a pull request.

@alexmojaki
Copy link
Collaborator

This works now. I'm guessing the error was essentially the same issue encountered in #109 and fixed in #112. Making the output look a little nicer is being worked on in #108, although I don't think it needs to be merged to count this as fixed.

@cool-RR cool-RR closed this as completed May 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants