def traceit(frame, event, trace_arg):
global stepping
if event == 'line':
if stepping or frame.f_lineno in breakpoints:
resume = False
while not resume:
print(event, frame.f_lineno, frame.f_code.co_name, frame.f_locals)
command = input_command()
resume = debug(command, frame.f_locals)
return traceit
Run Code Online (Sandbox Code Playgroud)
代码中最后一行的含义是什么?
编辑:
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
def main():
print (remove_html_markup('xyz'))
print (remove_html_markup('"<b>foo</b>"'))
print (remove_html_markup("'<b>foo</b>'"))
# globals
breakpoints = {9: True}
stepping = False
def debug(command, my_locals):
global stepping
global breakpoints
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('q'): # quit
sys.exit(0)
else:
print ("No such command", repr(command))
return False
commands = ['s', 's', 's', 'q']
def input_command():
#command = raw_input("(my-spyder) ")
global commands
command = commands.pop(0)
return command
def traceit(frame, event, trace_arg):
global stepping
if event == 'line':
if stepping or frame.f_lineno in breakpoints:
resume = False
while not resume:
print(event, frame.f_lineno, frame.f_code.co_name, frame.f_locals)
command = input_command()
resume = debug(command, frame.f_locals)
return traceit
# Using the tracer
sys.settrace(traceit)
main()
sys.settrace(None)
Run Code Online (Sandbox Code Playgroud)
Mat*_*lia 17
函数是一个像其他任何人一样的对象,所以返回自己没有问题.例如,它允许在同一行重复调用:
traceit("abc", "def", None)("ghi", "jkl", 3)("mno", "pqr", 4.3)
Run Code Online (Sandbox Code Playgroud)
编辑:sys.settrace设置全局跟踪功能,每次输入本地范围以调用本地跟踪功能时都会调用该功能.这里它返回自己,以处理同一函数中的所有跟踪.
有关详细信息,请参阅https://docs.python.org/2/library/sys.html#sys.settrace.
Fla*_*lau 10
由于Python中的所有函数都是作为对象创建的,因此它返回对函数的引用.
它可以在代码中稍后传递给另一个函数,或者使用任何函数调用参数调用.
def a(str):
print str
b = a # Assign an instance of a to b
b('hello') # Call b as if it were a
print type(b)
Run Code Online (Sandbox Code Playgroud)
打印:
hello
<type 'function'>
Run Code Online (Sandbox Code Playgroud)
https://docs.python.org/2/library/sys.html#sys.settrace
settrace允许您传递函数以用作调试器.每次输入新范围时,都会调用您传递的函数.它需要返回一个应该用于在该范围内进行调试的函数.
由于该代码的编写者希望始终使用相同的函数,因此该函数返回自身.
来自链接的相关位:
每当输入新的本地范围时,都会调用跟踪函数(事件设置为'call'); 它应返回对要使用该范围的本地跟踪函数的引用,如果不应跟踪范围,则返回None.
本地跟踪函数应该返回对自身的引用(或者返回另一个函数以便在该范围内进一步跟踪),或者返回None来关闭该范围内的跟踪.
| 归档时间: |
|
| 查看次数: |
1452 次 |
| 最近记录: |