-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-132063: ProcessPoolExecutor swallows falsy Exceptions #132129
base: main
Are you sure you want to change the base?
gh-132063: ProcessPoolExecutor swallows falsy Exceptions #132129
Conversation
@@ -0,0 +1 @@ | |||
Fix tests of exception attribute in :method:`process_result_item`of :class:`_ExecutorManagerThread` and :method:`__get_result` of :class:`Future` in module :lib:`concurrent`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love the effort, but this is too many technical details for a blurb entry.
Fix tests of exception attribute in :method:`process_result_item`of :class:`_ExecutorManagerThread` and :method:`__get_result` of :class:`Future` in module :lib:`concurrent`. | |
Prevent exceptions that evaluate as falsey from being ignored | |
by :class:`concurrent.futures.ProcessPoolExecutor`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not limited to __bool__
, it can be anything that is falsey, including a class which implements __len__
as well (https://docs.python.org/3/reference/datamodel.html#object.__bool__)
class MyException(Exception): | ||
def __bool__(self): | ||
return False | ||
|
||
@classmethod | ||
def raiser(cls): | ||
raise cls.MyException("foo") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should isolate these to the test case, not the test class. It should be something like this:
def test_swallows_falsy_exceptions(self):
class MyException(Exception):
...
def raiser():
raise MyException(...)
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add a class which does not specify __bool__
but specifies __len__
and returns 0? TiA
raise cls.MyException("foo") | ||
|
||
def test_swallows_falsy_exceptions(self): | ||
# fix gh-132063 issue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you give a brief description of the problem in this comment?
def test_swallows_falsy_exceptions(self): | ||
# fix gh-132063 issue | ||
with self.assertRaisesRegex(self.MyException, "foo"): | ||
with self.executor_type(max_workers=1) as executor: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, this test case should go in the ExecutorTest
base class.
@@ -112,6 +112,19 @@ def log_n_wait(ident): | |||
# ident='third' is cancelled because it remained in the collection of futures | |||
self.assertListEqual(log, ["ident='first' started", "ident='first' stopped"]) | |||
|
|||
class MyException(Exception): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we don't need to repeat the test case. Let's move it to ExecutorTest
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I never sent my review.
@@ -0,0 +1 @@ | |||
Fix tests of exception attribute in :method:`process_result_item`of :class:`_ExecutorManagerThread` and :method:`__get_result` of :class:`Future` in module :lib:`concurrent`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not limited to __bool__
, it can be anything that is falsey, including a class which implements __len__
as well (https://docs.python.org/3/reference/datamodel.html#object.__bool__)
class MyException(Exception): | ||
def __bool__(self): | ||
return False | ||
|
||
@classmethod | ||
def raiser(cls): | ||
raise cls.MyException("foo") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add a class which does not specify __bool__
but specifies __len__
and returns 0? TiA
ProcessPoolExecutor and ThreadPoolExecutor swallows falsy exception.
Fix tests about exception variable in 2 files of /Lib/concurrent/futures
Changes 2 tests as below:
Replace
if exception:
withif exception is not None: