1
# Copyright (C) 2006, 2007 Canonical Ltd
1
# Copyright (C) 2006, 2007, 2008 Canonical Ltd
2
2
# Authors: Robert Collins <robert.collins@canonical.com> and others
4
4
# This program is free software; you can redistribute it and/or modify
61
74
zero_ninety = "%s was deprecated in version 0.90."
62
75
zero_ninetyone = "%s was deprecated in version 0.91."
63
76
zero_ninetytwo = "%s was deprecated in version 0.92."
77
one_zero = "%s was deprecated in version 1.0."
78
zero_ninetythree = one_zero # Maintained for backwards compatibility
79
one_one = "%s was deprecated in version 1.1."
80
one_two = "%s was deprecated in version 1.2."
81
one_three = "%s was deprecated in version 1.3."
82
one_four = "%s was deprecated in version 1.4."
83
one_five = "%s was deprecated in version 1.5."
84
one_six = "%s was deprecated in version 1.6."
87
def deprecated_in(version_tuple):
88
"""Generate a message that something was deprecated in a release.
90
>>> deprecated_in((1, 4, 0))
91
'%s was deprecated in version 1.4.'
93
return ("%%s was deprecated in version %s."
94
% bzrlib._format_version_tuple(version_tuple))
66
97
def set_warning_method(method):
108
139
def decorated_function(*args, **kwargs):
109
140
"""This is the decorated function."""
141
from bzrlib import trace
142
trace.mutter_callsite(4, "Deprecated function called")
110
143
warn(deprecation_string(callable, deprecation_version),
111
144
DeprecationWarning, stacklevel=2)
112
145
return callable(*args, **kwargs)
134
167
def decorated_method(self, *args, **kwargs):
135
168
"""This is the decorated method."""
169
from bzrlib import trace
136
170
if callable.__name__ == '__init__':
137
171
symbol = "%s.%s" % (self.__class__.__module__,
138
172
self.__class__.__name__,
142
176
self.__class__.__name__,
143
177
callable.__name__
179
trace.mutter_callsite(4, "Deprecated method called")
145
180
warn(deprecation_version % symbol, DeprecationWarning, stacklevel=2)
146
181
return callable(self, *args, **kwargs)
147
182
_populate_decorated(callable, deprecation_version, "method",
160
195
# we cannot just forward to a new method name.I.e. in the following
161
196
# examples we would want to have callers that pass any value to 'bad' be
162
197
# given a warning - because we have applied:
163
# @deprecated_parameter('bad', zero_seven)
198
# @deprecated_parameter('bad', deprecated_in((1, 5, 0))
165
200
# def __init__(self, bad=None)
166
201
# def __init__(self, bad, other)
229
264
"""Create a dict that warns when read or modified.
231
:param deprecation_version: something like zero_nine
266
:param deprecation_version: string for the warning format to raise,
267
typically from deprecated_in()
232
268
:param initial_value: The contents of the dict
233
269
:param variable_name: This allows better warnings to be printed
234
270
:param advice: String of advice on what callers should do instead
253
289
initial_value, extra=None):
254
290
"""Create a list that warns when modified
256
:param deprecation_version: something like zero_nine
292
:param deprecation_version: string for the warning format to raise,
293
typically from deprecated_in()
257
294
:param initial_value: The contents of the list
258
295
:param variable_name: This allows better warnings to be printed
259
296
:param extra: Extra info to print when printing a warning
298
335
return self._warn_deprecated(list.pop)
300
337
return _DeprecatedList(initial_value)
340
def _check_for_filter(error_only):
341
"""Check if there is already a filter for deprecation warnings.
343
:param error_only: Only match an 'error' filter
344
:return: True if a filter is found, False otherwise
347
for filter in warnings.filters:
348
if issubclass(DeprecationWarning, filter[2]):
349
# This filter will effect DeprecationWarning
350
if not error_only or filter[0] == 'error':
355
def suppress_deprecation_warnings(override=True):
356
"""Call this function to suppress all deprecation warnings.
358
When this is a final release version, we don't want to annoy users with
359
lots of deprecation warnings. We only want the deprecation warnings when
360
running a dev or release candidate.
362
:param override: If True, always set the ignore, if False, only set the
363
ignore if there isn't already a filter.
366
if not override and _check_for_filter(error_only=False):
367
# If there is already a filter effecting suppress_deprecation_warnings,
370
warnings.filterwarnings('ignore', category=DeprecationWarning)
373
def activate_deprecation_warnings(override=True):
374
"""Call this function to activate deprecation warnings.
376
When running in a 'final' release we suppress deprecation warnings.
377
However, the test suite wants to see them. So when running selftest, we
378
re-enable the deprecation warnings.
380
Note: warnings that have already been issued under 'ignore' will not be
381
reported after this point. The 'warnings' module has already marked them as
382
handled, so they don't get issued again.
384
:param override: If False, only add a filter if there isn't an error filter
385
already. (This slightly differs from suppress_deprecation_warnings, in
386
because it always overrides everything but -Werror).
389
if not override and _check_for_filter(error_only=True):
390
# DeprecationWarnings are already turned into errors, don't downgrade
393
warnings.filterwarnings('default', category=DeprecationWarning)