本文整理汇总了Python中traceback.walk_tb方法的典型用法代码示例。如果您正苦于以下问题:Python traceback.walk_tb方法的具体用法?Python traceback.walk_tb怎么用?Python traceback.walk_tb使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类traceback
的用法示例。
在下文中一共展示了traceback.walk_tb方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: filtered_traceback_format
# 需要导入模块: import traceback [as 别名]
# 或者: from traceback import walk_tb [as 别名]
def filtered_traceback_format(tb_exception, chain=True):
if chain:
if tb_exception.__cause__ is not None:
yield from filtered_traceback_format(tb_exception.__cause__, chain=chain)
yield tb._cause_message
elif (
tb_exception.__context__ is not None
and not tb_exception.__suppress_context__
):
yield from filtered_traceback_format(tb_exception.__context__, chain=chain)
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
)
yield from stack.format()
current_tb = current_tb.tb_next
yield from tb_exception.format_exception_only()
# noinspection PyUnusedLocal
示例2: test_from_exception
# 需要导入模块: import traceback [as 别名]
# 或者: from traceback import walk_tb [as 别名]
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))
示例3: test_cause
# 需要导入模块: import traceback [as 别名]
# 或者: from traceback import walk_tb [as 别名]
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))
示例4: test_context
# 需要导入模块: import traceback [as 别名]
# 或者: from traceback import walk_tb [as 别名]
def test_context(self):
try:
try:
1/0
finally:
exc_info_context = sys.exc_info()
exc_context = traceback.TracebackException(*exc_info_context)
raise Exception("uh oh")
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(exc_context, 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))
示例5: format_exc
# 需要导入模块: import traceback [as 别名]
# 或者: from traceback import walk_tb [as 别名]
def format_exc(interested=None, source_path=None, with_normal=True):
if sys.exc_info() == (None, None, None):
return "NoTraceback"
source_path = source_path or os.getcwd()
_traceback = ""
for frame, lineno in walk_tb(sys.exc_info()[2]):
abs_path = frame.f_code.co_filename
if ".." not in os.path.relpath(abs_path, source_path):
_traceback += frame_operations.frame_format(
frame, interested=interested, frame_lineno=lineno
) + "\n"
if with_normal:
_traceback = "{}\n{}".format(traceback.format_exc(), _traceback)
return _traceback
示例6: get_data_from_traceback
# 需要导入模块: import traceback [as 别名]
# 或者: from traceback import walk_tb [as 别名]
def get_data_from_traceback(self, tb):
session = None
global_data_in_all_frames = list()
local_data_in_all_frames = list()
for frame, line_number in traceback.walk_tb(tb):
global_data_in_all_frames.append(str(frame.f_globals))
local_data_in_all_frames.append(str(frame.f_locals))
# Save the most recent PacuSession called "session", working backwards.
if session is None:
session = frame.f_locals.get('session', None)
if not isinstance(session, PacuSession):
session = None
return session, global_data_in_all_frames, local_data_in_all_frames
示例7: test_traceback
# 需要导入模块: import traceback [as 别名]
# 或者: from traceback import walk_tb [as 别名]
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(36)
assert_that(frames[1][0]).ends_with('base.py')
assert_that(frames[1][1]).is_equal_to('is_equal_to')
assert_that(frames[1][2]).is_greater_than(40)
assert_that(frames[2][0]).ends_with('assertpy.py')
assert_that(frames[2][1]).is_equal_to('error')
assert_that(frames[2][2]).is_greater_than(100)
示例8: test_walk_tb
# 需要导入模块: import traceback [as 别名]
# 或者: from traceback import walk_tb [as 别名]
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)
示例9: test_smoke
# 需要导入模块: import traceback [as 别名]
# 或者: from traceback import walk_tb [as 别名]
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))
示例10: walk_tb
# 需要导入模块: import traceback [as 别名]
# 或者: from traceback import walk_tb [as 别名]
def walk_tb(tb):
while tb is not None:
yield tb.tb_frame, tb.tb_lineno
tb = tb.tb_next