#14793: Unique representation for homsets
-------------------------------+--------------------------------------------
       Reporter:  nthiery      |         Owner:  nthiery  
           Type:  enhancement  |        Status:  new      
       Priority:  major        |     Milestone:  sage-5.12
      Component:  categories   |    Resolution:           
       Keywords:               |   Work issues:           
Report Upstream:  N/A          |     Reviewers:           
        Authors:  Simon King   |     Merged in:           
   Dependencies:               |      Stopgaps:           
-------------------------------+--------------------------------------------

Comment (by SimonKing):

 This works:
 {{{
 sage: M = ModularSymbols(6)
 sage: f = M.hecke_operator(7).matrix_form()
 sage: loads(dumps(M)) == M
 True
 }}}
 Continuing the example, this makes it fail:
 {{{
 sage: phi = f.hecke_module_morphism()
 sage: loads(dumps(M)) == M
 Traceback (most recent call last):
 ...
 <type 'str'>: (<type 'exceptions.AttributeError'>,
 AttributeError('ModularSymbolsAmbient_wt2_g0_with_category' object has no
 attribute '_HeckeModule_generic__level',))
 }}}
 Continuing the example, this makes it work again:
 {{{
 sage: del M._HeckeModule_generic__hecke_algebra
 sage: loads(dumps(M)) == M
 True
 }}}

 Hence, I think the following happens.

 - `s = dumps(M)` pickles the content of `M.__dict__`. `loads(s)` then
 starts with unpickling the stored content of `M.__dict__`, after creating
 a would-be-copy N of M, which is not initialised at this point (this is
 what Python does in those cases). The unpickled content of M.__dict__
 would later be assigned to N.__dict__
 - The Hecke algebra of M is stored as an attribute of M. `Hom(M,M)`
 somehow becomes stored as an attribute of `M.hecke_algebra()` when calling
 `.hecke_module_morphism()`. Hence, when deleting the attribute containing
 `M.hecke_algebra()`, all is good.
 - When unpickling the content of `M.__dict__`, Python eventually tries to
 unpickle `Hom(M,M)`, but replaces M with its would-be-copy N. But since N
 is not initialised, `Hom(N,N)` fails.

 This suggests a way out: We could implement "proper" unpickling of
 `M.hecke_algebra()`, so that `Hom(M,M)` will not be called prematurely.

 It might also be a good idea to use @cached_method when a method is
 cached. Namely, I often see stuff like
 {{{
 #!python
     def free_module(self):
         """
         Return the free module underlying this ambient Hecke module (the
         forgetful functor from Hecke modules to modules over the base
 ring)

         EXAMPLE::

             sage: ModularForms(59, 2).free_module()
             Vector space of dimension 6 over Rational Field
         """
         try:
             return self.__free_module
         except AttributeError:
             M = sage.modules.all.FreeModule(self.base_ring(), self.rank())
             self.__free_module = M
             return M
 }}}
 which uses `__dict__` and hence will result in errors during unpickling.

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