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

@ -8,6 +8,24 @@ class TestDefault:
self.dumps(type, default=repr),
self.dumps(repr(type)))
def test_bad_default(self):
def default(obj):
if obj is NotImplemented:
raise ValueError
if obj is ...:
return NotImplemented
if obj is type:
return collections
return [...]
with self.assertRaises(ValueError) as cm:
self.dumps(type, default=default)
self.assertEqual(cm.exception.__notes__,
['when serializing ellipsis object',
'when serializing list item 0',
'when serializing module object',
'when serializing type object'])
def test_ordereddict(self):
od = collections.OrderedDict(a=1, b=2, c=3, d=4)
od.move_to_end('b')