gh-122163: Add notes for JSON serialization errors (GH-122165)

This allows to identify the source of the error.
This commit is contained in:
Serhiy Storchaka 2024-07-23 20:02:54 +03:00 committed by GitHub
parent c908d1f87d
commit e6b25e9a09
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 135 additions and 66 deletions

View file

@ -100,8 +100,27 @@ class TestFail:
def test_not_serializable(self):
import sys
with self.assertRaisesRegex(TypeError,
'Object of type module is not JSON serializable'):
'Object of type module is not JSON serializable') as cm:
self.dumps(sys)
self.assertFalse(hasattr(cm.exception, '__notes__'))
with self.assertRaises(TypeError) as cm:
self.dumps([1, [2, 3, sys]])
self.assertEqual(cm.exception.__notes__,
['when serializing list item 2',
'when serializing list item 1'])
with self.assertRaises(TypeError) as cm:
self.dumps((1, (2, 3, sys)))
self.assertEqual(cm.exception.__notes__,
['when serializing tuple item 2',
'when serializing tuple item 1'])
with self.assertRaises(TypeError) as cm:
self.dumps({'a': {'b': sys}})
self.assertEqual(cm.exception.__notes__,
["when serializing dict item 'b'",
"when serializing dict item 'a'"])
def test_truncated_input(self):
test_cases = [