Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
assert oldval != val
# We have to collapse nodefwd and nodeinv into a single node, i.e. drop one of them.
# Drop nodeinv, so that the item with the same key is the one overwritten in place.
nodeinv.prv.nxt = nodeinv.nxt
nodeinv.nxt.prv = nodeinv.prv
# Don't remove nodeinv's references to its neighbors since
# if the update fails, we'll need them to undo this write.
# Update fwdm and invm.
tmp = fwdm.pop(oldkey)
assert tmp is nodeinv
tmp = invm.pop(oldval)
assert tmp is nodefwd
fwdm[key] = invm[val] = nodefwd
elif isdupkey:
oldval = invm.inverse[nodefwd] # pylint: disable=no-member
oldkey = _MISS
oldnodeinv = invm.pop(oldval)
assert oldnodeinv is nodefwd
invm[val] = nodefwd
else: # isdupval
oldkey = fwdm.inverse[nodeinv] # pylint: disable=no-member
oldval = _MISS
oldnodefwd = fwdm.pop(oldkey)
assert oldnodefwd is nodeinv
fwdm[key] = nodeinv
return _WriteResult(key, val, oldkey, oldval)
def pop(self, key, default=_MISS):
"""*x.pop(k[, d]) → v*
Remove specified key and return the corresponding value.
:raises KeyError: if *key* is not found and no *default* is provided.
"""
try:
return self._pop(key)
except KeyError:
if default is _MISS:
raise
return default
def pop(self, key, default=_MISS):
"""*x.pop(k[, d]) → v*
Remove specified key and return the corresponding value.
:raises KeyError: if *key* is not found and no *default* is provided.
"""
try:
return self._pop(key)
except KeyError:
if default is _MISS:
raise
return default
# if the update fails, we'll need them to undo this write.
# Update fwdm and invm.
tmp = fwdm.pop(oldkey)
assert tmp is nodeinv
tmp = invm.pop(oldval)
assert tmp is nodefwd
fwdm[key] = invm[val] = nodefwd
elif isdupkey:
oldval = invm.inverse[nodefwd] # pylint: disable=no-member
oldkey = _MISS
oldnodeinv = invm.pop(oldval)
assert oldnodeinv is nodefwd
invm[val] = nodefwd
else: # isdupval
oldkey = fwdm.inverse[nodeinv] # pylint: disable=no-member
oldval = _MISS
oldnodefwd = fwdm.pop(oldkey)
assert oldnodefwd is nodeinv
fwdm[key] = nodeinv
return _WriteResult(key, val, oldkey, oldval)
def _write_item(self, key, val, dedup_result): # pylint: disable=too-many-locals
fwdm = self._fwdm # bidict mapping keys to nodes
invm = self._invm # bidict mapping vals to nodes
isdupkey, isdupval, nodeinv, nodefwd = dedup_result
if not isdupkey and not isdupval:
# No key or value duplication -> create and append a new node.
sntl = self._sntl
last = sntl.prv
node = _Node(last, sntl)
last.nxt = sntl.prv = fwdm[key] = invm[val] = node
oldkey = oldval = _MISS
elif isdupkey and isdupval:
# Key and value duplication across two different nodes.
assert nodefwd is not nodeinv
oldval = invm.inverse[nodefwd] # pylint: disable=no-member
oldkey = fwdm.inverse[nodeinv] # pylint: disable=no-member
assert oldkey != key
assert oldval != val
# We have to collapse nodefwd and nodeinv into a single node, i.e. drop one of them.
# Drop nodeinv, so that the item with the same key is the one overwritten in place.
nodeinv.prv.nxt = nodeinv.nxt
nodeinv.nxt.prv = nodeinv.prv
# Don't remove nodeinv's references to its neighbors since
# if the update fails, we'll need them to undo this write.
# Update fwdm and invm.
tmp = fwdm.pop(oldkey)
assert tmp is nodeinv