Skip to content
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-99553: fix bug where an ExceptionGroup subclass can wrap a BaseException #99572

Merged
merged 1 commit into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Doc/library/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,10 @@ their subgroups based on the types of the contained exceptions.
def derive(self, excs):
return Errors(excs, self.exit_code)

Like :exc:`ExceptionGroup`, any subclass of :exc:`BaseExceptionGroup` which
is also a subclass of :exc:`Exception` can only wrap instances of
:exc:`Exception`.
Comment on lines +968 to +970
Copy link
Contributor

@Zac-HD Zac-HD Nov 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As written, consistency suggests that class MyEG(BaseExceptionGroup, ValueError): should be unable to wrap anything but ValueErrors!

That may in fact be the intention, and it wouldn't break the trio.MultiError-as-BaseExceptionGroup deprecation pathway, but it's not the implementation and the tests don't distinguish these cases.

Otherwise, I think we'd want to document the behavior which is that "any subclass of ExceptionGroup can only wrap instances of Exception, like ExceptionGroup itself.", and therefore class MyEG(BaseExceptionGroup, ValueError): would be able to contain BaseExceptions 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As written, this suggests that class MyEG(BaseExceptionGroup, ValueError): should be unable to wrap anything but ValueErrors!

Does it? In the doc the Exception becomes a hyperlink.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But feel free to make a PR with better wording.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect some users to read that as "if a subclass of BaseExceptionGroup is also a subclass of X, it can only wrap instances of X"; I'll open a PR to rephrase as "if it's also a subclass of Exception, it can't wrap BaseExceptions".

But then we're still treating Exception as particularly special for subclasses, which feels a bit strange.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zac, I'm confused why you think the sentence as written could be interpreted that way. It says Exception twice. In this context that is very clearly a reference to the specific built-in class named Exception (it even links there), not a "free variable" that must refer to the same class in both places.

Regardless, even if we clear that up, it feels like it would be expensive to inspect the class hierarchy to find other subclasses of BaseException and try to constrain the arguments to inherit from those. And that's definitely not what the PEP intended. We want the common case (where you're using one of the two built-in EG classes) to be foolproof, and other cases possible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for the confusion, that's on me 😞

I do think the behavior in this PR is good, and I'll open a PR with some additional tests and maybe docs to distinguish it from the case that I was wondering about. Thanks again to both of you for everything.


.. versionadded:: 3.11


Expand Down
22 changes: 18 additions & 4 deletions Lib/test/test_exception_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,30 @@ def test_BEG_wraps_BaseException__creates_BEG(self):
beg = BaseExceptionGroup("beg", [ValueError(1), KeyboardInterrupt(2)])
self.assertIs(type(beg), BaseExceptionGroup)

def test_EG_subclass_wraps_anything(self):
def test_EG_subclass_wraps_non_base_exceptions(self):
class MyEG(ExceptionGroup):
pass

self.assertIs(
type(MyEG("eg", [ValueError(12), TypeError(42)])),
MyEG)
self.assertIs(
type(MyEG("eg", [ValueError(12), KeyboardInterrupt(42)])),
MyEG)

def test_EG_subclass_does_not_wrap_base_exceptions(self):
class MyEG(ExceptionGroup):
pass

msg = "Cannot nest BaseExceptions in 'MyEG'"
with self.assertRaisesRegex(TypeError, msg):
MyEG("eg", [ValueError(12), KeyboardInterrupt(42)])

def test_BEG_and_E_subclass_does_not_wrap_base_exceptions(self):
class MyEG(BaseExceptionGroup, ValueError):
pass

msg = "Cannot nest BaseExceptions in 'MyEG'"
with self.assertRaisesRegex(TypeError, msg):
MyEG("eg", [ValueError(12), KeyboardInterrupt(42)])


def test_BEG_subclass_wraps_anything(self):
class MyBEG(BaseExceptionGroup):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix bug where an :exc:`ExceptionGroup` subclass can wrap a
:exc:`BaseException`.
14 changes: 13 additions & 1 deletion Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,19 @@ BaseExceptionGroup_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}
}
else {
/* Do nothing - we don't interfere with subclasses */
/* user-defined subclass */
if (nested_base_exceptions) {
int nonbase = PyObject_IsSubclass((PyObject*)cls, PyExc_Exception);
if (nonbase == -1) {
goto error;
}
else if (nonbase == 1) {
PyErr_Format(PyExc_TypeError,
"Cannot nest BaseExceptions in '%.200s'",
cls->tp_name);
goto error;
}
}
}

if (!cls) {
Expand Down