Hi Maarten!

On 17 Sep., 10:39, Maarten Derickx <m.derickx.stud...@gmail.com>
wrote:
> Note that there are also weak referenced versions of dictionairy so the 
> memory leak can be solved easily.

No, it can't. For the approach to work generally, for *any* object O,
it is not possible to use O *itself* as (weak) key of a dictionary of
custom docs - simply because O may be mutable. One could use, say,
id(O) as a key, but then one has the memory leak.

I just thought of another solution.

It requires that FINALLY someone reviews #11115 (which would be a good
idea anyway, since it provides a massive speed-up for cached methods,
since it fixes the fact that some cached methods have not been cached,
and since it is the only reason why Sage has no ideals in non-
commutative rings (see #11068)).

As I have mentioned earlier, for a custom doc, one needs a method
_sage_doc_. One could make a cached method out of it, that by default
raises an attribute error but allows to set a specific value to the
cache.

Proof of concept:

{{{
sage: cython("""
....: from sage.misc.cachefunc import cached_method
....: from sage.structure.parent cimport Parent
....: cpdef _sage_doc_helper_(x):
....:     raise AttributeError
....: cdef class MyParent(Parent):
....:     'generic doc'
....:     _sage_doc_ =
cached_method(_sage_doc_helper_,name="_sage_doc_")
....:     def set_custom_doc(self, d):
....:         self._sage_doc_.set_cache(d)
....: """)
sage: H = MyParent()
sage: H?
...
Definition:     H(self, x, *args, **kwds=0)
Docstring:
    File: _mnt_local_king__sage_temp_mpc622_30308_tmp_1_spyx_0.pyx
    (starting at line 11) generic doc

Constructor Docstring:
...
sage: H.set_custom_doc('Specific doc for H')
sage: H?
...
Definition:     H(self, x, *args, **kwds=0)
Docstring:
    Specific doc for H
Class Docstring:
...
}}}


Q: Why does it involve #11115? Cached methods existed before?
A:
   1. We want that base classes such as parent are cdef'd. Cached
methods do not work in cython code (not even in a Python class that is
defined in a .pyx file) without #11115
   2. If a class does not allow attribute assignment, then without
#11115 a cached method is in fact not cached.

Q: Disadvantage of this approach (there is no free lunch)?
A: It would not work for SageObject in general. It would only work for
     (a) classes that allow attribute assignment (hence, a Python
class derived from sage.structure.element.Element, for example), and
     (b) classes that are derived from sage.structure.parent.Parent
(even if attribute assignment is impossible).


Cheers,
Simon

-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to