❘
257
a
main()
function. Essentially, you encase the code in Listing 12-1 in the
main()
function and then
call it using the following code:
# Create an entry point for debugging.
if __name__ == “__main__“:
main()
Using the debugger is very much like old-style DOS debuggers such as the Debug utility. You issue
commands and the debugger responds without output based on the application environment and
variable content. The lack of a visual display may prove troublesome to developers who have never
used a character-mode debugger, but
pdb
is actually more effective than any of the graphical alter-
natives in helping you locate problems with your application — at least, in the Python code. Use
these steps to start the
pdb
:
1.
Start the IronPython console by selecting it from the Start menu or typing
IPY
at the
command line.
2.
Type
import pdb
and press Enter to import the Python debugger.
3.
Type
import
ApplicationName
where
ApplicationName
is the name of the file that
contains your application and press Enter. For example, if your application appears in
ShowFilters2.py
, then you’d type
import ShowFilters2
(without the file extension)
and press Enter.
4.
Type
pdb.run(‘
ApplicationName
.
FunctionName
()‘)
where
ApplicationName
is the
name of the application and
FunctionName
is the name of the function you want to test, and
press Enter. For example, if your application is named
ShowFilters2
and the function you
want to test is
main()
, you’d type
pdb.run(‘ShowFilters2.main()‘)
and press Enter. The
standard console prompt changes to a
pdb
prompt, as shown in Figure 12-9.
FIgURE 12-9:
The Python debugger uses a special pdb prompt where you can enter debugging commands.
Now that you have a debugger prompt, you can begin debugging your application. Here is a list of
standard debugger commands you can issue:
➤➤
a or args: Displays the list of arguments supplied to the current function. If there aren’t any
arguments, the call simply returns without displaying anything.
➤
➤
alias: Creates an alias for a complex command. For example, you might need to use a
for
loop to drill down into a
list
to see its contents. You could use an alias to create a com-
mand to perform that task without having to write the complete code every time. An alias
can include replaceable variables, just as you would use for a batch file.
548592c12.indd 257
2/24/10 12:48:48 PM
www.finebook.ir
Do'stlaringiz bilan baham: |