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

 The following does not work in terms of "clearing the cache":
 {{{
 def unpickle(cl,data):
     res = cl.__new__(cl)
     res.__dict__ = data
     return res

 class C(object):
     def __init__(self,x):
         self.x = x
     def __reduce__(self):
         return unpickle, (self.__class__,self.__dict__)
     @cached_method
     def f(self,i):
         return self.x+i
     @cached_method
     def g(self):
         return self.x*2

 from sage.misc.cachefunc import ClearCacheOnPickle
 # This doesn't work as it should:
 class D(ClearCacheOnPickle,C):
     pass
 }}}

 The reason is that D inherits the `__reduce__` method from C and inherits
 the `__getstate__` method from !ClearCacheOnPickle - but if both methods
 exists, `__reduce__` is chosen for pickling, and thus the caches are not
 cleared.

 In a better world, !ClearCacheOnPickle would work with both pickling
 methods, not just with the `__getstate__/__setstate__` method. I guess
 that would be possible somehow (the question is whether we want that new
 feature).

 Moreover, !ClearCacheOnPickle only works if you take an existing class
 with `__getstate__` pickling and form a new class that inherits from
 !ClearCacheOnPickle (must be in the first place!) and the other class. In
 an ideal world, !ClearCacheOnPickle would work similarly to
 !UniqueRepresentation: It would suffice to provide !ClearCacheOnPickle as
 a base class, but then one could implement pickling ''directly in the
 class'', rather than in another base class.

 That could be done using metaclasses, changing the way how instances of
 the to-be-created class are pickled. Note that this is different from
 !DynamicClassMetaclass, which changes the way how the to-be-created class
 itself (rather than their instances) are pickled.

 But then, in order to seriously use such a new metaclass, one would like
 to be able to create a class that combines the new metaclass with others
 (classcall etc). Hence, we are back at the discussion that I recently
 started at [http://groups.google.com/group/sage-combinat-
 devel/browse_thread/thread/ae09de5a608525e5/0ffd640ad6c964b7 sage-
 combinat-devel]: Ceterum censeo we should have meta-metaclasses!

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/12941#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 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.

Reply via email to