使用Pdb模块:breakpoint()方法
程序当中,至少要有一个breakpoint()
在你需要的打断点的位置,加上:
breakpoint()
查看调用栈:w | where
- 显示的是从上到下的
- 路径/文件.py(
括号里是行号)
(Pdb) w
d:\administrator\projects\pdb_test.py(12)<module>()
-> func_obj = func1()
> d:\administrator\projects\pdb_test.py(4)func1()
-> def inner(value):
'''
> 仅右箭头:表示当前frame
'''
切换帧(调整当前帧frame):u、d
- u | up
- d | down
(Pdb) u
> d:\administrator\projects\pdb_test.py(12)<module>()
-> func_obj = func1()
(Pdb) w
> d:\administrator\projects\pdb_test.py(12)<module>()
-> func_obj = func1()
d:\administrator\projects\pdb_test.py(4)func1()
-> def inner(value):
查看断点附近代码:l、ll
- l | lst:默认显示附近11行
- l . :回到当前行
- ll | longlst:查看当前函数全部代码
(Pdb) l
1 def func1():
2 data = []
3 breakpoint()
4 -> def inner(value):
5 data.append(value)
6 # 注意这里返回了外层变量,func1()(v)==>得到就是data对象
7 return data
8
9 return inner
10
11
(Pdb) l
12 func_obj = func1()
13
14 print(func_obj.__closure__)
15 #①
16 print(func_obj(1))
17
18 #②
19 # func1()(2)
20
21 print(hex(id(func_obj(3))))
[EOF]
(Pdb) ll
1 def func1():
2 data = []
3 breakpoint()
4 -> def inner(value):
5 data.append(value)
6 # 注意这里返回了外层变量,func1()(v)==>得到就是data对象
7 return data
8
9 return inner
(Pdb) l
[EOF]
(Pdb) l .
'''同最上面'''
打印当前变量:p | print 变量
(Pdb) func_obj(10)
[1, 3, 1, 1, 10]
(Pdb) p func_obj(10)
[1, 3, 1, 1, 10, 10]
跳到下一行(跳过函数调用):n | next
- 遇到函数调用会直接调用完
(Pdb) n
> d:\administrator\projects\pdb_test.py(9)func1()
-> return inner
(Pdb) inner
<function func1.<locals>.inner at 0x0000023A2A92CC10>
跳到下一步(进入函数内部):s | step
(Pdb) s
(<cell at 0x0000024D9992B3D0: list object at 0x0000024D99926040>,)
> d:\administrator\projects\pdb_test.py(16)<module>()
-> print(func_obj(1))
(Pdb) s
--Call-- #进行函数调用了
> d:\administrator\projects\pdb_test.py(4)inner()
-> def inner(value):
(Pdb) w
d:\administrator\projects\pdb_test.py(16)<module>()
-> print(func_obj(1))
> d:\administrator\projects\pdb_test.py(4)inner() #调用栈变成三层
-> def inner(value):
。。。
(Pdb) s
--Return-- #提示函数即将返回
> d:\administrator\projects\pdb_test.py(7)inner()->[1]
-> return data
(Pdb) retval #————可以拿到这个返回值
[1]
跳到下一个断点(无则结束):c | continue
跳到某一行前:until 行号
跳到函数返回前:r | return
- 会停在函数return前
- 可以查看返回值:retval
(Pdb) r
--Return-- # 提示即将返回
> d:\administrator\projects\pdb_test.py(9)func1()-><function fun...002BA80B4CC10>
-> return inner
(Pdb) ll
1 def func1():
2 data = []
3 breakpoint()
4 def inner(value):
5 data.append(value)
6 # 注意这里返回了外层变量,func1()(v)==>得到就是data对象
7 return data
8
9 -> return inner
(Pdb) retval
<function func1.<locals>.inner at 0x000002BA80B4CC10>
添加新断点、列出断点:b | break <行号>
- b | break 后面什么都不加:列出下面还有的断点
(Pdb) b 12
Breakpoint 1 at d:\administrator\projects\pdb_test.py:12
(Pdb) b
Num Type Disp Enb Where
1 breakpoint keep yes at d:\administrator\projects\pdb_test.py:12
高级:debug库(源代码)
如果引用了别的库,想调试进入
方式一:在引用前加入断点
breakpoint()
f = inspect.currentframe()
- 方式二:b + 引用(类似import)
(Pdb) b inspect.currentframe
Breakpoint 1 at c:\develop\python39\lib\inspect.py:1546
(Pdb) b
Num Type Disp Enb Where
1 breakpoint keep yes at c:\develop\python39\lib\inspect.py:1546
(Pdb) b inspect.currentframe
Breakpoint 1 at c:\develop\python39\lib\inspect.py:1546
(Pdb) b
Num Type Disp Enb Where
1 breakpoint keep yes at c:\develop\python39\lib\inspect.py:1546
删除断点:clear <断点编号>
- clear 后不加:删除所有断点