1
# Copyright (C) 2006, 2007, 2008, 2009 Canonical Ltd
1
# Copyright (C) 2006-2010 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
44
49
'%s was deprecated in version 1.4.0.'
46
51
return ("%%s was deprecated in version %s."
47
% bzrlib._format_version_tuple(version_tuple))
52
% breezy._format_version_tuple(version_tuple))
50
55
def set_warning_method(method):
66
71
"""Generate an automatic deprecation string for a_callable.
68
73
:param a_callable: The callable to substitute into deprecation_version.
69
:param deprecation_version: A deprecation format warning string. This should
70
have a single %s operator in it. a_callable will be turned into a nice
71
python symbol and then substituted into deprecation_version.
74
:param deprecation_version: A deprecation format warning string. This
75
should have a single %s operator in it. a_callable will be turned into
76
a nice python symbol and then substituted into deprecation_version.
73
# We also want to handle old-style classes, in particular exception, and
74
# they don't have an im_class attribute.
75
if getattr(a_callable, 'im_class', None) is None:
78
if getattr(a_callable, '__self__', None) is not None:
79
symbol = "%s.%s.%s" % (a_callable.__self__.__class__.__module__,
80
a_callable.__self__.__class__.__name__,
82
elif getattr(a_callable, '__qualname__', None) is not None and '<' not in a_callable.__qualname__:
83
symbol = "%s.%s" % (a_callable.__module__,
84
a_callable.__qualname__)
76
86
symbol = "%s.%s" % (a_callable.__module__,
77
87
a_callable.__name__)
79
symbol = "%s.%s.%s" % (a_callable.im_class.__module__,
80
a_callable.im_class.__name__,
83
88
return deprecation_version % symbol
92
97
def decorated_function(*args, **kwargs):
93
98
"""This is the decorated function."""
94
from bzrlib import trace
95
100
trace.mutter_callsite(4, "Deprecated function called")
96
101
warn(deprecation_string(callable, deprecation_version),
97
DeprecationWarning, stacklevel=2)
102
DeprecationWarning, stacklevel=2)
98
103
return callable(*args, **kwargs)
99
104
_populate_decorated(callable, deprecation_version, "function",
100
105
decorated_function)
120
125
def decorated_method(self, *args, **kwargs):
121
126
"""This is the decorated method."""
122
from bzrlib import trace
123
128
if callable.__name__ == '__init__':
124
129
symbol = "%s.%s" % (self.__class__.__module__,
125
130
self.__class__.__name__,
130
135
callable.__name__
132
137
trace.mutter_callsite(4, "Deprecated method called")
133
warn(deprecation_version % symbol, DeprecationWarning, stacklevel=2)
138
warn(deprecation_version %
139
symbol, DeprecationWarning, stacklevel=2)
134
140
return callable(self, *args, **kwargs)
135
141
_populate_decorated(callable, deprecation_version, "method",
136
142
decorated_method)
154
160
# def __init__(self, bad, other)
155
161
# def __init__(self, **kwargs)
157
return not parameter_value is DEPRECATED_PARAMETER
163
return parameter_value is not DEPRECATED_PARAMETER
160
166
def _decorate_docstring(callable, deprecation_version, label,
166
172
if len(docstring_lines) == 0:
167
173
decorated_callable.__doc__ = deprecation_version % ("This " + label)
168
174
elif len(docstring_lines) == 1:
169
decorated_callable.__doc__ = (callable.__doc__
172
+ deprecation_version % ("This " + label)
175
decorated_callable.__doc__ = (
176
callable.__doc__ + "\n" + "\n" +
177
deprecation_version % ("This " + label) + "\n")
175
179
spaces = len(docstring_lines[-1])
176
180
new_doc = callable.__doc__
296
300
:param error_only: Only match an 'error' filter
297
301
:return: True if a filter is found, False otherwise
300
303
for filter in warnings.filters:
301
304
if issubclass(DeprecationWarning, filter[2]):
302
305
# This filter will effect DeprecationWarning
311
def _remove_filter_callable(filter):
312
"""Build and returns a callable removing filter from the warnings.
314
:param filter: The filter to remove (can be None).
316
:return: A callable that will remove filter from warnings.filters.
321
warnings.filters.remove(filter)
322
except (ValueError, IndexError):
308
327
def suppress_deprecation_warnings(override=True):
309
328
"""Call this function to suppress all deprecation warnings.
315
334
:param override: If True, always set the ignore, if False, only set the
316
335
ignore if there isn't already a filter.
337
:return: A callable to remove the new warnings this added.
319
339
if not override and _check_for_filter(error_only=False):
320
340
# If there is already a filter effecting suppress_deprecation_warnings,
323
warnings.filterwarnings('ignore', category=DeprecationWarning)
344
warnings.filterwarnings('ignore', category=DeprecationWarning)
345
filter = warnings.filters[0]
346
return _remove_filter_callable(filter)
326
349
def activate_deprecation_warnings(override=True):
337
360
:param override: If False, only add a filter if there isn't an error filter
338
361
already. (This slightly differs from suppress_deprecation_warnings, in
339
362
because it always overrides everything but -Werror).
364
:return: A callable to remove the new warnings this added.
342
366
if not override and _check_for_filter(error_only=True):
343
367
# DeprecationWarnings are already turned into errors, don't downgrade
344
368
# them to 'default'.
346
warnings.filterwarnings('default', category=DeprecationWarning)
371
warnings.filterwarnings('default', category=DeprecationWarning)
372
filter = warnings.filters[0]
373
return _remove_filter_callable(filter)