本文整理汇总了Python中traceback.TracebackException方法的典型用法代码示例。如果您正苦于以下问题:Python traceback.TracebackException方法的具体用法?Python traceback.TracebackException怎么用?Python traceback.TracebackException使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类traceback
的用法示例。
在下文中一共展示了traceback.TracebackException方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: print_exception
# 需要导入模块: import traceback [as 别名]
# 或者: from traceback import TracebackException [as 别名]
def print_exception(etype, value, tb, limit=None, file=None, chain=True):
"""
避免每行有两个可跳转的,导致第二个可跳转的不被ide识别。
主要是针对print_exception,logging.exception里面会调用这个函数。
:param etype:
:param value:
:param tb:
:param limit:
:param file:
:param chain:
:return:
"""
if file is None:
file = sys.stderr
for line in traceback.TracebackException(
type(value), value, tb, limit=limit).format(chain=chain):
# print(line, file=file, end="")
if file != sys.stderr:
sys.stderr.write(f'{line} \n')
else:
print(line, file=file, end="")
# print = nb_print
示例2: test_from_exception
# 需要导入模块: import traceback [as 别名]
# 或者: from traceback import TracebackException [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 TracebackException [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 TracebackException [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: _traceback_to_str
# 需要导入模块: import traceback [as 别名]
# 或者: from traceback import TracebackException [as 别名]
def _traceback_to_str(traceback: TracebackException) -> str:
exception_trace_as_str = ""
for line in traceback.format(chain=False):
exception_trace_as_str += line
return exception_trace_as_str
示例6: _traceback_to_json
# 需要导入模块: import traceback [as 别名]
# 或者: from traceback import TracebackException [as 别名]
def _traceback_to_json(obj: TracebackException) -> JsonType:
return _traceback_to_str(obj)
示例7: _exc_info_to_string
# 需要导入模块: import traceback [as 别名]
# 或者: from traceback import TracebackException [as 别名]
def _exc_info_to_string(self, err, test):
"""Converts a sys.exc_info()-style tuple of values into a string."""
exctype, value, tb = err
# Skip test runner traceback levels
while tb and self._is_relevant_tb_level(tb):
tb = tb.tb_next
if exctype is test.failureException:
# Skip assert*() traceback levels
length = self._count_relevant_tb_levels(tb)
else:
length = None
tb_e = traceback.TracebackException(
exctype, value, tb, limit=length, capture_locals=self.tb_locals)
msgLines = list(tb_e.format())
if self.buffer:
output = sys.stdout.getvalue()
error = sys.stderr.getvalue()
if output:
if not output.endswith('\n'):
output += '\n'
msgLines.append(STDOUT_LINE % output)
if error:
if not error.endswith('\n'):
error += '\n'
msgLines.append(STDERR_LINE % error)
return ''.join(msgLines)
示例8: test_smoke
# 需要导入模块: import traceback [as 别名]
# 或者: from traceback import TracebackException [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))
示例9: test_lookup_lines
# 需要导入模块: import traceback [as 别名]
# 或者: from traceback import TracebackException [as 别名]
def test_lookup_lines(self):
linecache.clearcache()
e = Exception("uh oh")
c = test_code('/foo.py', 'method')
f = test_frame(c, None, None)
tb = test_tb(f, 6, None)
exc = traceback.TracebackException(Exception, e, tb, lookup_lines=False)
self.assertEqual({}, linecache.cache)
linecache.updatecache('/foo.py', globals())
self.assertEqual(exc.stack[0].line, "import sys")
示例10: test_locals
# 需要导入模块: import traceback [as 别名]
# 或者: from traceback import TracebackException [as 别名]
def test_locals(self):
linecache.updatecache('/foo.py', globals())
e = Exception("uh oh")
c = test_code('/foo.py', 'method')
f = test_frame(c, globals(), {'something': 1, 'other': 'string'})
tb = test_tb(f, 6, None)
exc = traceback.TracebackException(
Exception, e, tb, capture_locals=True)
self.assertEqual(
exc.stack[0].locals, {'something': '1', 'other': "'string'"})
示例11: test_no_locals
# 需要导入模块: import traceback [as 别名]
# 或者: from traceback import TracebackException [as 别名]
def test_no_locals(self):
linecache.updatecache('/foo.py', globals())
e = Exception("uh oh")
c = test_code('/foo.py', 'method')
f = test_frame(c, globals(), {'something': 1})
tb = test_tb(f, 6, None)
exc = traceback.TracebackException(Exception, e, tb)
self.assertEqual(exc.stack[0].locals, None)
示例12: test_traceback_header
# 需要导入模块: import traceback [as 别名]
# 或者: from traceback import TracebackException [as 别名]
def test_traceback_header(self):
# do not print a traceback header if exc_traceback is None
# see issue #24695
exc = traceback.TracebackException(Exception, Exception("haven"), None)
self.assertEqual(list(exc.format()), ["Exception: haven\n"])
示例13: serializable_error_info_from_exc_info
# 需要导入模块: import traceback [as 别名]
# 或者: from traceback import TracebackException [as 别名]
def serializable_error_info_from_exc_info(exc_info):
if hasattr(traceback, 'TracebackException'):
return _serializable_error_info_from_tb(traceback.TracebackException(*exc_info))
else: # fallback for our old pal py27
exc_type, exc_value, exc_tb = exc_info
return SerializableErrorInfo(
traceback.format_exception_only(exc_type, exc_value)[-1],
traceback.format_tb(tb=exc_tb),
exc_type.__name__,
)
示例14: print_exception_without_first_line
# 需要导入模块: import traceback [as 别名]
# 或者: from traceback import TracebackException [as 别名]
def print_exception_without_first_line(etype, value, tb, limit=None, file=None, chain=True):
if file is None:
file = sys.stderr
lines = iter(traceback.TracebackException(
type(value), value, tb, limit=limit).format(chain=chain))
next(lines)
for line in lines:
print(line, file=file, end="")
示例15: format_filtered_stacktrace
# 需要导入模块: import traceback [as 别名]
# 或者: from traceback import TracebackException [as 别名]
def format_filtered_stacktrace(filter_traceback="default"):
"""
Returns the traceback as `string`.
`filter_traceback` can be one of:
- 'always': always filter out sacred internals
- 'default': Default behaviour: filter out sacred internals
if the exception did not originate from within sacred, and
print just the internal stack trace otherwise
- 'never': don't filter, always print full traceback
- All other values will fall back to 'never'.
"""
exc_type, exc_value, exc_traceback = sys.exc_info()
# determine if last exception is from sacred
current_tb = exc_traceback
while current_tb.tb_next is not None:
current_tb = current_tb.tb_next
if filter_traceback == "default" and _is_sacred_frame(current_tb.tb_frame):
# just print sacred internal trace
header = [
"Exception originated from within Sacred.\n"
"Traceback (most recent calls):\n"
]
texts = tb.format_exception(exc_type, exc_value, current_tb)
return "".join(header + texts[1:]).strip()
elif filter_traceback in ("default", "always"):
# print filtered stacktrace
if sys.version_info >= (3, 5):
tb_exception = tb.TracebackException(
exc_type, exc_value, exc_traceback, limit=None
)
return "".join(filtered_traceback_format(tb_exception))
else:
s = "Traceback (most recent calls WITHOUT Sacred internals):"
current_tb = exc_traceback
while current_tb is not None:
if not _is_sacred_frame(current_tb.tb_frame):
tb.print_tb(current_tb, 1)
current_tb = current_tb.tb_next
s += "\n".join(tb.format_exception_only(exc_type, exc_value)).strip()
return s
elif filter_traceback == "never":
# print full stacktrace
return "\n".join(tb.format_exception(exc_type, exc_value, exc_traceback))
else:
raise ValueError("Unknown value for filter_traceback: " + filter_traceback)