home *** CD-ROM | disk | FTP | other *** search
/ PC Extra 07 & 08 / pca1507.iso / Software / psp8 / Data1.cab / traceback.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-22  |  13.5 KB  |  361 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. '''Extract, format and print information about Python stack traces.'''
  5. import linecache
  6. import sys
  7. import types
  8. __all__ = [
  9.     'extract_stack',
  10.     'extract_tb',
  11.     'format_exception',
  12.     'format_exception_only',
  13.     'format_list',
  14.     'format_stack',
  15.     'format_tb',
  16.     'print_exc',
  17.     'print_exception',
  18.     'print_last',
  19.     'print_stack',
  20.     'print_tb',
  21.     'tb_lineno']
  22.  
  23. def _print(file, str = '', terminator = '\n'):
  24.     file.write(str + terminator)
  25.  
  26.  
  27. def print_list(extracted_list, file = None):
  28.     '''Print the list of tuples as returned by extract_tb() or
  29.     extract_stack() as a formatted stack trace to the given file.'''
  30.     if not file:
  31.         file = sys.stderr
  32.     
  33.     for filename, lineno, name, line in extracted_list:
  34.         _print(file, '  File "%s", line %d, in %s' % (filename, lineno, name))
  35.         if line:
  36.             _print(file, '    %s' % line.strip())
  37.         
  38.     
  39.  
  40.  
  41. def format_list(extracted_list):
  42.     '''Format a list of traceback entry tuples for printing.
  43.  
  44.     Given a list of tuples as returned by extract_tb() or
  45.     extract_stack(), return a list of strings ready for printing.
  46.     Each string in the resulting list corresponds to the item with the
  47.     same index in the argument list.  Each string ends in a newline;
  48.     the strings may contain internal newlines as well, for those items
  49.     whose source text line is not None.
  50.     '''
  51.     list = []
  52.     for filename, lineno, name, line in extracted_list:
  53.         item = '  File "%s", line %d, in %s\n' % (filename, lineno, name)
  54.         if line:
  55.             item = item + '    %s\n' % line.strip()
  56.         
  57.         list.append(item)
  58.     
  59.     return list
  60.  
  61.  
  62. def print_tb(tb, limit = None, file = None):
  63.     """Print up to 'limit' stack trace entries from the traceback 'tb'.
  64.  
  65.     If 'limit' is omitted or None, all entries are printed.  If 'file'
  66.     is omitted or None, the output goes to sys.stderr; otherwise
  67.     'file' should be an open file or file-like object with a write()
  68.     method.
  69.     """
  70.     if not file:
  71.         file = sys.stderr
  72.     
  73.     if limit is None:
  74.         if hasattr(sys, 'tracebacklimit'):
  75.             limit = sys.tracebacklimit
  76.         
  77.     
  78.     n = 0
  79.     while tb is not None and limit is None or n < limit:
  80.         f = tb.tb_frame
  81.         lineno = tb_lineno(tb)
  82.         co = f.f_code
  83.         filename = co.co_filename
  84.         name = co.co_name
  85.         _print(file, '  File "%s", line %d, in %s' % (filename, lineno, name))
  86.         line = linecache.getline(filename, lineno)
  87.         if line:
  88.             _print(file, '    ' + line.strip())
  89.         
  90.         tb = tb.tb_next
  91.         n = n + 1
  92.  
  93.  
  94. def format_tb(tb, limit = None):
  95.     """A shorthand for 'format_list(extract_stack(f, limit))."""
  96.     return format_list(extract_tb(tb, limit))
  97.  
  98.  
  99. def extract_tb(tb, limit = None):
  100.     """Return list of up to limit pre-processed entries from traceback.
  101.  
  102.     This is useful for alternate formatting of stack traces.  If
  103.     'limit' is omitted or None, all entries are extracted.  A
  104.     pre-processed stack trace entry is a quadruple (filename, line
  105.     number, function name, text) representing the information that is
  106.     usually printed for a stack trace.  The text is a string with
  107.     leading and trailing whitespace stripped; if the source is not
  108.     available it is None.
  109.     """
  110.     if limit is None:
  111.         if hasattr(sys, 'tracebacklimit'):
  112.             limit = sys.tracebacklimit
  113.         
  114.     
  115.     list = []
  116.     n = 0
  117.     while tb is not None and limit is None or n < limit:
  118.         f = tb.tb_frame
  119.         lineno = tb_lineno(tb)
  120.         co = f.f_code
  121.         filename = co.co_filename
  122.         name = co.co_name
  123.         line = linecache.getline(filename, lineno)
  124.         if line:
  125.             line = line.strip()
  126.         else:
  127.             line = None
  128.         list.append((filename, lineno, name, line))
  129.         tb = tb.tb_next
  130.         n = n + 1
  131.     return list
  132.  
  133.  
  134. def print_exception(etype, value, tb, limit = None, file = None):
  135.     '''Print exception up to \'limit\' stack trace entries from \'tb\' to \'file\'.
  136.  
  137.     This differs from print_tb() in the following ways: (1) if
  138.     traceback is not None, it prints a header "Traceback (most recent
  139.     call last):"; (2) it prints the exception type and value after the
  140.     stack trace; (3) if type is SyntaxError and value has the
  141.     appropriate format, it prints the line where the syntax error
  142.     occurred with a caret on the next line indicating the approximate
  143.     position of the error.
  144.     '''
  145.     if not file:
  146.         file = sys.stderr
  147.     
  148.     if tb:
  149.         _print(file, 'Traceback (most recent call last):')
  150.         print_tb(tb, limit, file)
  151.     
  152.     lines = format_exception_only(etype, value)
  153.     for line in lines[:-1]:
  154.         _print(file, line, ' ')
  155.     
  156.     _print(file, lines[-1], '')
  157.  
  158.  
  159. def format_exception(etype, value, tb, limit = None):
  160.     '''Format a stack trace and the exception information.
  161.  
  162.     The arguments have the same meaning as the corresponding arguments
  163.     to print_exception().  The return value is a list of strings, each
  164.     ending in a newline and some containing internal newlines.  When
  165.     these lines are concatenated and printed, exactly the same text is
  166.     printed as does print_exception().
  167.     '''
  168.     if tb:
  169.         list = [
  170.             'Traceback (most recent call last):\n']
  171.         list = list + format_tb(tb, limit)
  172.     else:
  173.         list = []
  174.     list = list + format_exception_only(etype, value)
  175.     return list
  176.  
  177.  
  178. def format_exception_only(etype, value):
  179.     '''Format the exception part of a traceback.
  180.  
  181.     The arguments are the exception type and value such as given by
  182.     sys.last_type and sys.last_value. The return value is a list of
  183.     strings, each ending in a newline.  Normally, the list contains a
  184.     single string; however, for SyntaxError exceptions, it contains
  185.     several lines that (when printed) display detailed information
  186.     about where the syntax error occurred.  The message indicating
  187.     which exception occurred is the always last string in the list.
  188.     '''
  189.     list = []
  190.     if type(etype) == types.ClassType:
  191.         stype = etype.__name__
  192.     else:
  193.         stype = etype
  194.     if value is None:
  195.         list.append(str(stype) + '\n')
  196.     elif etype is SyntaxError:
  197.         
  198.         try:
  199.             (filename, lineno, offset, line) = (msg,)
  200.         except:
  201.             pass
  202.  
  203.         if not filename:
  204.             filename = '<string>'
  205.         
  206.         list.append('  File "%s", line %d\n' % (filename, lineno))
  207.         if line is not None:
  208.             i = 0
  209.             while i < len(line) and line[i].isspace():
  210.                 i = i + 1
  211.             list.append('    %s\n' % line.strip())
  212.             if offset is not None:
  213.                 s = '    '
  214.                 for c in line[i:offset - 1]:
  215.                     if c.isspace():
  216.                         s = s + c
  217.                     else:
  218.                         s = s + ' '
  219.                 
  220.                 list.append('%s^\n' % s)
  221.             
  222.             value = msg
  223.         
  224.     
  225.     s = _some_str(value)
  226.     if s:
  227.         list.append('%s: %s\n' % (str(stype), s))
  228.     else:
  229.         list.append('%s\n' % str(stype))
  230.     return list
  231.  
  232.  
  233. def _some_str(value):
  234.     
  235.     try:
  236.         return str(value)
  237.     except:
  238.         return '<unprintable %s object>' % type(value).__name__
  239.  
  240.  
  241.  
  242. def print_exc(limit = None, file = None):
  243.     """Shorthand for 'print_exception(sys.exc_type, sys.exc_value, sys.exc_traceback, limit, file)'.
  244.     (In fact, it uses sys.exc_info() to retrieve the same information
  245.     in a thread-safe way.)"""
  246.     if not file:
  247.         file = sys.stderr
  248.     
  249.     
  250.     try:
  251.         (etype, value, tb) = sys.exc_info()
  252.         print_exception(etype, value, tb, limit, file)
  253.     finally:
  254.         etype = value = tb = None
  255.  
  256.  
  257.  
  258. def print_last(limit = None, file = None):
  259.     """This is a shorthand for 'print_exception(sys.last_type,
  260.     sys.last_value, sys.last_traceback, limit, file)'."""
  261.     if not file:
  262.         file = sys.stderr
  263.     
  264.     print_exception(sys.last_type, sys.last_value, sys.last_traceback, limit, file)
  265.  
  266.  
  267. def print_stack(f = None, limit = None, file = None):
  268.     """Print a stack trace from its invocation point.
  269.  
  270.     The optional 'f' argument can be used to specify an alternate
  271.     stack frame at which to start. The optional 'limit' and 'file'
  272.     arguments have the same meaning as for print_exception().
  273.     """
  274.     if f is None:
  275.         
  276.         try:
  277.             raise ZeroDivisionError
  278.         except ZeroDivisionError:
  279.             f = sys.exc_info()[2].tb_frame.f_back
  280.  
  281.     
  282.     print_list(extract_stack(f, limit), file)
  283.  
  284.  
  285. def format_stack(f = None, limit = None):
  286.     """Shorthand for 'format_list(extract_stack(f, limit))'."""
  287.     if f is None:
  288.         
  289.         try:
  290.             raise ZeroDivisionError
  291.         except ZeroDivisionError:
  292.             f = sys.exc_info()[2].tb_frame.f_back
  293.  
  294.     
  295.     return format_list(extract_stack(f, limit))
  296.  
  297.  
  298. def extract_stack(f = None, limit = None):
  299.     """Extract the raw traceback from the current stack frame.
  300.  
  301.     The return value has the same format as for extract_tb().  The
  302.     optional 'f' and 'limit' arguments have the same meaning as for
  303.     print_stack().  Each item in the list is a quadruple (filename,
  304.     line number, function name, text), and the entries are in order
  305.     from oldest to newest stack frame.
  306.     """
  307.     if f is None:
  308.         
  309.         try:
  310.             raise ZeroDivisionError
  311.         except ZeroDivisionError:
  312.             f = sys.exc_info()[2].tb_frame.f_back
  313.  
  314.     
  315.     if limit is None:
  316.         if hasattr(sys, 'tracebacklimit'):
  317.             limit = sys.tracebacklimit
  318.         
  319.     
  320.     list = []
  321.     n = 0
  322.     while f is not None and limit is None or n < limit:
  323.         lineno = f.f_lineno
  324.         co = f.f_code
  325.         filename = co.co_filename
  326.         name = co.co_name
  327.         line = linecache.getline(filename, lineno)
  328.         if line:
  329.             line = line.strip()
  330.         else:
  331.             line = None
  332.         list.append((filename, lineno, name, line))
  333.         f = f.f_back
  334.         n = n + 1
  335.     list.reverse()
  336.     return list
  337.  
  338.  
  339. def tb_lineno(tb):
  340.     '''Calculate correct line number of traceback given in tb.
  341.  
  342.     Even works with -O on.
  343.     '''
  344.     c = tb.tb_frame.f_code
  345.     if not hasattr(c, 'co_lnotab'):
  346.         return tb.tb_lineno
  347.     
  348.     tab = c.co_lnotab
  349.     line = c.co_firstlineno
  350.     stopat = tb.tb_lasti
  351.     addr = 0
  352.     for i in range(0, len(tab), 2):
  353.         addr = addr + ord(tab[i])
  354.         if addr > stopat:
  355.             break
  356.         
  357.         line = line + ord(tab[i + 1])
  358.     
  359.     return line
  360.  
  361.