清单 7. 使用函数的例子
import pdb
def combine(s1,s2): # define subrouTIne combine, which.。。
s3 = s1 + s2 + s1 # sandwiches s2 between copies of s1, 。。。
s3 = ‘“’ + s3 +‘”’ # encloses it in double quotes,。。。
return s3 # and returns it.
a = “aaa”
pdb.set_trace()
b = “bbb”
c = “ccc”
final = combine(a,b)
print final
如果直接使用 n 进行 debug 则到 final=combine(a,b) 这句的时候会将其当做普通的赋值语句处理,进入到 print final。如果想要对函数进行 debug 如何处理呢 ? 可以直接使用 s 进入函数块。函数里面的单步调试与上面的介绍类似。如果不想在函数里单步调试可以在断点处直接按 r 退出到调用的地方。
清单 8. 对函数进行 debug
[root@rcc-pok-idg-2255 ~]# python epdb2.py
》 /root/epdb2.py(10)?()
-》 b = “bbb”
(Pdb) n
》 /root/epdb2.py(11)?()
-》 c = “ccc”
(Pdb) n
》 /root/epdb2.py(12)?()
-》 final = combine(a,b)
(Pdb) s
--Call--
》 /root/epdb2.py(3)combine()
-》 def combine(s1,s2): # define subroutine combine, which.。。
(Pdb) n
》 /root/epdb2.py(4)combine()
-》 s3 = s1 + s2 + s1 # sandwiches s2 between copies of s1, 。。。
(Pdb) list
import pdb
def combine(s1,s2): # define subroutine combine, which.。。
-》 s3 = s1 + s2 + s1 # sandwiches s2 between copies of s1, 。。。
s3 = ‘“’ + s3 +‘”’ # encloses it in double quotes,。。。
return s3 # and returns it.
a = “aaa”
pdb.set_trace()
b = “bbb”
c = “ccc”
(Pdb) n
》 /root/epdb2.py(5)combine()
-》 s3 = ‘“’ + s3 +‘”’ # encloses it in double quotes,。。。
(Pdb) n
》 /root/epdb2.py(6)combine()
-》 return s3 # and returns it.
(Pdb) n
--Return--
》 /root/epdb2.py(6)combine()-》‘“aaabbbaaa”’
-》 return s3 # and returns it.
(Pdb) n
》 /root/epdb2.py(13)?()
-》 print final
(Pdb)
在调试的时候动态改变值 。在调试的时候可以动态改变变量的值,具体如下实例。需要注意的是下面有个错误,原因是 b 已经被赋值了,如果想重新改变 b 的赋值,则应该使用! B。
清单 9. 在调试的时候动态改变值
[root@rcc-pok-idg-2255 ~]# python epdb2.py
》 /root/epdb2.py(10)?()
-》 b = “bbb”
(Pdb) var = “1234”
(Pdb) b = “avfe”
*** The specified object ‘= “avfe”’ is not a function
or was not found along sys.path.
(Pdb) !b=“afdfd”
(Pdb)
pdb 调试有个明显的缺陷就是对于多线程,远程调试等支持得不够好,同时没有较为直观的界面显示,不太适合大型的 python 项目。而在较大的 python 项目中,这些调试需求比较常见,因此需要使用更为高级的调试工具。