Ticket #11585: trac_11585_deprecated_function_alias-rc2.patch

File trac_11585_deprecated_function_alias-rc2.patch, 3.7 KB (added by hivert, 8 years ago)
  • sage/misc/misc.py

    # HG changeset patch
    # User Luca De Feo <luca.defeo@polytechnique.edu>
    # Date 1310417679 14400
    # Node ID 5842cd8bbce01b86adce88f8559ca7b4d53c308e
    # Parent  68e89260148df131fec1708338ceba3ea964b2bb
    Make deprecated_function_alias print the module path.
    When deprecated_function_alias is used to alias a function (and only a function, not a method) belonging to a different module, the full module path is printed in the deprecation warning.
    
    diff --git a/sage/misc/misc.py b/sage/misc/misc.py
    a b def deprecation(message, version=None): 
    22092209
    22102210
    22112211from sage.misc.lazy_attribute import lazy_attribute
     2212import inspect
     2213
    22122214class DeprecatedFunctionAlias(object):
    22132215    """
    22142216    A wrapper around methods or functions which automatically print the correct
    class DeprecatedFunctionAlias(object): 
    22172219    AUTHORS:
    22182220
    22192221     - Florent Hivert (2009-11-23), with the help of Mike Hansen.
     2222     - Luca De Feo (2011-07-11), printing the full module path when different from old path
    22202223    """
    2221     def __init__(self, func, version):
     2224    def __init__(self, func, version, module):
    22222225        """
    22232226        TESTS::
    22242227
    class DeprecatedFunctionAlias(object): 
    22352238        self.func = func
    22362239        self.version  = version
    22372240        self.instance = None # for use with methods
     2241        self.__module__ = module
    22382242        if type(func) == type(deprecation):
    22392243            sphinxrole = "func"
    22402244        else:
    class DeprecatedFunctionAlias(object): 
    23072311            doctest:1: DeprecationWarning: (Since Sage Version 42.132) blo is deprecated. Please use bla instead.
    23082312            42
    23092313        """
     2314        if self.instance is None and self.__module__ != self.func.__module__:
     2315            other = self.func.__module__ + "." + self.func.__name__
     2316        else:
     2317            other = self.func.__name__
     2318           
    23102319        deprecation("%s is deprecated. Please use %s instead."%(self.__name__,
    2311                                                                self.func.__name__),
     2320                                                                other),
    23122321                    self.version)
    23132322        if self.instance is None:
    23142323            return self.func(*args, **kwds)
    def deprecated_function_alias(func, vers 
    23532362        sage: g = deprecated_function_alias(number_of_partitions,
    23542363        ...     'Sage Version 42.132')
    23552364        sage: g(5)
    2356         doctest:...: DeprecationWarning: (Since Sage Version 42.132) g is deprecated. Please use number_of_partitions instead.
     2365        doctest:...: DeprecationWarning: (Since Sage Version 42.132) g is deprecated. Please use sage.combinat.partition.number_of_partitions instead.
    23572366        7
    23582367
    23592368    This also works for methods::
    def deprecated_function_alias(func, vers 
    23652374        sage: cls().old_meth()
    23662375        doctest:...: DeprecationWarning: (Since Sage Version 42.132) old_meth is deprecated. Please use new_meth instead.
    23672376        42
     2377       
     2378    Trac #11585::
     2379   
     2380        sage: def a(): pass
     2381        sage: b = deprecated_function_alias(a, 'Sage Version 42.132')
     2382        sage: b()
     2383        doctest:...: DeprecationWarning: (Since Sage Version 42.132) b is deprecated. Please use a instead.
    23682384
    23692385    AUTHORS:
    23702386
    23712387     - Florent Hivert (2009-11-23), with the help of Mike Hansen.
     2388     - Luca De Feo (2011-07-11), printing the full module path when different from old path
    23722389    """
    2373     return DeprecatedFunctionAlias(func, version)
     2390    module_name = inspect.getmodulename(
     2391        inspect.currentframe(1).f_code.co_filename)
     2392    if module_name is None:
     2393        module_name = '__main__'
     2394    return DeprecatedFunctionAlias(func, version, module_name)
    23742395
    23752396def deprecated_callable_import(module_name, globs, locs, fromlist, message=None):
    23762397    """