国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python pdb調(diào)試方法分享

2019-11-25 18:33:30
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

import pdb

def pdb_test(arg):
    for i in range(arg):
        print(i)
    return arg

pdb.run("pdb_test(3)")
 

 b 函數(shù)名、行號:

打斷點(diǎn),b可以查詢所有的斷點(diǎn)。

復(fù)制代碼 代碼如下:

(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)。

復(fù)制代碼 代碼如下:

(Pdb) c
> c:/users/plpcc/desktop/pdbtest.py(4)pdb_test()
-> for i in range(arg):

   l:

     查看斷點(diǎn)周圍的代碼

復(fù)制代碼 代碼如下:

(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ù)

復(fù)制代碼 代碼如下:

(Pdb) a
arg = 3

 s, n:


    單步運(yùn)行,區(qū)別s會(huì)進(jìn)入路徑中的函數(shù),n不會(huì)進(jìn)入

 p:

    查看表達(dá)式的值

復(fù)制代碼 代碼如下:

(Pdb) p i

 condition:
 

條件斷點(diǎn),只有條件為true斷點(diǎn)才命中

復(fù)制代碼 代碼如下:

> 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)用堆棧

復(fù)制代碼 代碼如下:

(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ù)返回
復(fù)制代碼 代碼如下:

(Pdb) r

--Return--
> c:/users/plpcc/desktop/pdbtest.py(6)pdb_test()->3 //代碼位置、函數(shù)返回值->3
-> return arg                                       //代碼位置的語句
(Pdb) l
    import pdb

    def pdb_test(arg):
        for i in range(arg):
            print(i)
 ->      return arg

    pdb.run("pdb_test(3)")

通過pdb.set_trace() 在代碼中指定位置嵌入一個(gè)斷點(diǎn),通常可以通過調(diào)試開關(guān)來控制

復(fù)制代碼 代碼如下:

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)行

復(fù)制代碼 代碼如下:

> 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]

通過pdb.pm()進(jìn)行事后調(diào)試,可以跟蹤異常程序最后的堆載信息:

復(fù)制代碼 代碼如下:

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)

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宁陕县| 宣城市| 太白县| 清苑县| 永靖县| 苍溪县| 高淳县| 社旗县| 团风县| 星子县| 敦化市| 亳州市| 张家界市| 湖口县| 泸州市| 扶风县| 新邵县| 安达市| 敦化市| 蓬安县| 龙胜| 金华市| 张家界市| 同心县| 伊金霍洛旗| 桓台县| 长治市| 西林县| 新竹市| 长子县| 游戏| 屏东县| 陇南市| 井陉县| 曲麻莱县| 雅江县| 山西省| 吉水县| 昆明市| 大关县| 林州市|