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):
}
}
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) {