Skip to content

Commit 82542f3

Browse files
committed
test_map_exception: improve readability
1 parent 85a3c36 commit 82542f3

1 file changed

Lines changed: 13 additions & 12 deletions

File tree

Lib/test/test_concurrent_futures/executor.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import traceback
66
import weakref
77
from concurrent import futures
8-
from operator import add, itemgetter
8+
from operator import add
99
from test import support
1010
from test.support import Py_GIL_DISABLED
1111

@@ -60,36 +60,37 @@ def test_map_exception(self):
6060

6161
exception = None
6262
try:
63-
next(i)
63+
i.__next__()
6464
except Exception as e:
6565
exception = e
6666
self.assertTrue(
6767
isinstance(exception, ZeroDivisionError),
68-
msg="next should raise a ZeroDivisionError",
68+
msg="should raise a ZeroDivisionError",
6969
)
7070

71-
# free-threading builds need this pause on Ubuntu (ARM) and Windows
71+
# pause needed for free-threading builds on Ubuntu (ARM) and Windows
7272
time.sleep(1)
7373

7474
self.assertFalse(
7575
gc.get_referrers(exception),
7676
msg="the exception should not have any referrers",
7777
)
7878

79-
frames = map(itemgetter(0), traceback.walk_tb(exception.__traceback__))
80-
81-
# skip current frame
82-
next(frames)
83-
8479
self.assertFalse(
8580
[
8681
(var, val)
87-
for frame in frames
82+
# go through the frames of the exception's traceback
83+
for frame, _ in traceback.walk_tb(exception.__traceback__)
84+
# skipping the current frame
85+
if frame is not exception.__traceback__.tb_frame
86+
# go through the locals captured in that frame
8887
for var, val in frame.f_locals.items()
88+
# check if one of them is an exception
8989
if isinstance(val, Exception)
90-
and frame in map(itemgetter(0), traceback.walk_tb(val.__traceback__))
90+
# check if it is captured in its own traceback
91+
and frame is val.__traceback__.tb_frame
9192
],
92-
msg=f"the exception's traceback should not contain an exception that captures itself in its own traceback",
93+
msg=f"the exception's traceback should not contain an exception captured in its own traceback",
9394
)
9495

9596
@support.requires_resource('walltime')

0 commit comments

Comments
 (0)