<s> """<STR_LIT>""" <EOL> import sys <EOL> import os <EOL> import types <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> class BdbQuit ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class Bdb : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . breaks = { } <EOL> self . fncache = { } <EOL> def canonic ( self , filename ) : <EOL> if filename == "<STR_LIT:<>" + filename [ <NUM_LIT:1> : - <NUM_LIT:1> ] + "<STR_LIT:>>" : <EOL> return filename <EOL> canonic = self . fncache . get ( filename ) <EOL> if not canonic : <EOL> canonic = os . path . abspath ( filename ) <EOL> canonic = os . path . normcase ( canonic ) <EOL> self . fncache [ filename ] = canonic <EOL> return canonic <EOL> def reset ( self ) : <EOL> import linecache <EOL> linecache . checkcache ( ) <EOL> self . botframe = None <EOL> self . stopframe = None <EOL> self . returnframe = None <EOL> self . quitting = <NUM_LIT:0> <EOL> def trace_dispatch ( self , frame , event , arg ) : <EOL> if self . quitting : <EOL> return <EOL> if event == '<STR_LIT>' : <EOL> return self . dispatch_line ( frame ) <EOL> if event == '<STR_LIT>' : <EOL> return self . dispatch_call ( frame , arg ) <EOL> if event == '<STR_LIT>' : <EOL> return self . dispatch_return ( frame , arg ) <EOL> if event == '<STR_LIT>' : <EOL> return self . dispatch_exception ( frame , arg ) <EOL> if event == '<STR_LIT>' : <EOL> return self . trace_dispatch <EOL> if event == '<STR_LIT>' : <EOL> return self . trace_dispatch <EOL> if event == '<STR_LIT>' : <EOL> return self . trace_dispatch <EOL> print '<STR_LIT>' , repr ( event ) <EOL> return self . trace_dispatch <EOL> def dispatch_line ( self , frame ) : <EOL> if self . stop_here ( frame ) or self . break_here ( frame ) : <EOL> self . user_line ( frame ) <EOL> if self . quitting : raise BdbQuit <EOL> return self . trace_dispatch <EOL> def dispatch_call ( self , frame , arg ) : <EOL> if self . botframe is None : <EOL> self . botframe = frame . f_back <EOL> return self . trace_dispatch <EOL> if not ( self . stop_here ( frame ) or self . break_anywhere ( frame ) ) : <EOL> return <EOL> self . user_call ( frame , arg ) <EOL> if self . quitting : raise BdbQuit <EOL> return self . trace_dispatch <EOL> def dispatch_return ( self , frame , arg ) : <EOL> if self . stop_here ( frame ) or frame == self . returnframe : <EOL> self . user_return ( frame , arg ) <EOL> if self . quitting : raise BdbQuit <EOL> return self . trace_dispatch <EOL> def dispatch_exception ( self , frame , arg ) : <EOL> if self . stop_here ( frame ) : <EOL> self . user_exception ( frame , arg ) <EOL> if self . quitting : raise BdbQuit <EOL> return self . trace_dispatch <EOL> def stop_here ( self , frame ) : <EOL> if frame is self . stopframe : <EOL> return True <EOL> while frame is not None and frame is not self . stopframe : <EOL> if frame is self . botframe : <EOL> return True <EOL> frame = frame . f_back <EOL> return False <EOL> def break_here ( self , frame ) : <EOL> filename = self . canonic ( frame . f_code . co_filename ) <EOL> if not filename in self . breaks : <EOL> return False <EOL> lineno = frame . f_lineno <EOL> if not lineno in self . breaks [ filename ] : <EOL> lineno = frame . f_code . co_firstlineno <EOL> if not lineno in self . breaks [ filename ] : <EOL> return False <EOL> ( bp , flag ) = effective ( filename , lineno , frame ) <EOL> if bp : <EOL> self . currentbp = bp . number <EOL> if ( flag and bp . temporary ) : <EOL> self . do_clear ( str ( bp . number ) ) <EOL> return True <EOL> else : <EOL> return False <EOL> def do_clear ( self , arg ) : <EOL> raise NotImplementedError , "<STR_LIT>" <EOL> def break_anywhere ( self , frame ) : <EOL> return self . breaks . has_key ( <EOL> self . canonic ( frame . f_code . co_filename ) ) <EOL> def user_call ( self , frame , argument_list ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def user_line ( self , frame ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def user_return ( self , frame , return_value ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def user_exception ( self , frame , ( exc_type , exc_value , exc_traceback ) ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def set_step ( self ) : <EOL> """<STR_LIT>""" <EOL> self . stopframe = None <EOL> self . returnframe = None <EOL> self . quitting = <NUM_LIT:0> <EOL> def set_next ( self , frame ) : <EOL> """<STR_LIT>""" <EOL> self . stopframe = frame <EOL> self . returnframe = None <EOL> self . quitting = <NUM_LIT:0> <EOL> def set_return ( self , frame ) : <EOL> """<STR_LIT>""" <EOL> self . stopframe = frame . f_back <EOL> self . returnframe = frame <EOL> self . quitting = <NUM_LIT:0> <EOL> def set_trace ( self , frame = None ) : <EOL> """<STR_LIT>""" <EOL> if frame is None : <EOL> frame = sys . _getframe ( ) . f_back <EOL> self . reset ( ) <EOL> while frame : <EOL> frame . f_trace = self . trace_dispatch <EOL> self . botframe = frame <EOL> frame = frame . f_back <EOL> self . set_step ( ) <EOL> sys . settrace ( self . trace_dispatch ) <EOL> def set_continue ( self ) : <EOL> self . stopframe = self . botframe <EOL> self . returnframe = None <EOL> self . quitting = <NUM_LIT:0> <EOL> if not self . breaks : <EOL> sys . settrace ( None ) <EOL> frame = sys . _getframe ( ) . f_back <EOL> while frame and frame is not self . botframe : <EOL> del frame . f_trace <EOL> frame = frame . f_back <EOL> def set_quit ( self ) : <EOL> self . stopframe = self . botframe <EOL> self . returnframe = None <EOL> self . quitting = <NUM_LIT:1> <EOL> sys . settrace ( None ) <EOL> def set_break ( self , filename , lineno , temporary = <NUM_LIT:0> , cond = None , <EOL> funcname = None ) : <EOL> filename = self . canonic ( filename ) <EOL> import linecache <EOL> line = linecache . getline ( filename , lineno ) <EOL> if not line : <EOL> return '<STR_LIT>' % ( filename , <EOL> lineno ) <EOL> if not filename in self . breaks : <EOL> self . breaks [ filename ] = [ ] <EOL> list = self . breaks [ filename ] <EOL> if not lineno in list : <EOL> list . append ( lineno ) <EOL> bp = Breakpoint ( filename , lineno , temporary , cond , funcname ) <EOL> def clear_break ( self , filename , lineno ) : <EOL> filename = self . canonic ( filename ) <EOL> if not filename in self . breaks : <EOL> return '<STR_LIT>' % filename <EOL> if lineno not in self . breaks [ filename ] : <EOL> return '<STR_LIT>' % ( filename , <EOL> lineno ) <EOL> for bp in Breakpoint . bplist [ filename , lineno ] [ : ] : <EOL> bp . deleteMe ( ) <EOL> if not Breakpoint . bplist . has_key ( ( filename , lineno ) ) : <EOL> self . breaks [ filename ] . remove ( lineno ) <EOL> if not self . breaks [ filename ] : <EOL> del self . breaks [ filename ] <EOL> def clear_bpbynumber ( self , arg ) : <EOL> try : <EOL> number = int ( arg ) <EOL> except : <EOL> return '<STR_LIT>' % arg <EOL> try : <EOL> bp = Breakpoint . bpbynumber [ number ] <EOL> except IndexError : <EOL> return '<STR_LIT>' % number <EOL> if not bp : <EOL> return '<STR_LIT>' % number <EOL> self . clear_break ( bp . file , bp . line ) <EOL> def clear_all_file_breaks ( self , filename ) : <EOL> filename = self . canonic ( filename ) <EOL> if not filename in self . breaks : <EOL> return '<STR_LIT>' % filename <EOL> for line in self . breaks [ filename ] : <EOL> blist = Breakpoint . bplist [ filename , line ] <EOL> for bp in blist : <EOL> bp . deleteMe ( ) <EOL> del self . breaks [ filename ] <EOL> def clear_all_breaks ( self ) : <EOL> if not self . breaks : <EOL> return '<STR_LIT>' <EOL> for bp in Breakpoint . bpbynumber : <EOL> if bp : <EOL> bp . deleteMe ( ) <EOL> self . breaks = { } <EOL> def get_break ( self , filename , lineno ) : <EOL> filename = self . canonic ( filename ) <EOL> return filename in self . breaks and lineno in self . breaks [ filename ] <EOL> def get_breaks ( self , filename , lineno ) : <EOL> filename = self . canonic ( filename ) <EOL> return filename in self . breaks and lineno in self . breaks [ filename ] and Breakpoint . bplist [ filename , lineno ] or [ ] <EOL> def get_file_breaks ( self , filename ) : <EOL> filename = self . canonic ( filename ) <EOL> if filename in self . breaks : <EOL> return self . breaks [ filename ] <EOL> else : <EOL> return [ ] <EOL> def get_all_breaks ( self ) : <EOL> return self . breaks <EOL> def get_stack ( self , f , t ) : <EOL> stack = [ ] <EOL> if t and t . tb_frame is f : <EOL> t = t . tb_next <EOL> while f is not None : <EOL> stack . append ( ( f , f . f_lineno ) ) <EOL> if f is self . botframe : <EOL> break <EOL> f = f . f_back <EOL> stack . reverse ( ) <EOL> i = max ( <NUM_LIT:0> , len ( stack ) - <NUM_LIT:1> ) <EOL> while t is not None : <EOL> stack . append ( ( t . tb_frame , t . tb_lineno ) ) <EOL> t = t . tb_next <EOL> return stack , i <EOL> def format_stack_entry ( self , frame_lineno , lprefix = '<STR_LIT>' ) : <EOL> import linecache , repr <EOL> frame , lineno = frame_lineno <EOL> filename = self . canonic ( frame . f_code . co_filename ) <EOL> s = '<STR_LIT>' % ( filename , lineno ) <EOL> if frame . f_code . co_name : <EOL> s = s + frame . f_code . co_name <EOL> else : <EOL> s = s + "<STR_LIT>" <EOL> if '<STR_LIT>' in frame . f_locals : <EOL> args = frame . f_locals [ '<STR_LIT>' ] <EOL> else : <EOL> args = None <EOL> if args : <EOL> s = s + repr . repr ( args ) <EOL> else : <EOL> s = s + '<STR_LIT>' <EOL> if '<STR_LIT>' in frame . f_locals : <EOL> rv = frame . f_locals [ '<STR_LIT>' ] <EOL> s = s + '<STR_LIT>' <EOL> s = s + repr . repr ( rv ) <EOL> line = linecache . getline ( filename , lineno ) <EOL> if line : s = s + lprefix + line . strip ( ) <EOL> return s <EOL> def run ( self , cmd , globals = None , locals = None ) : <EOL> if globals is None : <EOL> import __main__ <EOL> globals = __main__ . __dict__ <EOL> if locals is None : <EOL> locals = globals <EOL> self . reset ( ) <EOL> sys . settrace ( self . trace_dispatch ) <EOL> if not isinstance ( cmd , types . CodeType ) : <EOL> cmd = cmd + '<STR_LIT:\n>' <EOL> try : <EOL> try : <EOL> exec cmd in globals , locals <EOL> except BdbQuit : <EOL> pass <EOL> finally : <EOL> self . quitting = <NUM_LIT:1> <EOL> sys . settrace ( None ) <EOL> def runeval ( self , expr , globals = None , locals = None ) : <EOL> if globals is None : <EOL> import __main__ <EOL> globals = __main__ . __dict__ <EOL> if locals is None : <EOL> locals = globals <EOL> self . reset ( ) <EOL> sys . settrace ( self . trace_dispatch ) <EOL> if not isinstance ( expr , types . CodeType ) : <EOL> expr = expr + '<STR_LIT:\n>' <EOL> try : <EOL> try : <EOL> return eval ( expr , globals , locals ) <EOL> except BdbQuit : <EOL> pass <EOL> finally : <EOL> self . quitting = <NUM_LIT:1> <EOL> sys . settrace ( None ) <EOL> def runctx ( self , cmd , globals , locals ) : <EOL> self . run ( cmd , globals , locals ) <EOL> def runcall ( self , func , * args , ** kwds ) : <EOL> self . reset ( ) <EOL> sys . settrace ( self . trace_dispatch ) <EOL> res = None <EOL> try : <EOL> try : <EOL> res = func ( * args , ** kwds ) <EOL> except BdbQuit : <EOL> pass <EOL> finally : <EOL> self . quitting = <NUM_LIT:1> <EOL> sys . settrace ( None ) <EOL> return res <EOL> def set_trace ( ) : <EOL> Bdb ( ) . set_trace ( ) <EOL> class Breakpoint : <EOL> """<STR_LIT>""" <EOL> next = <NUM_LIT:1> <EOL> bplist = { } <EOL> bpbynumber = [ None ] <EOL> def __init__ ( self , file , line , temporary = <NUM_LIT:0> , cond = None , funcname = None ) : <EOL> self . funcname = funcname <EOL> self . func_first_executable_line = None <EOL> self . file = file <EOL> self . line = line <EOL> self . temporary = temporary <EOL> self . cond = cond <EOL> self . enabled = <NUM_LIT:1> <EOL> self . ignore = <NUM_LIT:0> <EOL> self . hits = <NUM_LIT:0> <EOL> self . number = Breakpoint . next <EOL> Breakpoint . next = Breakpoint . next + <NUM_LIT:1> <EOL> self . bpbynumber . append ( self ) <EOL> if self . bplist . has_key ( ( file , line ) ) : <EOL> self . bplist [ file , line ] . append ( self ) <EOL> else : <EOL> self . bplist [ file , line ] = [ self ] <EOL> def deleteMe ( self ) : <EOL> index = ( self . file , self . line ) <EOL> self . bpbynumber [ self . number ] = None <EOL> self . bplist [ index ] . remove ( self ) <EOL> if not self . bplist [ index ] : <EOL> del self . bplist [ index ] <EOL> def enable ( self ) : <EOL> self . enabled = <NUM_LIT:1> <EOL> def disable ( self ) : <EOL> self . enabled = <NUM_LIT:0> <EOL> def bpprint ( self ) : <EOL> if self . temporary : <EOL> disp = '<STR_LIT>' <EOL> else : <EOL> disp = '<STR_LIT>' <EOL> if self . enabled : <EOL> disp = disp + '<STR_LIT:yes>' <EOL> else : <EOL> disp = disp + '<STR_LIT>' <EOL> print '<STR_LIT>' % ( self . number , disp , <EOL> self . file , self . line ) <EOL> if self . cond : <EOL> print '<STR_LIT>' % ( self . cond , ) <EOL> if self . ignore : <EOL> print '<STR_LIT>' % ( self . ignore ) <EOL> if ( self . hits ) : <EOL> if ( self . hits > <NUM_LIT:1> ) : ss = '<STR_LIT:s>' <EOL> else : ss = '<STR_LIT>' <EOL> print ( '<STR_LIT>' % <EOL> ( self . hits , ss ) ) <EOL> def checkfuncname ( b , frame ) : <EOL> """<STR_LIT>""" <EOL> if not b . funcname : <EOL> if b . line != frame . f_lineno : <EOL> return False <EOL> return True <EOL> if frame . f_code . co_name != b . funcname : <EOL> return False <EOL> if not b . func_first_executable_line : <EOL> b . func_first_executable_line = frame . f_lineno <EOL> if b . func_first_executable_line != frame . f_lineno : <EOL> return False <EOL> return True <EOL> def effective ( file , line , frame ) : <EOL> """<STR_LIT>""" <EOL> possibles = Breakpoint . bplist [ file , line ] <EOL> for i in range ( <NUM_LIT:0> , len ( possibles ) ) : <EOL> b = possibles [ i ] <EOL> if b . enabled == <NUM_LIT:0> : <EOL> continue <EOL> if not checkfuncname ( b , frame ) : <EOL> continue <EOL> b . hits = b . hits + <NUM_LIT:1> <EOL> if not b . cond : <EOL> if b . ignore > <NUM_LIT:0> : <EOL> b . ignore = b . ignore - <NUM_LIT:1> <EOL> continue <EOL> else : <EOL> return ( b , <NUM_LIT:1> ) <EOL> else : <EOL> try : <EOL> val = eval ( b . cond , frame . f_globals , <EOL> frame . f_locals ) <EOL> if val : <EOL> if b . ignore > <NUM_LIT:0> : <EOL> b . ignore = b . ignore - <NUM_LIT:1> <EOL> else : <EOL> return ( b , <NUM_LIT:1> ) <EOL> except : <EOL> return ( b , <NUM_LIT:0> ) <EOL> return ( None , None ) <EOL> class Tdb ( Bdb ) : <EOL> def user_call ( self , frame , args ) : <EOL> name = frame . f_code . co_name <EOL> if not name : name = '<STR_LIT>' <EOL> print '<STR_LIT>' , name , args <EOL> def user_line ( self , frame ) : <EOL> import linecache <EOL> name = frame . f_code . co_name <EOL> if not name : name = '<STR_LIT>' <EOL> fn = self . canonic ( frame . f_code . co_filename ) <EOL> line = linecache . getline ( fn , frame . f_lineno ) <EOL> print '<STR_LIT>' , fn , frame . f_lineno , name , '<STR_LIT::>' , line . strip ( ) <EOL> def user_return ( self , frame , retval ) : <EOL> print '<STR_LIT>' , retval <EOL> def user_exception ( self , frame , exc_stuff ) : <EOL> print '<STR_LIT>' , exc_stuff <EOL> self . set_continue ( ) <EOL> def foo ( n ) : <EOL> print '<STR_LIT>' , n , '<STR_LIT:)>' <EOL> x = bar ( n * <NUM_LIT:10> ) <EOL> print '<STR_LIT>' , x <EOL> def bar ( a ) : <EOL> print '<STR_LIT>' , a , '<STR_LIT:)>' <EOL> return a / <NUM_LIT:2> <EOL> def test ( ) : <EOL> t = Tdb ( ) <EOL> t . run ( '<STR_LIT>' ) </s>
