Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(4)

Side by Side Diff: Lib/dbm/dumb.py

Issue 19385: dbm.dumb should be consistent when the database is closed
Patch Set: Created 6 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """A dumb and slow but simple dbm clone. 1 """A dumb and slow but simple dbm clone.
2 2
3 For database spam, spam.dir contains the index (a text file), 3 For database spam, spam.dir contains the index (a text file),
4 spam.bak *may* contain a backup of the index (also a text file), 4 spam.bak *may* contain a backup of the index (also a text file),
5 while spam.dat contains the data (a binary file). 5 while spam.dat contains the data (a binary file).
6 6
7 XXX TO DO: 7 XXX TO DO:
8 8
9 - seems to contain a bug when updating... 9 - seems to contain a bug when updating...
10 10
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 self._verify_open() 209 self._verify_open()
210 # The blocks used by the associated value are lost. 210 # The blocks used by the associated value are lost.
211 del self._index[key] 211 del self._index[key]
212 # XXX It's unclear why we do a _commit() here (the code always 212 # XXX It's unclear why we do a _commit() here (the code always
213 # XXX has, so I'm not changing it). __setitem__ doesn't try to 213 # XXX has, so I'm not changing it). __setitem__ doesn't try to
214 # XXX keep the directory file in synch. Why should we? Or 214 # XXX keep the directory file in synch. Why should we? Or
215 # XXX why shouldn't __setitem__? 215 # XXX why shouldn't __setitem__?
216 self._commit() 216 self._commit()
217 217
218 def keys(self): 218 def keys(self):
219 self._verify_open() 219 try:
220 return list(self._index.keys()) 220 return list(self._index)
221 except TypeError:
222 raise error('DBM object has already been closed') from None
221 223
222 def items(self): 224 def items(self):
223 self._verify_open() 225 self._verify_open()
224 return [(key, self[key]) for key in self._index.keys()] 226 return [(key, self[key]) for key in self._index.keys()]
225 227
226 def __contains__(self, key): 228 def __contains__(self, key):
227 if isinstance(key, str): 229 if isinstance(key, str):
228 key = key.encode('utf-8') 230 key = key.encode('utf-8')
229 self._verify_open() 231 try:
230 return key in self._index 232 return key in self._index
233 except TypeError:
234 if self._index is None:
235 raise error('DBM object has already been closed') from None
236 else:
237 raise
231 238
232 def iterkeys(self): 239 def iterkeys(self):
233 self._verify_open() 240 try:
234 return iter(self._index.keys()) 241 return iter(self._index)
242 except TypeError:
243 raise error('DBM object has already been closed') from None
235 __iter__ = iterkeys 244 __iter__ = iterkeys
236 245
237 def __len__(self): 246 def __len__(self):
238 self._verify_open() 247 try:
239 return len(self._index) 248 return len(self._index)
249 except TypeError:
250 raise error('DBM object has already been closed') from None
240 251
241 def close(self): 252 def close(self):
242 self._commit() 253 self._commit()
243 self._index = self._datfile = self._dirfile = self._bakfile = None 254 self._index = self._datfile = self._dirfile = self._bakfile = None
244 255
245 __del__ = close 256 __del__ = close
246 257
247 def _chmod(self, file): 258 def _chmod(self, file):
248 if hasattr(self._os, 'chmod'): 259 if hasattr(self._os, 'chmod'):
249 self._os.chmod(file, self._mode) 260 self._os.chmod(file, self._mode)
(...skipping 24 matching lines...) Expand all
274 try: 285 try:
275 um = _os.umask(0) 286 um = _os.umask(0)
276 _os.umask(um) 287 _os.umask(um)
277 except AttributeError: 288 except AttributeError:
278 pass 289 pass
279 else: 290 else:
280 # Turn off any bits that are set in the umask 291 # Turn off any bits that are set in the umask
281 mode = mode & (~um) 292 mode = mode & (~um)
282 293
283 return _Database(file, mode) 294 return _Database(file, mode)
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+