#13394: Write a WeakValueDictionary with safer key removal
-------------------------------------+-------------------------------------
Reporter: nbruin | Owner: rlm
Type: enhancement | Status: new
Priority: major | Milestone: sage-5.13
Component: memleak | Resolution:
Keywords: | Merged in:
Authors: | Reviewers:
Report Upstream: N/A | Work issues:
Branch: | Commit:
u/SimonKing/ticket/13394 | f0ed60fbdf5f2b1c7cd15f6bab98099f4ff8b822
Dependencies: | Stopgaps:
-------------------------------------+-------------------------------------
Comment (by SimonKing):
Replying to [comment:12 nbruin]:
> There are already some issues reported:
> [http://bugs.python.org/issue7105]
I did not address this one: I do not switch garbage collection off during
iteration. I could, of course. Later perhaps.
> [http://bugs.python.org/issue17816].
This is fixed in my implementation. A slight variation of the example that
issue 17816 is proposing, showing that the weakref version of
`WeakValueDict` is a mess:
{{{
sage: import weakref
sage: import sage.misc.weak_dict
sage: class A(object):
....: def __init__(self,n):
....: self.n=n
....: def __repr__(self):
....: return "A(%d)"%self.n
....:
sage: def mess(D, n):
....: L=[A(i) for i in range(n)]
....: for i in range(n-1):
....: j=(i+10)%n
....: D[L[i]]=L[j]
....: return D
....:
sage: D = weakref.WeakValueDictionary()
sage: D = mess(D,10000)
sage: len(D)
sage: D.clear()
Exception KeyError: (A(6760),) in <function remove at 0xbe5110c> ignored
Exception KeyError: (A(6761),) in <function remove at 0xbe5110c> ignored
...
Exception KeyError: (A(968),) in <function remove at 0xbe5110c> ignored
Exception KeyError: (A(958),) in <function remove at 0xbe5110c> ignored
sage: len(D) # in spite of the errors, the items *are* removed
0
}}}
Instead, `sage.misc.weak_dict.WeakValueDictionary` works just fine:
{{{
sage: D = sage.misc.weak_dict.WeakValueDictionary()
sage: D = mess(D,10000)
sage: len(D)
9000
sage: D.clear()
sage: len(D)
0
}}}
--
Ticket URL: <http://trac.sagemath.org/ticket/13394#comment:13>
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.