gh-89770: Implement PEP-678 - Exception notes (GH-31317)

This commit is contained in:
Irit Katriel 2022-04-16 19:59:52 +01:00 committed by GitHub
parent 7fa3a5a219
commit d4c4a76ed1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 383 additions and 144 deletions

View file

@ -547,26 +547,32 @@ class ExceptionTests(unittest.TestCase):
'pickled "%r", attribute "%s' %
(e, checkArgName))
def test_note(self):
def test_notes(self):
for e in [BaseException(1), Exception(2), ValueError(3)]:
with self.subTest(e=e):
self.assertIsNone(e.__note__)
e.__note__ = "My Note"
self.assertEqual(e.__note__, "My Note")
self.assertFalse(hasattr(e, '__notes__'))
e.add_note("My Note")
self.assertEqual(e.__notes__, ["My Note"])
with self.assertRaises(TypeError):
e.__note__ = 42
self.assertEqual(e.__note__, "My Note")
e.add_note(42)
self.assertEqual(e.__notes__, ["My Note"])
e.__note__ = "Your Note"
self.assertEqual(e.__note__, "Your Note")
e.add_note("Your Note")
self.assertEqual(e.__notes__, ["My Note", "Your Note"])
del e.__notes__
self.assertFalse(hasattr(e, '__notes__'))
e.add_note("Our Note")
self.assertEqual(e.__notes__, ["Our Note"])
e.__notes__ = 42
self.assertEqual(e.__notes__, 42)
with self.assertRaises(TypeError):
del e.__note__
self.assertEqual(e.__note__, "Your Note")
e.__note__ = None
self.assertIsNone(e.__note__)
e.add_note("will not work")
self.assertEqual(e.__notes__, 42)
def testWithTraceback(self):
try: