本文整理汇总了Python中traceback.walk_tb函数的典型用法代码示例。如果您正苦于以下问题:Python walk_tb函数的具体用法?Python walk_tb怎么用?Python walk_tb使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了walk_tb函数的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_walk_tb
def test_walk_tb(self):
try:
1/0
except Exception:
_, _, tb = sys.exc_info()
s = list(traceback.walk_tb(tb))
self.assertEqual(len(s), 1)
示例2: filtered_traceback_format
def filtered_traceback_format(tb_exception, chain=True):
if chain:
if tb_exception.__cause__ is not None:
for line in filtered_traceback_format(tb_exception.__cause__,
chain=chain):
yield line
yield tb._cause_message
elif (tb_exception.__context__ is not None and
not tb_exception.__suppress_context__):
for line in filtered_traceback_format(tb_exception.__context__,
chain=chain):
yield line
yield tb._context_message
yield 'Traceback (most recent calls WITHOUT Sacred internals):\n'
current_tb = tb_exception.exc_traceback
while current_tb is not None:
if not _is_sacred_frame(current_tb.tb_frame):
stack = tb.StackSummary.extract(tb.walk_tb(current_tb),
limit=1,
lookup_lines=True,
capture_locals=False)
for line in stack.format():
yield line
current_tb = current_tb.tb_next
for line in tb_exception.format_exception_only():
yield line
示例3: printNotImplementedError
def printNotImplementedError(cls, ex):
from traceback import walk_tb
Init.init()
frame, _ = [x for x in walk_tb(ex.__traceback__)][-1]
filename = frame.f_code.co_filename
funcName = frame.f_code.co_name
print("{RED}Not implemented:{NOCOLOR} {function} in file '{filename}': {message}".format(function=funcName, filename=filename, message=str(ex), **Init.Foreground))
Exit.exit(1)
示例4: printNotImplementedError
def printNotImplementedError(cls, ex):
from traceback import walk_tb
Init.init()
frame, _ = [x for x in walk_tb(ex.__traceback__)][-1]
filename = frame.f_code.co_filename
funcName = frame.f_code.co_name
print("{RED}NOT IMPLEMENTED:{NOCOLOR} {function} in file '{filename}': {message!s}".format(function=funcName, filename=filename, message=ex, **Init.Foreground))
print(("{RED}" + ("-" * 80) + "{NOCOLOR}").format(**Init.Foreground))
print(("{RED}Please report this bug at GitHub: https://github.com/VLSI-EDA/PoC/issues{NOCOLOR}").format(**Init.Foreground))
print(("{RED}" + ("-" * 80) + "{NOCOLOR}").format(**Init.Foreground))
Exit.exit(1)
示例5: test_limit
def test_limit(self):
def recurse(n):
if n:
recurse(n-1)
else:
1/0
try:
recurse(10)
except Exception:
exc_info = sys.exc_info()
exc = traceback.TracebackException(*exc_info, limit=5)
expected_stack = traceback.StackSummary.extract(
traceback.walk_tb(exc_info[2]), limit=5)
self.assertEqual(expected_stack, exc.stack)
示例6: test_smoke
def test_smoke(self):
try:
1/0
except Exception:
exc_info = sys.exc_info()
exc = traceback.TracebackException(*exc_info)
expected_stack = traceback.StackSummary.extract(
traceback.walk_tb(exc_info[2]))
self.assertEqual(None, exc.__cause__)
self.assertEqual(None, exc.__context__)
self.assertEqual(False, exc.__suppress_context__)
self.assertEqual(expected_stack, exc.stack)
self.assertEqual(exc_info[0], exc.exc_type)
self.assertEqual(str(exc_info[1]), str(exc))
示例7: printException
def printException(cls, ex):
from traceback import print_tb, walk_tb
Init.init()
print("{RED}FATAL: An unknown or unhandled exception reached the topmost exception handler!{NOCOLOR}".format(message=ex.__str__(), **Init.Foreground))
print("{YELLOW} Exception type:{NOCOLOR} {type}".format(type=ex.__class__.__name__, **Init.Foreground))
print("{YELLOW} Exception message:{NOCOLOR} {message}".format(message=ex.__str__(), **Init.Foreground))
frame,sourceLine = [x for x in walk_tb(ex.__traceback__)][-1]
filename = frame.f_code.co_filename
funcName = frame.f_code.co_name
print("{YELLOW} Caused by:{NOCOLOR} {function} in file '{filename}' at line {line}".format(function=funcName, filename=filename, line=sourceLine, **Init.Foreground))
print("-" * 80)
print_tb(ex.__traceback__)
print("-" * 80)
Exit.exit(1)
示例8: printException
def printException(cls, ex):
from traceback import print_tb, walk_tb
Init.init()
print("{RED}FATAL: An unknown or unhandled exception reached the topmost exception handler!{NOCOLOR}".format(**Init.Foreground))
print("{YELLOW} Exception type:{NOCOLOR} {type}".format(type=ex.__class__.__name__, **Init.Foreground))
print("{YELLOW} Exception message:{NOCOLOR} {message!s}".format(message=ex, **Init.Foreground))
if (ex.__cause__ is not None):
print("{YELLOW} Caused by type:{NOCOLOR} {type}".format(message=ex.__cause__.__class__.__name__, **Init.Foreground))
print("{YELLOW} Caused by message:{NOCOLOR} {message!s}".format(message=ex.__cause__, **Init.Foreground))
frame,sourceLine = [x for x in walk_tb(ex.__traceback__)][-1]
filename = frame.f_code.co_filename
funcName = frame.f_code.co_name
print("{YELLOW} Caused by:{NOCOLOR} {function} in file '{filename}' at line {line}".format(function=funcName, filename=filename, line=sourceLine, **Init.Foreground))
print(("{RED}" + ("-" * 80) + "{NOCOLOR}").format(**Init.Foreground))
print_tb(ex.__traceback__)
print(("{RED}" + ("-" * 80) + "{NOCOLOR}").format(**Init.Foreground))
print(("{RED}Please report this bug at GitHub: https://github.com/VLSI-EDA/PoC/issues{NOCOLOR}").format(**Init.Foreground))
print(("{RED}" + ("-" * 80) + "{NOCOLOR}").format(**Init.Foreground))
Exit.exit(1)
示例9: test_cause
def test_cause(self):
try:
try:
1/0
finally:
exc_info_context = sys.exc_info()
exc_context = traceback.TracebackException(*exc_info_context)
cause = Exception("cause")
raise Exception("uh oh") from cause
except Exception:
exc_info = sys.exc_info()
exc = traceback.TracebackException(*exc_info)
expected_stack = traceback.StackSummary.extract(
traceback.walk_tb(exc_info[2]))
exc_cause = traceback.TracebackException(Exception, cause, None)
self.assertEqual(exc_cause, exc.__cause__)
self.assertEqual(exc_context, exc.__context__)
self.assertEqual(True, exc.__suppress_context__)
self.assertEqual(expected_stack, exc.stack)
self.assertEqual(exc_info[0], exc.exc_type)
self.assertEqual(str(exc_info[1]), str(exc))
示例10: test_from_exception
def test_from_exception(self):
# Check all the parameters are accepted.
def foo():
1/0
try:
foo()
except Exception as e:
exc_info = sys.exc_info()
self.expected_stack = traceback.StackSummary.extract(
traceback.walk_tb(exc_info[2]), limit=1, lookup_lines=False,
capture_locals=True)
self.exc = traceback.TracebackException.from_exception(
e, limit=1, lookup_lines=False, capture_locals=True)
expected_stack = self.expected_stack
exc = self.exc
self.assertEqual(None, exc.__cause__)
self.assertEqual(None, exc.__context__)
self.assertEqual(False, exc.__suppress_context__)
self.assertEqual(expected_stack, exc.stack)
self.assertEqual(exc_info[0], exc.exc_type)
self.assertEqual(str(exc_info[1]), str(exc))
示例11: test_traceback
def test_traceback():
try:
assert_that('foo').is_equal_to('bar')
fail('should have raised error')
except AssertionError as ex:
assert_that(str(ex)).is_equal_to('Expected <foo> to be equal to <bar>, but was not.')
assert_that(ex).is_type_of(AssertionError)
# extract all stack frames from the traceback
_, _, tb = sys.exc_info()
assert_that(tb).is_not_none()
# walk_tb added in 3.5
if sys.version_info[0] == 3 and sys.version_info[1] >= 5:
frames = [(f.f_code.co_filename, f.f_code.co_name, lineno) for f,lineno in traceback.walk_tb(tb)]
assert_that(frames).is_length(3)
assert_that(frames[0][0]).ends_with('test_traceback.py')
assert_that(frames[0][1]).is_equal_to('test_traceback')
assert_that(frames[0][2]).is_equal_to(35)
assert_that(frames[1][0]).ends_with('assertpy.py')
assert_that(frames[1][1]).is_equal_to('is_equal_to')
assert_that(frames[1][2]).is_greater_than(160)
assert_that(frames[2][0]).ends_with('assertpy.py')
assert_that(frames[2][1]).is_equal_to('_err')
assert_that(frames[2][2]).is_greater_than(1000)
示例12: compile_flow_subgraph
#.........这里部分代码省略.........
inputs.append(in_expr)
if not node.inputmap[in_name] or node.inputmap[in_name][0] not in node_uids:
# it's not even satisfied by another path within the subgraph,
# and needs to be provided as input to the emerging callable
if use_unique_input_names:
thunk['input_sources'].append(('kwargs', -1, "%s_%s" % (node.uid, in_name)))
else:
thunk['input_sources'].append(('kwargs', -1, in_name))
dangling_inputs.append((node.uid, in_name))
else:
# this input will be satisfied by another path within the subgraph
source_uid, source_name = node.inputmap[in_name]
for idx, p in enumerate(paths):
if self.get_node(source_uid) in p['members']:
# record which thunk, and which index of its output-array satisfies this input
thunk['input_sources'].append(('path', idx, thunks[idx]['outputs'].index((source_uid, source_name))))
buildargs.append(in_expr)
else:
# this input is satisfied within this path
source_uid, source_name = node.inputmap[in_name]
buildargs.append(outexpressions[source_uid][self.get_node(source_uid).outputs.index(source_name)])
# build the outexpression
try:
if len(node.outputs) <= 1:
original_outex = [node.build(*buildargs)]
elif node.implementation == 'python':
func = node.build(*buildargs)
original_outex = [func] * len(node.outputs)
else:
original_outex = node.build(*buildargs)
except Exception as err:
import traceback as tb
frame = [f[0] for f in tb.walk_tb(err.__traceback__) if f[0].f_code.co_filename == node.definition.get('path', '')]
lineno = "<unknown>" if len(frame) == 0 else str(frame[0].f_lineno)
self.logger.error("Error in Flowmodule %s at line %s: %s: %s" % (str(node), lineno, err.__class__.__name__, str(err)))
post_mortem()
skip = True
break
outexpressions[node.uid] = original_outex
flattened_outex = []
outputlengths = []
flattened_markers = []
# check if this node has a list as one of its return values:
for idx, ex in enumerate(original_outex):
if type(ex) == list:
# if so, flatten the outputs, and mark the offset and length of the flattened output
# so that we can later reconstruct the nested output-structure
flattened_markers.append((len(outputs) + idx, len(ex)))
outputlengths.append(len(ex))
for item in ex:
flattened_outex.append(item)
else:
flattened_outex.append(ex)
outputlengths.append(1)
# offset for indexing the flattened_outexpression by output_index
node_flattened_output_offset = 0
# go thorugh the nodes outputs, and see how they will be used:
for out_idx, out_name in enumerate(node.outputs):
dangling = ['external']
if node.outputmap[out_name]:
# if this output is used, we have to see where every connection goes
# iterate through every connection, and note if it's used path-internally,