#15432: Use a callback with a weak reference to WeakValueDictionary
-------------------------------------+-------------------------------------
Reporter: nbruin | Owner:
Type: enhancement | Status: needs_review
Priority: major | Milestone: sage-6.0
Component: memleak | Resolution:
Keywords: | Merged in:
Authors: | Reviewers:
Report Upstream: N/A | Work issues:
Branch: | Commit:
u/nbruin/ticket/15432 | 4ac686619284500adaf201c397a82a9b7409e41f
Dependencies: | Stopgaps:
-------------------------------------+-------------------------------------
Comment (by nbruin):
Incidentally, I think we can do away with the `_IterationContext` by using
a `try/finally`:
{{{
cython("""
def gen():
try:
while True:
yield 1
finally:
print "finally clause executed"
""")
sage: G=gen()
sage: G.next()
1
sage: del G
finally clause executed
sage: G=gen()
sage: del G #code is never entered, so `try/finally` clause is also not
encountered
}}}
So instead of using a `with` context manager, I think we can get away with
writing our iterators as
{{{
cdef PyObject *key, *wr
cdef Py_ssize_t pos = 0
try:
self._enter_iter()
while PyDict_Next(self, &pos, &key, &wr):
#this check doesn't really say anything: by the time
#the key makes it to the customer, it may have already
turned
#invalid. It's a cheap check, though.
if PyWeakref_GetObject(wr)!=Py_None:
yield <object>key
finally:
self._exit_iter()
}}}
where `_enter_iter` and `_exit_iter` can be the `__enter__` and `__exit__`
methods on `_IterationContext`, adapted to be methods of
`WeakValueDictionary` immediately. That saves us a weakref and an extra
class (see pushed commit).
----
New commits:
||[[http://git.sagemath.org/sage.git/commit/?id=4ac6866|4ac6866]]||trac
#15432: Code context manager in iterator via try/finally. This saves an
entire class and some reference management.Z||
--
Ticket URL: <http://trac.sagemath.org/ticket/15432#comment:8>
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.