@@ -71,33 +71,36 @@ class Shelf(UserDict.DictMixin):
71
71
See the module's __doc__ string for an overview of the interface.
72
72
"""
73
73
74
- def __init__ (self , dict , protocol = None , writeback = False ):
74
+ def __init__ (self , dict , protocol = None , writeback = False ,
75
+ keyencoding = "utf-8" ):
75
76
self .dict = dict
76
77
if protocol is None :
77
78
protocol = 0
78
79
self ._protocol = protocol
79
80
self .writeback = writeback
80
81
self .cache = {}
82
+ self .keyencoding = "utf-8"
81
83
82
84
def keys (self ):
83
- return self .dict .keys ()
85
+ for k in self .dict .keys ():
86
+ yield k .decode (self .keyencoding )
84
87
85
88
def __len__ (self ):
86
89
return len (self .dict )
87
90
88
91
def __contains__ (self , key ):
89
- return key in self .dict
92
+ return key . encode ( self . keyencoding ) in self .dict
90
93
91
94
def get (self , key , default = None ):
92
- if key in self .dict :
95
+ if key . encode ( self . keyencoding ) in self .dict :
93
96
return self [key ]
94
97
return default
95
98
96
99
def __getitem__ (self , key ):
97
100
try :
98
101
value = self .cache [key ]
99
102
except KeyError :
100
- f = BytesIO (self .dict [key ])
103
+ f = BytesIO (self .dict [key . encode ( self . keyencoding ) ])
101
104
value = Unpickler (f ).load ()
102
105
if self .writeback :
103
106
self .cache [key ] = value
@@ -109,10 +112,10 @@ def __setitem__(self, key, value):
109
112
f = BytesIO ()
110
113
p = Pickler (f , self ._protocol )
111
114
p .dump (value )
112
- self .dict [key ] = f .getvalue ()
115
+ self .dict [key . encode ( self . keyencoding ) ] = f .getvalue ()
113
116
114
117
def __delitem__ (self , key ):
115
- del self .dict [key ]
118
+ del self .dict [key . encode ( self . keyencoding ) ]
116
119
try :
117
120
del self .cache [key ]
118
121
except KeyError :
@@ -156,33 +159,34 @@ class BsdDbShelf(Shelf):
156
159
See the module's __doc__ string for an overview of the interface.
157
160
"""
158
161
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 )
161
165
162
166
def set_location (self , key ):
163
167
(key , value ) = self .dict .set_location (key )
164
168
f = BytesIO (value )
165
- return (key , Unpickler (f ).load ())
169
+ return (key . decode ( self . keyencoding ) , Unpickler (f ).load ())
166
170
167
171
def next (self ):
168
172
(key , value ) = next (self .dict )
169
173
f = BytesIO (value )
170
- return (key , Unpickler (f ).load ())
174
+ return (key . decode ( self . keyencoding ) , Unpickler (f ).load ())
171
175
172
176
def previous (self ):
173
177
(key , value ) = self .dict .previous ()
174
178
f = BytesIO (value )
175
- return (key , Unpickler (f ).load ())
179
+ return (key . decode ( self . keyencoding ) , Unpickler (f ).load ())
176
180
177
181
def first (self ):
178
182
(key , value ) = self .dict .first ()
179
183
f = BytesIO (value )
180
- return (key , Unpickler (f ).load ())
184
+ return (key . decode ( self . keyencoding ) , Unpickler (f ).load ())
181
185
182
186
def last (self ):
183
187
(key , value ) = self .dict .last ()
184
188
f = BytesIO (value )
185
- return (key , Unpickler (f ).load ())
189
+ return (key . decode ( self . keyencoding ) , Unpickler (f ).load ())
186
190
187
191
188
192
class DbfilenameShelf (Shelf ):
0 commit comments