#13394: Write a WeakValueDictionary with safer key removal
-------------------------------------+-------------------------------------
Reporter: nbruin | Owner: rlm
Type: enhancement | Status: needs_review
Priority: major | Milestone: sage-5.13
Component: memleak | Resolution:
Keywords: | Merged in:
Authors: Simon King, Nils | Reviewers: Simon King
Bruin | Work issues:
Report Upstream: None of the above | Commit:
- read trac for reasoning. | 851cc9522dde332561101f1c84182a0a84b8eed4
Branch: | Stopgaps:
u/SimonKing/ticket/13394 |
Dependencies: |
-------------------------------------+-------------------------------------
Comment (by nbruin):
Aw, shoot. We do have a problem:
{{{
sage: from sage.misc.weak_dict import WeakValueDictionary
sage: D=WeakValueDictionary()
sage: V=set([2])
sage: W=set([3])
sage: D[1]=V
sage: i=D.iteritems(); d=i.next(); del d #raise the guard
sage: del V #queue a callback
sage: D[1]=W #invalidate the callback (but it's queued!)
sage: len(D)
1
sage: del i #lower guard; callback gets triggered.
Exception KeyError: KeyError('key not found',) in <generator object at
0x65b0140> ignored
sage: len(D)
1
}}}
so it seems indeed that if there are queued removals, any other
destructive mutation (changing the value of an existing key or removing a
key) should check if the corresponding value is queued and, if so, remove
it.
'''Lemma:''' We can get away with silently abandoning the callback if we
don't find the value. This is OK if we're sure the value we're looking for
(the weakref) still exists in its original form and hasn't been reused.
And we know that because we're holding an `<object>`, not just an id of an
object! So we've kept the weakref alive ourselves and prevented that id
from being reused until the callback actually happens or is thrown away.
So: yes, we should remain silent on failure.
--
Ticket URL: <http://trac.sagemath.org/ticket/13394#comment:62>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/groups/opt_out.