python pdb調(diào)試方法分享
import pdb
def pdb_test(arg):
for i in range(arg):
print(i)
return arg
pdb.run("pdb_test(3)")
b 函數(shù)名、行號(hào):
打斷點(diǎn),b可以查詢(xún)所有的斷點(diǎn)。
(Pdb) b pdb_test
Breakpoint 1 at c:\users\plpcc\desktop\pdbtest.py:3
(Pdb) b
Num Type Disp Enb Where
breakpoint keep yes at c:\users\plpcc\desktop\pdbtest.py:3
c:
運(yùn)行程序,直到遇到斷點(diǎn)。
(Pdb) c
> c:\users\plpcc\desktop\pdbtest.py(4)pdb_test()
-> for i in range(arg):
l:
查看斷點(diǎn)周?chē)拇a
(Pdb) l
import pdb
B def pdb_test(arg):
-> for i in range(arg):
print(i)
return arg
pdb.run("pdb_test(3)")
a:
查看參數(shù)
(Pdb) a
arg = 3
s, n:
單步運(yùn)行,區(qū)別s會(huì)進(jìn)入路徑中的函數(shù),n不會(huì)進(jìn)入
p:
查看表達(dá)式的值
(Pdb) p i
condition:
條件斷點(diǎn),只有條件為true斷點(diǎn)才命中
> c:\users\plpcc\desktop\pdbtest.py(5)pdb_test()
-> print(i)
(Pdb) l
import pdb
def pdb_test(arg):
for i in range(arg):
B-> print(i)
return arg
pdb.run("pdb_test(3)")
[EOF]
(Pdb) b
Num Type Disp Enb Where
breakpoint keep yes at c:\users\plpcc\desktop\pdbtest.py:5
(Pdb) condition 2 i==1 //i==1時(shí)才觸發(fā)斷點(diǎn)2
New condition set for breakpoint 2.
(Pdb) b
Num Type Disp Enb Where
breakpoint keep yes at c:\users\plpcc\desktop\pdbtest.py:5
stop only if i==1
(Pdb) c
//i==0直接打印未斷住
> c:\users\plpcc\desktop\pdbtest.py(5)pdb_test()
-> print(i) //觸發(fā)斷點(diǎn),i==1
(Pdb) p i
bt:
查看調(diào)用堆棧
(Pdb) bt
c:\python33\lib\bdb.py(405)run()
-> exec(cmd, globals, locals)
<string>(1)<module>()
> c:\users\plpcc\desktop\pdbtest.py(5)pdb_test()
-> print(i)
r:
執(zhí)行到函數(shù)返回
(Pdb) r
--Return--
> c:\users\plpcc\desktop\pdbtest.py(6)pdb_test()->3 //代碼位置、函數(shù)返回值->3
-> return arg //代碼位置的語(yǔ)句
(Pdb) l
import pdb
def pdb_test(arg):
for i in range(arg):
print(i)
-> return arg
pdb.run("pdb_test(3)")
通過(guò)pdb.set_trace() 在代碼中指定位置嵌入一個(gè)斷點(diǎn),通??梢酝ㄟ^(guò)調(diào)試開(kāi)關(guān)來(lái)控制
import pdb
__DEBUG__ = True
def pdb_test(arg):
if True == __DEBUG__:
pdb.set_trace()
for i in range(arg):
print(i)
return arg
pdb_test(3)
運(yùn)行后在pdb.set_trace()位置被斷住,當(dāng)__DEBUG__ = False,代碼正常運(yùn)行
> c:\users\plpcc\desktop\pdbtest.py(8)pdb_test()
-> for i in range(arg):
(Pdb) l
__DEBUG__ = True
def pdb_test(arg):
if True == __DEBUG__:
pdb.set_trace()
-> for i in range(arg):
print(i)
return arg
pdb_test(3)
[EOF]
通過(guò)pdb.pm()進(jìn)行事后調(diào)試,可以跟蹤異常程序最后的堆載信息:
Traceback (most recent call last):
File "C:\Users\plpcc\Desktop\pdbTest.py", line 13, in <module>
pdb_test(3)
File "C:\Users\plpcc\Desktop\pdbTest.py", line 10, in pdb_test
1/0
ZeroDivisionError: division by zero
>>> import pdb
>>> pdb.pm()
> c:\users\plpcc\desktop\pdbtest.py(10)pdb_test()
-> 1/0
(Pdb) l
def pdb_test(arg):
if True == __DEBUG__:
pdb.set_trace()
for i in range(arg):
print(i)
-> 1/0
return arg
pdb_test(3)
相關(guān)文章
Python使用captcha庫(kù)制作帶參數(shù)輸入驗(yàn)證碼案例
這篇文章主要介紹了Python使用captcha庫(kù)制作驗(yàn)證碼,帶參數(shù)輸入,本文通過(guò)實(shí)例案例解析給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
Python?調(diào)用GPT-3?API實(shí)現(xiàn)過(guò)程詳解
這篇文章主要為大家介紹了Python?調(diào)用GPT-3?API實(shí)現(xiàn)過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Python類(lèi)的常用高級(jí)函數(shù)匯總
這篇文章主要介紹了Python類(lèi)的常用高級(jí)函數(shù)匯總,文章圍繞python類(lèi)函數(shù)展開(kāi)詳細(xì)內(nèi)容,具有一的的參考價(jià)值,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助2022-03-03
使用Python的Flask框架來(lái)搭建第一個(gè)Web應(yīng)用程序
Flask框架是一個(gè)以輕量級(jí)著稱(chēng)的Web開(kāi)發(fā)框架,近兩年來(lái)在Web領(lǐng)域獲得了極高的人氣,這里我們就來(lái)看如何使用Python的Flask框架來(lái)搭建第一個(gè)Web應(yīng)用程序2016-06-06
python3 寫(xiě)一個(gè)WAV音頻文件播放器的代碼
本文通過(guò)實(shí)例代碼給大家介紹了python3 寫(xiě)一個(gè)WAV音頻文件播放器,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
嘗試用最短的Python代碼來(lái)實(shí)現(xiàn)服務(wù)器和代理服務(wù)器
"人生苦短、我用Python",Python最大的特點(diǎn)便是代碼簡(jiǎn)潔,得益于Python自帶的wsgiref包和socket模塊,這里我們將嘗試用最短的Python代碼來(lái)實(shí)現(xiàn)服務(wù)器和代理服務(wù)器:2016-06-06

