Ticket #11585: trac_11585_deprecated_function_alias+rc1.patch

File trac_11585_deprecated_function_alias+rc1.patch, 3.6 KB (added by defeo, 9 years ago)

Rebased against rc1

  • sage/misc/misc.py

    # HG changeset patch
    # User Luca De Feo <luca.defeo@polytechnique.edu>
    # Date 1310417679 14400
    # Node ID eead8cd947b8766ee99709bb401dc44c5ddde3cb
    # Parent  10b014bbae9d0986a86a93ffcf80e59328fbc2d4
    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 -r 10b014bbae9d -r eead8cd947b8 sage/misc/misc.py
    a b  
    21562156
    21572157
    21582158from sage.misc.lazy_attribute import lazy_attribute
     2159import inspect
     2160
    21592161class DeprecatedFunctionAlias(object):
    21602162    """
    21612163    A wrapper around methods or functions which automatically print the correct
     
    21642166    AUTHORS:
    21652167
    21662168     - Florent Hivert (2009-11-23), with the help of Mike Hansen.
     2169     - Luca De Feo (2011-07-11), printing the full module path when different from old path
    21672170    """
    2168     def __init__(self, func, version):
     2171    def __init__(self, func, version, module):
    21692172        """
    21702173        TESTS::
    21712174
     
    21822185        self.func = func
    21832186        self.version  = version
    21842187        self.instance = None # for use with methods
     2188        self.__module__ = module
    21852189        if type(func) == type(deprecation):
    21862190            sphinxrole = "func"
    21872191        else:
     
    22212225            doctest:1: DeprecationWarning: (Since Sage Version 42.132) blo is deprecated. Please use bla instead.
    22222226            42
    22232227        """
     2228        if self.instance is None and self.__module__ != self.func.__module__:
     2229            other = self.func.__module__ + "." + self.func.__name__
     2230        else:
     2231            other = self.func.__name__
     2232           
    22242233        deprecation("%s is deprecated. Please use %s instead."%(self.__name__,
    2225                                                                self.func.__name__),
     2234                                                                other),
    22262235                    self.version)
    22272236        if self.instance is None:
    22282237            return self.func(*args, **kwds)
     
    22672276        sage: g = deprecated_function_alias(number_of_partitions,
    22682277        ...     'Sage Version 42.132')
    22692278        sage: g(5)
    2270         doctest:...: DeprecationWarning: (Since Sage Version 42.132) g is deprecated. Please use number_of_partitions instead.
     2279        doctest:...: DeprecationWarning: (Since Sage Version 42.132) g is deprecated. Please use sage.combinat.partition.number_of_partitions instead.
    22712280        7
    22722281
    22732282    This also works for methods::
     
    22792288        sage: cls().old_meth()
    22802289        doctest:...: DeprecationWarning: (Since Sage Version 42.132) old_meth is deprecated. Please use new_meth instead.
    22812290        42
     2291       
     2292    Trac #11585::
     2293   
     2294        sage: def a(): pass
     2295        sage: b = deprecated_function_alias(a, 'Sage Version 42.132')
     2296        sage: b()
     2297        doctest:...: DeprecationWarning: (Since Sage Version 42.132) b is deprecated. Please use a instead.
    22822298
    22832299    AUTHORS:
    22842300
    22852301     - Florent Hivert (2009-11-23), with the help of Mike Hansen.
     2302     - Luca De Feo (2011-07-11), printing the full module path when different from old path
    22862303    """
    2287     return DeprecatedFunctionAlias(func, version)
     2304    module = inspect.getmodule(inspect.stack()[1][0])
     2305    if module is None:
     2306        module_name = '__main__'
     2307    else:
     2308        module_name = module.__name__
     2309    return DeprecatedFunctionAlias(func, version, module_name)
    22882310
    22892311def deprecated_callable_import(module_name, globs, locs, fromlist, message=None):
    22902312    """