summaryrefslogtreecommitdiffstats
path: root/Lib/shelve.py
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2007-08-11 06:57:14 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2007-08-11 06:57:14 (GMT)
commit79c320898d43eaafa28c1c67a105a17cb9b233bf (patch)
treeac3fef2daac57020eefaaaec337829a181488a2c /Lib/shelve.py
parent8dfc4a9baca7b039048b6e1dab3e4eb09f7af463 (diff)
downloadcpython-79c320898d43eaafa28c1c67a105a17cb9b233bf.zip
cpython-79c320898d43eaafa28c1c67a105a17cb9b233bf.tar.gz
cpython-79c320898d43eaafa28c1c67a105a17cb9b233bf.tar.bz2
Change shelve to require a bytes-oriented dict as
the underlying storage, and yet provide string keys.
Diffstat (limited to 'Lib/shelve.py')
-rw-r--r--Lib/shelve.py32
1 files changed, 18 insertions, 14 deletions
diff --git a/Lib/shelve.py b/Lib/shelve.py
index 5759d4e..586d253 100644
--- a/Lib/shelve.py
+++ b/Lib/shelve.py
@@ -71,25 +71,28 @@ class Shelf(UserDict.DictMixin):
See the module's __doc__ string for an overview of the interface.
"""
- def __init__(self, dict, protocol=None, writeback=False):
+ def __init__(self, dict, protocol=None, writeback=False,
+ keyencoding="utf-8"):
self.dict = dict
if protocol is None:
protocol = 0
self._protocol = protocol
self.writeback = writeback
self.cache = {}
+ self.keyencoding = "utf-8"
def keys(self):
- return self.dict.keys()
+ for k in self.dict.keys():
+ yield k.decode(self.keyencoding)
def __len__(self):
return len(self.dict)
def __contains__(self, key):
- return key in self.dict
+ return key.encode(self.keyencoding) in self.dict
def get(self, key, default=None):
- if key in self.dict:
+ if key.encode(self.keyencoding) in self.dict:
return self[key]
return default
@@ -97,7 +100,7 @@ class Shelf(UserDict.DictMixin):
try:
value = self.cache[key]
except KeyError:
- f = BytesIO(self.dict[key])
+ f = BytesIO(self.dict[key.encode(self.keyencoding)])
value = Unpickler(f).load()
if self.writeback:
self.cache[key] = value
@@ -109,10 +112,10 @@ class Shelf(UserDict.DictMixin):
f = BytesIO()
p = Pickler(f, self._protocol)
p.dump(value)
- self.dict[key] = f.getvalue()
+ self.dict[key.encode(self.keyencoding)] = f.getvalue()
def __delitem__(self, key):
- del self.dict[key]
+ del self.dict[key.encode(self.keyencoding)]
try:
del self.cache[key]
except KeyError:
@@ -156,33 +159,34 @@ class BsdDbShelf(Shelf):
See the module's __doc__ string for an overview of the interface.
"""
- def __init__(self, dict, protocol=None, writeback=False):
- Shelf.__init__(self, dict, protocol, writeback)
+ def __init__(self, dict, protocol=None, writeback=False,
+ keyencoding="utf-8"):
+ Shelf.__init__(self, dict, protocol, writeback, keyencoding)
def set_location(self, key):
(key, value) = self.dict.set_location(key)
f = BytesIO(value)
- return (key, Unpickler(f).load())
+ return (key.decode(self.keyencoding), Unpickler(f).load())
def next(self):
(key, value) = next(self.dict)
f = BytesIO(value)
- return (key, Unpickler(f).load())
+ return (key.decode(self.keyencoding), Unpickler(f).load())
def previous(self):
(key, value) = self.dict.previous()
f = BytesIO(value)
- return (key, Unpickler(f).load())
+ return (key.decode(self.keyencoding), Unpickler(f).load())
def first(self):
(key, value) = self.dict.first()
f = BytesIO(value)
- return (key, Unpickler(f).load())
+ return (key.decode(self.keyencoding), Unpickler(f).load())
def last(self):
(key, value) = self.dict.last()
f = BytesIO(value)
- return (key, Unpickler(f).load())
+ return (key.decode(self.keyencoding), Unpickler(f).load())
class DbfilenameShelf(Shelf):