@@ -66,9 +66,9 @@ def __init__(self, filebasename, mode):
6666
6767 # Mod by Jack: create data file if needed
6868 try :
69- f = _io .open (self ._datfile , 'r' )
69+ f = _io .open (self ._datfile , 'r' , encoding = "Latin-1" )
7070 except IOError :
71- f = _io .open (self ._datfile , 'w' )
71+ f = _io .open (self ._datfile , 'w' , encoding = "Latin-1" )
7272 self ._chmod (self ._datfile )
7373 f .close ()
7474 self ._update ()
@@ -77,7 +77,7 @@ def __init__(self, filebasename, mode):
7777 def _update (self ):
7878 self ._index = {}
7979 try :
80- f = _io .open (self ._dirfile , 'r' )
80+ f = _io .open (self ._dirfile , 'r' , encoding = "Latin-1" )
8181 except IOError :
8282 pass
8383 else :
@@ -108,7 +108,7 @@ def _commit(self):
108108 except self ._os .error :
109109 pass
110110
111- f = self ._io .open (self ._dirfile , 'w' )
111+ f = self ._io .open (self ._dirfile , 'w' , encoding = "Latin-1" )
112112 self ._chmod (self ._dirfile )
113113 for key , pos_and_siz_pair in self ._index .items ():
114114 # Use Latin-1 since it has no qualms with any value in any
@@ -159,18 +159,20 @@ def _setval(self, pos, val):
159159 # the in-memory index dict, and append one to the directory file.
160160 def _addkey (self , key , pos_and_siz_pair ):
161161 self ._index [key ] = pos_and_siz_pair
162- f = _io .open (self ._dirfile , 'a' )
162+ f = _io .open (self ._dirfile , 'a' , encoding = "Latin-1" )
163163 self ._chmod (self ._dirfile )
164- f .write ("%r, %r\n " % (key , pos_and_siz_pair ))
164+ f .write ("%r, %r\n " % (key . decode ( "Latin-1" ) , pos_and_siz_pair ))
165165 f .close ()
166166
167167 def __setitem__ (self , key , val ):
168168 if isinstance (key , str ):
169169 key = key .encode ('utf-8' )
170170 elif not isinstance (key , (bytes , bytearray )):
171171 raise TypeError ("keys must be bytes or strings" )
172- if not isinstance (val , (bytes , bytearray )):
173- raise TypeError ("values must be bytes" )
172+ if isinstance (val , str ):
173+ val = val .encode ('utf-8' )
174+ elif not isinstance (val , (bytes , bytearray )):
175+ raise TypeError ("values must be bytes or strings" )
174176 if key not in self ._index :
175177 self ._addkey (key , self ._addval (val ))
176178 else :
0 commit comments