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)
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'"})
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))
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)
def test_unhashable(self): class UnhashableException(Exception): def __eq__(self, other): return True ex1 = UnhashableException('ex1') ex2 = UnhashableException('ex2') try: raise ex2 from ex1 except UnhashableException: try: raise ex1 except UnhashableException: exc_info = sys.exc_info() exc = traceback.TracebackException(*exc_info) formatted = list(exc.format()) self.assertIn('UnhashableException: ex2\n', formatted[2]) self.assertIn('UnhashableException: ex1\n', formatted[6])
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"])