# 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): |
| 2209 | 2209 | |
| 2210 | 2210 | |
| 2211 | 2211 | from sage.misc.lazy_attribute import lazy_attribute |
| | 2212 | import inspect |
| | 2213 | |
| 2212 | 2214 | class DeprecatedFunctionAlias(object): |
| 2213 | 2215 | """ |
| 2214 | 2216 | A wrapper around methods or functions which automatically print the correct |
| … |
… |
class DeprecatedFunctionAlias(object): |
| 2217 | 2219 | AUTHORS: |
| 2218 | 2220 | |
| 2219 | 2221 | - 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 |
| 2220 | 2223 | """ |
| 2221 | | def __init__(self, func, version): |
| | 2224 | def __init__(self, func, version, module): |
| 2222 | 2225 | """ |
| 2223 | 2226 | TESTS:: |
| 2224 | 2227 | |
| … |
… |
class DeprecatedFunctionAlias(object): |
| 2235 | 2238 | self.func = func |
| 2236 | 2239 | self.version = version |
| 2237 | 2240 | self.instance = None # for use with methods |
| | 2241 | self.__module__ = module |
| 2238 | 2242 | if type(func) == type(deprecation): |
| 2239 | 2243 | sphinxrole = "func" |
| 2240 | 2244 | else: |
| … |
… |
class DeprecatedFunctionAlias(object): |
| 2307 | 2311 | doctest:1: DeprecationWarning: (Since Sage Version 42.132) blo is deprecated. Please use bla instead. |
| 2308 | 2312 | 42 |
| 2309 | 2313 | """ |
| | 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 | |
| 2310 | 2319 | deprecation("%s is deprecated. Please use %s instead."%(self.__name__, |
| 2311 | | self.func.__name__), |
| | 2320 | other), |
| 2312 | 2321 | self.version) |
| 2313 | 2322 | if self.instance is None: |
| 2314 | 2323 | return self.func(*args, **kwds) |
| … |
… |
def deprecated_function_alias(func, vers |
| 2353 | 2362 | sage: g = deprecated_function_alias(number_of_partitions, |
| 2354 | 2363 | ... 'Sage Version 42.132') |
| 2355 | 2364 | 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. |
| 2357 | 2366 | 7 |
| 2358 | 2367 | |
| 2359 | 2368 | This also works for methods:: |
| … |
… |
def deprecated_function_alias(func, vers |
| 2365 | 2374 | sage: cls().old_meth() |
| 2366 | 2375 | doctest:...: DeprecationWarning: (Since Sage Version 42.132) old_meth is deprecated. Please use new_meth instead. |
| 2367 | 2376 | 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. |
| 2368 | 2384 | |
| 2369 | 2385 | AUTHORS: |
| 2370 | 2386 | |
| 2371 | 2387 | - 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 |
| 2372 | 2389 | """ |
| 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) |
| 2374 | 2395 | |
| 2375 | 2396 | def deprecated_callable_import(module_name, globs, locs, fromlist, message=None): |
| 2376 | 2397 | """ |