@@ -71,33 +71,36 @@ class Shelf(UserDict.DictMixin):
7171 See the module's __doc__ string for an overview of the interface.
7272 """
7373
74- def __init__ (self , dict , protocol = None , writeback = False ):
74+ def __init__ (self , dict , protocol = None , writeback = False ,
75+ keyencoding = "utf-8" ):
7576 self .dict = dict
7677 if protocol is None :
7778 protocol = 0
7879 self ._protocol = protocol
7980 self .writeback = writeback
8081 self .cache = {}
82+ self .keyencoding = "utf-8"
8183
8284 def keys (self ):
83- return self .dict .keys ()
85+ for k in self .dict .keys ():
86+ yield k .decode (self .keyencoding )
8487
8588 def __len__ (self ):
8689 return len (self .dict )
8790
8891 def __contains__ (self , key ):
89- return key in self .dict
92+ return key . encode ( self . keyencoding ) in self .dict
9093
9194 def get (self , key , default = None ):
92- if key in self .dict :
95+ if key . encode ( self . keyencoding ) in self .dict :
9396 return self [key ]
9497 return default
9598
9699 def __getitem__ (self , key ):
97100 try :
98101 value = self .cache [key ]
99102 except KeyError :
100- f = BytesIO (self .dict [key ])
103+ f = BytesIO (self .dict [key . encode ( self . keyencoding ) ])
101104 value = Unpickler (f ).load ()
102105 if self .writeback :
103106 self .cache [key ] = value
@@ -109,10 +112,10 @@ def __setitem__(self, key, value):
109112 f = BytesIO ()
110113 p = Pickler (f , self ._protocol )
111114 p .dump (value )
112- self .dict [key ] = f .getvalue ()
115+ self .dict [key . encode ( self . keyencoding ) ] = f .getvalue ()
113116
114117 def __delitem__ (self , key ):
115- del self .dict [key ]
118+ del self .dict [key . encode ( self . keyencoding ) ]
116119 try :
117120 del self .cache [key ]
118121 except KeyError :
@@ -156,33 +159,34 @@ class BsdDbShelf(Shelf):
156159 See the module's __doc__ string for an overview of the interface.
157160 """
158161
159- def __init__ (self , dict , protocol = None , writeback = False ):
160- Shelf .__init__ (self , dict , protocol , writeback )
162+ def __init__ (self , dict , protocol = None , writeback = False ,
163+ keyencoding = "utf-8" ):
164+ Shelf .__init__ (self , dict , protocol , writeback , keyencoding )
161165
162166 def set_location (self , key ):
163167 (key , value ) = self .dict .set_location (key )
164168 f = BytesIO (value )
165- return (key , Unpickler (f ).load ())
169+ return (key . decode ( self . keyencoding ) , Unpickler (f ).load ())
166170
167171 def next (self ):
168172 (key , value ) = next (self .dict )
169173 f = BytesIO (value )
170- return (key , Unpickler (f ).load ())
174+ return (key . decode ( self . keyencoding ) , Unpickler (f ).load ())
171175
172176 def previous (self ):
173177 (key , value ) = self .dict .previous ()
174178 f = BytesIO (value )
175- return (key , Unpickler (f ).load ())
179+ return (key . decode ( self . keyencoding ) , Unpickler (f ).load ())
176180
177181 def first (self ):
178182 (key , value ) = self .dict .first ()
179183 f = BytesIO (value )
180- return (key , Unpickler (f ).load ())
184+ return (key . decode ( self . keyencoding ) , Unpickler (f ).load ())
181185
182186 def last (self ):
183187 (key , value ) = self .dict .last ()
184188 f = BytesIO (value )
185- return (key , Unpickler (f ).load ())
189+ return (key . decode ( self . keyencoding ) , Unpickler (f ).load ())
186190
187191
188192class DbfilenameShelf (Shelf ):
0 commit comments