Move shelve over to BytesIO as pickle.(Pickler | Unpickler) expect binary

files, not text files.

test_shelve still fails thanks to bsddb not having been fixed.
This commit is contained in:
Brett Cannon 2007-07-26 03:07:02 +00:00
parent 2f2fffb766
commit d24fffe7c6
2 changed files with 16 additions and 16 deletions

View File

@ -57,7 +57,7 @@ the persistent dictionary on disk, if feasible).
""" """
from pickle import Pickler, Unpickler from pickle import Pickler, Unpickler
from io import StringIO from io import BytesIO
import UserDict import UserDict
import warnings import warnings
@ -97,7 +97,7 @@ class Shelf(UserDict.DictMixin):
try: try:
value = self.cache[key] value = self.cache[key]
except KeyError: except KeyError:
f = StringIO(self.dict[key]) f = BytesIO(self.dict[key])
value = Unpickler(f).load() value = Unpickler(f).load()
if self.writeback: if self.writeback:
self.cache[key] = value self.cache[key] = value
@ -106,7 +106,7 @@ class Shelf(UserDict.DictMixin):
def __setitem__(self, key, value): def __setitem__(self, key, value):
if self.writeback: if self.writeback:
self.cache[key] = value self.cache[key] = value
f = StringIO() f = BytesIO()
p = Pickler(f, self._protocol) p = Pickler(f, self._protocol)
p.dump(value) p.dump(value)
self.dict[key] = f.getvalue() self.dict[key] = f.getvalue()
@ -161,27 +161,27 @@ class BsdDbShelf(Shelf):
def set_location(self, key): def set_location(self, key):
(key, value) = self.dict.set_location(key) (key, value) = self.dict.set_location(key)
f = StringIO(value) f = BytesIO(value)
return (key, Unpickler(f).load()) return (key, Unpickler(f).load())
def next(self): def next(self):
(key, value) = next(self.dict) (key, value) = next(self.dict)
f = StringIO(value) f = BytesIO(value)
return (key, Unpickler(f).load()) return (key, Unpickler(f).load())
def previous(self): def previous(self):
(key, value) = self.dict.previous() (key, value) = self.dict.previous()
f = StringIO(value) f = BytesIO(value)
return (key, Unpickler(f).load()) return (key, Unpickler(f).load())
def first(self): def first(self):
(key, value) = self.dict.first() (key, value) = self.dict.first()
f = StringIO(value) f = BytesIO(value)
return (key, Unpickler(f).load()) return (key, Unpickler(f).load())
def last(self): def last(self):
(key, value) = self.dict.last() (key, value) = self.dict.last()
f = StringIO(value) f = BytesIO(value)
return (key, Unpickler(f).load()) return (key, Unpickler(f).load())

View File

@ -21,13 +21,13 @@ def _delete_files():
pass pass
class AnyDBMTestCase(unittest.TestCase): class AnyDBMTestCase(unittest.TestCase):
_dict = {'0': b'', _dict = {str8('0'): b'',
'a': b'Python:', str8('a'): b'Python:',
'b': b'Programming', str8('b'): b'Programming',
'c': b'the', str8('c'): b'the',
'd': b'way', str8('d'): b'way',
'f': b'Guido', str8('f'): b'Guido',
'g': b'intended', str8('g'): b'intended',
} }
def __init__(self, *args): def __init__(self, *args):
@ -44,7 +44,7 @@ class AnyDBMTestCase(unittest.TestCase):
def test_anydbm_modification(self): def test_anydbm_modification(self):
self.init_db() self.init_db()
f = anydbm.open(_fname, 'c') f = anydbm.open(_fname, 'c')
self._dict['g'] = f['g'] = b"indented" self._dict[str8('g')] = f[str8('g')] = b"indented"
self.read_helper(f) self.read_helper(f)
f.close() f.close()