Skip to content

Commit 2e3ea9b

Browse files
committed
Add comments clarifying exceptions when serializer returns None
1 parent 5b9f61b commit 2e3ea9b

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

Lib/test/test_shelve.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def serializer(obj, protocol):
174174
if isinstance(obj, (bytes, bytearray, str)):
175175
if protocol == 5:
176176
if isinstance(obj, bytearray):
177-
return bytes(obj)
177+
return bytes(obj) # DBM backends expect bytes
178178
return obj
179179
return type(obj).__name__
180180
elif isinstance(obj, array.array):
@@ -234,6 +234,8 @@ def serializer(obj, protocol=None):
234234
def deserializer(data):
235235
return data.decode("utf-8")
236236

237+
# Since the serializer returns None, dbm.error is raised
238+
# by dbm.sqlite3 and TypeError is raised by other backends.
237239
with self.assertRaises((TypeError, dbm.error)):
238240
with shelve.open(self.fn, serializer=serializer,
239241
deserializer=deserializer) as s:
@@ -427,7 +429,8 @@ def deserializer(data):
427429
serializer=serializer,
428430
deserializer=deserializer
429431
) as s:
430-
# Serializer returns None for the value, but dbm expects bytes
432+
# Since the serializer returns None, dbm.error is raised
433+
# by dbm.sqlite3 and TypeError is raised by other backends.
431434
with self.assertRaises((TypeError, dbm.error)):
432435
s["foo"] = "bar"
433436

0 commit comments

Comments
 (0)