cool-RR / PySnooper

Never use print for debugging again
MIT License
16.35k stars 951 forks source link

Decorator library adds spurious lines #109

Closed alexmojaki closed 5 years ago

alexmojaki commented 5 years ago

Example script:

import pysnooper

@pysnooper.snoop(depth=2)
def factorial(x):
    if x <= 1:
        return 1
    return factorial(x - 1)

factorial(2)

Output:

Starting var:.. x = 2
18:12:31.470110 call         5 def factorial(x):
18:12:31.470305 line         6     if x <= 1:
18:12:31.470348 line         8     return factorial(x - 1)
    Starting var:.. x = 1
    18:12:31.470411 call         1 SOURCE IS UNAVAILABLE
    18:12:31.470513 line         2 SOURCE IS UNAVAILABLE
Starting var:.. x = 1
18:12:31.470591 call         5 def factorial(x):
18:12:31.470634 line         6     if x <= 1:
18:12:31.470671 line         7         return 1
18:12:31.470706 return       7         return 1
Return value:.. 1
    18:12:31.470774 return       2 SOURCE IS UNAVAILABLE
    Return value:.. 1
18:12:31.470824 return       8     return factorial(x - 1)
Return value:.. 1

When it tries to read the source it gets an error:

[Errno 2] No such file or directory: '<decorator-gen-1>'

Obviously not being able to read the source is bad, but even if it could, it looks like we would get various unwanted lines, as any wrapper function is going to pollute the stack.

cool-RR commented 5 years ago

Do you prefer that we switch to functools.wraps and get rid of the decorator module?

alexmojaki commented 5 years ago

Yes. I haven't looked into the decorator module, but if all it does is preserve signatures, that doesn't seem very useful for this library.

cool-RR commented 5 years ago

Okay, feel free to remove it from the repo and switch to functools.wraps. Yay, one less bundled third-party module!

alexmojaki commented 5 years ago

Fixed in #112