#12941: Cache should be cleared on cloning
----------------------------------------+-----------------------------------
Reporter: hivert | Owner: hivert
Type: defect | Status: new
Priority: major | Milestone: sage-5.1
Component: combinatorics | Resolution:
Keywords: Clone, cache, days38 | Work issues:
Report Upstream: N/A | Reviewers:
Authors: Florent Hivert | Merged in:
Dependencies: | Stopgaps:
----------------------------------------+-----------------------------------
Comment (by SimonKing):
I just observed another difficulty: If you copy a `CachedMethodCaller`
(and that is what you find in the dictionary of the to-be-copied object,
after calling a cached method) then the copy will still refer to the
''old'' instance. Hence, deleting the cached method caller from the
dictionary (as suggested by Florent) is indeed needed. But in addition,
one ought to clear the cache:
Define
{{{
#!python
from sage.misc.cachefunc import CachedMethodCaller,
CachedMethodCallerNoArgs, cached_method
class A(object):
def __init__(self,x):
self.x = x
def __copy__(self):
res = self.__class__.__new__(self.__class__)
d = copy(self.__dict__)
CM = []
for s,v in self.__dict__.iteritems():
if isinstance(v, CachedMethodCaller):
d.__delitem__(s)
CM.append(s)
elif isinstance(v, CachedMethodCallerNoArgs):
d.__delitem__(s)
res.__dict__ = d
for s in CM:
getattr(res,s).clear_cache()
return res
@cached_method
def f(self,i):
return self.x+i
@cached_method
def g(self):
return self.x*2
}}}
and then
{{{
sage: a = A(5)
sage: a.f(7)
12
sage: a.g()
10
sage: b = copy(a)
sage: b.x = 7
sage: b.f(7)
14
sage: b.g()
14
}}}
--
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/12941#comment:10>
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 post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sage-trac?hl=en.