To debug a Python program instance with a PDB module

Source: Internet
Author: User
Tags exit define function square root in python

This article mainly introduces the use of PDB modules to debug Python program instances, this article focuses on the Pdb.run () function, Pdb.runeval () function, Pdb.runcall () function, Pdb.set_trace () The use of functions and the PDB debugging commands and so on content, the need for friends can refer to the following

In Python, syntax errors can be found by the Python interpreter, but logic errors or variable usage errors are not easy to find, and if the results do not match expectations, you need to debug, a good debugging tool: Python's own PDB module. The PDB is a debugging module with Python. Use the PDB module to set breakpoints for a script, step through, view variable values, and so on.

The PDB can be started by using command-line arguments, or it can be imported and reused using import.

The code is as follows:

>>> dir (PDB)

[' Pdb ', ' repr ', ' restart ', ' testCMD ',....., ' re ', ' Run ', ' Runcall ', ' runctx ', ' runeval ', ' set_trace ', ' sys ', ' Test ', ' Trac ' Eback ']

Common PDB functions have the following:

"Pdb.run () function"

>>> This function is mainly used for debugging statement blocks

The basic usage of >>> is as follows

The code is as follows:

>>> Help (Pdb.run)

Help on function run in module PDB:

Run (statement, Globals=none, Locals=none)

>>> parameter meaning

Statement: The block of statements to be debugged, expressed as a string

Globals: Optional parameter, SET statement run Global environment variable

Locals: Optional parameters, setting the local environment variable for the statement run

>>> Simple Example

The code is as follows:

>>> Import PDB # importing Debug module

>>> Pdb.run ("") Call the Run () function to perform a For loop

For I in range (3):

I *= 3

Print (i)

''')

> (2) ()

(PDB) n # (PDB) is a debug command prompt indicating that you can enter a debug command

> (3) ()

(PDB) n # N (next) indicates execution of the next line

> (4) ()

(Pdb) print (i) # Prints the value of the variable i

0

(PDB) Continue # Continue to run the program

0

3

6

"Pdb.runeval () function"

>>> This function is primarily used for debugging expressions

The basic usage of >>> is as follows

The code is as follows:

>>> Help (Pdb.runeval)

Help on function runeval in module PDB:

Runeval (expression, Globals=none, Locals=none)

>>> parameter meaning

Expression: To debug,

Globals: Optional parameter, SET statement run Global environment variable

Locals: Optional parameters, setting the local environment variable for the statement run

>>> Simple Example

The code is as follows:

>>> Imports PDB # import PDB module

>>> LST = [1, 2, 3] # define a list

>>> pdb.runeval (' lst[1] ') # calls the Runaval () function to debug an expression lst[1]

> (1) ()

(PDB) n # enters the debug state, using the n command, stepping

--return--

> (1) ()->2

(PDB) N # Stepping

2 # Returns the value of an expression

>>> Pdb.runeval (' 3 + 5*6/2 ') # Use the Runaval () function to debug an expression 3+5*6/2

> (1) ()->2

(PDB) n

--return--

> (1) ()->18

(PDB) n # using the n command to step

18 # The value of the expression is finally drawn

"Pdb.runcall () function"

>>> This function is mainly used for debugging functions

The basic usage of >>> is as follows

The code is as follows:

>>> Help (Pdb.runcall)

Help on function Runcall in module PDB:

Runcall (*args, **kwds)

>>> parameter meaning

Function: Name of function

Args (Kwds): Parameters of a function

>>> Simple Example

The code is as follows:

>>> Import PDB # Imports Module

>>> def sum (*args): # define function sum, find all parameters

res = 0

For Arg in args:

res = arg

return res

>>> pdb.runcall (SUM, 1, 2, 3, 4) # using the Runcall debug function sum

>

(PDB) n # enters debug state, stepping

>

(PDB) N # Stepping

>

(Pdb) print (res) prints the value of res with print

0

(PDB) Continue # continues to execute

10

>>> pdb.runcall (SUM, 1, 2, 3, 4, 5, 6) # Call the Runcall debug function sum, with different parameters

>

(PDB) Continue # continues to execute

21 # Function finally returns the result

"Pdb.set_trace () function"

>>> This function is primarily used to set hard breakpoints in scripts

The basic usage of >>> is as follows

The code is as follows:

>>> Help (Pdb.set_trace)

Help on function set_trace in module PDB:

Set_trace ()

>>> Simple Example

The code is as follows:

# file:test.py

Import PDB

Pdb.set_trace ()

For I in range (5):

I *= 5

Print (i)

After running the script, it displays:

The code is as follows:

> d:learnpythontest.py (6) ()

-> for I in range (5):

(PDB) List # Use list to list script content

1 # file:test.py

2

3 Import PDB

4

5 pdb.set_trace () # Use Set_trace () to set a hard breakpoint

6-> for I in range (5):

7 I *= 5

8 Print (i)

[EOF] # List The end of the script content

(PDB) Continue # Use continue to continue execution

0

5

10

15

20

"PDB debug Command"

Debug commands in the PDB can complete stepping, printing variable values, setting breakpoints, and so on. The PDB main commands are as follows

The code is as follows:

------------------------------------------------------------------------------

# Complete Command Shorthand command description

------------------------------------------------------------------------------

# args A to print the parameters of the current function

# Break B Set Breakpoints

# Clear CL Clears breakpoints

# Condition no set conditional breakpoint

# Continue C continues to run until a breakpoint is encountered or the script ends

# Disable no disabled breakpoints

# Enable no breakpoint enabled

# help H View PDB assistance

# Ignore No Ignore breakpoint

# Jump J jumps to the specified number of rows run

# list L List script list

# Next N executes the following statement and encounters a function that does not enter its internal

# print p Printing variable values

# Quit Q Exit PDB

# return R runs consistently to function returns

# Tbreak No temporary breakpoint set, breakpoint only interrupted once

# step S executes the next statement, encounters a function into its internal

# where W to see where

# ! No statements are executed in the PDB

>>> Simple Example

The code is as follows:

#-*-CODING:GBK-*-

# file:prime.py

#

Import Math

# IsPrime function to determine whether an integer is a prime

# If I can be divisible by any number within the square root of 2 to I,

# Then I am not prime, return 0, otherwise I is prime, return 1.

def isprime (i):

For T in range (2, int (math.sqrt (i)) + 1):

If I% t = 0:

return 0

Print (the prime number between ' 100~110 has: ')

For I in range (100, 110):

If IsPrime (i):

Print (i)

Run the following command first:

The code is as follows:

D:LEARNPYTHON>PYTHON-M PDB prime.py

And then enter the following command:

The code is as follows:

D:LEARNPYTHON>PYTHON-M PDB prime.py

> d:learnpythonprime.py (4) ()

-> Import Math

(PDB) List # After running the previous command, stop here, list defaults to list only 11 lines

1 #-*-CODING:GBK-*-

2 # file:prime.py

3 #

4-> Import Math

5 # IsPrime function to determine whether an integer is a prime

6 # If I can be divisible by any number within the square root of 2 to I,

7 # Then I am not prime, return 0, otherwise I is prime, return 1.

8 def isprime (i):

9 for T in range (2, int (math.sqrt (i)) + 1):

Ten if I% = = 0:

return 0

(Pdb) L 14,17 # using the list command, list 14 lines, to 17 lines

Print (' prime number between ' 100~110: ')

For I in range (100, 110):

If IsPrime (i):

Print (i)

(Pdb) b 14 # Use the break command to set breakpoints

Breakpoint 1 at D:learnpythonprime.py:14 # return breakpoint Number: 1

(Pdb) b isprime # Set breakpoints in function IsPrime

Breakpoint 2 at D:learnpythonprime.py:8 # return breakpoint Number: 2

(PDB) C # run script with c command

> d:learnpythonprime.py () # Stop at Breakpoint 1, line 14th

-> print (prime between ' 100~110: ')

(PDB) C # continue to run scripts using the C command

Prime between 100~110: # line 14th script output

> d:learnpythonprime.py (9) IsPrime () # Stop at Breakpoint 2, IsPrime function

-> for T in range (2, int (math.sqrt (i)) + 1):

(Pdb) b 15 # Set a breakpoint at line 15th

Breakpoint 3 at D:learnpythonprime.py:15

(Pdb) Disable 2 # Disables breakpoint 2, which is the breakpoint at the IsPrime function

(PDB) C # continue to run scripts using the C command

> d:learnpythonprime.py () # Stop at Breakpoint 3, line 15th

-> for I in range (100, 110):

(Pdb) print (i) # Prints the value of the variable I with print

100

(PDB) C # continue to run the script

> d:learnpythonprime.py ( )

-> for I in range (100, 110):

(PDB) P I # Prints the value of I

101

(PDB) Enable 2 # Restore Breakpoint 2, that is, the breakpoint at the IsPrime function

(PDB) C # continue to run the script

> d:learnpythonprime.py (9) IsPrime ()

-> for T in range (2, int (math.sqrt (i)) + 1):

(PDB) n # Stepping to the next statement

> d:learnpythonprime.py (isprime) ()

-> if I% t = 0:

(Pdb) print (t) # Prints the value of the variable T with print

2

(PDB) CL # Clear All breakpoints, enter Y confirmation

Clear all breaks? Y

(PDB) C # continue to run the script

103

105

107

109

(PDB) Q # Use Quit (q) to exit Pdb debugging

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.