Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
traceback Namespace Reference

Functions

def print_list
 
def format_list
 
def print_tb
 
def format_tb
 
def extract_tb
 
def print_exception
 
def format_exception
 
def format_exception_only
 
def print_exc
 
def print_last
 
def print_stack
 
def format_stack
 
def extract_stack
 
def tb_lineno
 

Variables

list __all__
 

Detailed Description

Extract, format and print information about Python stack traces.

Function Documentation

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 246 of file traceback.py.

References linecache.getline().

247 def extract_stack(f=None, limit = None):
248  """Extract the raw traceback from the current stack frame.
249 
250  The return value has the same format as for extract_tb(). The
251  optional 'f' and 'limit' arguments have the same meaning as for
252  print_stack(). Each item in the list is a quadruple (filename,
253  line number, function name, text), and the entries are in order
254  from oldest to newest stack frame.
255  """
256  if f is None:
257  try:
258  raise ZeroDivisionError
259  except ZeroDivisionError:
260  f = sys.exc_info()[2].tb_frame.f_back
261  if limit is None:
262  if hasattr(sys, 'tracebacklimit'):
263  limit = sys.tracebacklimit
264  list = []
265  n = 0
266  while f is not None and (limit is None or n < limit):
267  lineno = f.f_lineno # XXX Too bad if -O is used
268  co = f.f_code
269  filename = co.co_filename
270  name = co.co_name
271  line = linecache.getline(filename, lineno)
272  if line: line = line.strip()
273  else: line = None
274  list.append((filename, lineno, name, line))
275  f = f.f_back
276  n = n+1
277  list.reverse()
278  return list
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.

References linecache.getline(), and tb_lineno().

77 
78 def extract_tb(tb, limit = None):
79  """Return list of up to limit pre-processed entries from traceback.
80 
81  This is useful for alternate formatting of stack traces. If
82  'limit' is omitted or None, all entries are extracted. A
83  pre-processed stack trace entry is a quadruple (filename, line
84  number, function name, text) representing the information that is
85  usually printed for a stack trace. The text is a string with
86  leading and trailing whitespace stripped; if the source is not
87  available it is None.
88  """
89  if limit is None:
90  if hasattr(sys, 'tracebacklimit'):
91  limit = sys.tracebacklimit
92  list = []
93  n = 0
94  while tb is not None and (limit is None or n < limit):
95  f = tb.tb_frame
96  lineno = tb_lineno(tb)
97  co = f.f_code
98  filename = co.co_filename
99  name = co.co_name
100  line = linecache.getline(filename, lineno)
101  if line: line = line.strip()
102  else: line = None
103  list.append((filename, lineno, name, line))
104  tb = tb.tb_next
105  n = n+1
106  return list
107 
def traceback.format_exception (   etype,
  value,
  tb,
  limit = None 
)
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 129 of file traceback.py.

References format_exception_only(), and format_tb().

130 def format_exception(etype, value, tb, limit = None):
131  """Format a stack trace and the exception information.
132 
133  The arguments have the same meaning as the corresponding arguments
134  to print_exception(). The return value is a list of strings, each
135  ending in a newline and some containing internal newlines. When
136  these lines are concatenated and printed, exactly the same text is
137  printed as does print_exception().
138  """
139  if tb:
140  list = ['Traceback (most recent call last):\n']
141  list = list + format_tb(tb, limit)
142  else:
143  list = []
144  list = list + format_exception_only(etype, value)
145  return list
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 the always last string in the list.

Definition at line 146 of file traceback.py.

References locale.str().

147 def format_exception_only(etype, value):
148  """Format the exception part of a traceback.
149 
150  The arguments are the exception type and value such as given by
151  sys.last_type and sys.last_value. The return value is a list of
152  strings, each ending in a newline. Normally, the list contains a
153  single string; however, for SyntaxError exceptions, it contains
154  several lines that (when printed) display detailed information
155  about where the syntax error occurred. The message indicating
156  which exception occurred is the always last string in the list.
157  """
158  list = []
159  if type(etype) == types.ClassType:
160  stype = etype.__name__
161  else:
162  stype = etype
163  if value is None:
164  list.append(str(stype) + '\n')
165  else:
166  if etype is SyntaxError:
167  try:
168  msg, (filename, lineno, offset, line) = value
169  except:
170  pass
171  else:
172  if not filename: filename = "<string>"
173  list.append(' File "%s", line %d\n' %
174  (filename, lineno))
175  if line is not None:
176  i = 0
177  while i < len(line) and line[i].isspace():
178  i = i+1
179  list.append(' %s\n' % line.strip())
180  if offset is not None:
181  s = ' '
182  for c in line[i:offset-1]:
183  if c.isspace():
184  s = s + c
185  else:
186  s = s + ' '
187  list.append('%s^\n' % s)
188  value = msg
189  s = _some_str(value)
190  if s:
191  list.append('%s: %s\n' % (str(stype), s))
192  else:
193  list.append('%s\n' % str(stype))
194  return list
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 27 of file traceback.py.

27 
28 def format_list(extracted_list):
29  """Format a list of traceback entry tuples for printing.
30 
31  Given a list of tuples as returned by extract_tb() or
32  extract_stack(), return a list of strings ready for printing.
33  Each string in the resulting list corresponds to the item with the
34  same index in the argument list. Each string ends in a newline;
35  the strings may contain internal newlines as well, for those items
36  whose source text line is not None.
37  """
38  list = []
39  for filename, lineno, name, line in extracted_list:
40  item = ' File "%s", line %d, in %s\n' % (filename,lineno,name)
41  if line:
42  item = item + ' %s\n' % line.strip()
43  list.append(item)
44  return list
45 
def traceback.format_stack (   f = None,
  limit = None 
)
Shorthand for 'format_list(extract_stack(f, limit))'.

Definition at line 237 of file traceback.py.

References extract_stack(), and format_list().

238 def format_stack(f=None, limit=None):
239  """Shorthand for 'format_list(extract_stack(f, limit))'."""
240  if f is None:
241  try:
242  raise ZeroDivisionError
243  except ZeroDivisionError:
244  f = sys.exc_info()[2].tb_frame.f_back
245  return format_list(extract_stack(f, limit))
def traceback.format_tb (   tb,
  limit = None 
)
A shorthand for 'format_list(extract_stack(f, limit)).

Definition at line 73 of file traceback.py.

References extract_tb(), and format_list().

73 
74 def format_tb(tb, limit = None):
75  """A shorthand for 'format_list(extract_stack(f, limit))."""
76  return format_list(extract_tb(tb, limit))
def traceback.print_exc (   limit = None,
  file = None 
)
Shorthand for 'print_exception(sys.exc_type, sys.exc_value, sys.exc_traceback, limit, file)'.
(In fact, it uses sys.exc_info() to retrieve the same information
in a thread-safe way.)

Definition at line 202 of file traceback.py.

References print_exception().

203 def print_exc(limit=None, file=None):
204  """Shorthand for 'print_exception(sys.exc_type, sys.exc_value, sys.exc_traceback, limit, file)'.
205  (In fact, it uses sys.exc_info() to retrieve the same information
206  in a thread-safe way.)"""
207  if not file:
208  file = sys.stderr
209  try:
210  etype, value, tb = sys.exc_info()
211  print_exception(etype, value, tb, limit, file)
212  finally:
213  etype = value = tb = None
def traceback.print_exception (   etype,
  value,
  tb,
  limit = None,
  file = None 
)
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 108 of file traceback.py.

References format_exception_only(), and print_tb().

109 def print_exception(etype, value, tb, limit=None, file=None):
110  """Print exception up to 'limit' stack trace entries from 'tb' to 'file'.
111 
112  This differs from print_tb() in the following ways: (1) if
113  traceback is not None, it prints a header "Traceback (most recent
114  call last):"; (2) it prints the exception type and value after the
115  stack trace; (3) if type is SyntaxError and value has the
116  appropriate format, it prints the line where the syntax error
117  occurred with a caret on the next line indicating the approximate
118  position of the error.
119  """
120  if not file:
121  file = sys.stderr
122  if tb:
123  _print(file, 'Traceback (most recent call last):')
124  print_tb(tb, limit, file)
125  lines = format_exception_only(etype, value)
126  for line in lines[:-1]:
127  _print(file, line, ' ')
128  _print(file, lines[-1], '')
def traceback.print_last (   limit = None,
  file = None 
)
This is a shorthand for 'print_exception(sys.last_type,
sys.last_value, sys.last_traceback, limit, file)'.

Definition at line 214 of file traceback.py.

References print_exception().

215 def print_last(limit=None, file=None):
216  """This is a shorthand for 'print_exception(sys.last_type,
217  sys.last_value, sys.last_traceback, limit, file)'."""
218  if not file:
219  file = sys.stderr
220  print_exception(sys.last_type, sys.last_value, sys.last_traceback,
221  limit, file)
222 
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 16 of file traceback.py.

16 
17 def print_list(extracted_list, file=None):
18  """Print the list of tuples as returned by extract_tb() or
19  extract_stack() as a formatted stack trace to the given file."""
20  if not file:
21  file = sys.stderr
22  for filename, lineno, name, line in extracted_list:
23  _print(file,
24  ' File "%s", line %d, in %s' % (filename,lineno,name))
25  if line:
26  _print(file, ' %s' % line.strip())
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 223 of file traceback.py.

References extract_stack(), and print_list().

224 def print_stack(f=None, limit=None, file=None):
225  """Print a stack trace from its invocation point.
226 
227  The optional 'f' argument can be used to specify an alternate
228  stack frame at which to start. The optional 'limit' and 'file'
229  arguments have the same meaning as for print_exception().
230  """
231  if f is None:
232  try:
233  raise ZeroDivisionError
234  except ZeroDivisionError:
235  f = sys.exc_info()[2].tb_frame.f_back
236  print_list(extract_stack(f, limit), file)
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 46 of file traceback.py.

References linecache.getline(), and tb_lineno().

46 
47 def print_tb(tb, limit=None, file=None):
48  """Print up to 'limit' stack trace entries from the traceback 'tb'.
49 
50  If 'limit' is omitted or None, all entries are printed. If 'file'
51  is omitted or None, the output goes to sys.stderr; otherwise
52  'file' should be an open file or file-like object with a write()
53  method.
54  """
55  if not file:
56  file = sys.stderr
57  if limit is None:
58  if hasattr(sys, 'tracebacklimit'):
59  limit = sys.tracebacklimit
60  n = 0
61  while tb is not None and (limit is None or n < limit):
62  f = tb.tb_frame
63  lineno = tb_lineno(tb)
64  co = f.f_code
65  filename = co.co_filename
66  name = co.co_name
67  _print(file,
68  ' File "%s", line %d, in %s' % (filename,lineno,name))
69  line = linecache.getline(filename, lineno)
70  if line: _print(file, ' ' + line.strip())
71  tb = tb.tb_next
72  n = n+1
def traceback.tb_lineno (   tb)
Calculate correct line number of traceback given in tb.

Even works with -O on.

Definition at line 279 of file traceback.py.

280 def tb_lineno(tb):
281  """Calculate correct line number of traceback given in tb.
282 
283  Even works with -O on.
284  """
285  # Coded by Marc-Andre Lemburg from the example of PyCode_Addr2Line()
286  # in compile.c.
287  # Revised version by Jim Hugunin to work with JPython too.
288 
289  c = tb.tb_frame.f_code
290  if not hasattr(c, 'co_lnotab'):
291  return tb.tb_lineno
292 
293  tab = c.co_lnotab
294  line = c.co_firstlineno
295  stopat = tb.tb_lasti
296  addr = 0
297  for i in range(0, len(tab), 2):
298  addr = addr + ord(tab[i])
299  if addr > stopat:
300  break
301  line = line + ord(tab[i+1])
302  return line

Variable Documentation

list __all__
Initial value:
1 = ['extract_stack', 'extract_tb', 'format_exception',
2  'format_exception_only', 'format_list', 'format_stack',
3  'format_tb', 'print_exc', 'print_exception', 'print_last',
4  'print_stack', 'print_tb', 'tb_lineno']

Definition at line 7 of file traceback.py.