In article <CnE9H5.DnH@spk.hp.com>, Bill Baker <baker@spk.hp.com> wrote:
>
> Question: How does one single-step a python program (note: this is
> not how do you debug a python statement or group of
> statements but how is an entire program single-stepped)
>
>From pdb.doc:
| s(tep)
| Execute the current line, stop at the first possible occasion
| (either in a function that is called or in the current function).
|
| n(ext)
| Continue execution until the next line in the current function
| is reached or it returns.
| (!) statement
| Execute the (one-line) statement in the context of
| the current stack frame.
| The exclamation point can be omitted unless the first word
| of the statement resembles a debugger command.
| To assign to a global variable you must always prefix the
| command with a 'global' command, e.g.:
| (Pdb) global list_options; list_options = ['-l']
| (Pdb)
Thus you can:
$ python
Python 1.0.1 (Mar 15 1994)
Copyright 1991-1994 Stichting Mathematisch Centrum, Amsterdam
>>> import pdb
>>> pdb.run( 'import fin' )
> <string>(0)
(Pdb) s
> <string>(1)
(Pdb) s
> ./fin.py(0)
(Pdb) s
> ./fin.py(1): import sys
(Pdb) n
> ./fin.py(2): import rand
(Pdb) n
> ./fin.py(4): def func():
[ lines deleted ... Note the 2 occurances of 'def main' - one
is the function being defined, the other is displayed when
main() is actually executed. This is because the debugger gets
the source line from lineno in the source file. The one problem
with pdb is that if you define a function interactively, you
don't get the source line display - you just get "<stdin>" . ]
> ./fin.py(40): def main():
(Pdb) s
> ./fin.py(48): main()
(Pdb) s
> ./fin.py(40)main(): def main():
(Pdb) s
> ./fin.py(41)main(): for i in range(20):
(Pdb) s
> ./fin.py(41)main(): for i in range(20):
(Pdb) s
> ./fin.py(42)main(): try:
(Pdb) s
> ./fin.py(43)main(): test()
(Pdb) s
> ./fin.py(14)test(): def test():
(Pdb) s
> ./fin.py(15)test(): try:
(Pdb) s
> ./fin.py(16)test(): catch_type = "" # assume we catch nothing
(Pdb) !print catch_type # haven't stepped thru that line, so not yet defined
*** NameError: catch_type
(Pdb) s
> ./fin.py(17)test(): problem = 1 # presume an exception will take place
(Pdb) !print catch_type # now it's defined to be the null-string
(Pdb)
>I read the pdb.doc file and have tried several things but there doesn't seem
>to be as easy a method as perl's '-d' comand-line parameter to enter
>debugging mode. The only way I have found is to comment-out mainline code
>and:
>> python
>Python 1.0.1 (26 January 1994)
>Copyright 1991-1994 Stichting Mathematisch Centrum, Amsterdam
>>>> import pdb
>>>> pdb.run('import myprogram')
>> <string>(0)
>(Pdb)
>.
>.
>.
>
>This lets me re-enter the mainline through the keyboard but I _do_ miss
>being able to emulate cdb inside of a running perl program through '-d'.
>I _must_ be missing something!
>
But I'm not sure if you mean something else there. ( I'm not much of a
Perl hacker, and I've only used debug mode to force Perl to be
interactive, which is already the default for Python. )
If you just mean being able to do it from the command line, you can:
$ python -c 'import pdb; pdb.run( "import myprogram" )
I don't understand what you need to comment out - unless you are
referring to a difference in scope between
$ ./myprogram.py
where myprogram is executing within __main__ 's scope, and the previous
case, where myprogram has it's own scope.
[ If this is a problem, you can use a technique like I used in
"ImportModule" to coerce the default namespace. That function
was posted to the python-list mailing list - I'll probably
repost a newer version here when I get the chance. ]
Note: Python has a '-d' switch, but that is debugging for the
Python interpreter's parser. 'pdb', and some of the other debugging
modules are just normal user written modules. It might be a nice
idea to add another command line option to automatically put you into
the debugger, but we would need to add an environment variable option,
PYTHONDEBUGMODULE or PYTHONDEBUGGER, to indicate *which* debugger to
load. I think it's a nice feature to have a user extesible Python
debugger written IN Python.
- Steve Majewski (804-982-0831) <sdm7g@Virginia.EDU>
- UVA Department of Molecular Physiology and Biological Physics