Launching pdb from within a script

  • Thread starter Graham Nicholls
  • Start date
G

Graham Nicholls

I asked this before, but got no response, but I KNOW YOU KNOW ;-)
A year or so ago, I was given a really useful answer to a question posed
here, which showed me how to start the debugger from withing a script.
Yes, of course I've lost the mail (and I have trawled through all my old
mail), and yes, I know I'm stupid!

I'm surprised this isn't a FAQ, BTW, as it seems such a useful facility.

So come on guys, cough it up!
Thanks
Graham Nicholls
 
P

Peter Otten

Graham said:
A year or so ago, I was given a really useful answer to a question posed
here, which showed me how to start the debugger from withing a script.

Are you looking for pdb.set_trace()?

Peter
 
G

Graham Nicholls

Graham said:
I think so, thanks!
Graham
Or maybe not.
I'm sure I was able to print the value of variables like this:

except :
pdb.set_trace()

run the program:
error message
(Pdb) d <return> (not sure why!)
(Pdb) print dentry.path
(Pdb) more debugging here

But pdb complains that there are no such variables. It may be that I had to
add some incantation around the script startup - I just can't remember (and
its frustrating!).

Thanks
Graham
 
P

Peter Otten

Graham said:
I'm sure I was able to print the value of variables like this:

except :
pdb.set_trace() [...]
But pdb complains that there are no such variables. It may be that I had
to add some incantation around the script startup - I just can't remember
(and its frustrating!).

<debugger.py>
import pdb
abc = 1
try:
print "before"
1/0
except:
pdb.set_trace()
print "after"
</debugger.py>

Now run it:

before
--Return--
/(suppressed)/python2.3/pdb.py(992)set_trace()->None
-> Pdb().set_trace()
(Pdb) abc
*** NameError: name 'abc' is not defined
(Pdb) u
/(suppressed)/debugger.py(7)?()
-> pdb.set_trace()
(Pdb) abc
1
(Pdb)

If the above sample session doesn't help, you could actually read the pdb
documentation - or do something new and dangerous: search google groups
for, say, Nicholls and set_trace :)

Peter
 
D

Dennis Lee Bieber

Graham Nicholls fed this fish to the penguins on Sunday 04 January 2004
11:27 am:

But pdb complains that there are no such variables. It may be that I
had to add some incantation around the script startup - I just can't
remember (and its frustrating!).
Did you remember to import pdb at the start?

--
 
G

Graham Nicholls

Peter said:
Graham said:
I'm sure I was able to print the value of variables like this:

except :
pdb.set_trace() [...]
But pdb complains that there are no such variables. It may be that I had
to add some incantation around the script startup - I just can't remember
(and its frustrating!).

<debugger.py>
import pdb
abc = 1
try:
print "before"
1/0
except:
pdb.set_trace()
print "after"
</debugger.py>

Now run it:

before
--Return--
/(suppressed)/python2.3/pdb.py(992)set_trace()->None
-> Pdb().set_trace()
(Pdb) abc
*** NameError: name 'abc' is not defined
(Pdb) u
/(suppressed)/debugger.py(7)?()
-> pdb.set_trace()
(Pdb) abc
1
(Pdb)

If the above sample session doesn't help, you could actually read the pdb
I have had a look, but its rather a case of you don't know what you don't
know, IYSWIM.
documentation - or do something new and dangerous: search google groups
for, say, Nicholls and set_trace :)

What a good idea!
Thanks again

PS 1 minute later and I've found the relevant posting, which indeed mentions
using set_trace().

I have in ordinary google searches of course returned ng postings, but had
never used their group searches, so thnkas on two counts.

Regards
Graham
 
M

Mike C. Fletcher

Peter said:
Graham Nicholls wrote:

....
....

-> Pdb().set_trace()
(Pdb) abc
*** NameError: name 'abc' is not defined
(Pdb) u
This is the key line, 'n' would work as well. The reason your variables
weren't being found is that you were still inside the call to
set_trace(). You need to return from that (either by going "u"p to the
calling context, or to the "n"ext instruction, which returns to the
calling context by finishing the set_trace() call).

Enjoy,
Mike

_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/
 
P

Peter Hansen

Mike C. Fletcher said:
This is the key line, 'n' would work as well. The reason your variables
weren't being found is that you were still inside the call to
set_trace(). You need to return from that (either by going "u"p to the
calling context, or to the "n"ext instruction, which returns to the
calling context by finishing the set_trace() call).

"r" (for "return") works as well....

Wow! Three obvious ways to do the same thing. ;-)

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top