diff options
author | Skip Montanaro <skip@pobox.com> | 2002-12-08 18:36:24 (GMT) |
---|---|---|
committer | Skip Montanaro <skip@pobox.com> | 2002-12-08 18:36:24 (GMT) |
commit | 3bf99e3e876cb367cff34c5b9d659361b5ca9525 (patch) | |
tree | 459649bf36f5bd3ea77d0d382ee9d0571f04cb33 /Lib | |
parent | ea7f75d423342ebab09d1e12e02af6c2bab128ec (diff) | |
download | cpython-3bf99e3e876cb367cff34c5b9d659361b5ca9525.zip cpython-3bf99e3e876cb367cff34c5b9d659361b5ca9525.tar.gz cpython-3bf99e3e876cb367cff34c5b9d659361b5ca9525.tar.bz2 |
Add support for binary pickles to the shelve module. In some situations
this can result in significantly smaller files. All classes as well as the
open function now accept an optional binary parameter, which defaults to
False for backward compatibility. Added a small test suite, updated the
libref documentation (including documenting the exported classes and fixing
a few other nits) and added a note about the change to Misc/NEWS.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/shelve.py | 17 | ||||
-rw-r--r-- | Lib/test/test_shelve.py | 51 |
2 files changed, 60 insertions, 8 deletions
diff --git a/Lib/shelve.py b/Lib/shelve.py index 7a318a6..e262d79 100644 --- a/Lib/shelve.py +++ b/Lib/shelve.py @@ -51,8 +51,9 @@ class Shelf(UserDict.DictMixin): See the module's __doc__ string for an overview of the interface. """ - def __init__(self, dict): + def __init__(self, dict, binary=False): self.dict = dict + self.binary = binary def keys(self): return self.dict.keys() @@ -77,7 +78,7 @@ class Shelf(UserDict.DictMixin): def __setitem__(self, key, value): f = StringIO() - p = Pickler(f) + p = Pickler(f, self.binary) p.dump(value) self.dict[key] = f.getvalue() @@ -112,8 +113,8 @@ class BsdDbShelf(Shelf): See the module's __doc__ string for an overview of the interface. """ - def __init__(self, dict): - Shelf.__init__(self, dict) + def __init__(self, dict, binary=False): + Shelf.__init__(self, dict, binary) def set_location(self, key): (key, value) = self.dict.set_location(key) @@ -148,16 +149,16 @@ class DbfilenameShelf(Shelf): See the module's __doc__ string for an overview of the interface. """ - def __init__(self, filename, flag='c'): + def __init__(self, filename, flag='c', binary=False): import anydbm - Shelf.__init__(self, anydbm.open(filename, flag)) + Shelf.__init__(self, anydbm.open(filename, flag), binary) -def open(filename, flag='c'): +def open(filename, flag='c', binary=False): """Open a persistent dictionary for reading and writing. Argument is the filename for the dbm database. See the module's __doc__ string for an overview of the interface. """ - return DbfilenameShelf(filename, flag) + return DbfilenameShelf(filename, flag, binary) diff --git a/Lib/test/test_shelve.py b/Lib/test/test_shelve.py new file mode 100644 index 0000000..29af82e --- /dev/null +++ b/Lib/test/test_shelve.py @@ -0,0 +1,51 @@ +import os +import unittest +import shelve +import glob +from test import test_support + +class TestCase(unittest.TestCase): + + fn = "shelftemp.db" + + def test_ascii_file_shelf(self): + try: + s = shelve.open(self.fn, binary=False) + s['key1'] = (1,2,3,4) + self.assertEqual(s['key1'], (1,2,3,4)) + s.close() + finally: + for f in glob.glob(self.fn+"*"): + os.unlink(f) + + def test_binary_file_shelf(self): + try: + s = shelve.open(self.fn, binary=True) + s['key1'] = (1,2,3,4) + self.assertEqual(s['key1'], (1,2,3,4)) + s.close() + finally: + for f in glob.glob(self.fn+"*"): + os.unlink(f) + + def test_in_memory_shelf(self): + d1 = {} + s = shelve.Shelf(d1, binary=False) + s['key1'] = (1,2,3,4) + self.assertEqual(s['key1'], (1,2,3,4)) + s.close() + d2 = {} + s = shelve.Shelf(d2, binary=True) + s['key1'] = (1,2,3,4) + self.assertEqual(s['key1'], (1,2,3,4)) + s.close() + + self.assertEqual(len(d1), 1) + self.assertNotEqual(d1, d2) + +def test_main(): + test_support.run_unittest(TestCase) + + +if __name__ == "__main__": + test_main() |