Python traceback.TracebackException() Examples
The following are 28
code examples of traceback.TracebackException().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
traceback
, or try the search function
.
Example #1
Source File: monkey_print2.py From distributed_framework with Apache License 2.0 | 6 votes |
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
Example #2
Source File: test_traceback.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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))
Example #3
Source File: test_traceback.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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))
Example #4
Source File: test_traceback.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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))
Example #5
Source File: test_traceback.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
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))
Example #6
Source File: test_traceback.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
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))
Example #7
Source File: test_traceback.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
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))
Example #8
Source File: error.py From dagster with Apache License 2.0 | 5 votes |
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__, )
Example #9
Source File: result.py From android_universal with MIT License | 5 votes |
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)
Example #10
Source File: result.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
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)
Example #11
Source File: result.py From odoo13-x64 with GNU General Public License v3.0 | 5 votes |
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)
Example #12
Source File: runner.py From win-unicode-console with MIT License | 5 votes |
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="")
Example #13
Source File: test_traceback.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
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"])
Example #14
Source File: test_traceback.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
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)
Example #15
Source File: test_traceback.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
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'"})
Example #16
Source File: test_traceback.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
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")
Example #17
Source File: test_traceback.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
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))
Example #18
Source File: result.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
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)
Example #19
Source File: json.py From sslyze with GNU Affero General Public License v3.0 | 5 votes |
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
Example #20
Source File: result.py From Imogen with MIT License | 5 votes |
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)
Example #21
Source File: test_traceback.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
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"])
Example #22
Source File: test_traceback.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
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)
Example #23
Source File: test_traceback.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
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'"})
Example #24
Source File: test_traceback.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
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")
Example #25
Source File: test_traceback.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
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))
Example #26
Source File: result.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
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)
Example #27
Source File: json.py From sslyze with GNU Affero General Public License v3.0 | 5 votes |
def _traceback_to_json(obj: TracebackException) -> JsonType: return _traceback_to_str(obj)
Example #28
Source File: utils.py From sacred with MIT License | 4 votes |
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)