def test_print_exception_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:
                with captured_stderr() as output:
                    with mock.patch.object(run, 'cleanup_traceback') as ct:
                        ct.side_effect = lambda t, e: t
                        run.print_exception()

        tb = output.getvalue().strip().splitlines()
        if has_no_debug_ranges():
            self.assertEqual(11, len(tb))
            self.assertIn('UnhashableException: ex2', tb[3])
            self.assertIn('UnhashableException: ex1', tb[10])
        else:
            self.assertEqual(13, len(tb))
            self.assertIn('UnhashableException: ex2', tb[4])
            self.assertIn('UnhashableException: ex1', tb[12])
示例#2
0
    def doTraceback(self, module):
        try:
            module.do_raise()
        except:
            tb = sys.exc_info()[2].tb_next

            f, lno, n, line = extract_tb(tb, 1)[0]
            self.assertEqual(line, raise_src.strip())

            f, lno, n, line = extract_stack(tb.tb_frame, 1)[0]
            self.assertEqual(line, raise_src.strip())

            s = io.StringIO()
            print_tb(tb, 1, s)
            self.assertTrue(
                s.getvalue().endswith('    def do_raise(): raise TypeError\n'
                                      '' if support.has_no_debug_ranges() else
                                      '                    ^^^^^^^^^^^^^^^\n'))
        else:
            raise AssertionError("This ought to be impossible")