The bdb Module

The bdb module provides a framework for debuggers. You can use this to create your own custom debuggers, as Example 11-3 shows.

To implement custom behavior, subclass the Bdb class, and override the user methods (which are called whenever the debugger stops). To control the debugger, use the various set methods.

Example 11-3. Using the bdb Mdule

File: bdb-example-1.py

import bdb
import time

def spam(n):
 j = 0
 for i in range(n):
 j = j + i
 return n

def egg(n):
 spam(n)
 spam(n)
 spam(n)
 spam(n)

def test(n):
 egg(n)

class myDebugger(bdb.Bdb):

 run = 0

 def user_call(self, frame, args):
 name = frame.f_code.co_name or ""
 print "call", name, args
 self.set_continue() # continue

 def user_line(self, frame):
 if self.run:
 self.run = 0
 self.set_trace() # start tracing
 else:
 # arrived at breakpoint
 name = frame.f_code.co_name or ""
 filename = self.canonic(frame.f_code.co_filename)
 print "break at", filename, frame.f_lineno, "in", name
 print "continue..."
 self.set_continue() # continue to next breakpoint

 def user_return(self, frame, value):
 name = frame.f_code.co_name or ""
 print "return from", name, value
 print "continue..."
 self.set_continue() # continue

 def user_exception(self, frame, exception):
 name = frame.f_code.co_name or ""
 print "exception in", name, exception
 print "continue..."
 self.set_continue() # continue

db = myDebugger()
db.run = 1
db.set_break("bdb-example-1.py", 7)
db.runcall(test, 1)

continue...
call egg None
call spam None
break at C:ematterlibrarybookdb-example-1.py 7 in spam
continue...
call spam None
break at C:ematterlibrarybookdb-example-1.py 7 in spam
continue...
call spam None
break at C:ematterlibrarybookdb-example-1.py 7 in spam
continue...
call spam None
break at C:ematterlibrarybookdb-example-1.py 7 in spam
continue...

Core Modules

More Standard Modules

Threads and Processes

Data Representation

File Formats

Mail and News Message Processing

Network Protocols

Internationalization

Multimedia Modules

Data Storage

Tools and Utilities

Platform-Specific Modules

Implementation Support Modules

Other Modules



Python Standard Library
Python Standard Library (Nutshell Handbooks) with
ISBN: 0596000960
EAN: 2147483647
Year: 2000
Pages: 252
Authors: Fredrik Lundh

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net