Python PDB summary

Source: Internet
Author: User

The debug feature is very important for developer, and Python provides the appropriate module PDB so that you can debug with a text editor to write scripts. The PDB is the abbreviation for Python debugger.
Some of the commonly used commands are:
Command use
Break or B to set a breakpoint
Continue or C continue to execute the program
List or L view the code snippet for the current row
Step or S entry function
Return or R executes the code until it returns from the current function
Exit or Q Abort and exit
Next or N executes the next line
Value of the PP print variable
Help
Start by describing how to use the PDB.
Test code used by 1:epdb1.py
Import PDB
A = "AAA"
Pdb.set_trace ()
b = "BBB"
c = "CCC"
Final = a + B + C
Print final
About Set_trace ()
Pdb.set_trace ()
Enter the debugger at the calling stack frame. This was useful to Hard-code abreakpoint at a given point in a program, even if the code was not otherwisebeing debugged (E. G. When an assertion fails).
1 Start Debugging:
[email protected] ~]# python epdb1.py
>/root/epdb1.py (4)? ()
b = "BBB"
(PDB) n
>/root/epdb1.py (5)? ()
c = "CCC"
(PDB)
>/root/epdb1.py (6)? ()
Final = a + B + C
(PDB) List
1 Import PDB
2 A = "AAA"
3 Pdb.set_trace ()
4 B = "BBB"
5 c = "CCC"
6-final = a + B + C
7 Print Final
[EOF]
(PDB)
[EOF]
(PDB) n
>/root/epdb1.py (7)? ()
Print final
(PDB)
Using N+enter to perform the current statement, you can press ENTER to repeat the previous debug command after the first time you press the N+enter.
If you press ENTER without entering anything, the PDB would re-execute the last command that you gave it.

Quit or Q can exit the current debug, but quit quits the program in a very rude way, directly crash
[[email protected] ~]# python epdb1.py
>/root/ epdb1.py (4)? ()
, B = "BBB"
(PDB) n
>/root/epdb1.py (5)? ()
C = "CCC"
(PDB) Q
Traceback (most recent call last):
File "epdb1.py", line 5, in?
C = "CCC"
F Ile "Epdb1.py", line 5, in?
C = "CCC"
File "/usr/lib64/python2.4/bdb.py", line $, in Trace_dispatch
return Self.dispatch_line (frame)
File "/usr/lib64/python2.4/bdb.py", line, Dispatch_line
if Self.quitting:raise bdbquit
BdB. Bdbquit


In the process of printing the value of the variable, you can directly use p plus the variable name, but it should be noted that printing only after the current statement has been executed to see the specific value, otherwise it will be reported Nameerror: <exceptions. Nameerror. > Error.
[email protected] ~]# python epdb1.py
>/root/epdb1.py (4)? ()
b = "BBB"
(PDB) n
>/root/epdb1.py (5)? ()
c = "CCC"
(PDB) P b
' BBB '
(PDB)
' BBB '
(PDB) n
>/root/epdb1.py (6)? ()
Final = a + B + C
(PDB) P C
' CCC '
(PDB) P final
Nameerror: <exceptions. Nameerror instance at 0x1551b710>
(PDB) n
>/root/epdb1.py (7)? ()
Print final
(PDB) P final
' AAABBBCCC '
(PDB)

Use C to stop the current debug so that the program continues execution. If you continue to have set_statement () in the following program, you will re-enter the state of Debug.
[email protected] ~]# python epdb1.py
>/root/epdb1.py (4)? ()
b = "BBB"
(PDB) n
>/root/epdb1.py (5)? ()
c = "CCC"
(PDB) C
Aaabbbccc

You can add set_trace () validation before the code print final.

If the code process does not necessarily remember that the current code is fast at debug time, it can be displayed by using the list or the L command. List will point to the current debug statement with arrows
[email protected] ~]# python epdb1.py
>/root/epdb1.py (4)? ()
b = "BBB"
(PDB) List
1 Import PDB
2 A = "AAA"
3 Pdb.set_trace ()
4-B = "BBB"
5 c = "CCC"
6 final = a + B + C
7 Pdb.set_trace ()
8 Print Final
[EOF]
(PDB) C
>/root/epdb1.py (8)? ()
Print final
(PDB) List
3 Pdb.set_trace ()
4 B = "BBB"
5 c = "CCC"
6 final = a + B + C
7 Pdb.set_trace ()
Print Final, 8
[EOF]
(PDB)
For debug with a function:
epdb2.py--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

If you use N for debug, it will be treated as a normal assignment statement when you go to Final=combine, and you will get to print final. What if you want to debug a function? You can use S to enter function blocks directly.
[email protected] ~]# 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
1 Import PDB
2
3 def Combine (S1,S2): # define subroutine Combine, which ...
4-s3 = S1 + s2 + S1 # Sandwiches S2 between copies of S1, ...
5 s3 = ' "' + s3 + '" ' # encloses it in double quotes,...
6 return S3 # and returns it.
7
8 A = "AAA"
9 Pdb.set_trace ()
Ten 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)

If you do not want to step into the function, you can exit the breakpoint by pressing R directly to the place where you called it.

Dynamically change the value while debugging. Note that there is an error because B is already assigned, and if you want to change the assignment of B, you should use it! B
[email protected] ~]# python epdb2.py
>/root/epdb2.py (10)? ()
b = "BBB"
(Pdb) var = "1234"
(Pdb) b = "Avfe"
The specified object ' = ' Avfe ' is not a function
Or is not found along Sys.path.
(Pdb)!b= "AFDFD"
(PDB)

Post a Good article: http://onlamp.com/pub/a/python/2005/09/01/debugger.html?page=1
Debugger Module Contents
The PDB module contains the debugger. PDB Containsone class, PDB, which inherits from Bdb.bdb. Thedebugger documentation mentions six functions, which create an Interactivedebugging session:
Pdb.run (statement[, globals[, locals])
Pdb.runeval (expression[, globals[, locals])
Pdb.runcall (function[, argument, ...])
Pdb.set_trace ()
Pdb.post_mortem (Traceback)
PDB.PM ()
All six functions provide a slightly different mechanism for dropping a userinto the debugger.
Pdb.run (statement[, globals[, locals])
Pdb.run () Executes the string statement under Thedebugger ' s control. Global and local dictionaries are optional parameters:
#!/usr/bin/env python

Import PDB

def test_debugger (Some_int):
Print "Start some_int>>", Some_int
Return_int = 10/some_int
Print "End some_int>>", Some_int
Return Return_int

if __name__ = = "__main__":
Pdb.run ("Test_debugger (0)")
Pdb.runeval (expression[,globals[, locals]])
Pdb.runeval () is identical to Pdb.run (), Exceptthat pdb.runeval () returns the value of the evaluated stringexpression:
#!/usr/bin/env python

Import PDB

def test_debugger (Some_int):
Print "Start some_int>>", Some_int
Return_int = 10/some_int
Print "End some_int>>", Some_int
Return Return_int

if __name__ = = "__main__":
Pdb.runeval ("Test_debugger (0)")
Pdb.runcall (Function[,argument, ...])
Pdb.runcall () calls the specified function andpasses any specified arguments to it:
#!/usr/bin/env python

Import PDB

def test_debugger (Some_int):
Print "Start some_int>>", Some_int
Return_int = 10/some_int
Print "End some_int>>", Some_int
Return Return_int

if __name__ = = "__main__":
Pdb.runcall (Test_debugger, 0)
Pdb.set_trace ()
Pdb.set_trace () drops the code into the debugger when executionhits it:
#!/usr/bin/env python

Import PDB

def test_debugger (Some_int):
Pdb.set_trace ()
Print "Start some_int>>", Some_int
Return_int = 10/some_int
Print "End some_int>>", Some_int
Return Return_int

if __name__ = = "__main__":
Test_debugger (0)
Pdb.post_mortem (Traceback)
Pdb.post_mortem () performs postmortem debugging of thespecified Traceback:
#!/usr/bin/env python

Import PDB

def test_debugger (Some_int):
Print "Start some_int>>", Some_int
Return_int = 10/some_int
Print "End some_int>>", Some_int
Return Return_int

if __name__ = = "__main__":
Try
Test_debugger (0)
Except
Import Sys
TB = Sys.exc_info () [2]
Pdb.post_mortem (TB)
PDB.PM ()
PDB.PM () performs postmortem debugging of the tracebackcontained in Sys.last_traceback:
#!/usr/bin/env python

Import PDB
Import Sys

def test_debugger (Some_int):
Print "Start some_int>>", Some_int
Return_int = 10/some_int
Print "End some_int>>", Some_int
Return Return_int

def do_debugger (type, value, TB):
PDB.PM ()

if __name__ = = "__main__":
Sys.excepthook = Do_debugger
Test_debugger (0)

Python PDB summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.