~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/symbol_versioning.py

  • Committer: Martin Pool
  • Date: 2009-03-11 07:04:55 UTC
  • mto: This revision was merged to the branch mainline in revision 4144.
  • Revision ID: mbp@sourcefrog.net-20090311070455-gay4qlayiamcq50k
Remove obsolete comments

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 by Canonical Ltd
2
 
#   Authors: Robert Collins <robert.collins@canonical.com>
 
1
# Copyright (C) 2006, 2007, 2008 Canonical Ltd
 
2
#   Authors: Robert Collins <robert.collins@canonical.com> and others
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
5
5
# it under the terms of the GNU General Public License as published by
21
21
"""
22
22
 
23
23
__all__ = ['deprecated_function',
 
24
           'deprecated_in',
24
25
           'deprecated_list',
25
26
           'deprecated_method',
26
27
           'DEPRECATED_PARAMETER',
27
28
           'deprecated_passed',
28
 
           'warn', 'set_warning_method', 'zero_seven',
 
29
           'set_warning_method',
 
30
           'warn',
 
31
           'zero_seven',
29
32
           'zero_eight',
30
33
           'zero_nine',
31
34
           'zero_ten',
32
35
           'zero_eleven',
33
36
           'zero_twelve',
 
37
           'zero_thirteen',
 
38
           'zero_fourteen',
 
39
           'zero_fifteen',
 
40
           'zero_sixteen',
 
41
           'zero_seventeen',
 
42
           'zero_eighteen',
 
43
           'zero_ninety',
 
44
           'zero_ninetyone',
 
45
           'zero_ninetytwo',
 
46
           'zero_ninetythree',
 
47
           'one_zero',
 
48
           'one_one',
 
49
           'one_two',
 
50
           'one_three',
 
51
           'one_four',
 
52
           'one_five',
 
53
           'one_six',
34
54
           ]
35
55
 
36
56
from warnings import warn
37
57
 
 
58
import bzrlib
 
59
 
38
60
 
39
61
DEPRECATED_PARAMETER = "A deprecated parameter marker."
40
62
zero_seven = "%s was deprecated in version 0.7."
43
65
zero_ten = "%s was deprecated in version 0.10."
44
66
zero_eleven = "%s was deprecated in version 0.11."
45
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."
 
85
 
 
86
 
 
87
def deprecated_in(version_tuple):
 
88
    """Generate a message that something was deprecated in a release.
 
89
 
 
90
    >>> deprecated_in((1, 4, 0))
 
91
    '%s was deprecated in version 1.4.'
 
92
    """
 
93
    return ("%%s was deprecated in version %s."
 
94
            % bzrlib._format_version_tuple(version_tuple))
46
95
 
47
96
 
48
97
def set_warning_method(method):
68
117
        have a single %s operator in it. a_callable will be turned into a nice
69
118
        python symbol and then substituted into deprecation_version.
70
119
    """
 
120
    # We also want to handle old-style classes, in particular exception, and
 
121
    # they don't have an im_class attribute.
71
122
    if getattr(a_callable, 'im_class', None) is None:
72
123
        symbol = "%s.%s" % (a_callable.__module__,
73
124
                            a_callable.__name__)
84
135
 
85
136
    def function_decorator(callable):
86
137
        """This is the function python calls to perform the decoration."""
87
 
        
 
138
 
88
139
        def decorated_function(*args, **kwargs):
89
140
            """This is the decorated function."""
 
141
            from bzrlib import trace
 
142
            trace.mutter_callsite(4, "Deprecated function called")
90
143
            warn(deprecation_string(callable, deprecation_version),
91
144
                DeprecationWarning, stacklevel=2)
92
145
            return callable(*args, **kwargs)
98
151
 
99
152
def deprecated_method(deprecation_version):
100
153
    """Decorate a method so that use of it will trigger a warning.
101
 
    
 
154
 
 
155
    To deprecate a static or class method, use
 
156
 
 
157
        @staticmethod
 
158
        @deprecated_function
 
159
        def ...
 
160
 
102
161
    To deprecate an entire class, decorate __init__.
103
162
    """
104
163
 
105
164
    def method_decorator(callable):
106
165
        """This is the function python calls to perform the decoration."""
107
 
        
 
166
 
108
167
        def decorated_method(self, *args, **kwargs):
109
168
            """This is the decorated method."""
110
 
            symbol = "%s.%s.%s" % (self.__class__.__module__,
111
 
                                   self.__class__.__name__,
112
 
                                   callable.__name__
113
 
                                   )
 
169
            from bzrlib import trace
 
170
            if callable.__name__ == '__init__':
 
171
                symbol = "%s.%s" % (self.__class__.__module__,
 
172
                                    self.__class__.__name__,
 
173
                                    )
 
174
            else:
 
175
                symbol = "%s.%s.%s" % (self.__class__.__module__,
 
176
                                       self.__class__.__name__,
 
177
                                       callable.__name__
 
178
                                       )
 
179
            trace.mutter_callsite(4, "Deprecated method called")
114
180
            warn(deprecation_version % symbol, DeprecationWarning, stacklevel=2)
115
181
            return callable(self, *args, **kwargs)
116
182
        _populate_decorated(callable, deprecation_version, "method",
121
187
 
122
188
def deprecated_passed(parameter_value):
123
189
    """Return True if parameter_value was used."""
124
 
    # FIXME: it might be nice to have a parameter deprecation decorator. 
 
190
    # FIXME: it might be nice to have a parameter deprecation decorator.
125
191
    # it would need to handle positional and *args and **kwargs parameters,
126
192
    # which means some mechanism to describe how the parameter was being
127
193
    # passed before deprecation, and some way to deprecate parameters that
129
195
    # we cannot just forward to a new method name.I.e. in the following
130
196
    # examples we would want to have callers that pass any value to 'bad' be
131
197
    # given a warning - because we have applied:
132
 
    # @deprecated_parameter('bad', zero_seven)
 
198
    # @deprecated_parameter('bad', deprecated_in((1, 5, 0))
133
199
    #
134
200
    # def __init__(self, bad=None)
135
201
    # def __init__(self, bad, other)
147
213
    if len(docstring_lines) == 0:
148
214
        decorated_callable.__doc__ = deprecation_version % ("This " + label)
149
215
    elif len(docstring_lines) == 1:
150
 
        decorated_callable.__doc__ = (callable.__doc__ 
 
216
        decorated_callable.__doc__ = (callable.__doc__
151
217
                                    + "\n"
152
218
                                    + "\n"
153
219
                                    + deprecation_version % ("This " + label)
172
238
    decorated_callable.is_deprecated = True
173
239
 
174
240
 
 
241
def _dict_deprecation_wrapper(wrapped_method):
 
242
    """Returns a closure that emits a warning and calls the superclass"""
 
243
    def cb(dep_dict, *args, **kwargs):
 
244
        msg = 'access to %s' % (dep_dict._variable_name, )
 
245
        msg = dep_dict._deprecation_version % (msg,)
 
246
        if dep_dict._advice:
 
247
            msg += ' ' + dep_dict._advice
 
248
        warn(msg, DeprecationWarning, stacklevel=2)
 
249
        return wrapped_method(dep_dict, *args, **kwargs)
 
250
    return cb
 
251
 
 
252
 
 
253
class DeprecatedDict(dict):
 
254
    """A dictionary that complains when read or written."""
 
255
 
 
256
    is_deprecated = True
 
257
 
 
258
    def __init__(self,
 
259
        deprecation_version,
 
260
        variable_name,
 
261
        initial_value,
 
262
        advice,
 
263
        ):
 
264
        """Create a dict that warns when read or modified.
 
265
 
 
266
        :param deprecation_version: string for the warning format to raise,
 
267
            typically from deprecated_in()
 
268
        :param initial_value: The contents of the dict
 
269
        :param variable_name: This allows better warnings to be printed
 
270
        :param advice: String of advice on what callers should do instead
 
271
            of using this variable.
 
272
        """
 
273
        self._deprecation_version = deprecation_version
 
274
        self._variable_name = variable_name
 
275
        self._advice = advice
 
276
        dict.__init__(self, initial_value)
 
277
 
 
278
    # This isn't every possible method but it should trap anyone using the
 
279
    # dict -- add more if desired
 
280
    __len__ = _dict_deprecation_wrapper(dict.__len__)
 
281
    __getitem__ = _dict_deprecation_wrapper(dict.__getitem__)
 
282
    __setitem__ = _dict_deprecation_wrapper(dict.__setitem__)
 
283
    __delitem__ = _dict_deprecation_wrapper(dict.__delitem__)
 
284
    keys = _dict_deprecation_wrapper(dict.keys)
 
285
    __contains__ = _dict_deprecation_wrapper(dict.__contains__)
 
286
 
 
287
 
175
288
def deprecated_list(deprecation_version, variable_name,
176
289
                    initial_value, extra=None):
177
290
    """Create a list that warns when modified
178
291
 
179
 
    :param deprecation_version: something like zero_nine
 
292
    :param deprecation_version: string for the warning format to raise,
 
293
        typically from deprecated_in()
180
294
    :param initial_value: The contents of the list
181
295
    :param variable_name: This allows better warnings to be printed
182
296
    :param extra: Extra info to print when printing a warning
195
309
        def _warn_deprecated(self, func, *args, **kwargs):
196
310
            warn(msg, DeprecationWarning, stacklevel=3)
197
311
            return func(self, *args, **kwargs)
198
 
            
 
312
 
199
313
        def append(self, obj):
200
314
            """appending to %s is deprecated""" % (variable_name,)
201
315
            return self._warn_deprecated(list.append, obj)
221
335
                return self._warn_deprecated(list.pop)
222
336
 
223
337
    return _DeprecatedList(initial_value)
 
338
 
 
339
 
 
340
def _check_for_filter(error_only):
 
341
    """Check if there is already a filter for deprecation warnings.
 
342
 
 
343
    :param error_only: Only match an 'error' filter
 
344
    :return: True if a filter is found, False otherwise
 
345
    """
 
346
    import warnings
 
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':
 
351
                return True
 
352
    return False
 
353
 
 
354
 
 
355
def suppress_deprecation_warnings(override=True):
 
356
    """Call this function to suppress all deprecation warnings.
 
357
 
 
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.
 
361
 
 
362
    :param override: If True, always set the ignore, if False, only set the
 
363
        ignore if there isn't already a filter.
 
364
    """
 
365
    import warnings
 
366
    if not override and _check_for_filter(error_only=False):
 
367
        # If there is already a filter effecting suppress_deprecation_warnings,
 
368
        # then skip it.
 
369
        return
 
370
    warnings.filterwarnings('ignore', category=DeprecationWarning)
 
371
 
 
372
 
 
373
def activate_deprecation_warnings(override=True):
 
374
    """Call this function to activate deprecation warnings.
 
375
 
 
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.
 
379
 
 
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.
 
383
 
 
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).
 
387
    """
 
388
    import warnings
 
389
    if not override and _check_for_filter(error_only=True):
 
390
        # DeprecationWarnings are already turned into errors, don't downgrade
 
391
        # them to 'default'.
 
392
        return
 
393
    warnings.filterwarnings('default', category=DeprecationWarning)