#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 SimonKing):

 Arrgh, I am so stupid!! Of course, if `ki==ko` then `vi` lurks around and
 prevents the item in `D2` from "death-by-garbage-collection"!

 This works:
 {{{
 sage: from sage.misc.weak_dict import WeakValueDictionary
 sage: class C(object):
 ....:     def __init__(self, n):
 ....:         self.n = n
 ....:     def __hash__(self):
 ....:         return hash(self.n)
 ....:     def __cmp__(self, other):
 ....:         return cmp(type(self),type(other)) or cmp(self.n, other.n)
 sage: B=10
 sage: L = [None]*B
 sage: D1 = WeakValueDictionary()
 sage: D2 = WeakValueDictionary()
 sage: import gc
 sage: for i in range(10000):
 ....:     ki = floor(random()*B)
 ....:     vi = C(floor(random()*B))
 ....:     D1[ki] = vi
 ....:     D2[ki] = vi
 ....:     L[ki]  = vi
 ....:     del vi
 ....:     ko = floor(random()*B)
 ....:     if ko in D1:
 ....:         del D1[ko]
 ....:         L[ko] = None
 ....:     assert D1 == D2
 }}}

 Do you agree that this test is an indirect version of your test, as
 `del_dictitem_by_exact_value` is triggered by the line `L[ko] = None`?

--
Ticket URL: <http://trac.sagemath.org/ticket/13394#comment:75>
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.

Reply via email to