23
23
__all__ = ['deprecated_function',
24
26
'deprecated_method',
25
27
'DEPRECATED_PARAMETER',
26
28
'deprecated_passed',
27
'warn', 'set_warning_method', 'zero_seven',
31
56
from warnings import warn
34
61
DEPRECATED_PARAMETER = "A deprecated parameter marker."
35
62
zero_seven = "%s was deprecated in version 0.7."
36
63
zero_eight = "%s was deprecated in version 0.8."
64
zero_nine = "%s was deprecated in version 0.9."
65
zero_ten = "%s was deprecated in version 0.10."
66
zero_eleven = "%s was deprecated in version 0.11."
67
zero_twelve = "%s was deprecated in version 0.12."
68
zero_thirteen = "%s was deprecated in version 0.13."
69
zero_fourteen = "%s was deprecated in version 0.14."
70
zero_fifteen = "%s was deprecated in version 0.15."
71
zero_sixteen = "%s was deprecated in version 0.16."
72
zero_seventeen = "%s was deprecated in version 0.17."
73
zero_eighteen = "%s was deprecated in version 0.18."
74
zero_ninety = "%s was deprecated in version 0.90."
75
zero_ninetyone = "%s was deprecated in version 0.91."
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))
39
97
def set_warning_method(method):
50
108
# add that on top of the primitives, once we have all three written
112
def deprecation_string(a_callable, deprecation_version):
113
"""Generate an automatic deprecation string for a_callable.
115
:param a_callable: The callable to substitute into deprecation_version.
116
:param deprecation_version: A deprecation format warning string. This should
117
have a single %s operator in it. a_callable will be turned into a nice
118
python symbol and then substituted into deprecation_version.
120
# We also want to handle old-style classes, in particular exception, and
121
# they don't have an im_class attribute.
122
if getattr(a_callable, 'im_class', None) is None:
123
symbol = "%s.%s" % (a_callable.__module__,
126
symbol = "%s.%s.%s" % (a_callable.im_class.__module__,
127
a_callable.im_class.__name__,
130
return deprecation_version % symbol
53
133
def deprecated_function(deprecation_version):
54
134
"""Decorate a function so that use of it will trigger a warning."""
140
232
decorated_callable.__module__ = callable.__module__
141
233
decorated_callable.__name__ = callable.__name__
142
234
decorated_callable.is_deprecated = True
237
def _dict_deprecation_wrapper(wrapped_method):
238
"""Returns a closure that emits a warning and calls the superclass"""
239
def cb(dep_dict, *args, **kwargs):
240
msg = 'access to %s' % (dep_dict._variable_name, )
241
msg = dep_dict._deprecation_version % (msg,)
243
msg += ' ' + dep_dict._advice
244
warn(msg, DeprecationWarning, stacklevel=2)
245
return wrapped_method(dep_dict, *args, **kwargs)
249
class DeprecatedDict(dict):
250
"""A dictionary that complains when read or written."""
260
"""Create a dict that warns when read or modified.
262
:param deprecation_version: string for the warning format to raise,
263
typically from deprecated_in()
264
:param initial_value: The contents of the dict
265
:param variable_name: This allows better warnings to be printed
266
:param advice: String of advice on what callers should do instead
267
of using this variable.
269
self._deprecation_version = deprecation_version
270
self._variable_name = variable_name
271
self._advice = advice
272
dict.__init__(self, initial_value)
274
# This isn't every possible method but it should trap anyone using the
275
# dict -- add more if desired
276
__len__ = _dict_deprecation_wrapper(dict.__len__)
277
__getitem__ = _dict_deprecation_wrapper(dict.__getitem__)
278
__setitem__ = _dict_deprecation_wrapper(dict.__setitem__)
279
__delitem__ = _dict_deprecation_wrapper(dict.__delitem__)
280
keys = _dict_deprecation_wrapper(dict.keys)
281
__contains__ = _dict_deprecation_wrapper(dict.__contains__)
284
def deprecated_list(deprecation_version, variable_name,
285
initial_value, extra=None):
286
"""Create a list that warns when modified
288
:param deprecation_version: string for the warning format to raise,
289
typically from deprecated_in()
290
:param initial_value: The contents of the list
291
:param variable_name: This allows better warnings to be printed
292
:param extra: Extra info to print when printing a warning
295
subst_text = 'Modifying %s' % (variable_name,)
296
msg = deprecation_version % (subst_text,)
300
class _DeprecatedList(list):
301
__doc__ = list.__doc__ + msg
305
def _warn_deprecated(self, func, *args, **kwargs):
306
warn(msg, DeprecationWarning, stacklevel=3)
307
return func(self, *args, **kwargs)
309
def append(self, obj):
310
"""appending to %s is deprecated""" % (variable_name,)
311
return self._warn_deprecated(list.append, obj)
313
def insert(self, index, obj):
314
"""inserting to %s is deprecated""" % (variable_name,)
315
return self._warn_deprecated(list.insert, index, obj)
317
def extend(self, iterable):
318
"""extending %s is deprecated""" % (variable_name,)
319
return self._warn_deprecated(list.extend, iterable)
321
def remove(self, value):
322
"""removing from %s is deprecated""" % (variable_name,)
323
return self._warn_deprecated(list.remove, value)
325
def pop(self, index=None):
326
"""pop'ing from from %s is deprecated""" % (variable_name,)
328
return self._warn_deprecated(list.pop, index)
331
return self._warn_deprecated(list.pop)
333
return _DeprecatedList(initial_value)
336
def _check_for_filter(error_only):
337
"""Check if there is already a filter for deprecation warnings.
339
:param error_only: Only match an 'error' filter
340
:return: True if a filter is found, False otherwise
343
for filter in warnings.filters:
344
if issubclass(DeprecationWarning, filter[2]):
345
# This filter will effect DeprecationWarning
346
if not error_only or filter[0] == 'error':
351
def suppress_deprecation_warnings(override=True):
352
"""Call this function to suppress all deprecation warnings.
354
When this is a final release version, we don't want to annoy users with
355
lots of deprecation warnings. We only want the deprecation warnings when
356
running a dev or release candidate.
358
:param override: If True, always set the ignore, if False, only set the
359
ignore if there isn't already a filter.
362
if not override and _check_for_filter(error_only=False):
363
# If there is already a filter effecting suppress_deprecation_warnings,
366
warnings.filterwarnings('ignore', category=DeprecationWarning)
369
def activate_deprecation_warnings(override=True):
370
"""Call this function to activate deprecation warnings.
372
When running in a 'final' release we suppress deprecation warnings.
373
However, the test suite wants to see them. So when running selftest, we
374
re-enable the deprecation warnings.
376
Note: warnings that have already been issued under 'ignore' will not be
377
reported after this point. The 'warnings' module has already marked them as
378
handled, so they don't get issued again.
380
:param override: If False, only add a filter if there isn't an error filter
381
already. (This slightly differs from suppress_deprecation_warnings, in
382
because it always overrides everything but -Werror).
385
if not override and _check_for_filter(error_only=True):
386
# DeprecationWarnings are already turned into errors, don't downgrade
389
warnings.filterwarnings('default', category=DeprecationWarning)