Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what can cause pdb.set_trace() to be ignored?

I'm trying to debug a Python program and I inserted a classic 'import pdb;pdb.set_trace()' line in a function, just before a call which generates a stack trace. However that call seems to be ignored, i.e. nothing happens and I don't get a pdb prompt.

At that point of the program, there is only one active thread. No monkey patching of the pdb module was detected.

Any help on what could cause the call to set_trace to be ignored is welcome. Thanks.

Platform info: Debian squeeze + python 2.6.5

Code extract:

import threading
print threading.active_count()
import pdb
print pdb
pdb.set_trace()
print "*****"
root_resource.init_publisher() # before changing uid

output:

<lots of stuff>
1
<module 'pdb' from '/usr/lib/python2.6/pdb.pyc'>
*****
<stack trace in init_publisher>
like image 819
gurney alex Avatar asked Aug 12 '10 10:08

gurney alex


People also ask

What is pdb Set_trace ()?

pdb. set_trace(*, header=None) Enter the debugger at the calling stack frame. This is useful to hard-code a breakpoint at a given point in a program, even if the code is not otherwise being debugged (e.g. when an assertion fails). If given, header is printed to the console just before debugging begins.

What does import pdb pdb Set_trace () do?

To start debugging within the program just insert import pdb, pdb. set_trace() commands. Run your script normally, and execution will stop where we have introduced a breakpoint. So basically we are hard coding a breakpoint on a line below where we call set_trace().

How do I stop a pdb set trace?

To start execution, you use the continue or c command. If the program executes successfully, you will be taken back to the (Pdb) prompt where you can restart the execution again. At this point, you can use quit / q or Ctrl+D to exit the debugger.

What is PBD in Python?

Pdb is a powerful tool for finding syntax errors, spelling mistakes, missing code, and other issues in your Python code.


2 Answers

You are probably not running that statement, either because:

  • the stacktrace is not where you thought it was
  • you inserted the set_trace call in a similar but wrong place
  • you are running a different .py file than the one you edited
  • you have your own local pdb.py file that is getting imported instead of the one from the stdlib
like image 83
PaulMcG Avatar answered Sep 23 '22 15:09

PaulMcG


This is going to waste the time of a number Python developers. Tonight I added myself to their ranks. I wish I had found this post before I spent 2 hours discovering a similar problem with a large library I am using. Subsequent Google searches hardly shed much light on the issue of pdb and pysco incompatability. The problem should be more visible to users starting out with pdb.

Deep within the guts of a library I was importing was a file which contained the following code:

try:
    import psyco
    psyco.bind(bdecode)
    psyco.bind(bencode)
except ImportError:
    pass

What a lovely gesture of the author, who obviously assumed no one using their code, and who had also installed psyco, would ever like to use a tool such as pdb to debug it ;-) Mind you, you could ask how were they mean't to know anyway?

Whilst exploring the problem I found that usage of:

import pdb; pdb.set_trace() 

after the import of psyco, is just passed over; for neither rythme nor reason. Very frustrating indeed.

The problem does not effect debugging with PyDev or, I presume, other more advanced debuggers, which is I guess why it falls outside the radar of initial Google searches.

like image 27
codeasone Avatar answered Sep 22 '22 15:09

codeasone