#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         |    Reviewers:
Report Upstream:  None of the above  |  Work issues:
  - read trac for reasoning.         |       Commit:
         Branch:                     |  fab0ed4112b9f798e2690f4c885b57cd711ea698
  u/SimonKing/ticket/13394           |     Stopgaps:
   Dependencies:                     |
-------------------------------------+-------------------------------------

Comment (by SimonKing):

 Haha! Python's <dict> does in fact '''not''' prevent this from happening!
 I think I have just found a bug in Python!

 Here is an example that exposes the problem:
 {{{
 sage: import sage.misc.weak_dict
 sage: D = sage.misc.weak_dict.WeakValueDictionary()
 sage: class Key(object):
 ....:     def __hash__(self):
 ....:         print "hash of",id(self)
 ....:         return 5
 ....:     def __cmp__(self, other):
 ....:         print "inserting 5"
 ....:         D[5] = ZZ
 ....:         return self is other
 sage: D[Key()] = QQ
 hash of 215823980
 sage: D[Key()] = ZZ
 hash of 216179948
 inserting 5
 Traceback (most recent call last):
 ...
 RuntimeError: Can not add items while iterating over the dictionary
 }}}

 And this is what Python's dict does. There is no error, but it silently
 fails:
 {{{
 sage: D = {}
 sage: class Key(object):
 ....:     def __hash__(self):
 ....:         print "hash of",id(self)
 ....:         return 5
 ....:     def __cmp__(self, other):
 ....:         print "inserting 5"
 ....:         D[5] = ZZ
 ....:         return self is other
 sage: D[Key()] = QQ
 hash of 215853068
 sage: D[Key()] = ZZ
 hash of 215853580
 inserting 5
 sage: len(D)
 2
 sage: D.keys()
 [<__main__.Key at 0xcdda80c>, 5]
 sage: id(_[0])
 215853068
 }}}
 In other words, one of the second of the explicitly inserted items is
 missing! That's a bug, I believe.

 The same of course also happens with Python's weak value dictionaries (of
 course, since they use a dict internally). Hence, Sage's
 `CachedRepresentation` has been running into this bug all the time.

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