home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 56 / CDPowerplay56Disc2.iso / demos / blade / data1.cab / Program_Executable_Files / Lib / PythonLib / pdb.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-10-27  |  20.4 KB  |  556 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. import string
  5. import sys
  6. import linecache
  7. import cmd
  8. import bdb
  9. import repr
  10. line_prefix = '\n-> '
  11.  
  12. class Pdb(bdb.Bdb, cmd.Cmd):
  13.     
  14.     def __init__(self):
  15.         bdb.Bdb.__init__(self)
  16.         cmd.Cmd.__init__(self)
  17.         self.prompt = '(Pdb) '
  18.  
  19.     
  20.     def reset(self):
  21.         bdb.Bdb.reset(self)
  22.         self.forget()
  23.  
  24.     
  25.     def forget(self):
  26.         self.lineno = None
  27.         self.stack = []
  28.         self.curindex = 0
  29.         self.curframe = None
  30.  
  31.     
  32.     def setup(self, f, t):
  33.         self.forget()
  34.         (self.stack, self.curindex) = self.get_stack(f, t)
  35.         self.curframe = self.stack[self.curindex][0]
  36.  
  37.     
  38.     def user_line(self, frame):
  39.         self.interaction(frame, None)
  40.  
  41.     
  42.     def user_return(self, frame, return_value):
  43.         frame.f_locals['__return__'] = return_value
  44.         print '--Return--'
  45.         self.interaction(frame, None)
  46.  
  47.     
  48.     def user_exception(self, frame, .4):
  49.         (exc_type, exc_value, exc_traceback) = .4
  50.         frame.f_locals['__exception__'] = (exc_type, exc_value)
  51.         if type(exc_type) == type(''):
  52.             exc_type_name = exc_type
  53.         else:
  54.             exc_type_name = exc_type.__name__
  55.         print exc_type_name + ':', repr.repr(exc_value)
  56.         self.interaction(frame, exc_traceback)
  57.  
  58.     
  59.     def interaction(self, frame, traceback):
  60.         self.setup(frame, traceback)
  61.         self.print_stack_entry(self.stack[self.curindex])
  62.         self.cmdloop()
  63.         self.forget()
  64.  
  65.     
  66.     def default(self, line):
  67.         if line[:1] == '!':
  68.             line = line[1:]
  69.         
  70.         locals = self.curframe.f_locals
  71.         globals = self.curframe.f_globals
  72.         globals['__privileged__'] = 1
  73.         
  74.         try:
  75.             code = compile(line + '\n', '<stdin>', 'single')
  76.             exec code in globals, locals
  77.         except:
  78.             (t, v) = sys.exc_info()[:2]
  79.             if type(t) == type(''):
  80.                 exc_type_name = t
  81.             else:
  82.                 exc_type_name = t.__name__
  83.             print '***', exc_type_name + ':', v
  84.  
  85.  
  86.     do_h = cmd.Cmd.do_help
  87.     
  88.     def do_break(self, arg):
  89.         if not arg:
  90.             print self.get_all_breaks()
  91.             return None
  92.         
  93.         
  94.         try:
  95.             arg = eval(arg, self.curframe.f_globals, self.curframe.f_locals)
  96.         except:
  97.             print '*** Could not eval argument:', arg
  98.             return None
  99.  
  100.         
  101.         try:
  102.             (arg, cond) = arg
  103.         except:
  104.             (arg, cond) = (arg, None)
  105.  
  106.         
  107.         try:
  108.             lineno = int(arg)
  109.             filename = self.curframe.f_code.co_filename
  110.         except:
  111.             
  112.             try:
  113.                 func = arg
  114.                 if hasattr(func, 'im_func'):
  115.                     func = func.im_func
  116.                 
  117.                 code = func.func_code
  118.             except:
  119.                 print '*** The specified object', 'is not a function', arg
  120.                 return None
  121.  
  122.             lineno = code.co_firstlineno
  123.             filename = code.co_filename
  124.  
  125.         err = self.set_break(filename, lineno, cond)
  126.         if err:
  127.             print '***', err
  128.         
  129.  
  130.     do_b = do_break
  131.     
  132.     def do_clear(self, arg):
  133.         if not arg:
  134.             
  135.             try:
  136.                 reply = raw_input('Clear all breaks? ')
  137.             except EOFError:
  138.                 reply = 'no'
  139.  
  140.             reply = string.lower(string.strip(reply))
  141.             if reply in ('y', 'yes'):
  142.                 self.clear_all_breaks()
  143.             
  144.             return None
  145.         
  146.         
  147.         try:
  148.             lineno = int(eval(arg))
  149.         except:
  150.             print '*** Error in argument:', `arg`
  151.             return None
  152.  
  153.         filename = self.curframe.f_code.co_filename
  154.         err = self.clear_break(filename, lineno)
  155.         if err:
  156.             print '***', err
  157.         
  158.  
  159.     do_cl = do_clear
  160.     
  161.     def do_where(self, arg):
  162.         self.print_stack_trace()
  163.  
  164.     do_w = do_where
  165.     
  166.     def do_up(self, arg):
  167.         if self.curindex == 0:
  168.             print '*** Oldest frame'
  169.         else:
  170.             self.curindex = self.curindex - 1
  171.             self.curframe = self.stack[self.curindex][0]
  172.             self.print_stack_entry(self.stack[self.curindex])
  173.             self.lineno = None
  174.  
  175.     do_u = do_up
  176.     
  177.     def do_down(self, arg):
  178.         if self.curindex + 1 == len(self.stack):
  179.             print '*** Newest frame'
  180.         else:
  181.             self.curindex = self.curindex + 1
  182.             self.curframe = self.stack[self.curindex][0]
  183.             self.print_stack_entry(self.stack[self.curindex])
  184.             self.lineno = None
  185.  
  186.     do_d = do_down
  187.     
  188.     def do_step(self, arg):
  189.         self.set_step()
  190.         return 1
  191.  
  192.     do_s = do_step
  193.     
  194.     def do_next(self, arg):
  195.         self.set_next(self.curframe)
  196.         return 1
  197.  
  198.     do_n = do_next
  199.     
  200.     def do_return(self, arg):
  201.         self.set_return(self.curframe)
  202.         return 1
  203.  
  204.     do_r = do_return
  205.     
  206.     def do_continue(self, arg):
  207.         self.set_continue()
  208.         return 1
  209.  
  210.     do_c = do_cont = do_continue
  211.     
  212.     def do_quit(self, arg):
  213.         self.set_quit()
  214.         return 1
  215.  
  216.     do_q = do_quit
  217.     
  218.     def do_args(self, arg):
  219.         f = self.curframe
  220.         co = f.f_code
  221.         dict = f.f_locals
  222.         n = co.co_argcount
  223.         if co.co_flags & 4:
  224.             n = n + 1
  225.         
  226.         if co.co_flags & 8:
  227.             n = n + 1
  228.         
  229.         for i in range(n):
  230.             name = co.co_varnames[i]
  231.             print name, '=',
  232.             if dict.has_key(name):
  233.                 print dict[name]
  234.             else:
  235.                 print '*** undefined ***'
  236.         
  237.  
  238.     do_a = do_args
  239.     
  240.     def do_retval(self, arg):
  241.         if self.curframe.f_locals.has_key('__return__'):
  242.             print self.curframe.f_locals['__return__']
  243.         else:
  244.             print '*** Not yet returned!'
  245.  
  246.     do_rv = do_retval
  247.     
  248.     def do_p(self, arg):
  249.         self.curframe.f_globals['__privileged__'] = 1
  250.         
  251.         try:
  252.             value = eval(arg, self.curframe.f_globals, self.curframe.f_locals)
  253.         except:
  254.             (t, v) = sys.exc_info()[:2]
  255.             if type(t) == type(''):
  256.                 exc_type_name = t
  257.             else:
  258.                 exc_type_name = t.__name__
  259.             print '***', exc_type_name + ':', `v`
  260.             return None
  261.  
  262.         print `value`
  263.  
  264.     
  265.     def do_list(self, arg):
  266.         self.lastcmd = 'list'
  267.         last = None
  268.         if arg:
  269.             
  270.             try:
  271.                 x = eval(arg, { }, { })
  272.                 if type(x) == type(()):
  273.                     (first, last) = x
  274.                     first = int(first)
  275.                     last = int(last)
  276.                     if last < first:
  277.                         last = first + last
  278.                     
  279.                 else:
  280.                     first = max(1, int(x) - 5)
  281.             except:
  282.                 print '*** Error in argument:', `arg`
  283.                 return None
  284.  
  285.         elif self.lineno is None:
  286.             first = max(1, self.curframe.f_lineno - 5)
  287.         else:
  288.             first = self.lineno + 1
  289.         if last == None:
  290.             last = first + 10
  291.         
  292.         filename = self.curframe.f_code.co_filename
  293.         breaklist = self.get_file_breaks(filename)
  294.         
  295.         try:
  296.             for lineno in range(first, last + 1):
  297.                 line = linecache.getline(filename, lineno)
  298.                 if not line:
  299.                     print '[EOF]'
  300.                     break
  301.                 else:
  302.                     s = string.rjust(`lineno`, 3)
  303.                     if len(s) < 4:
  304.                         s = s + ' '
  305.                     
  306.                     if lineno in breaklist:
  307.                         s = s + 'B'
  308.                     else:
  309.                         s = s + ' '
  310.                     if lineno == self.curframe.f_lineno:
  311.                         s = s + '->'
  312.                     
  313.                     print s + '\t' + line,
  314.                     self.lineno = lineno
  315.         except KeyboardInterrupt:
  316.             pass
  317.  
  318.  
  319.     do_l = do_list
  320.     
  321.     def do_whatis(self, arg):
  322.         
  323.         try:
  324.             value = eval(arg, self.curframe.f_globals, self.curframe.f_locals)
  325.         except:
  326.             (t, v) = sys.exc_info()[:2]
  327.             if type(t) == type(''):
  328.                 exc_type_name = t
  329.             else:
  330.                 exc_type_name = t.__name__
  331.             print '***', exc_type_name + ':', `v`
  332.             return None
  333.  
  334.         code = None
  335.         
  336.         try:
  337.             code = value.func_code
  338.         except:
  339.             pass
  340.  
  341.         if code:
  342.             print 'Function', code.co_name
  343.             return None
  344.         
  345.         
  346.         try:
  347.             code = value.im_func.func_code
  348.         except:
  349.             pass
  350.  
  351.         if code:
  352.             print 'Method', code.co_name
  353.             return None
  354.         
  355.         print type(value)
  356.  
  357.     
  358.     def print_stack_trace(self):
  359.         
  360.         try:
  361.             for frame_lineno in self.stack:
  362.                 self.print_stack_entry(frame_lineno)
  363.         except KeyboardInterrupt:
  364.             pass
  365.  
  366.  
  367.     
  368.     def print_stack_entry(self, frame_lineno, prompt_prefix = line_prefix):
  369.         (frame, lineno) = frame_lineno
  370.         if frame is self.curframe:
  371.             print '>',
  372.         else:
  373.             print ' ',
  374.         print self.format_stack_entry(frame_lineno, prompt_prefix)
  375.  
  376.     
  377.     def help_help(self):
  378.         self.help_h()
  379.  
  380.     
  381.     def help_h(self):
  382.         print 'h(elp)\n\tWithout argument, print the list of available commands.\n\tWith a command name as argument, print help about that command\n\t"help pdb" pipes the full documentation file to the $PAGER\n\t"help exec" gives help on the ! command'
  383.  
  384.     
  385.     def help_where(self):
  386.         self.help_w()
  387.  
  388.     
  389.     def help_w(self):
  390.         print 'w(here)\n\tPrint a stack trace, with the most recent frame at the bottom.\n\tAn arrow indicates the "current frame", which determines the\n\tcontext of most commands.'
  391.  
  392.     
  393.     def help_down(self):
  394.         self.help_d()
  395.  
  396.     
  397.     def help_d(self):
  398.         print 'd(own)\n\tMove the current frame one level down in the stack trace\n\t(to an older frame).'
  399.  
  400.     
  401.     def help_up(self):
  402.         self.help_u()
  403.  
  404.     
  405.     def help_u(self):
  406.         print 'u(p)\n\tMove the current frame one level up in the stack trace\n\t(to a newer frame).'
  407.  
  408.     
  409.     def help_break(self):
  410.         self.help_b()
  411.  
  412.     
  413.     def help_b(self):
  414.         print 'b(reak) [lineno | function] [, "condition"]\n\tWith a line number argument, set a break there in the current\n\tfile.  With a function name, set a break at the entry of that\n\tfunction.  Without argument, list all breaks.  If a second\n\targument is present, it is a string specifying an expression\n\twhich must evaluate to true before the breakpoint is honored.\n\t'
  415.  
  416.     
  417.     def help_clear(self):
  418.         self.help_cl()
  419.  
  420.     
  421.     def help_cl(self):
  422.         print 'cl(ear) [lineno]\n\tWith a line number argument, clear that break in the current file.\n\tWithout argument, clear all breaks (but first ask confirmation).'
  423.  
  424.     
  425.     def help_step(self):
  426.         self.help_s()
  427.  
  428.     
  429.     def help_s(self):
  430.         print 's(tep)\n\tExecute the current line, stop at the first possible occasion\n\t(either in a function that is called or in the current function).'
  431.  
  432.     
  433.     def help_next(self):
  434.         self.help_n()
  435.  
  436.     
  437.     def help_n(self):
  438.         print 'n(ext)\n\tContinue execution until the next line in the current function\n\tis reached or it returns.'
  439.  
  440.     
  441.     def help_return(self):
  442.         self.help_r()
  443.  
  444.     
  445.     def help_r(self):
  446.         print 'r(eturn)\n\tContinue execution until the current function returns.'
  447.  
  448.     
  449.     def help_continue(self):
  450.         self.help_c()
  451.  
  452.     
  453.     def help_cont(self):
  454.         self.help_c()
  455.  
  456.     
  457.     def help_c(self):
  458.         print 'c(ont(inue))\n\tContinue execution, only stop when a breakpoint is encountered.'
  459.  
  460.     
  461.     def help_list(self):
  462.         self.help_l()
  463.  
  464.     
  465.     def help_l(self):
  466.         print 'l(ist) [first [,last]]\n\tList source code for the current file.\n\tWithout arguments, list 11 lines around the current line\n\tor continue the previous listing.\n\tWith one argument, list 11 lines starting at that line.\n\tWith two arguments, list the given range;\n\tif the second argument is less than the first, it is a count.'
  467.  
  468.     
  469.     def help_args(self):
  470.         self.help_a()
  471.  
  472.     
  473.     def help_a(self):
  474.         print 'a(rgs)\n\tPrint the arguments of the current function.'
  475.  
  476.     
  477.     def help_p(self):
  478.         print 'p expression\n\tPrint the value of the expression.'
  479.  
  480.     
  481.     def help_exec(self):
  482.         print "(!) statement\n\tExecute the (one-line) statement in the context of\n\tthe current stack frame.\n\tThe exclamation point can be omitted unless the first word\n\tof the statement resembles a debugger command.\n\tTo assign to a global variable you must always prefix the\n\tcommand with a 'global' command, e.g.:\n\t(Pdb) global list_options; list_options = ['-l']\n\t(Pdb)"
  483.  
  484.     
  485.     def help_quit(self):
  486.         self.help_q()
  487.  
  488.     
  489.     def help_q(self):
  490.         print 'q(uit)\tQuit from the debugger.\n\tThe program being executed is aborted.'
  491.  
  492.     
  493.     def help_pdb(self):
  494.         help()
  495.  
  496.  
  497.  
  498. def run(statement, globals = None, locals = None):
  499.     Pdb().run(statement, globals, locals)
  500.  
  501.  
  502. def runeval(expression, globals = None, locals = None):
  503.     return Pdb().runeval(expression, globals, locals)
  504.  
  505.  
  506. def runctx(statement, globals, locals):
  507.     run(statement, globals, locals)
  508.  
  509.  
  510. def runcall(*args):
  511.     return apply(Pdb().runcall, args)
  512.  
  513.  
  514. def set_trace():
  515.     Pdb().set_trace()
  516.  
  517.  
  518. def post_mortem(t):
  519.     p = Pdb()
  520.     p.reset()
  521.     while t.tb_next != None:
  522.         t = t.tb_next
  523.     p.interaction(t.tb_frame, t)
  524.  
  525.  
  526. def pm():
  527.     import sys
  528.     post_mortem(sys.last_traceback)
  529.  
  530. TESTCMD = 'import x; x.main()'
  531.  
  532. def test():
  533.     run(TESTCMD)
  534.  
  535.  
  536. def help():
  537.     import os
  538.     for dirname in sys.path:
  539.         fullname = os.path.join(dirname, 'pdb.doc')
  540.     else:
  541.         print 'Sorry, can\'t find the help file "pdb.doc"', 'along the Python search path'
  542.  
  543. if __name__ == '__main__':
  544.     import sys
  545.     import os
  546.     if not sys.argv[1:]:
  547.         print 'usage: pdb.py scriptfile [arg] ...'
  548.         sys.exit(2)
  549.     
  550.     filename = sys.argv[1]
  551.     del sys.argv[0]
  552.     sys.path.insert(0, os.path.dirname(filename))
  553.     run('execfile(' + `filename` + ')', {
  554.         '__name__': '__main__' })
  555.  
  556.