-
Notifications
You must be signed in to change notification settings - Fork 0
/
final-1.py
141 lines (126 loc) · 3.9 KB
/
final-1.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python
# Simple debugger
# See instructions around lines 36
import sys
import readline
# Our buggy program
def remove_html_markup(s):
tag = False
quote = False
out = ""
for c in s:
if c == '<' and not quote:
tag = True
elif c == '>' and not quote:
tag = False
elif c == '"' or c == "'" and tag:
quote = not quote
elif not tag:
out = out + c
return out
# main program that runs the buggy program
def main():
print remove_html_markup('xyz')
print remove_html_markup('"<b>foo</b>"')
print remove_html_markup("'<b>foo</b>'")
# globals
breakpoints = {14: True}
watchpoints = {'c': True}
stepping = False
"""
Our debug function
Improve and expand the debug function to accept a new command:
a delete command 'd <type> <argument>', where <type> is either b for breakpoint,
or w for watchpoint. The following argument should be either a number
for the breakpoint or a string for the watchpoint.
If there is mismatch between type and argument, you should print out
"Incorrect command".
In the case of "d b <argument>" you should delete that breakpoint from the
breakpoint dictionary, or print "No such breakpoint defined", repr(argument)
In case of watchpoint, you should delete the watchpoint if such variable exists,
or print: variable, "is not defined as watchpoint"
"""
def debug(command, my_locals):
global stepping
global breakpoints
global watchpoints
if command.find(' ') > 0:
arg = command.split(' ')[1]
else:
arg = None
if command.startswith('s'): # step
stepping = True
return True
elif command.startswith('c'): # continue
stepping = False
return True
elif command.startswith('p'): # print
# PS1 CODE
pass
elif command.startswith('b'): # breakpoint
# PS1 CODE
pass
elif command.startswith('w'): # watch variable
# PS1 CODE
elif command.startswith('d'): # delete watch/break point
# YOUR CODE HERE
commandList = command.split()
if len(commandList) != 3:
print "Incorrect command"
return True
if commandList[1] == 'b':
if not all(['0' <= cc <= '9' for cc in commandList[2]]):
print "Incorrect command"
return True
else:
cc = int(commandList[2])
del(breakpoints[cc])
return True
if commandList[1] == 'w':
if type(commandList[2]) != str:
print "Incorrect command"
return True
else:
del(watchpoints[commandList[2]])
return True
else:
print "Incorrect command"
return True
elif command.startswith('q'): # quit
print "Exiting my-spyder..."
sys.exit(0)
else:
print "No such command", repr(command)
return False
commands = ["w out", "d w out", "w out", "b 12", "b", "d b 14", "b", "q"]
def input_command():
#command = raw_input("(my-spyder) ")
global commands
command = commands.pop(0)
return command
"""
Our traceit function
"""
def traceit(frame, event, trace_arg):
global stepping
if event == 'line':
if stepping or breakpoints.has_key(frame.f_lineno):
resume = False
print event, frame.f_lineno, frame.f_code.co_name, frame.f_locals
while not resume:
command = input_command()
resume = debug(command, frame.f_locals)
return traceit
# Using the tracer
#sys.settrace(traceit)
#main()
#sys.settrace(None)
#Simple test
watchpoints = {'s': True}
print watchpoints
debug("d w s", {'s': 'xyz', 'tag': False})
print watchpoints
breakpoints = { 8: True, 12: True, 20: True}
print breakpoints
debug("d b 12", {'s': 'xyz', 'tag': False})
print breakpoints