@@ -79,9 +79,10 @@ def _create(self, flag):
7979 try :
8080 f = _io .open (self ._datfile , 'r' , encoding = "Latin-1" )
8181 except OSError :
82- f = _io .open (self ._datfile , 'w' , encoding = "Latin-1" )
83- self ._chmod (self ._datfile )
84- f .close ()
82+ with _io .open (self ._datfile , 'w' , encoding = "Latin-1" ) as f :
83+ self ._chmod (self ._datfile )
84+ else :
85+ f .close ()
8586
8687 # Read directory file into the in-memory index dict.
8788 def _update (self ):
@@ -91,12 +92,12 @@ def _update(self):
9192 except OSError :
9293 pass
9394 else :
94- for line in f :
95- line = line . rstrip ()
96- key , pos_and_siz_pair = eval ( line )
97- key = key . encode ( 'Latin-1' )
98- self . _index [ key ] = pos_and_siz_pair
99- f . close ()
95+ with f :
96+ for line in f :
97+ line = line . rstrip ( )
98+ key , pos_and_siz_pair = eval ( line )
99+ key = key . encode ( 'Latin-1' )
100+ self . _index [ key ] = pos_and_siz_pair
100101
101102 # Write the index dict to the directory file. The original directory
102103 # file (if any) is renamed with a .bak extension first. If a .bak
@@ -118,13 +119,13 @@ def _commit(self):
118119 except OSError :
119120 pass
120121
121- f = self ._io .open (self ._dirfile , 'w' , encoding = "Latin-1" )
122- self ._chmod (self ._dirfile )
123- for key , pos_and_siz_pair in self ._index .items ():
124- # Use Latin-1 since it has no qualms with any value in any
125- # position; UTF-8, though, does care sometimes.
126- f . write ( "%r, %r\n " % (key .decode ('Latin-1' ), pos_and_siz_pair ) )
127- f . close ( )
122+ with self ._io .open (self ._dirfile , 'w' , encoding = "Latin-1" ) as f :
123+ self ._chmod (self ._dirfile )
124+ for key , pos_and_siz_pair in self ._index .items ():
125+ # Use Latin-1 since it has no qualms with any value in any
126+ # position; UTF-8, though, does care sometimes.
127+ entry = "%r, %r\n " % (key .decode ('Latin-1' ), pos_and_siz_pair )
128+ f . write ( entry )
128129
129130 sync = _commit
130131
@@ -137,47 +138,43 @@ def __getitem__(self, key):
137138 key = key .encode ('utf-8' )
138139 self ._verify_open ()
139140 pos , siz = self ._index [key ] # may raise KeyError
140- f = _io .open (self ._datfile , 'rb' )
141- f .seek (pos )
142- dat = f .read (siz )
143- f .close ()
141+ with _io .open (self ._datfile , 'rb' ) as f :
142+ f .seek (pos )
143+ dat = f .read (siz )
144144 return dat
145145
146146 # Append val to the data file, starting at a _BLOCKSIZE-aligned
147147 # offset. The data file is first padded with NUL bytes (if needed)
148148 # to get to an aligned offset. Return pair
149149 # (starting offset of val, len(val))
150150 def _addval (self , val ):
151- f = _io .open (self ._datfile , 'rb+' )
152- f .seek (0 , 2 )
153- pos = int (f .tell ())
154- npos = ((pos + _BLOCKSIZE - 1 ) // _BLOCKSIZE ) * _BLOCKSIZE
155- f .write (b'\0 ' * (npos - pos ))
156- pos = npos
157- f .write (val )
158- f .close ()
151+ with _io .open (self ._datfile , 'rb+' ) as f :
152+ f .seek (0 , 2 )
153+ pos = int (f .tell ())
154+ npos = ((pos + _BLOCKSIZE - 1 ) // _BLOCKSIZE ) * _BLOCKSIZE
155+ f .write (b'\0 ' * (npos - pos ))
156+ pos = npos
157+ f .write (val )
159158 return (pos , len (val ))
160159
161160 # Write val to the data file, starting at offset pos. The caller
162161 # is responsible for ensuring that there's enough room starting at
163162 # pos to hold val, without overwriting some other value. Return
164163 # pair (pos, len(val)).
165164 def _setval (self , pos , val ):
166- f = _io .open (self ._datfile , 'rb+' )
167- f .seek (pos )
168- f .write (val )
169- f .close ()
165+ with _io .open (self ._datfile , 'rb+' ) as f :
166+ f .seek (pos )
167+ f .write (val )
170168 return (pos , len (val ))
171169
172170 # key is a new key whose associated value starts in the data file
173171 # at offset pos and with length siz. Add an index record to
174172 # the in-memory index dict, and append one to the directory file.
175173 def _addkey (self , key , pos_and_siz_pair ):
176174 self ._index [key ] = pos_and_siz_pair
177- f = _io .open (self ._dirfile , 'a' , encoding = "Latin-1" )
178- self ._chmod (self ._dirfile )
179- f .write ("%r, %r\n " % (key .decode ("Latin-1" ), pos_and_siz_pair ))
180- f .close ()
175+ with _io .open (self ._dirfile , 'a' , encoding = "Latin-1" ) as f :
176+ self ._chmod (self ._dirfile )
177+ f .write ("%r, %r\n " % (key .decode ("Latin-1" ), pos_and_siz_pair ))
181178
182179 def __setitem__ (self , key , val ):
183180 if isinstance (key , str ):
0 commit comments