-
Notifications
You must be signed in to change notification settings - Fork 951
/
pysnooper.py
76 lines (53 loc) · 1.91 KB
/
pysnooper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Copyright 2019 Ram Rachum.
# This program is distributed under the MIT license.
import sys
import os
import inspect
import types
import datetime as datetime_module
import re
import collections
import decorator
from . import utils
from . import pycompat
from .tracer import Tracer
def get_write_function(output):
if output is None:
def write(s):
stderr = sys.stderr
stderr.write(s)
elif isinstance(output, (pycompat.PathLike, str)):
def write(s):
with open(output_path, 'a') as output_file:
output_file.write(s)
else:
assert isinstance(output, utils.WritableStream)
def write(s):
output.write(s)
return write
def snoop(output=None, variables=(), depth=1, prefix=''):
'''
Snoop on the function, writing everything it's doing to stderr.
This is useful for debugging.
When you decorate a function with `@pysnooper.snoop()`, you'll get a log of
every line that ran in the function and a play-by-play of every local
variable that changed.
If stderr is not easily accessible for you, you can redirect the output to
a file::
@pysnooper.snoop('/my/log/file.log')
See values of some variables that aren't local variables::
@pysnooper.snoop(variables=('foo.bar', 'self.whatever'))
Show snoop lines for functions that your function calls::
@pysnooper.snoop(depth=2)
Start all snoop lines with a prefix, to grep for them easily::
@pysnooper.snoop(prefix='ZZZ ')
'''
write = get_write_function(output)
@decorator.decorator
def decorate(function, *args, **kwargs):
target_code_object = function.__code__
with Tracer(target_code_object=target_code_object,
write=write, variables=variables,
depth=depth, prefix=prefix):
return function(*args, **kwargs)
return decorate