cool-RR / PySnooper

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

SOURCE IS UNAVAILABLE #230

Open lixuanhng opened 1 year ago

lixuanhng commented 1 year ago

Hi, I tried the demo but get SOURCE IS UNAVAILABLE instead of code line in output. Any idea how to fix this? Thanks

【code】 import pysnooper

@pysnooper.snoop() def number_to_bits(number): if number: bits = [] while number: number, remainder = divmod(number, 2) bits.insert(0, remainder) return bits else: return [0]

number_to_bits(6)

【output】 Source path:... /tmp/ipykernel_114851/428676888.py Starting var:.. number = 6 11:57:49.141100 call 3 SOURCE IS UNAVAILABLE 11:57:49.141751 line 5 SOURCE IS UNAVAILABLE 11:57:49.141791 line 6 SOURCE IS UNAVAILABLE New var:....... bits = [] 11:57:49.141826 line 7 SOURCE IS UNAVAILABLE 11:57:49.141883 line 8 SOURCE IS UNAVAILABLE Modified var:.. number = 3 New var:....... remainder = 0 11:57:49.141916 line 9 SOURCE IS UNAVAILABLE Modified var:.. bits = [0] 11:57:49.141975 line 7 SOURCE IS UNAVAILABLE 11:57:49.142021 line 8 SOURCE IS UNAVAILABLE Modified var:.. number = 1 Modified var:.. remainder = 1 11:57:49.142054 line 9 SOURCE IS UNAVAILABLE Modified var:.. bits = [1, 0] 11:57:49.142108 line 7 SOURCE IS UNAVAILABLE 11:57:49.142153 line 8 SOURCE IS UNAVAILABLE Modified var:.. number = 0 11:57:49.142183 line 9 SOURCE IS UNAVAILABLE Modified var:.. bits = [1, 1, 0] 11:57:49.142226 line 7 SOURCE IS UNAVAILABLE 11:57:49.142268 line 10 SOURCE IS UNAVAILABLE 11:57:49.142299 return 10 SOURCE IS UNAVAILABLE Return value:.. [1, 1, 0]

cool-RR commented 1 year ago

I'm guessing you didn't put the code in the file, but rather in some shell. I'm guessing that shell isn't IPython. Which shell is it?

guyb2022 commented 1 year ago

I run the script on win10 python 3.11.0 It works correctlly Output: Starting var:.. number = 6 11:50:32.973112 call 4 def number_to_bits(number): 11:50:32.975097 line 5 if number: 11:50:32.976097 line 6 bits = [] New var:....... bits = [] 11:50:32.976097 line 7 while number: 11:50:32.976097 line 8 number, remainder = divmod(number, 2) Modified var:.. number = 3 New var:....... remainder = 0 11:50:32.977117 line 9 bits.insert(0, remainder) Modified var:.. bits = [0] 11:50:32.977117 line 10 return bits 11:50:32.978115 return 10 return bits Return value:.. [0] Elapsed time: 00:00:00.005987

Sionwage commented 9 months ago

I get the same output when using Jupyterlab.

from functools import reduce
import pysnooper

@pysnooper.snoop()
def multi(x,y):
    return x*y

print(reduce(multi,range(1,4)))
multi(5,2)

Output

Source path:... C:\Users\...\AppData\Local\Temp\ipykernel_17756\20432242.py
Starting var:.. x = 1
Starting var:.. y = 2
17:51:19.570314 call         4 SOURCE IS UNAVAILABLE
17:51:19.570314 line         6 SOURCE IS UNAVAILABLE
17:51:19.570314 return       6 SOURCE IS UNAVAILABLE
Return value:.. 2
Elapsed time: 00:00:00.000000
Starting var:.. x = 2
Starting var:.. y = 3
17:51:19.570314 call         4 SOURCE IS UNAVAILABLE
17:51:19.570314 line         6 SOURCE IS UNAVAILABLE
17:51:19.570314 return       6 SOURCE IS UNAVAILABLE
Return value:.. 6
Elapsed time: 00:00:00.000000
Starting var:.. x = 5
Starting var:.. y = 2
17:51:19.570314 call         4 SOURCE IS UNAVAILABLE
17:51:19.570314 line         6 SOURCE IS UNAVAILABLE
17:51:19.570314 return       6 SOURCE IS UNAVAILABLE
Return value:.. 10
Elapsed time: 00:00:00.000000
Sionwage commented 9 months ago

Calling just regular python repl from the command prompt:

Python 3.11.4 (tags/v3.11.4:d2340ef, Jun  7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pysnooper
>>> def multi(x,y):
...     return x*y
...
>>> with pysnooper.snoop()
  File "<stdin>", line 1
    with pysnooper.snoop()
                          ^
SyntaxError: expected ':'
>>> with pysnooper.snoop():
...     multi(5,2)
...
Source path:... <stdin>
New var:....... __name__ = '__main__'
New var:....... __doc__ = None
New var:....... __package__ = None
New var:....... __loader__ = <class '_frozen_importlib.BuiltinImporter'>
New var:....... __spec__ = None
New var:....... __annotations__ = {}
New var:....... __builtins__ = <module 'builtins' (built-in)>
New var:....... pysnooper = <module 'pysnooper' from 'C:\\Users\\....\\Ap....11\\Lib\\site-packages\\pysnooper\\__init__.py'>
New var:....... multi = <function multi at 0x000002049302E980>
18:03:12.070700 line         2 SOURCE IS UNAVAILABLE
10
18:03:12.071645 line         1 SOURCE IS UNAVAILABLE
Elapsed time: 00:00:00.000945
>>>
cool-RR commented 9 months ago

@Sionwage Thanks for reporting. It would be good to be able to get the source in these cases, but I probably won't work on this because it's not important enough for me. If you're interested feel free to dig in and implement it.

Sionwage commented 9 months ago

Ok, I think I found why I get the source is unavailable for me.

I was running this with Jupyterlab in a cell. If I move the function to a '.py' file like 'test.py' with this code:

import pysnooper

@pysnooper.snoop()
def adder(x, y):
    answer = x + y
    return answer

if __name__ == '__main__':
    print(adder(5, 20))

and call the function from Jupyterlab by running this:

import test

test.adder(5, 20)

I get this output that matches the functionality outlined in the readme:

Source path:... C:\Users\...\OneDrive - ...\Documents\Python\temp\test.py
Starting var:.. x = 5
Starting var:.. y = 20
11:24:16.302453 call         4 def adder(x, y):
11:24:16.302453 line         5     answer = x + y
New var:....... answer = 25
11:24:16.302453 line         6     return answer
11:24:16.302453 return       6     return answer
Return value:.. 25
Elapsed time: 00:00:00.000000

25

I think that this cannot be used with its full functionality in Jupyterlab or Ipython unless there is an actual source file for PySnooper to scrape from?

cool-RR commented 9 months ago

I guess so. It's possible it could be implemented, but you'll need to roll up your sleeves for that.

Sionwage commented 9 months ago

I'm just tickled I got this working as it is very handy. This is probably my cue to get out of Jupyter and start using a more mature workflow.