Here are the examples of the python api sys.last_traceback taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
19 Examples
5
Example 1
Project: python2-trepan Source File: post_mortem.py
def get_last_or_frame_exception():
"""Intended to be used going into post mortem routines. If
sys.last_traceback is set, we will return that and assume that
this is what post-mortem will want. If sys.last_traceback has not
been set, then perhaps we *about* to raise an error and are
fielding an exception. So assume that sys.exc_info()[2]
is where we want to look."""
try:
if inspect.istraceback(sys.last_traceback):
# We do have a traceback so prefer that.
return sys.last_type, sys.last_value, sys.last_traceback
except AttributeError:
pass
return sys.exc_info()
3
Example 2
def distb(tb=None):
"""Disassemble a traceback (default: last traceback)."""
if tb is None:
try:
tb = sys.last_traceback
except AttributeError:
raise RuntimeError, "no last traceback to disassemble"
while tb.tb_next: tb = tb.tb_next
disassemble(tb.tb_frame.f_code, tb.tb_lasti)
3
Example 3
def get_stack(self, tb):
if tb is None:
tb = sys.last_traceback
stack = []
if tb and tb.tb_frame is None:
tb = tb.tb_next
while tb is not None:
stack.append((tb.tb_frame, tb.tb_lineno))
tb = tb.tb_next
return stack
3
Example 4
def distb(tb=None):
"""Disassemble a traceback (default: last traceback)."""
if tb is None:
try:
tb = sys.last_traceback
except AttributeError:
raise RuntimeError("no last traceback to disassemble")
while tb.tb_next: tb = tb.tb_next
disassemble(tb.tb_frame.f_code, tb.tb_lasti)
3
Example 5
Project: brython Source File: test_dis.py
def test_dis_none(self):
try:
del sys.last_traceback
except AttributeError:
pass
self.assertRaises(RuntimeError, dis.dis, None)
3
Example 6
Project: brython Source File: test_dis.py
def test_dis_traceback(self):
try:
del sys.last_traceback
except AttributeError:
pass
try:
1/0
except Exception as e:
tb = e.__traceback__
sys.last_traceback = tb
tb_dis = self.get_disassemble_as_string(tb.tb_frame.f_code, tb.tb_lasti)
self.do_disassembly_test(None, tb_dis)
3
Example 7
def distb(tb=None):
"""Disassemble a traceback (default: last traceback)."""
if not tb:
try:
tb = sys.last_traceback
except AttributeError:
raise RuntimeError, "no last traceback to disassemble"
while tb.tb_next: tb = tb.tb_next
disassemble(tb.tb_frame.f_code, tb.tb_lasti)
3
Example 8
def distb(tb=None):
"""Disassemble a traceback (default: last traceback)."""
if not tb:
try:
tb = sys.last_traceback
except AttributeError:
raise RuntimeError, "no last traceback to disassemble"
while tb.tb_next: tb = tb.tb_next
disassemble(tb.tb_frame.f_code, tb.tb_lasti)
3
Example 9
def distb(tb=None, *, file=None):
"""Disassemble a traceback (default: last traceback)."""
if tb is None:
try:
tb = sys.last_traceback
except AttributeError:
raise RuntimeError("no last traceback to disassemble")
while tb.tb_next: tb = tb.tb_next
disassemble(tb.tb_frame.f_code, tb.tb_lasti, file=file)
3
Example 10
def distb(tb=None):
"""Disassemble a traceback (default: last traceback)."""
if tb is None:
try:
tb = sys.last_traceback
except AttributeError:
raise RuntimeError("no last traceback to disassemble")
while tb.tb_next:
tb = tb.tb_next
return disassemble(tb.tb_frame.f_code, tb.tb_lasti)
2
Example 11
Project: flexx Source File: test_handlers.py
def test_exceptions():
h = event.HasEvents()
@h.connect('foo')
def handle_foo(*events):
1/0
h.emit('foo', {})
sys.last_traceback = None
assert sys.last_traceback is None
# No exception should be thrown here
event.loop.iter()
event.loop.iter()
# But we should have prepared for PM debugging
if sys.version_info[0] >= 3: # not sure why
assert sys.last_traceback
# Its different for a direct call
with raises(ZeroDivisionError):
handle_foo()
0
Example 12
def showsyntaxerror(self, filename=None):
"""Display the syntax error that just occurred.
This doesn't display a stack trace because there isn't one.
If a filename is given, it is stuffed in the exception instead
of what was there before (because Python's parser always uses
"<string>" when reading from a string).
The output is written by self.write(), below.
"""
type, value, sys.last_traceback = sys.exc_info()
sys.last_type = type
sys.last_value = value
if filename and type is SyntaxError:
# Work hard to stuff the correct filename in the exception
try:
msg, (dummy_filename, lineno, offset, line) = value
except:
# Not the format we expect; leave it alone
pass
else:
# Stuff in the right filename
value = SyntaxError(msg, (filename, lineno, offset, line))
sys.last_value = value
list = traceback.format_exception_only(type, value)
map(self.write, list)
0
Example 13
def pm():
post_mortem(sys.last_traceback)
0
Example 14
def syntaxerror(self):
"""Return nicely formatted syntax error. """
etype, value, sys.last_traceback = sys.exc_info()
sys.last_type = etype
sys.last_value = value
if etype is SyntaxError:
try:
msg, (dummy_filename, line, offset, source) = value
except:
pass
else:
value = SyntaxError(msg, (self._file, line, offset, source))
sys.last_value = value
text = [self._header]
text = text + traceback.format_exception_only(etype, value)
return ''.join(text), line
0
Example 15
def showsyntaxerror(self, filename=None):
"""Display the syntax error that just occurred.
This doesn't display a stack trace because there isn't one.
If a filename is given, it is stuffed in the exception instead
of what was there before (because Python's parser always uses
"<string>" when reading from a string).
The output is written by self.write(), below.
"""
type, value, sys.last_traceback = sys.exc_info()
sys.last_type = type
sys.last_value = value
if filename and type is SyntaxError:
# Work hard to stuff the correct filename in the exception
try:
msg, (dummy_filename, lineno, offset, line) = value.args
except ValueError:
# Not the format we expect; leave it alone
pass
else:
# Stuff in the right filename
value = SyntaxError(msg, (filename, lineno, offset, line))
sys.last_value = value
lines = traceback.format_exception_only(type, value)
self.write(''.join(lines))
0
Example 16
def spm():
spost_mortem(sys.last_traceback)
0
Example 17
def pm():
"""use our custom debugger"""
dbg = Debugger(sys.last_traceback)
dbg.start()
0
Example 18
def log_function(self, stream_name, properties={},
log_function_stack_trace=False,
log_exception_stack_trace=False,
namespace=None):
"""
Logs each call to the function as an event in the stream with name
`stream_name`. If `log_stack_trace` is set, it will log the stack trace
under the `stack_trace` key. `properties` is an optional mapping fron key
name to some function which expects the same arguments as the function
`function` being decorated. The event will be populated with keys in
`properties` mapped to the return values of the
`properties[key_name](*args, **kwargs)`.
Usage:
@kronos_client.log_function('mystreamname',
properties={'a': lambda x, y: x,
'b': lambda x, y: y})
def myfunction(a, b):
<some code here>
"""
namespace = namespace or self.namespace
def decorator(function):
@functools.wraps(function)
def wrapper(*args, **kwargs):
event = {}
start_time = time.time()
if log_function_stack_trace:
event['stack_trace'] = traceback.extract_stack()
try:
return function(*args, **kwargs)
except Exception as exception:
self._log_exception(event, exception,
(sys.last_traceback if log_exception_stack_trace
else None))
raise exception
finally:
event['duration'] = time.time() - start_time
for key, value_getter in properties.iteritems():
event[key] = value_getter(*args, **kwargs)
self.put({stream_name: [event]}, namespace=namespace)
return wrapper
return decorator
0
Example 19
Project: chronology Source File: client.py
@contextmanager
def log_scope(self, stream_name, properties={}, log_scope_stack_trace=False,
log_exception_stack_trace=False, namespace=None):
"""
Identical to `log_function` except that `log_scope` is used to log blocks
of code. The API is identical except that keys in `properties` are mapped to
real values rather than getter functions. Usage:
with kronos_client.log_scope('mystreamname', properties={ 'lol':'cat' },
log_scope_stack_trace=True):
<some code here>
"""
start_time = time.time()
namespace = namespace or self.namespace
event = properties.copy()
if log_scope_stack_trace:
event['stack_trace'] = traceback.extract_stack()
try:
yield event
except Exception, exception:
self._log_exception(event, exception,
(sys.last_traceback if log_exception_stack_trace
else None))
event['duration'] = time.time() - start_time
self.put({stream_name: [event]}, namespace=namespace)