Ticket #11585: trac_11585_deprecated_function_alias.patch

File trac_11585_deprecated_function_alias.patch, 3.5 KB (added by defeo, 9 years ago)
  • sage/misc/misc.py

    # HG changeset patch
    # User Luca De Feo <luca.defeo@polytechnique.edu>
    # Date 1310417679 14400
    # Node ID f79c24ff91a0eb6d05a7fff76a5af23496c56322
    # Parent  8532a2ad1e558cbc91ddaaa6b7cc79956dd1e8ba
    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 8532a2ad1e55 -r f79c24ff91a0 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
     
    21832186        self.func = func
    21842187        self.version  = version
    21852188        self.instance = None # for use with methods
     2189        self.__module__ = module
    21862190
    21872191    @lazy_attribute
    21882192    def __name__(self):
     
    22162220            doctest:1: DeprecationWarning: (Since Sage Version 42.132) blo is deprecated. Please use bla instead.
    22172221            42
    22182222        """
     2223        if self.instance is None and self.__module__ != self.func.__module__:
     2224            other = self.func.__module__ + "." + self.func.__name__
     2225        else:
     2226            other = self.func.__name__
     2227           
    22192228        deprecation("%s is deprecated. Please use %s instead."%(self.__name__,
    2220                                                                self.func.__name__),
     2229                                                                other),
    22212230                    self.version)
    22222231        if self.instance is None:
    22232232            return self.func(*args, **kwds)
     
    22622271        sage: g = deprecated_function_alias(number_of_partitions,
    22632272        ...     'Sage Version 42.132')
    22642273        sage: g(5)
    2265         doctest:...: DeprecationWarning: (Since Sage Version 42.132) g is deprecated. Please use number_of_partitions instead.
     2274        doctest:...: DeprecationWarning: (Since Sage Version 42.132) g is deprecated. Please use sage.combinat.partition.number_of_partitions instead.
    22662275        7
    22672276
    22682277    This also works for methods::
     
    22742283        sage: cls().old_meth()
    22752284        doctest:...: DeprecationWarning: (Since Sage Version 42.132) old_meth is deprecated. Please use new_meth instead.
    22762285        42
     2286       
     2287    Trac #11585::
     2288   
     2289        sage: def a(): pass
     2290        sage: b = deprecated_function_alias(a, 'Sage Version 42.132')
     2291        sage: b()
     2292        doctest:...: DeprecationWarning: (Since Sage Version 42.132) b is deprecated. Please use a instead.
    22772293
    22782294    AUTHORS:
    22792295
    22802296     - Florent Hivert (2009-11-23), with the help of Mike Hansen.
     2297     - Luca De Feo (2011-07-11), printing the full module path when different from old path
    22812298    """
    2282     return DeprecatedFunctionAlias(func, version)
     2299    module = inspect.getmodule(inspect.stack()[1][0])
     2300    if module is None:
     2301        module_name = '__main__'
     2302    else:
     2303        module_name = module.__name__
     2304    return DeprecatedFunctionAlias(func, version, module_name)
    22832305
    22842306def deprecated_callable_import(module_name, globs, locs, fromlist, message=None):
    22852307    """