diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2017-10-17 22:14:19 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2017-10-17 22:14:19 (GMT) |
commit | 2712247ec94dcc12cf9abeec78f18f54fcc3357a (patch) | |
tree | 75e8771fb391d7f199ed20cc464cb93724b72f39 /Lib/test | |
parent | 858ea4354fafa36e57859d2dfd70f8a057984075 (diff) | |
download | cpython-2712247ec94dcc12cf9abeec78f18f54fcc3357a.zip cpython-2712247ec94dcc12cf9abeec78f18f54fcc3357a.tar.gz cpython-2712247ec94dcc12cf9abeec78f18f54fcc3357a.tar.bz2 |
[3.6] bpo-28603: Fix formatting tracebacks for unhashable exceptions (GH-4014) (#4024)
(cherry picked from commit de86073a761cd3539aaca6f886a1f55effc0d9da)
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_traceback.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index e483353..bffc03e 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -443,6 +443,33 @@ class TracebackFormatTests(unittest.TestCase): ' return traceback.format_stack()\n' % (__file__, lineno+1), ]) + @cpython_only + def test_unhashable(self): + from _testcapi import exception_print + + 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_type, exc_val, exc_tb = sys.exc_info() + + with captured_output("stderr") as stderr_f: + exception_print(exc_val) + + tb = stderr_f.getvalue().strip().splitlines() + self.assertEqual(11, len(tb)) + self.assertEqual(context_message.strip(), tb[5]) + self.assertIn('UnhashableException: ex2', tb[3]) + self.assertIn('UnhashableException: ex1', tb[10]) + cause_message = ( "\nThe above exception was the direct cause " @@ -994,6 +1021,25 @@ class TestTracebackException(unittest.TestCase): self.assertEqual(exc_info[0], exc.exc_type) self.assertEqual(str(exc_info[1]), str(exc)) + 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_limit(self): def recurse(n): if n: |