#11768: Get source code for parent/element classes of categories
-----------------------------------------+----------------------------------
       Reporter:  SimonKing              |         Owner:  jason       
           Type:  enhancement            |        Status:  needs_review
       Priority:  major                  |     Milestone:  sage-5.0    
      Component:  misc                   |    Resolution:              
       Keywords:  sources dynamic class  |   Work issues:              
Report Upstream:  N/A                    |     Reviewers:              
        Authors:  Simon King             |     Merged in:              
   Dependencies:                         |      Stopgaps:              
-----------------------------------------+----------------------------------

Old description:

> Currently (sage-4.7.2.alpha2) one can not interactively get the source
> code of the parent or element classes of a category:
> {{{
> sage: C.parent_class??
> Type:           DynamicMetaclass
> Base Class:     <class 'sage.structure.dynamic_class.DynamicMetaclass'>
> String Form:    <class 'sage.categories.rings.Rings.parent_class'>
> Namespace:      Interactive
> File:
> /mnt/local/king/SAGE/sage-4.7.2.alpha1/local/lib/python2.6/site-
> packages/sage/categories/rings.py
> Source:
> class DynamicMetaclass(type):
>     """
>     A metaclass implementing an appropriate reduce-by-construction method
>     """
>
>     def __reduce__(self):
>         """
>         See sage.structure.dynamic_class.dynamic_class? for non trivial
> tests.
>
>         TESTS::
>
> ...
> }}}
>
> As one can see, it gets the source file right, but can not find the class
> definition in it.
>
> I tried to track it down.
>
>  * For a dynamic metaclass, we want to obtain the documentation and
> source of the base class (resp. the class that is passed too the dynamic
> metaclass by the `doccls` argument).
>  * The module is correctly provided, and thus Python's inspect module is
> able to find the source file.
>  * If it is a class then Python's inspect module tries to find something
> like `"class "+cls.__name__+":"` in the sources (they use a more
> sophisticated regular expression).
>  * The base class is `Rings().ParentMethods`. Unfortunately, it is
> defined by `class ParentMethods`, but the name is differently:
>  {{{
> sage: Rings().ParentMethods.__name__
> 'Rings.ParentMethods'
>  }}}
>
> I do not suggest to change the name: If the name is just
> `'ParentMethods'` then Python's inspect module would always find the
> first occurence of that name, which is bad if one module defines several
> categories.
>
> Instead, I suggest to create a new function in `sage.misc.sageinspect`
> that recursively finds the sources for classes whose sources can't be
> found by other methods and whose name contains a dot:
>
> The function that I plan to write does not start with the whole source
> file of `sage.categories.rings`, but it starts with the sources of
> `sage.categories.rings.Rings` and then searches `class ParentMethods`
> (with the regular expression from the inspect module) only in that small
> part of the full source file.
>
> I think "getting sources" belongs to the component "misc", even though
> the problem is  relevant for not getting confused by the category
> framework. Thus, cc to Nicolas...

New description:

 Currently (sage-4.7.2.alpha2) one can not interactively get the source
 code of the parent or element classes of a category:
 {{{
 sage: C.parent_class??
 Type:           DynamicMetaclass
 Base Class:     <class 'sage.structure.dynamic_class.DynamicMetaclass'>
 String Form:    <class 'sage.categories.rings.Rings.parent_class'>
 Namespace:      Interactive
 File:           /mnt/local/king/SAGE/sage-4.7.2.alpha1/local/lib/python2.6
 /site-packages/sage/categories/rings.py
 Source:
 class DynamicMetaclass(type):
     """
     A metaclass implementing an appropriate reduce-by-construction method
     """

     def __reduce__(self):
         """
         See sage.structure.dynamic_class.dynamic_class? for non trivial
 tests.

         TESTS::

 ...
 }}}

 As one can see, it gets the source file right, but can not find the class
 definition in it.

 I tried to track it down.

  * For a dynamic metaclass, we want to obtain the documentation and source
 of the base class (resp. the class that is passed too the dynamic
 metaclass by the `doccls` argument).
  * The module is correctly provided, and thus Python's inspect module is
 able to find the source file.
  * If it is a class then Python's inspect module tries to find something
 like `"class "+cls.__name__+":"` in the sources (they use a more
 sophisticated regular expression).
  * The base class is `Rings().ParentMethods`. Unfortunately, it is defined
 by `class ParentMethods`, but the name is differently:
  {{{
 sage: Rings().ParentMethods.__name__
 'Rings.ParentMethods'
  }}}

 I do not suggest to change the name: If the name is just `'ParentMethods'`
 then Python's inspect module would always find the first occurence of that
 name, which is bad if one module defines several categories.

 Instead, I suggest to create a new function in `sage.misc.sageinspect`
 that recursively finds the sources for classes whose sources can't be
 found by other methods and whose name contains a dot:

 The function that I plan to write does not start with the whole source
 file of `sage.categories.rings`, but it starts with the sources of
 `sage.categories.rings.Rings` and then searches `class ParentMethods`
 (with the regular expression from the inspect module) only in that small
 part of the full source file.

 I think "getting sources" belongs to the component "misc", even though the
 problem is  relevant for not getting confused by the category framework.
 Thus, cc to Nicolas...

 '''__Apply__'''

  * [attachment:trac11768_source_of_dynamic_class.patch]
  * [attachment:trac11768_docfix.patch]

--

Comment (by SimonKing):

 With the second patch, the doctest becomes independent of whether or not
 #12876 is applied.

 Apply trac11768_source_of_dynamic_class.patch trac11768_docfix.patch

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