gh-102799: remove unnecessary calls to sys.exc_info() in tests (#102800)
This commit is contained in:
parent
72186aa637
commit
b3cc11a08e
@ -1889,8 +1889,8 @@ class TestFork(unittest.IsolatedAsyncioTestCase):
|
|||||||
os.write(w, b'LOOP:' + str(id(loop)).encode())
|
os.write(w, b'LOOP:' + str(id(loop)).encode())
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
os.write(w, b'NO LOOP')
|
os.write(w, b'NO LOOP')
|
||||||
except:
|
except BaseException as e:
|
||||||
os.write(w, b'ERROR:' + ascii(sys.exc_info()).encode())
|
os.write(w, b'ERROR:' + ascii(e).encode())
|
||||||
finally:
|
finally:
|
||||||
os._exit(0)
|
os._exit(0)
|
||||||
else:
|
else:
|
||||||
|
@ -599,8 +599,8 @@ class ExceptionTests(unittest.TestCase):
|
|||||||
def testWithTraceback(self):
|
def testWithTraceback(self):
|
||||||
try:
|
try:
|
||||||
raise IndexError(4)
|
raise IndexError(4)
|
||||||
except:
|
except Exception as e:
|
||||||
tb = sys.exc_info()[2]
|
tb = e.__traceback__
|
||||||
|
|
||||||
e = BaseException().with_traceback(tb)
|
e = BaseException().with_traceback(tb)
|
||||||
self.assertIsInstance(e, BaseException)
|
self.assertIsInstance(e, BaseException)
|
||||||
@ -653,8 +653,8 @@ class ExceptionTests(unittest.TestCase):
|
|||||||
def testNoneClearsTracebackAttr(self):
|
def testNoneClearsTracebackAttr(self):
|
||||||
try:
|
try:
|
||||||
raise IndexError(4)
|
raise IndexError(4)
|
||||||
except:
|
except Exception as e:
|
||||||
tb = sys.exc_info()[2]
|
tb = e.__traceback__
|
||||||
|
|
||||||
e = Exception()
|
e = Exception()
|
||||||
e.__traceback__ = tb
|
e.__traceback__ = tb
|
||||||
@ -1337,11 +1337,11 @@ class ExceptionTests(unittest.TestCase):
|
|||||||
def g():
|
def g():
|
||||||
try:
|
try:
|
||||||
return g()
|
return g()
|
||||||
except RecursionError:
|
except RecursionError as e:
|
||||||
return sys.exc_info()
|
return e
|
||||||
e, v, tb = g()
|
exc = g()
|
||||||
self.assertIsInstance(v, RecursionError, type(v))
|
self.assertIsInstance(exc, RecursionError, type(exc))
|
||||||
self.assertIn("maximum recursion depth exceeded", str(v))
|
self.assertIn("maximum recursion depth exceeded", str(exc))
|
||||||
|
|
||||||
|
|
||||||
@cpython_only
|
@cpython_only
|
||||||
|
@ -5492,10 +5492,10 @@ class TCPTimeoutTest(SocketTCPTest):
|
|||||||
self.fail("caught timeout instead of Alarm")
|
self.fail("caught timeout instead of Alarm")
|
||||||
except Alarm:
|
except Alarm:
|
||||||
pass
|
pass
|
||||||
except:
|
except BaseException as e:
|
||||||
self.fail("caught other exception instead of Alarm:"
|
self.fail("caught other exception instead of Alarm:"
|
||||||
" %s(%s):\n%s" %
|
" %s(%s):\n%s" %
|
||||||
(sys.exc_info()[:2] + (traceback.format_exc(),)))
|
(type(e), e, traceback.format_exc()))
|
||||||
else:
|
else:
|
||||||
self.fail("nothing caught")
|
self.fail("nothing caught")
|
||||||
finally:
|
finally:
|
||||||
|
@ -1649,8 +1649,8 @@ class SizeofTest(unittest.TestCase):
|
|||||||
check(_ast.AST(), size('P'))
|
check(_ast.AST(), size('P'))
|
||||||
try:
|
try:
|
||||||
raise TypeError
|
raise TypeError
|
||||||
except TypeError:
|
except TypeError as e:
|
||||||
tb = sys.exc_info()[2]
|
tb = e.__traceback__
|
||||||
# traceback
|
# traceback
|
||||||
if tb is not None:
|
if tb is not None:
|
||||||
check(tb, size('2P2i'))
|
check(tb, size('2P2i'))
|
||||||
|
@ -296,15 +296,15 @@ class TracebackCases(unittest.TestCase):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
try:
|
try:
|
||||||
x = 1 / 0
|
x = 1 / 0
|
||||||
except Exception:
|
except Exception as e:
|
||||||
self.exc_info = sys.exc_info()
|
self.exc = e
|
||||||
# self.exc_info[1] (traceback) contains frames:
|
# self.exc.__traceback__ contains frames:
|
||||||
# explicitly clear the reference to self in the current
|
# explicitly clear the reference to self in the current
|
||||||
# frame to break a reference cycle
|
# frame to break a reference cycle
|
||||||
self = None
|
self = None
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
traceback.print_exception(*self.exc_info)
|
traceback.print_exception(self.exc)
|
||||||
|
|
||||||
# Keep a reference in the module namespace to call the destructor
|
# Keep a reference in the module namespace to call the destructor
|
||||||
# when the module is unloaded
|
# when the module is unloaded
|
||||||
@ -923,8 +923,8 @@ class TracebackFormatTests(unittest.TestCase):
|
|||||||
from _testcapi import traceback_print
|
from _testcapi import traceback_print
|
||||||
try:
|
try:
|
||||||
self.some_exception()
|
self.some_exception()
|
||||||
except KeyError:
|
except KeyError as e:
|
||||||
type_, value, tb = sys.exc_info()
|
tb = e.__traceback__
|
||||||
if cleanup_func is not None:
|
if cleanup_func is not None:
|
||||||
# Clear the inner frames, not this one
|
# Clear the inner frames, not this one
|
||||||
cleanup_func(tb.tb_next)
|
cleanup_func(tb.tb_next)
|
||||||
@ -1228,8 +1228,8 @@ class TracebackFormatTests(unittest.TestCase):
|
|||||||
except UnhashableException:
|
except UnhashableException:
|
||||||
try:
|
try:
|
||||||
raise ex1
|
raise ex1
|
||||||
except UnhashableException:
|
except UnhashableException as e:
|
||||||
exc_type, exc_val, exc_tb = sys.exc_info()
|
exc_val = e
|
||||||
|
|
||||||
with captured_output("stderr") as stderr_f:
|
with captured_output("stderr") as stderr_f:
|
||||||
exception_print(exc_val)
|
exception_print(exc_val)
|
||||||
@ -2133,8 +2133,8 @@ class LimitTests(unittest.TestCase):
|
|||||||
def test_extract_tb(self):
|
def test_extract_tb(self):
|
||||||
try:
|
try:
|
||||||
self.last_raises5()
|
self.last_raises5()
|
||||||
except Exception:
|
except Exception as e:
|
||||||
exc_type, exc_value, tb = sys.exc_info()
|
tb = e.__traceback__
|
||||||
def extract(**kwargs):
|
def extract(**kwargs):
|
||||||
return traceback.extract_tb(tb, **kwargs)
|
return traceback.extract_tb(tb, **kwargs)
|
||||||
|
|
||||||
@ -2160,12 +2160,12 @@ class LimitTests(unittest.TestCase):
|
|||||||
def test_format_exception(self):
|
def test_format_exception(self):
|
||||||
try:
|
try:
|
||||||
self.last_raises5()
|
self.last_raises5()
|
||||||
except Exception:
|
except Exception as e:
|
||||||
exc_type, exc_value, tb = sys.exc_info()
|
exc = e
|
||||||
# [1:-1] to exclude "Traceback (...)" header and
|
# [1:-1] to exclude "Traceback (...)" header and
|
||||||
# exception type and value
|
# exception type and value
|
||||||
def extract(**kwargs):
|
def extract(**kwargs):
|
||||||
return traceback.format_exception(exc_type, exc_value, tb, **kwargs)[1:-1]
|
return traceback.format_exception(exc, **kwargs)[1:-1]
|
||||||
|
|
||||||
with support.swap_attr(sys, 'tracebacklimit', 1000):
|
with support.swap_attr(sys, 'tracebacklimit', 1000):
|
||||||
nolim = extract()
|
nolim = extract()
|
||||||
@ -2203,8 +2203,8 @@ class MiscTracebackCases(unittest.TestCase):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
outer()
|
outer()
|
||||||
except:
|
except BaseException as e:
|
||||||
type_, value, tb = sys.exc_info()
|
tb = e.__traceback__
|
||||||
|
|
||||||
# Initial assertion: there's one local in the inner frame.
|
# Initial assertion: there's one local in the inner frame.
|
||||||
inner_frame = tb.tb_next.tb_next.tb_next.tb_frame
|
inner_frame = tb.tb_next.tb_next.tb_next.tb_frame
|
||||||
@ -2282,8 +2282,8 @@ class TestStack(unittest.TestCase):
|
|||||||
def test_walk_tb(self):
|
def test_walk_tb(self):
|
||||||
try:
|
try:
|
||||||
1/0
|
1/0
|
||||||
except Exception:
|
except Exception as e:
|
||||||
_, _, tb = sys.exc_info()
|
tb = e.__traceback__
|
||||||
s = list(traceback.walk_tb(tb))
|
s = list(traceback.walk_tb(tb))
|
||||||
self.assertEqual(len(s), 1)
|
self.assertEqual(len(s), 1)
|
||||||
|
|
||||||
@ -2386,10 +2386,10 @@ class TestStack(unittest.TestCase):
|
|||||||
def g():
|
def g():
|
||||||
try:
|
try:
|
||||||
f()
|
f()
|
||||||
except:
|
except Exception as e:
|
||||||
return sys.exc_info()
|
return e.__traceback__
|
||||||
|
|
||||||
exc_info = g()
|
tb = g()
|
||||||
|
|
||||||
class Skip_G(traceback.StackSummary):
|
class Skip_G(traceback.StackSummary):
|
||||||
def format_frame_summary(self, frame_summary):
|
def format_frame_summary(self, frame_summary):
|
||||||
@ -2398,7 +2398,7 @@ class TestStack(unittest.TestCase):
|
|||||||
return super().format_frame_summary(frame_summary)
|
return super().format_frame_summary(frame_summary)
|
||||||
|
|
||||||
stack = Skip_G.extract(
|
stack = Skip_G.extract(
|
||||||
traceback.walk_tb(exc_info[2])).format()
|
traceback.walk_tb(tb)).format()
|
||||||
|
|
||||||
self.assertEqual(len(stack), 1)
|
self.assertEqual(len(stack), 1)
|
||||||
lno = f.__code__.co_firstlineno + 1
|
lno = f.__code__.co_firstlineno + 1
|
||||||
@ -2416,17 +2416,17 @@ class TestTracebackException(unittest.TestCase):
|
|||||||
def test_smoke(self):
|
def test_smoke(self):
|
||||||
try:
|
try:
|
||||||
1/0
|
1/0
|
||||||
except Exception:
|
except Exception as e:
|
||||||
exc_info = sys.exc_info()
|
exc_obj = e
|
||||||
exc = traceback.TracebackException(*exc_info)
|
exc = traceback.TracebackException.from_exception(e)
|
||||||
expected_stack = traceback.StackSummary.extract(
|
expected_stack = traceback.StackSummary.extract(
|
||||||
traceback.walk_tb(exc_info[2]))
|
traceback.walk_tb(e.__traceback__))
|
||||||
self.assertEqual(None, exc.__cause__)
|
self.assertEqual(None, exc.__cause__)
|
||||||
self.assertEqual(None, exc.__context__)
|
self.assertEqual(None, exc.__context__)
|
||||||
self.assertEqual(False, exc.__suppress_context__)
|
self.assertEqual(False, exc.__suppress_context__)
|
||||||
self.assertEqual(expected_stack, exc.stack)
|
self.assertEqual(expected_stack, exc.stack)
|
||||||
self.assertEqual(exc_info[0], exc.exc_type)
|
self.assertEqual(type(exc_obj), exc.exc_type)
|
||||||
self.assertEqual(str(exc_info[1]), str(exc))
|
self.assertEqual(str(exc_obj), str(exc))
|
||||||
|
|
||||||
def test_from_exception(self):
|
def test_from_exception(self):
|
||||||
# Check all the parameters are accepted.
|
# Check all the parameters are accepted.
|
||||||
@ -2435,9 +2435,10 @@ class TestTracebackException(unittest.TestCase):
|
|||||||
try:
|
try:
|
||||||
foo()
|
foo()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
exc_info = sys.exc_info()
|
exc_obj = e
|
||||||
|
tb = e.__traceback__
|
||||||
self.expected_stack = traceback.StackSummary.extract(
|
self.expected_stack = traceback.StackSummary.extract(
|
||||||
traceback.walk_tb(exc_info[2]), limit=1, lookup_lines=False,
|
traceback.walk_tb(tb), limit=1, lookup_lines=False,
|
||||||
capture_locals=True)
|
capture_locals=True)
|
||||||
self.exc = traceback.TracebackException.from_exception(
|
self.exc = traceback.TracebackException.from_exception(
|
||||||
e, limit=1, lookup_lines=False, capture_locals=True)
|
e, limit=1, lookup_lines=False, capture_locals=True)
|
||||||
@ -2447,8 +2448,8 @@ class TestTracebackException(unittest.TestCase):
|
|||||||
self.assertEqual(None, exc.__context__)
|
self.assertEqual(None, exc.__context__)
|
||||||
self.assertEqual(False, exc.__suppress_context__)
|
self.assertEqual(False, exc.__suppress_context__)
|
||||||
self.assertEqual(expected_stack, exc.stack)
|
self.assertEqual(expected_stack, exc.stack)
|
||||||
self.assertEqual(exc_info[0], exc.exc_type)
|
self.assertEqual(type(exc_obj), exc.exc_type)
|
||||||
self.assertEqual(str(exc_info[1]), str(exc))
|
self.assertEqual(str(exc_obj), str(exc))
|
||||||
|
|
||||||
def test_cause(self):
|
def test_cause(self):
|
||||||
try:
|
try:
|
||||||
@ -2459,18 +2460,18 @@ class TestTracebackException(unittest.TestCase):
|
|||||||
exc_context = traceback.TracebackException(*exc_info_context)
|
exc_context = traceback.TracebackException(*exc_info_context)
|
||||||
cause = Exception("cause")
|
cause = Exception("cause")
|
||||||
raise Exception("uh oh") from cause
|
raise Exception("uh oh") from cause
|
||||||
except Exception:
|
except Exception as e:
|
||||||
exc_info = sys.exc_info()
|
exc_obj = e
|
||||||
exc = traceback.TracebackException(*exc_info)
|
exc = traceback.TracebackException.from_exception(e)
|
||||||
expected_stack = traceback.StackSummary.extract(
|
expected_stack = traceback.StackSummary.extract(
|
||||||
traceback.walk_tb(exc_info[2]))
|
traceback.walk_tb(e.__traceback__))
|
||||||
exc_cause = traceback.TracebackException(Exception, cause, None)
|
exc_cause = traceback.TracebackException(Exception, cause, None)
|
||||||
self.assertEqual(exc_cause, exc.__cause__)
|
self.assertEqual(exc_cause, exc.__cause__)
|
||||||
self.assertEqual(exc_context, exc.__context__)
|
self.assertEqual(exc_context, exc.__context__)
|
||||||
self.assertEqual(True, exc.__suppress_context__)
|
self.assertEqual(True, exc.__suppress_context__)
|
||||||
self.assertEqual(expected_stack, exc.stack)
|
self.assertEqual(expected_stack, exc.stack)
|
||||||
self.assertEqual(exc_info[0], exc.exc_type)
|
self.assertEqual(type(exc_obj), exc.exc_type)
|
||||||
self.assertEqual(str(exc_info[1]), str(exc))
|
self.assertEqual(str(exc_obj), str(exc))
|
||||||
|
|
||||||
def test_context(self):
|
def test_context(self):
|
||||||
try:
|
try:
|
||||||
@ -2480,17 +2481,17 @@ class TestTracebackException(unittest.TestCase):
|
|||||||
exc_info_context = sys.exc_info()
|
exc_info_context = sys.exc_info()
|
||||||
exc_context = traceback.TracebackException(*exc_info_context)
|
exc_context = traceback.TracebackException(*exc_info_context)
|
||||||
raise Exception("uh oh")
|
raise Exception("uh oh")
|
||||||
except Exception:
|
except Exception as e:
|
||||||
exc_info = sys.exc_info()
|
exc_obj = e
|
||||||
exc = traceback.TracebackException(*exc_info)
|
exc = traceback.TracebackException.from_exception(e)
|
||||||
expected_stack = traceback.StackSummary.extract(
|
expected_stack = traceback.StackSummary.extract(
|
||||||
traceback.walk_tb(exc_info[2]))
|
traceback.walk_tb(e.__traceback__))
|
||||||
self.assertEqual(None, exc.__cause__)
|
self.assertEqual(None, exc.__cause__)
|
||||||
self.assertEqual(exc_context, exc.__context__)
|
self.assertEqual(exc_context, exc.__context__)
|
||||||
self.assertEqual(False, exc.__suppress_context__)
|
self.assertEqual(False, exc.__suppress_context__)
|
||||||
self.assertEqual(expected_stack, exc.stack)
|
self.assertEqual(expected_stack, exc.stack)
|
||||||
self.assertEqual(exc_info[0], exc.exc_type)
|
self.assertEqual(type(exc_obj), exc.exc_type)
|
||||||
self.assertEqual(str(exc_info[1]), str(exc))
|
self.assertEqual(str(exc_obj), str(exc))
|
||||||
|
|
||||||
def test_long_context_chain(self):
|
def test_long_context_chain(self):
|
||||||
def f():
|
def f():
|
||||||
@ -2501,12 +2502,12 @@ class TestTracebackException(unittest.TestCase):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
f()
|
f()
|
||||||
except RecursionError:
|
except RecursionError as e:
|
||||||
exc_info = sys.exc_info()
|
exc_obj = e
|
||||||
else:
|
else:
|
||||||
self.fail("Exception not raised")
|
self.fail("Exception not raised")
|
||||||
|
|
||||||
te = traceback.TracebackException(*exc_info)
|
te = traceback.TracebackException.from_exception(exc_obj)
|
||||||
res = list(te.format())
|
res = list(te.format())
|
||||||
|
|
||||||
# many ZeroDiv errors followed by the RecursionError
|
# many ZeroDiv errors followed by the RecursionError
|
||||||
@ -2524,18 +2525,18 @@ class TestTracebackException(unittest.TestCase):
|
|||||||
finally:
|
finally:
|
||||||
cause = Exception("cause")
|
cause = Exception("cause")
|
||||||
raise Exception("uh oh") from cause
|
raise Exception("uh oh") from cause
|
||||||
except Exception:
|
except Exception as e:
|
||||||
exc_info = sys.exc_info()
|
exc_obj = e
|
||||||
exc = traceback.TracebackException(*exc_info, compact=True)
|
exc = traceback.TracebackException.from_exception(exc_obj, compact=True)
|
||||||
expected_stack = traceback.StackSummary.extract(
|
expected_stack = traceback.StackSummary.extract(
|
||||||
traceback.walk_tb(exc_info[2]))
|
traceback.walk_tb(exc_obj.__traceback__))
|
||||||
exc_cause = traceback.TracebackException(Exception, cause, None)
|
exc_cause = traceback.TracebackException(Exception, cause, None)
|
||||||
self.assertEqual(exc_cause, exc.__cause__)
|
self.assertEqual(exc_cause, exc.__cause__)
|
||||||
self.assertEqual(None, exc.__context__)
|
self.assertEqual(None, exc.__context__)
|
||||||
self.assertEqual(True, exc.__suppress_context__)
|
self.assertEqual(True, exc.__suppress_context__)
|
||||||
self.assertEqual(expected_stack, exc.stack)
|
self.assertEqual(expected_stack, exc.stack)
|
||||||
self.assertEqual(exc_info[0], exc.exc_type)
|
self.assertEqual(type(exc_obj), exc.exc_type)
|
||||||
self.assertEqual(str(exc_info[1]), str(exc))
|
self.assertEqual(str(exc_obj), str(exc))
|
||||||
|
|
||||||
def test_compact_no_cause(self):
|
def test_compact_no_cause(self):
|
||||||
try:
|
try:
|
||||||
@ -2545,37 +2546,37 @@ class TestTracebackException(unittest.TestCase):
|
|||||||
exc_info_context = sys.exc_info()
|
exc_info_context = sys.exc_info()
|
||||||
exc_context = traceback.TracebackException(*exc_info_context)
|
exc_context = traceback.TracebackException(*exc_info_context)
|
||||||
raise Exception("uh oh")
|
raise Exception("uh oh")
|
||||||
except Exception:
|
except Exception as e:
|
||||||
exc_info = sys.exc_info()
|
exc_obj = e
|
||||||
exc = traceback.TracebackException(*exc_info, compact=True)
|
exc = traceback.TracebackException.from_exception(e, compact=True)
|
||||||
expected_stack = traceback.StackSummary.extract(
|
expected_stack = traceback.StackSummary.extract(
|
||||||
traceback.walk_tb(exc_info[2]))
|
traceback.walk_tb(exc_obj.__traceback__))
|
||||||
self.assertEqual(None, exc.__cause__)
|
self.assertEqual(None, exc.__cause__)
|
||||||
self.assertEqual(exc_context, exc.__context__)
|
self.assertEqual(exc_context, exc.__context__)
|
||||||
self.assertEqual(False, exc.__suppress_context__)
|
self.assertEqual(False, exc.__suppress_context__)
|
||||||
self.assertEqual(expected_stack, exc.stack)
|
self.assertEqual(expected_stack, exc.stack)
|
||||||
self.assertEqual(exc_info[0], exc.exc_type)
|
self.assertEqual(type(exc_obj), exc.exc_type)
|
||||||
self.assertEqual(str(exc_info[1]), str(exc))
|
self.assertEqual(str(exc_obj), str(exc))
|
||||||
|
|
||||||
def test_no_refs_to_exception_and_traceback_objects(self):
|
def test_no_refs_to_exception_and_traceback_objects(self):
|
||||||
try:
|
try:
|
||||||
1/0
|
1/0
|
||||||
except Exception:
|
except Exception as e:
|
||||||
exc_info = sys.exc_info()
|
exc_obj = e
|
||||||
|
|
||||||
refcnt1 = sys.getrefcount(exc_info[1])
|
refcnt1 = sys.getrefcount(exc_obj)
|
||||||
refcnt2 = sys.getrefcount(exc_info[2])
|
refcnt2 = sys.getrefcount(exc_obj.__traceback__)
|
||||||
exc = traceback.TracebackException(*exc_info)
|
exc = traceback.TracebackException.from_exception(exc_obj)
|
||||||
self.assertEqual(sys.getrefcount(exc_info[1]), refcnt1)
|
self.assertEqual(sys.getrefcount(exc_obj), refcnt1)
|
||||||
self.assertEqual(sys.getrefcount(exc_info[2]), refcnt2)
|
self.assertEqual(sys.getrefcount(exc_obj.__traceback__), refcnt2)
|
||||||
|
|
||||||
def test_comparison_basic(self):
|
def test_comparison_basic(self):
|
||||||
try:
|
try:
|
||||||
1/0
|
1/0
|
||||||
except Exception:
|
except Exception as e:
|
||||||
exc_info = sys.exc_info()
|
exc_obj = e
|
||||||
exc = traceback.TracebackException(*exc_info)
|
exc = traceback.TracebackException.from_exception(exc_obj)
|
||||||
exc2 = traceback.TracebackException(*exc_info)
|
exc2 = traceback.TracebackException.from_exception(exc_obj)
|
||||||
self.assertIsNot(exc, exc2)
|
self.assertIsNot(exc, exc2)
|
||||||
self.assertEqual(exc, exc2)
|
self.assertEqual(exc, exc2)
|
||||||
self.assertNotEqual(exc, object())
|
self.assertNotEqual(exc, object())
|
||||||
@ -2594,28 +2595,28 @@ class TestTracebackException(unittest.TestCase):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
raise_with_locals()
|
raise_with_locals()
|
||||||
except Exception:
|
except Exception as e:
|
||||||
exc_info = sys.exc_info()
|
exc_obj = e
|
||||||
|
|
||||||
exc = traceback.TracebackException(*exc_info)
|
exc = traceback.TracebackException.from_exception(exc_obj)
|
||||||
exc1 = traceback.TracebackException(*exc_info, limit=10)
|
exc1 = traceback.TracebackException.from_exception(exc_obj, limit=10)
|
||||||
exc2 = traceback.TracebackException(*exc_info, limit=2)
|
exc2 = traceback.TracebackException.from_exception(exc_obj, limit=2)
|
||||||
|
|
||||||
self.assertEqual(exc, exc1) # limit=10 gets all frames
|
self.assertEqual(exc, exc1) # limit=10 gets all frames
|
||||||
self.assertNotEqual(exc, exc2) # limit=2 truncates the output
|
self.assertNotEqual(exc, exc2) # limit=2 truncates the output
|
||||||
|
|
||||||
# locals change the output
|
# locals change the output
|
||||||
exc3 = traceback.TracebackException(*exc_info, capture_locals=True)
|
exc3 = traceback.TracebackException.from_exception(exc_obj, capture_locals=True)
|
||||||
self.assertNotEqual(exc, exc3)
|
self.assertNotEqual(exc, exc3)
|
||||||
|
|
||||||
# there are no locals in the innermost frame
|
# there are no locals in the innermost frame
|
||||||
exc4 = traceback.TracebackException(*exc_info, limit=-1)
|
exc4 = traceback.TracebackException.from_exception(exc_obj, limit=-1)
|
||||||
exc5 = traceback.TracebackException(*exc_info, limit=-1, capture_locals=True)
|
exc5 = traceback.TracebackException.from_exception(exc_obj, limit=-1, capture_locals=True)
|
||||||
self.assertEqual(exc4, exc5)
|
self.assertEqual(exc4, exc5)
|
||||||
|
|
||||||
# there are locals in the next-to-innermost frame
|
# there are locals in the next-to-innermost frame
|
||||||
exc6 = traceback.TracebackException(*exc_info, limit=-2)
|
exc6 = traceback.TracebackException.from_exception(exc_obj, limit=-2)
|
||||||
exc7 = traceback.TracebackException(*exc_info, limit=-2, capture_locals=True)
|
exc7 = traceback.TracebackException.from_exception(exc_obj, limit=-2, capture_locals=True)
|
||||||
self.assertNotEqual(exc6, exc7)
|
self.assertNotEqual(exc6, exc7)
|
||||||
|
|
||||||
def test_comparison_equivalent_exceptions_are_equal(self):
|
def test_comparison_equivalent_exceptions_are_equal(self):
|
||||||
@ -2623,8 +2624,8 @@ class TestTracebackException(unittest.TestCase):
|
|||||||
for _ in range(2):
|
for _ in range(2):
|
||||||
try:
|
try:
|
||||||
1/0
|
1/0
|
||||||
except:
|
except Exception as e:
|
||||||
excs.append(traceback.TracebackException(*sys.exc_info()))
|
excs.append(traceback.TracebackException.from_exception(e))
|
||||||
self.assertEqual(excs[0], excs[1])
|
self.assertEqual(excs[0], excs[1])
|
||||||
self.assertEqual(list(excs[0].format()), list(excs[1].format()))
|
self.assertEqual(list(excs[0].format()), list(excs[1].format()))
|
||||||
|
|
||||||
@ -2640,9 +2641,9 @@ class TestTracebackException(unittest.TestCase):
|
|||||||
except UnhashableException:
|
except UnhashableException:
|
||||||
try:
|
try:
|
||||||
raise ex1
|
raise ex1
|
||||||
except UnhashableException:
|
except UnhashableException as e:
|
||||||
exc_info = sys.exc_info()
|
exc_obj = e
|
||||||
exc = traceback.TracebackException(*exc_info)
|
exc = traceback.TracebackException.from_exception(exc_obj)
|
||||||
formatted = list(exc.format())
|
formatted = list(exc.format())
|
||||||
self.assertIn('UnhashableException: ex2\n', formatted[2])
|
self.assertIn('UnhashableException: ex2\n', formatted[2])
|
||||||
self.assertIn('UnhashableException: ex1\n', formatted[6])
|
self.assertIn('UnhashableException: ex1\n', formatted[6])
|
||||||
@ -2655,11 +2656,10 @@ class TestTracebackException(unittest.TestCase):
|
|||||||
1/0
|
1/0
|
||||||
try:
|
try:
|
||||||
recurse(10)
|
recurse(10)
|
||||||
except Exception:
|
except Exception as e:
|
||||||
exc_info = sys.exc_info()
|
exc = traceback.TracebackException.from_exception(e, limit=5)
|
||||||
exc = traceback.TracebackException(*exc_info, limit=5)
|
|
||||||
expected_stack = traceback.StackSummary.extract(
|
expected_stack = traceback.StackSummary.extract(
|
||||||
traceback.walk_tb(exc_info[2]), limit=5)
|
traceback.walk_tb(e.__traceback__), limit=5)
|
||||||
self.assertEqual(expected_stack, exc.stack)
|
self.assertEqual(expected_stack, exc.stack)
|
||||||
|
|
||||||
def test_lookup_lines(self):
|
def test_lookup_lines(self):
|
||||||
@ -2706,9 +2706,9 @@ class TestTracebackException(unittest.TestCase):
|
|||||||
x = 12
|
x = 12
|
||||||
try:
|
try:
|
||||||
x/0
|
x/0
|
||||||
except Exception:
|
except Exception as e:
|
||||||
return sys.exc_info()
|
return e
|
||||||
exc = traceback.TracebackException(*f(), capture_locals=True)
|
exc = traceback.TracebackException.from_exception(f(), capture_locals=True)
|
||||||
output = StringIO()
|
output = StringIO()
|
||||||
exc.print(file=output)
|
exc.print(file=output)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
@ -2723,7 +2723,7 @@ class TestTracebackException(unittest.TestCase):
|
|||||||
class TestTracebackException_ExceptionGroups(unittest.TestCase):
|
class TestTracebackException_ExceptionGroups(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super().setUp()
|
super().setUp()
|
||||||
self.eg_info = self._get_exception_group()
|
self.eg = self._get_exception_group()
|
||||||
|
|
||||||
def _get_exception_group(self):
|
def _get_exception_group(self):
|
||||||
def f():
|
def f():
|
||||||
@ -2753,26 +2753,26 @@ class TestTracebackException_ExceptionGroups(unittest.TestCase):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
exc4 = e
|
exc4 = e
|
||||||
raise ExceptionGroup("eg2", [exc3, exc4])
|
raise ExceptionGroup("eg2", [exc3, exc4])
|
||||||
except ExceptionGroup:
|
except ExceptionGroup as eg:
|
||||||
return sys.exc_info()
|
return eg
|
||||||
self.fail('Exception Not Raised')
|
self.fail('Exception Not Raised')
|
||||||
|
|
||||||
def test_exception_group_construction(self):
|
def test_exception_group_construction(self):
|
||||||
eg_info = self.eg_info
|
eg = self.eg
|
||||||
teg1 = traceback.TracebackException(*eg_info)
|
teg1 = traceback.TracebackException(type(eg), eg, eg.__traceback__)
|
||||||
teg2 = traceback.TracebackException.from_exception(eg_info[1])
|
teg2 = traceback.TracebackException.from_exception(eg)
|
||||||
self.assertIsNot(teg1, teg2)
|
self.assertIsNot(teg1, teg2)
|
||||||
self.assertEqual(teg1, teg2)
|
self.assertEqual(teg1, teg2)
|
||||||
|
|
||||||
def test_exception_group_format_exception_only(self):
|
def test_exception_group_format_exception_only(self):
|
||||||
teg = traceback.TracebackException(*self.eg_info)
|
teg = traceback.TracebackException.from_exception(self.eg)
|
||||||
formatted = ''.join(teg.format_exception_only()).split('\n')
|
formatted = ''.join(teg.format_exception_only()).split('\n')
|
||||||
expected = "ExceptionGroup: eg2 (2 sub-exceptions)\n".split('\n')
|
expected = "ExceptionGroup: eg2 (2 sub-exceptions)\n".split('\n')
|
||||||
|
|
||||||
self.assertEqual(formatted, expected)
|
self.assertEqual(formatted, expected)
|
||||||
|
|
||||||
def test_exception_group_format(self):
|
def test_exception_group_format(self):
|
||||||
teg = traceback.TracebackException(*self.eg_info)
|
teg = traceback.TracebackException.from_exception(self.eg)
|
||||||
|
|
||||||
formatted = ''.join(teg.format()).split('\n')
|
formatted = ''.join(teg.format()).split('\n')
|
||||||
lno_f = self.lno_f
|
lno_f = self.lno_f
|
||||||
@ -2884,18 +2884,18 @@ class TestTracebackException_ExceptionGroups(unittest.TestCase):
|
|||||||
|
|
||||||
def test_comparison(self):
|
def test_comparison(self):
|
||||||
try:
|
try:
|
||||||
raise self.eg_info[1]
|
raise self.eg
|
||||||
except ExceptionGroup:
|
except ExceptionGroup as e:
|
||||||
exc_info = sys.exc_info()
|
exc = e
|
||||||
for _ in range(5):
|
for _ in range(5):
|
||||||
try:
|
try:
|
||||||
raise exc_info[1]
|
raise exc
|
||||||
except:
|
except Exception as e:
|
||||||
exc_info = sys.exc_info()
|
exc_obj = e
|
||||||
exc = traceback.TracebackException(*exc_info)
|
exc = traceback.TracebackException.from_exception(exc_obj)
|
||||||
exc2 = traceback.TracebackException(*exc_info)
|
exc2 = traceback.TracebackException.from_exception(exc_obj)
|
||||||
exc3 = traceback.TracebackException(*exc_info, limit=300)
|
exc3 = traceback.TracebackException.from_exception(exc_obj, limit=300)
|
||||||
ne = traceback.TracebackException(*exc_info, limit=3)
|
ne = traceback.TracebackException.from_exception(exc_obj, limit=3)
|
||||||
self.assertIsNot(exc, exc2)
|
self.assertIsNot(exc, exc2)
|
||||||
self.assertEqual(exc, exc2)
|
self.assertEqual(exc, exc2)
|
||||||
self.assertEqual(exc, exc3)
|
self.assertEqual(exc, exc3)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user