Python traceback.walk_tb() Examples

The following are 15 code examples of traceback.walk_tb(). 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: utils.py    From sacred with MIT License 6 votes vote down vote up
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 
Example #2
Source File: test_traceback.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
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 vote down vote up
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 vote down vote up
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: traceback2.py    From ver-observer with GNU General Public License v3.0 6 votes vote down vote up
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 
Example #6
Source File: pacu.py    From pacu with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 
Example #7
Source File: test_traceback.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
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: test_traceback.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
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 #9
Source File: test_traceback.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 6 votes vote down vote up
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 #10
Source File: test_traceback.py    From assertpy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #11
Source File: test_traceback.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #12
Source File: test_traceback.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
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 #13
Source File: traceback2.py    From ver-observer with GNU General Public License v3.0 5 votes vote down vote up
def walk_tb(tb):
        while tb is not None:
            yield tb.tb_frame, tb.tb_lineno
            tb = tb.tb_next 
Example #14
Source File: test_traceback.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #15
Source File: test_traceback.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
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))