-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebugging.py
82 lines (61 loc) · 2.62 KB
/
debugging.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
75
76
77
78
79
80
81
82
# This file is part of ClIDE
# ClIDE - A live-modifiable command-line IDE that can accept commands as pseudocode
# @author Andrew Phillips
# @copyright 2017 Andrew Phillips <[email protected]>
# ClIDE is free software; you can redistribute it and/or
# modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
# License as published by the Free Software Foundation; either
# version 3 of the License, or any later version.
# ClIDE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU AFFERO GENERAL PUBLIC LICENSE for more details.
# You should have received a copy of the GNU Affero General Public
# License along with ClIDE. If not, see <http://www.gnu.org/licenses/>.
'''
'''
import trace
import linecache
class Trace(trace.Trace):
watchlist = {}
def add_watch(self, *vars):
for var in vars:
if not var in self.watchlist: self.watchlist[var] = None
print('Added "%s" to the watchlist' % var)
return
def pop_watch(self, var):
if var in self.watchlist: print('Removed "%s" from the watchlist' % var)
return self.watchlist.pop(var, None)
def get_watchlist(self):
list_ = [var for var in self.watchlist]
print('Watchlist contains: %s' % ' '.join(list_))
return list_
def del_watchlist(self):
self.watchlist = {}
return
def __init__(self, **kwargs):
super().__init__(**kwargs)
def check_watchlist(self, frame):
filename = frame.f_code.co_filename
lineno = frame.f_lineno
line = linecache.getline(filename, lineno)
funcname = frame.f_code.co_name
if self.watchlist:
frame_vars = frame.f_locals
for var in self.watchlist:
func = '*'
if '-' in var:
# separate specific function
func = var.split('-')[0]
var = var.split('-')[1]
if var in line and var in frame_vars and (func == funcname or func == '*'):
# crude way to find if a var is about to be accessed
print(' -- in %s, %s = "%s"' % (funcname, var, str(frame_vars[var])))
def localtrace_trace_and_count(self, frame, why, arg):
result = super().localtrace_trace_and_count(frame, why, arg)
self.check_watchlist(frame)
return result
def localtrace_trace(self, frame, why, arg):
result = super().localtrace_trace(frame, why, arg)
self.check_watchlist(frame)
return result