Skip to content

Commit 5b9f61b

Browse files
committed
add a test case for custom serializer that returning wrong type
1 parent 79e335e commit 5b9f61b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Lib/test/test_shelve.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,28 @@ def deserializer(data):
409409
self.assertRaises(shelve.ShelveError, shelve.Shelf, {}, **kwargs)
410410
self.assertRaises(shelve.ShelveError, shelve.BsdDbShelf, {}, **kwargs)
411411

412+
def test_custom_serializer_returns_wrong_type_for_key(self):
413+
os.mkdir(self.dirname)
414+
self.addCleanup(os_helper.rmtree, self.dirname)
415+
416+
def serializer(obj, protocol):
417+
# Return None instead of bytes, which is wrong for dbm keys
418+
return None
419+
420+
def deserializer(data):
421+
return data.decode("utf-8") if data else ""
422+
423+
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
424+
with self.subTest(proto=proto), shelve.open(
425+
self.fn,
426+
protocol=proto,
427+
serializer=serializer,
428+
deserializer=deserializer
429+
) as s:
430+
# Serializer returns None for the value, but dbm expects bytes
431+
with self.assertRaises((TypeError, dbm.error)):
432+
s["foo"] = "bar"
433+
412434

413435
class TestShelveBase:
414436
type2test = shelve.Shelf

0 commit comments

Comments
 (0)