From ccf80a09c6bab9497cb670cd92f67a21c173e8ab Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Thu, 26 Nov 2020 16:11:03 +0000 Subject: [PATCH 1/3] bpo-42474: add TracebackException comparison tests for non-equal instances of this type --- Lib/test/test_traceback.py | 41 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 91688ff72bbea1..14d8d6d42abf15 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -1123,7 +1123,7 @@ def test_context(self): self.assertEqual(exc_info[0], exc.exc_type) self.assertEqual(str(exc_info[1]), str(exc)) - def test_comparison(self): + def test_comparison_basic(self): try: 1/0 except Exception: @@ -1135,6 +1135,43 @@ def test_comparison(self): self.assertNotEqual(exc, object()) self.assertEqual(exc, ALWAYS_EQ) + def test_comparison_params_variations(self): + def raise_exc(): + try: + raise ValueError('bad value') + except: + raise + + def raise_with_locals(): + x, y = 1, 2 + raise_exc() + + try: + raise_with_locals() + except Exception: + exc_info = sys.exc_info() + + exc = traceback.TracebackException(*exc_info) + exc1 = traceback.TracebackException(*exc_info, limit=10) + exc2 = traceback.TracebackException(*exc_info, limit=2) + + self.assertEqual(exc, exc1) # limit=10 gets all frames + self.assertNotEqual(exc, exc2) # limit=1 truncates the output + + # locals change the output + exc3 = traceback.TracebackException(*exc_info, capture_locals=True) + self.assertNotEqual(exc, exc3) + + # there are no locals in the next-to-innermost frame + exc4 = traceback.TracebackException(*exc_info, limit=-1) + exc5 = traceback.TracebackException(*exc_info, limit=-1, capture_locals=True) + self.assertEqual(exc4, exc5) + + # there are locals in the next-to-innermost frame + exc6 = traceback.TracebackException(*exc_info, limit=-2) + exc7 = traceback.TracebackException(*exc_info, limit=-2, capture_locals=True) + self.assertNotEqual(exc6, exc7) + def test_unhashable(self): class UnhashableException(Exception): def __eq__(self, other): @@ -1176,7 +1213,7 @@ def test_lookup_lines(self): f = test_frame(c, None, None) tb = test_tb(f, 6, None) exc = traceback.TracebackException(Exception, e, tb, lookup_lines=False) - self.assertEqual({}, linecache.cache) + self.assertEqual(linecache.cache, {}) linecache.updatecache('/foo.py', globals()) self.assertEqual(exc.stack[0].line, "import sys") From 5db2200288a84b52cb78a031442249e2ef1927c8 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 27 Nov 2020 08:55:42 +0000 Subject: [PATCH 2/3] Update Lib/test/test_traceback.py Co-authored-by: Guido van Rossum --- Lib/test/test_traceback.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 14d8d6d42abf15..bca1fbd766082c 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -1156,7 +1156,7 @@ def raise_with_locals(): exc2 = traceback.TracebackException(*exc_info, limit=2) self.assertEqual(exc, exc1) # limit=10 gets all frames - self.assertNotEqual(exc, exc2) # limit=1 truncates the output + self.assertNotEqual(exc, exc2) # limit=2 truncates the output # locals change the output exc3 = traceback.TracebackException(*exc_info, capture_locals=True) From 4484d8c6d0d055b50f978fa7a72e8fc1c8e93b5f Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 27 Nov 2020 08:56:10 +0000 Subject: [PATCH 3/3] Update Lib/test/test_traceback.py Co-authored-by: Guido van Rossum --- Lib/test/test_traceback.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index bca1fbd766082c..f86afc673d86e7 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -1162,7 +1162,7 @@ def raise_with_locals(): exc3 = traceback.TracebackException(*exc_info, capture_locals=True) self.assertNotEqual(exc, exc3) - # there are no locals in the next-to-innermost frame + # there are no locals in the innermost frame exc4 = traceback.TracebackException(*exc_info, limit=-1) exc5 = traceback.TracebackException(*exc_info, limit=-1, capture_locals=True) self.assertEqual(exc4, exc5)