Ticket #12808: trac_12808_nested_class_cython.patch

File trac_12808_nested_class_cython.patch, 3.5 KB (added by SimonKing, 10 years ago)

Cythonise nested classes

  • module_list.py

    # HG changeset patch
    # User Simon King <simon.king@uni-jena.de>
    # Date 1335469346 -7200
    # Node ID 468d4ec08552ebb531ed7ea9a5159b2db226a0d7
    # Parent  af439b34dce2e25c7d3396502ca9502cf2bb16b1
    #12808: Make NestedClassMetaclass an extension type of type, and speed-up its initialisation.
    
    diff --git a/module_list.py b/module_list.py
    a b  
    10731073    Extension('sage.misc.misc_c',
    10741074              sources = ['sage/misc/misc_c.pyx']),
    10751075
     1076    Extension('sage.misc.nested_class',
     1077              sources = ['sage/misc/nested_class.pyx']),
     1078
    10761079    Extension('sage.misc.parser',
    10771080              sources = ['sage/misc/parser.pyx']),
    10781081
  • new file sage/misc/nested_class.pxd

    diff --git a/sage/misc/nested_class.pxd b/sage/misc/nested_class.pxd
    new file mode 100644
    - +  
     1cdef extern from "object.h":
     2    ctypedef class __builtin__.type [object PyHeapTypeObject]:
     3        pass
     4   
     5cdef class NestedClassMetaclass(type):
     6    pass
  • (a) a/sage/misc/nested_class.py vs. (b) b/sage/misc/nested_class.pyx

    diff --git a/sage/misc/nested_class.py b/sage/misc/nested_class.pyx
    rename from sage/misc/nested_class.py
    rename to sage/misc/nested_class.pyx
    a b  
    6868"""
    6969
    7070import sys
     71cdef dict sys_modules = sys.modules
     72
     73import types
     74from types import ClassType
    7175
    7276__all__ = ['modify_for_nested_pickle', 'nested_pickle',
    7377           'NestedClassMetaclass', 'MainClass'
     
    7579           #, 'SubClass', 'CopiedClass', 'A1'
    7680           ]
    7781
    78 def modify_for_nested_pickle(cls, name_prefix, module):
     82cpdef modify_for_nested_pickle(cls, str name_prefix, module):
    7983    r"""
    8084    Modify the subclasses of the given class to be picklable, by
    8185    giving them a mangled name and putting the mangled name in the
     
    108112        <class '__main__.A.B'>
    109113
    110114    """
    111     import types
     115    cdef str name, dotted_name
     116    cdef str mod_name = module.__name__
    112117    for (name, v) in cls.__dict__.iteritems():
    113         if isinstance(v, (type, types.ClassType)):
    114             if v.__name__ == name and v.__module__ == module.__name__ and getattr(module, name, None) is not v:
     118        if isinstance(v, (type, ClassType)):
     119            if v.__name__ == name and v.__module__ == mod_name and getattr(module, name, None) is not v:
    115120                # OK, probably this is a nested class.
    116121                dotted_name = name_prefix + '.' + name
    117122                v.__name__ = dotted_name
     
    159164        sage: loads(dumps(MainClass.NestedClass())) # indirect doctest
    160165        <sage.misc.nested_class.MainClass.NestedClass object at 0x...>
    161166    """
    162     modify_for_nested_pickle(cls, cls.__name__, sys.modules[cls.__module__])
     167    modify_for_nested_pickle(cls, cls.__name__, sys_modules[cls.__module__])
    163168    return cls
    164169
    165 
    166 class NestedClassMetaclass(type):
     170cdef class NestedClassMetaclass(type):
    167171    r"""
    168172    A metaclass for nested pickling.
    169173
     
    198202        sage: getattr(sys.modules['__main__'], 'A.B', 'Not found')
    199203        <class '__main__.A.B'>
    200204        """
    201         nested_pickle(self)
    202 
     205        modify_for_nested_pickle(self, self.__name__, sys_modules[self.__module__])
    203206
    204207class MainClass(object):
    205208    r"""
     
    253256
    254257nested_pickle(SubClass)
    255258
     259def _provide_SubClass():
     260    return SubClass
     261
    256262class CopiedClass(object):
    257263    r"""
    258264    A simple class to test nested_pickle.
     
    267273    """
    268274    NestedClass = MainClass.NestedClass
    269275    NestedSubClass = MainClass.NestedClass.NestedSubClass
    270     SubClass = SubClass
     276    SubClass = _provide_SubClass()
    271277
    272278nested_pickle(CopiedClass)
    273279