Python debugger

Guillermo Fernandez guillermo.fernandez at epfl.ch
Thu Oct 16 16:18:43 EDT 2003


Hi,

I'm trying to play with the python debugger. The idea is to put lines in the 
code to automatically define breakpoints at the starting of the debugger.

(like:

1 def a():
2 #--- break
3     print 'done'

would automatically insert a break in the line number 2)

I did the code appended (I'm still quite a beginner...) but it makes me problems.
Traceback (most recent call last):
   File "C:\Python23\lib\site-packages\sm\scriptutils.py", line 52, in run
     sdb.run('exec codeObject in mainDict', locals(), mainDict)
   File "C:\Python23\lib\site-packages\sm\spedb.py", line 29, in run
     Sdb().run(statement, globals, locals)
   File "C:\Python23\lib\site-packages\sm\spedb.py", line 23, in run
     pdb.Pdb.do_break(self, str(linenbr))
   File "C:\Python23\lib\pdb.py", line 262, in do_break
     filename = self.defaultFile()
   File "C:\Python23\lib\pdb.py", line 278, in defaultFile
     filename = self.curframe.f_code.co_filename
AttributeError: Sdb instance has no attribute 'curframe'
Exception raised while running script  <source>

I run the program as:
import spedb
mainDict=__main__.__dict__
# mainDict can also be another argument passed,
# I think it's also locals() by default
mainDict['codeObject']=codeObject
spedb.run('exec codeObject in mainDict', locals(), mainDict)


I think it's because the breakpoint it's instaured before the run, but I really 
don't know how to put the breakpoint "inside" the run.

Furthermore, if I define the method run like:
     def run(self, statement, globals, locals):
         bdb.Bdb.run(self, statement, globals, locals)
it does not even prompt the usual prompt (not any prompt at all...)!
I got this:
 >>> Running 'C:\Python23\Lib\site-packages\sm\spedb.py' ...
Script '<source>' returned exit code 0

Could someone give me a hint about:
1) Why it does not work?
2) How to modify it the way I want?

Thanks a lot.

G

import pdb
import bdb

class Sdb(bdb.Bdb,pdb.Pdb):
     def __init__(self):
         pdb.Pdb.__init__(self)
         bdb.Bdb.__init__(self)

     def run(self, statement, globals, locals):
         functions={
         '#--- break' : pdb.Pdb.do_break
         }
         lines=globals['source'].splitlines()
         linenbr=0
         for line in lines:
             linenbr+=1
             if line in functions:
                 pdb.Pdb.do_break(self, str(linenbr))
         bdb.Bdb.run(self, statement, globals, locals)

def run(statement, globals=None, locals=None):
     Sdb().run(statement, globals, locals)






More information about the Python-list mailing list