Skip to content

gh-99631: Add custom loads and dumps support for the shelve module #99632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 17 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add custom loads & dumps test
  • Loading branch information
furkanonder committed Dec 3, 2022
commit ef7ba367259e5f06fbfa4d2d6dab58043f6cac7c
17 changes: 17 additions & 0 deletions Lib/test/test_shelve.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import shelve
import pickle
import os
from io import BytesIO

from test.support import os_helper
from collections.abc import MutableMapping
Expand Down Expand Up @@ -165,6 +166,22 @@ def test_default_protocol(self):
with shelve.Shelf({}) as s:
self.assertEqual(s._protocol, pickle.DEFAULT_PROTOCOL)

def test_custom_loads_and_dumps(self):
def custom_dumps(obj, protocol=None):
return bytes(f"{type(obj)}", 'utf-8')

def custom_loads(data):
value = BytesIO(data).read()
return value.decode("utf-8")

os.mkdir(self.dirname)
self.addCleanup(os_helper.rmtree, self.dirname)

with shelve.open(self.fn, custom_dumps=custom_dumps, custom_loads=custom_loads) as s:
num = 1
s['number'] = num
self.assertEqual(s['number'], f"{type(num)}")


class TestShelveBase:
type2test = shelve.Shelf
Expand Down