Skip to content

Commit

Permalink
Increase test coverage of Objects/exceptions.c (GH-94674)
Browse files Browse the repository at this point in the history
  • Loading branch information
iritkatriel authored Jul 8, 2022
1 parent 9dff9f4 commit 4bed0db
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
12 changes: 12 additions & 0 deletions Lib/test/test_exception_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,18 @@ def test_split_does_not_copy_non_sequence_notes(self):
self.assertFalse(hasattr(match, '__notes__'))
self.assertFalse(hasattr(rest, '__notes__'))

def test_drive_invalid_return_value(self):
class MyEg(ExceptionGroup):
def derive(self, excs):
return 42

eg = MyEg('eg', [TypeError(1), ValueError(2)])
msg = "derive must return an instance of BaseExceptionGroup"
with self.assertRaisesRegex(TypeError, msg):
eg.split(TypeError)
with self.assertRaisesRegex(TypeError, msg):
eg.subgroup(TypeError)


class NestedExceptionGroupSubclassSplitTest(ExceptionGroupSplitTestBase):

Expand Down
52 changes: 47 additions & 5 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,29 @@ def testAttributes(self):
'pickled "%r", attribute "%s' %
(e, checkArgName))

def test_setstate(self):
e = Exception(42)
e.blah = 53
self.assertEqual(e.args, (42,))
self.assertEqual(e.blah, 53)
self.assertRaises(AttributeError, getattr, e, 'a')
self.assertRaises(AttributeError, getattr, e, 'b')
e.__setstate__({'a': 1 , 'b': 2})
self.assertEqual(e.args, (42,))
self.assertEqual(e.blah, 53)
self.assertEqual(e.a, 1)
self.assertEqual(e.b, 2)
e.__setstate__({'a': 11, 'args': (1,2,3), 'blah': 35})
self.assertEqual(e.args, (1,2,3))
self.assertEqual(e.blah, 35)
self.assertEqual(e.a, 11)
self.assertEqual(e.b, 2)

def test_invalid_setstate(self):
e = Exception(42)
with self.assertRaisesRegex(TypeError, "state is not a dictionary"):
e.__setstate__(42)

def test_notes(self):
for e in [BaseException(1), Exception(2), ValueError(3)]:
with self.subTest(e=e):
Expand Down Expand Up @@ -602,11 +625,30 @@ def testInvalidTraceback(self):
else:
self.fail("No exception raised")

def testInvalidAttrs(self):
self.assertRaises(TypeError, setattr, Exception(), '__cause__', 1)
self.assertRaises(TypeError, delattr, Exception(), '__cause__')
self.assertRaises(TypeError, setattr, Exception(), '__context__', 1)
self.assertRaises(TypeError, delattr, Exception(), '__context__')
def test_invalid_setattr(self):
TE = TypeError
exc = Exception()
msg = "'int' object is not iterable"
self.assertRaisesRegex(TE, msg, setattr, exc, 'args', 1)
msg = "__traceback__ must be a traceback or None"
self.assertRaisesRegex(TE, msg, setattr, exc, '__traceback__', 1)
msg = "exception cause must be None or derive from BaseException"
self.assertRaisesRegex(TE, msg, setattr, exc, '__cause__', 1)
msg = "exception context must be None or derive from BaseException"
self.assertRaisesRegex(TE, msg, setattr, exc, '__context__', 1)

def test_invalid_delattr(self):
TE = TypeError
try:
raise IndexError(4)
except Exception as e:
exc = e

msg = "may not be deleted"
self.assertRaisesRegex(TE, msg, delattr, exc, 'args')
self.assertRaisesRegex(TE, msg, delattr, exc, '__traceback__')
self.assertRaisesRegex(TE, msg, delattr, exc, '__cause__')
self.assertRaisesRegex(TE, msg, delattr, exc, '__context__')

def testNoneClearsTracebackAttr(self):
try:
Expand Down

0 comments on commit 4bed0db

Please sign in to comment.