Back to index

python3.2  3.2.2
Functions | Variables
traceback Namespace Reference

Functions

def _print
def print_list
def format_list
def print_tb
def format_tb
def extract_tb
def _iter_chain
def print_exception
def format_exception
def format_exception_only
def _format_final_exc_line
def _some_str
def print_exc
def format_exc
def print_last
def print_stack
def format_stack
def extract_stack

Variables

list __all__
tuple _cause_message
tuple _context_message

Detailed Description

Extract, format and print information about Python stack traces.

Function Documentation

def traceback._format_final_exc_line (   etype,
  value 
) [private]

Definition at line 238 of file traceback.py.

00238 
00239 def _format_final_exc_line(etype, value):
00240     valuestr = _some_str(value)
00241     if value is None or not valuestr:
00242         line = "%s\n" % etype
00243     else:
00244         line = "%s: %s\n" % (etype, valuestr)
00245     return line

Here is the call graph for this function:

Here is the caller graph for this function:

def traceback._iter_chain (   exc,
  custom_tb = None,
  seen = None 
) [private]

Definition at line 117 of file traceback.py.

00117 
00118 def _iter_chain(exc, custom_tb=None, seen=None):
00119     if seen is None:
00120         seen = set()
00121     seen.add(exc)
00122     its = []
00123     cause = exc.__cause__
00124     if cause is not None and cause not in seen:
00125         its.append(_iter_chain(cause, None, seen))
00126         its.append([(_cause_message, None)])
00127     else:
00128         context = exc.__context__
00129         if context is not None and context not in seen:
00130             its.append(_iter_chain(context, None, seen))
00131             its.append([(_context_message, None)])
00132     its.append([(exc, custom_tb or exc.__traceback__)])
00133     # itertools.chain is in an extension module and may be unavailable
00134     for it in its:
00135         for x in it:
00136             yield x
00137 

Here is the call graph for this function:

Here is the caller graph for this function:

def traceback._print (   file,
  str = '',
  terminator = '\n' 
) [private]

Definition at line 11 of file traceback.py.

00011 
00012 def _print(file, str='', terminator='\n'):
00013     file.write(str+terminator)
00014 

Here is the caller graph for this function:

def traceback._some_str (   value) [private]

Definition at line 246 of file traceback.py.

00246 
00247 def _some_str(value):
00248     try:
00249         return str(value)
00250     except:
00251         return '<unprintable %s object>' % type(value).__name__
00252 

Here is the caller graph for this function:

def traceback.extract_stack (   f = None,
  limit = None 
)
Extract the raw traceback from the current stack frame.

The return value has the same format as for extract_tb().  The
optional 'f' and 'limit' arguments have the same meaning as for
print_stack().  Each item in the list is a quadruple (filename,
line number, function name, text), and the entries are in order
from oldest to newest stack frame.

Definition at line 308 of file traceback.py.

00308 
00309 def extract_stack(f=None, limit=None):
00310     """Extract the raw traceback from the current stack frame.
00311 
00312     The return value has the same format as for extract_tb().  The
00313     optional 'f' and 'limit' arguments have the same meaning as for
00314     print_stack().  Each item in the list is a quadruple (filename,
00315     line number, function name, text), and the entries are in order
00316     from oldest to newest stack frame.
00317     """
00318     if f is None:
00319         try:
00320             raise ZeroDivisionError
00321         except ZeroDivisionError:
00322             f = sys.exc_info()[2].tb_frame.f_back
00323     if limit is None:
00324         if hasattr(sys, 'tracebacklimit'):
00325             limit = sys.tracebacklimit
00326     list = []
00327     n = 0
00328     while f is not None and (limit is None or n < limit):
00329         lineno = f.f_lineno
00330         co = f.f_code
00331         filename = co.co_filename
00332         name = co.co_name
00333         linecache.checkcache(filename)
00334         line = linecache.getline(filename, lineno, f.f_globals)
00335         if line: line = line.strip()
00336         else: line = None
00337         list.append((filename, lineno, name, line))
00338         f = f.f_back
00339         n = n+1
00340     list.reverse()
00341     return list

Here is the call graph for this function:

Here is the caller graph for this function:

def traceback.extract_tb (   tb,
  limit = None 
)
Return list of up to limit pre-processed entries from traceback.

This is useful for alternate formatting of stack traces.  If
'limit' is omitted or None, all entries are extracted.  A
pre-processed stack trace entry is a quadruple (filename, line
number, function name, text) representing the information that is
usually printed for a stack trace.  The text is a string with
leading and trailing whitespace stripped; if the source is not
available it is None.

Definition at line 77 of file traceback.py.

00077 
00078 def extract_tb(tb, limit=None):
00079     """Return list of up to limit pre-processed entries from traceback.
00080 
00081     This is useful for alternate formatting of stack traces.  If
00082     'limit' is omitted or None, all entries are extracted.  A
00083     pre-processed stack trace entry is a quadruple (filename, line
00084     number, function name, text) representing the information that is
00085     usually printed for a stack trace.  The text is a string with
00086     leading and trailing whitespace stripped; if the source is not
00087     available it is None.
00088     """
00089     if limit is None:
00090         if hasattr(sys, 'tracebacklimit'):
00091             limit = sys.tracebacklimit
00092     list = []
00093     n = 0
00094     while tb is not None and (limit is None or n < limit):
00095         f = tb.tb_frame
00096         lineno = tb.tb_lineno
00097         co = f.f_code
00098         filename = co.co_filename
00099         name = co.co_name
00100         linecache.checkcache(filename)
00101         line = linecache.getline(filename, lineno, f.f_globals)
00102         if line: line = line.strip()
00103         else: line = None
00104         list.append((filename, lineno, name, line))
00105         tb = tb.tb_next
00106         n = n+1
00107     return list
00108 

Here is the call graph for this function:

Here is the caller graph for this function:

def traceback.format_exc (   limit = None,
  chain = True 
)
Like print_exc() but return a string.

Definition at line 264 of file traceback.py.

00264 
00265 def format_exc(limit=None, chain=True):
00266     """Like print_exc() but return a string."""
00267     try:
00268         etype, value, tb = sys.exc_info()
00269         return ''.join(
00270             format_exception(etype, value, tb, limit, chain))
00271     finally:
00272         etype = value = tb = None
00273 

Here is the call graph for this function:

Here is the caller graph for this function:

def traceback.format_exception (   etype,
  value,
  tb,
  limit = None,
  chain = True 
)
Format a stack trace and the exception information.

The arguments have the same meaning as the corresponding arguments
to print_exception().  The return value is a list of strings, each
ending in a newline and some containing internal newlines.  When
these lines are concatenated and printed, exactly the same text is
printed as does print_exception().

Definition at line 166 of file traceback.py.

00166 
00167 def format_exception(etype, value, tb, limit=None, chain=True):
00168     """Format a stack trace and the exception information.
00169 
00170     The arguments have the same meaning as the corresponding arguments
00171     to print_exception().  The return value is a list of strings, each
00172     ending in a newline and some containing internal newlines.  When
00173     these lines are concatenated and printed, exactly the same text is
00174     printed as does print_exception().
00175     """
00176     list = []
00177     if chain:
00178         values = _iter_chain(value, tb)
00179     else:
00180         values = [(value, tb)]
00181     for value, tb in values:
00182         if isinstance(value, str):
00183             list.append(value + '\n')
00184             continue
00185         if tb:
00186             list.append('Traceback (most recent call last):\n')
00187             list.extend(format_tb(tb, limit))
00188         list.extend(format_exception_only(type(value), value))
00189     return list

Here is the call graph for this function:

Here is the caller graph for this function:

def traceback.format_exception_only (   etype,
  value 
)
Format the exception part of a traceback.

The arguments are the exception type and value such as given by
sys.last_type and sys.last_value. The return value is a list of
strings, each ending in a newline.

Normally, the list contains a single string; however, for
SyntaxError exceptions, it contains several lines that (when
printed) display detailed information about where the syntax
error occurred.

The message indicating which exception occurred is always the last
string in the list.

Definition at line 190 of file traceback.py.

00190 
00191 def format_exception_only(etype, value):
00192     """Format the exception part of a traceback.
00193 
00194     The arguments are the exception type and value such as given by
00195     sys.last_type and sys.last_value. The return value is a list of
00196     strings, each ending in a newline.
00197 
00198     Normally, the list contains a single string; however, for
00199     SyntaxError exceptions, it contains several lines that (when
00200     printed) display detailed information about where the syntax
00201     error occurred.
00202 
00203     The message indicating which exception occurred is always the last
00204     string in the list.
00205 
00206     """
00207     # Gracefully handle (the way Python 2.4 and earlier did) the case of
00208     # being called with (None, None).
00209     if etype is None:
00210         return [_format_final_exc_line(etype, value)]
00211 
00212     stype = etype.__name__
00213     smod = etype.__module__
00214     if smod not in ("__main__", "builtins"):
00215         stype = smod + '.' + stype
00216 
00217     if not issubclass(etype, SyntaxError):
00218         return [_format_final_exc_line(stype, value)]
00219 
00220     # It was a syntax error; show exactly where the problem was found.
00221     lines = []
00222     filename = value.filename or "<string>"
00223     lineno = str(value.lineno) or '?'
00224     lines.append('  File "%s", line %s\n' % (filename, lineno))
00225     badline = value.text
00226     offset = value.offset
00227     if badline is not None:
00228         lines.append('    %s\n' % badline.strip())
00229         if offset is not None:
00230             caretspace = badline.rstrip('\n')[:offset].lstrip()
00231             # non-space whitespace (likes tabs) must be kept for alignment
00232             caretspace = ((c.isspace() and c or ' ') for c in caretspace)
00233             # only three spaces to account for offset1 == pos 0
00234             lines.append('   %s^\n' % ''.join(caretspace))
00235     msg = value.msg or "<no detail available>"
00236     lines.append("%s: %s\n" % (stype, msg))
00237     return lines

Here is the call graph for this function:

Here is the caller graph for this function:

def traceback.format_list (   extracted_list)
Format a list of traceback entry tuples for printing.

Given a list of tuples as returned by extract_tb() or
extract_stack(), return a list of strings ready for printing.
Each string in the resulting list corresponds to the item with the
same index in the argument list.  Each string ends in a newline;
the strings may contain internal newlines as well, for those items
whose source text line is not None.

Definition at line 26 of file traceback.py.

00026 
00027 def format_list(extracted_list):
00028     """Format a list of traceback entry tuples for printing.
00029 
00030     Given a list of tuples as returned by extract_tb() or
00031     extract_stack(), return a list of strings ready for printing.
00032     Each string in the resulting list corresponds to the item with the
00033     same index in the argument list.  Each string ends in a newline;
00034     the strings may contain internal newlines as well, for those items
00035     whose source text line is not None.
00036     """
00037     list = []
00038     for filename, lineno, name, line in extracted_list:
00039         item = '  File "%s", line %d, in %s\n' % (filename,lineno,name)
00040         if line:
00041             item = item + '    %s\n' % line.strip()
00042         list.append(item)
00043     return list
00044 

Here is the caller graph for this function:

def traceback.format_stack (   f = None,
  limit = None 
)
Shorthand for 'format_list(extract_stack(f, limit))'.

Definition at line 299 of file traceback.py.

00299 
00300 def format_stack(f=None, limit=None):
00301     """Shorthand for 'format_list(extract_stack(f, limit))'."""
00302     if f is None:
00303         try:
00304             raise ZeroDivisionError
00305         except ZeroDivisionError:
00306             f = sys.exc_info()[2].tb_frame.f_back
00307     return format_list(extract_stack(f, limit))

Here is the call graph for this function:

Here is the caller graph for this function:

def traceback.format_tb (   tb,
  limit = None 
)
A shorthand for 'format_list(extract_stack(f, limit)).

Definition at line 73 of file traceback.py.

00073 
00074 def format_tb(tb, limit=None):
00075     """A shorthand for 'format_list(extract_stack(f, limit))."""
00076     return format_list(extract_tb(tb, limit))

Here is the call graph for this function:

Here is the caller graph for this function:

def traceback.print_exc (   limit = None,
  file = None,
  chain = True 
)
Shorthand for 'print_exception(*sys.exc_info(), limit, file)'.

Definition at line 253 of file traceback.py.

00253 
00254 def print_exc(limit=None, file=None, chain=True):
00255     """Shorthand for 'print_exception(*sys.exc_info(), limit, file)'."""
00256     if file is None:
00257         file = sys.stderr
00258     try:
00259         etype, value, tb = sys.exc_info()
00260         print_exception(etype, value, tb, limit, file, chain)
00261     finally:
00262         etype = value = tb = None
00263 

Here is the call graph for this function:

Here is the caller graph for this function:

def traceback.print_exception (   etype,
  value,
  tb,
  limit = None,
  file = None,
  chain = True 
)
Print exception up to 'limit' stack trace entries from 'tb' to 'file'.

This differs from print_tb() in the following ways: (1) if
traceback is not None, it prints a header "Traceback (most recent
call last):"; (2) it prints the exception type and value after the
stack trace; (3) if type is SyntaxError and value has the
appropriate format, it prints the line where the syntax error
occurred with a caret on the next line indicating the approximate
position of the error.

Definition at line 138 of file traceback.py.

00138 
00139 def print_exception(etype, value, tb, limit=None, file=None, chain=True):
00140     """Print exception up to 'limit' stack trace entries from 'tb' to 'file'.
00141 
00142     This differs from print_tb() in the following ways: (1) if
00143     traceback is not None, it prints a header "Traceback (most recent
00144     call last):"; (2) it prints the exception type and value after the
00145     stack trace; (3) if type is SyntaxError and value has the
00146     appropriate format, it prints the line where the syntax error
00147     occurred with a caret on the next line indicating the approximate
00148     position of the error.
00149     """
00150     if file is None:
00151         file = sys.stderr
00152     if chain:
00153         values = _iter_chain(value, tb)
00154     else:
00155         values = [(value, tb)]
00156     for value, tb in values:
00157         if isinstance(value, str):
00158             _print(file, value)
00159             continue
00160         if tb:
00161             _print(file, 'Traceback (most recent call last):')
00162             print_tb(tb, limit, file)
00163         lines = format_exception_only(type(value), value)
00164         for line in lines:
00165             _print(file, line, '')

Here is the call graph for this function:

Here is the caller graph for this function:

def traceback.print_last (   limit = None,
  file = None,
  chain = True 
)
This is a shorthand for 'print_exception(sys.last_type,
sys.last_value, sys.last_traceback, limit, file)'.

Definition at line 274 of file traceback.py.

00274 
00275 def print_last(limit=None, file=None, chain=True):
00276     """This is a shorthand for 'print_exception(sys.last_type,
00277     sys.last_value, sys.last_traceback, limit, file)'."""
00278     if not hasattr(sys, "last_type"):
00279         raise ValueError("no last exception")
00280     if file is None:
00281         file = sys.stderr
00282     print_exception(sys.last_type, sys.last_value, sys.last_traceback,
00283                     limit, file, chain)
00284 

Here is the call graph for this function:

def traceback.print_list (   extracted_list,
  file = None 
)
Print the list of tuples as returned by extract_tb() or
extract_stack() as a formatted stack trace to the given file.

Definition at line 15 of file traceback.py.

00015 
00016 def print_list(extracted_list, file=None):
00017     """Print the list of tuples as returned by extract_tb() or
00018     extract_stack() as a formatted stack trace to the given file."""
00019     if file is None:
00020         file = sys.stderr
00021     for filename, lineno, name, line in extracted_list:
00022         _print(file,
00023                '  File "%s", line %d, in %s' % (filename,lineno,name))
00024         if line:
00025             _print(file, '    %s' % line.strip())

Here is the call graph for this function:

Here is the caller graph for this function:

def traceback.print_stack (   f = None,
  limit = None,
  file = None 
)
Print a stack trace from its invocation point.

The optional 'f' argument can be used to specify an alternate
stack frame at which to start. The optional 'limit' and 'file'
arguments have the same meaning as for print_exception().

Definition at line 285 of file traceback.py.

00285 
00286 def print_stack(f=None, limit=None, file=None):
00287     """Print a stack trace from its invocation point.
00288 
00289     The optional 'f' argument can be used to specify an alternate
00290     stack frame at which to start. The optional 'limit' and 'file'
00291     arguments have the same meaning as for print_exception().
00292     """
00293     if f is None:
00294         try:
00295             raise ZeroDivisionError
00296         except ZeroDivisionError:
00297             f = sys.exc_info()[2].tb_frame.f_back
00298     print_list(extract_stack(f, limit), file)

Here is the call graph for this function:

Here is the caller graph for this function:

def traceback.print_tb (   tb,
  limit = None,
  file = None 
)
Print up to 'limit' stack trace entries from the traceback 'tb'.

If 'limit' is omitted or None, all entries are printed.  If 'file'
is omitted or None, the output goes to sys.stderr; otherwise
'file' should be an open file or file-like object with a write()
method.

Definition at line 45 of file traceback.py.

00045 
00046 def print_tb(tb, limit=None, file=None):
00047     """Print up to 'limit' stack trace entries from the traceback 'tb'.
00048 
00049     If 'limit' is omitted or None, all entries are printed.  If 'file'
00050     is omitted or None, the output goes to sys.stderr; otherwise
00051     'file' should be an open file or file-like object with a write()
00052     method.
00053     """
00054     if file is None:
00055         file = sys.stderr
00056     if limit is None:
00057         if hasattr(sys, 'tracebacklimit'):
00058             limit = sys.tracebacklimit
00059     n = 0
00060     while tb is not None and (limit is None or n < limit):
00061         f = tb.tb_frame
00062         lineno = tb.tb_lineno
00063         co = f.f_code
00064         filename = co.co_filename
00065         name = co.co_name
00066         _print(file,
00067                '  File "%s", line %d, in %s' % (filename, lineno, name))
00068         linecache.checkcache(filename)
00069         line = linecache.getline(filename, lineno, f.f_globals)
00070         if line: _print(file, '    ' + line.strip())
00071         tb = tb.tb_next
00072         n = n+1

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Initial value:
00001 ['extract_stack', 'extract_tb', 'format_exception',
00002            'format_exception_only', 'format_list', 'format_stack',
00003            'format_tb', 'print_exc', 'format_exc', 'print_exception',
00004            'print_last', 'print_stack', 'print_tb']

Definition at line 6 of file traceback.py.

Initial value:
00001 (
00002     "\nThe above exception was the direct cause "
00003     "of the following exception:\n")

Definition at line 109 of file traceback.py.

Initial value:
00001 (
00002     "\nDuring handling of the above exception, "
00003     "another exception occurred:\n")

Definition at line 113 of file traceback.py.