MCPcopy Index your code
hub / github.com/RustPython/RustPython / __setitem__

Method __setitem__

Lib/dbm/dumb.py:189–224  ·  view source on GitHub ↗
(self, key, val)

Source from the content-addressed store, hash-verified

187 f.write("%r, %r\n" % (key.decode("Latin-1"), pos_and_siz_pair))
188
189 def __setitem__(self, key, val):
190 if self._readonly:
191 raise error('The database is opened for reading only')
192 if isinstance(key, str):
193 key = key.encode('utf-8')
194 elif not isinstance(key, (bytes, bytearray)):
195 raise TypeError("keys must be bytes or strings")
196 if isinstance(val, str):
197 val = val.encode('utf-8')
198 elif not isinstance(val, (bytes, bytearray)):
199 raise TypeError("values must be bytes or strings")
200 self._verify_open()
201 self._modified = True
202 if key not in self._index:
203 self._addkey(key, self._addval(val))
204 else:
205 # See whether the new value is small enough to fit in the
206 # (padded) space currently occupied by the old value.
207 pos, siz = self._index[key]
208 oldblocks = (siz + _BLOCKSIZE - 1) // _BLOCKSIZE
209 newblocks = (len(val) + _BLOCKSIZE - 1) // _BLOCKSIZE
210 if newblocks <= oldblocks:
211 self._index[key] = self._setval(pos, val)
212 else:
213 # The new value doesn't fit in the (padded) space used
214 # by the old value. The blocks used by the old value are
215 # forever lost.
216 self._index[key] = self._addval(val)
217
218 # Note that _index may be out of synch with the directory
219 # file now: _setval() and _addval() don't update the directory
220 # file. This also means that the on-disk directory and data
221 # files are in a mutually inconsistent state, and they'll
222 # remain that way until _commit() is called. Note that this
223 # is a disaster (for the database) if the program crashes
224 # (so that _commit() never gets called).
225
226 def __delitem__(self, key):
227 if self._readonly:

Callers

nothing calls this directly

Calls 8

_verify_openMethod · 0.95
_addkeyMethod · 0.95
_addvalMethod · 0.95
_setvalMethod · 0.95
isinstanceFunction · 0.85
lenFunction · 0.85
errorClass · 0.70
encodeMethod · 0.45

Tested by

no test coverage detected