Ticket #11115: trac11115_cached_function_pickling.patch

File trac11115_cached_function_pickling.patch, 3.1 KB (added by SimonKing, 5 years ago)

Pickling of cached functions

  • sage/misc/cachefunc.pyx

    # HG changeset patch
    # User Simon King <simon.king@uni-jena.de>
    # Date 1315762581 -7200
    # Node ID 8a39d4c00ae744e28099cb98ef543e555d2e5682
    # Parent  00f9c408bdc6b67ce026910dc042d9bb520e4e88
    #11115: Pickling of cached functions
    
    diff --git a/sage/misc/cachefunc.pyx b/sage/misc/cachefunc.pyx
    a b  
    226226    sage: print sage_getdoc(test_pfunc)
    227227       Some documentation
    228228    sage: print sage_getdoc(O.wrapped_method)
    229     File: ...
    230     (starting at line ...) some doc for a wrapped cython method
     229    some doc for a wrapped cython method
     230    <BLANKLINE>
    231231    sage: print sage_getsource(O.wrapped_method)
    232232    cpdef test_meth(self,x):
    233233        "some doc for a wrapped cython method"
     
    262262########################################################################
    263263#       Copyright (C) 2008 William Stein <wstein@gmail.com>
    264264#                          Mike Hansen <mhansen@gmail.com>
     265#                     2011 Simon King <simon.king@uni-jena.de>
    265266#
    266267#  Distributed under the terms of the GNU General Public License (GPL)
    267268#
     
    271272import os
    272273from sage.misc.sageinspect import sage_getfile, sage_getsourcelines
    273274
     275def _cached_function_unpickle(module,name):
     276    """
     277    Unpickling of cached functions.
     278
     279    NOTE:
     280
     281    Pickling and unpickling of cached functions is by importing
     282    from the module in which the function is defined.
     283
     284    INPUT:
     285
     286    - ``module``: A string, describing the module to import the
     287      function from.
     288    - ``name``: A string, name of the to-be-imported cached function.
     289
     290    EXAMPLE::
     291
     292        sage: type(cunningham_prime_factors)
     293        <type 'sage.misc.cachefunc.CachedFunction'>
     294        sage: loads(dumps(cunningham_prime_factors)) is cunningham_prime_factors #indirect doctest
     295        True
     296
     297    """
     298    return getattr(__import__(module, fromlist=['']),name)
     299
    274300cdef class CachedFunction(object):
    275301    """
    276302    Create a cached version of a function, which only recomputes
     
    391417            self._argument_fixer = argument_fixer
    392418            self._fix_to_pos = argument_fixer.fix_to_pos
    393419
     420    def __reduce__(self):
     421        """
     422        Pickling of cached functions.
     423
     424        TEST::
     425
     426            sage: type(cunningham_prime_factors)
     427            <type 'sage.misc.cachefunc.CachedFunction'>
     428            sage: loads(dumps(cunningham_prime_factors)) is cunningham_prime_factors #indirect doctest
     429            True
     430
     431        """
     432        return _cached_function_unpickle, (self.__module__, self.__name__)
     433
    394434    #########
    395435    ## Introspection
    396436    ##
     
    411451            sage: I = P*[x,y]
    412452            sage: from sage.misc.sageinspect import sage_getdoc
    413453            sage: print sage_getdoc(I.groebner_basis) # indirect doctest
    414             File: sage/rings/polynomial/multi_polynomial_ideal.py (starting at
    415             line ...)
     454               Return the reduced Groebner basis of this ideal.
    416455            ...
    417456               ALGORITHM: Uses Singular, Magma (if available), Macaulay2 (if
    418457               available), or a toy implementation.