/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/symbol_versioning.py

  • Committer: Robert Collins
  • Date: 2010-05-06 23:41:35 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506234135-yivbzczw1sejxnxc
Lock methods on ``Tree``, ``Branch`` and ``Repository`` are now
expected to return an object which can be used to unlock them. This reduces
duplicate code when using cleanups. The previous 'tokens's returned by
``Branch.lock_write`` and ``Repository.lock_write`` are now attributes
on the result of the lock_write. ``repository.RepositoryWriteLockResult``
and ``branch.BranchWriteLockResult`` document this. (Robert Collins)

``log._get_info_for_log_files`` now takes an add_cleanup callable.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006, 2007, 2008, 2009 Canonical Ltd
2
2
#
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
19
19
The methods here allow for api symbol versioning.
20
20
"""
21
21
 
22
 
from __future__ import absolute_import
23
 
 
24
22
__all__ = ['deprecated_function',
25
23
           'deprecated_in',
26
24
           'deprecated_list',
31
29
           'warn',
32
30
           ]
33
31
 
34
 
 
35
 
import warnings
36
 
# Import the 'warn' symbol so breezy can call it even if we redefine it
37
32
from warnings import warn
38
33
 
39
 
import breezy
 
34
import bzrlib
40
35
 
41
36
 
42
37
DEPRECATED_PARAMETER = "A deprecated parameter marker."
49
44
    '%s was deprecated in version 1.4.0.'
50
45
    """
51
46
    return ("%%s was deprecated in version %s."
52
 
            % breezy._format_version_tuple(version_tuple))
 
47
            % bzrlib._format_version_tuple(version_tuple))
53
48
 
54
49
 
55
50
def set_warning_method(method):
71
66
    """Generate an automatic deprecation string for a_callable.
72
67
 
73
68
    :param a_callable: The callable to substitute 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.
 
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.
77
72
    """
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__,
81
 
                               a_callable.__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__)
85
 
    else:
 
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:
86
76
        symbol = "%s.%s" % (a_callable.__module__,
87
77
                            a_callable.__name__)
 
78
    else:
 
79
        symbol = "%s.%s.%s" % (a_callable.im_class.__module__,
 
80
                               a_callable.im_class.__name__,
 
81
                               a_callable.__name__
 
82
                               )
88
83
    return deprecation_version % symbol
89
84
 
90
85
 
96
91
 
97
92
        def decorated_function(*args, **kwargs):
98
93
            """This is the decorated function."""
99
 
            from . import trace
 
94
            from bzrlib import trace
100
95
            trace.mutter_callsite(4, "Deprecated function called")
101
96
            warn(deprecation_string(callable, deprecation_version),
102
 
                 DeprecationWarning, stacklevel=2)
 
97
                DeprecationWarning, stacklevel=2)
103
98
            return callable(*args, **kwargs)
104
99
        _populate_decorated(callable, deprecation_version, "function",
105
100
                            decorated_function)
124
119
 
125
120
        def decorated_method(self, *args, **kwargs):
126
121
            """This is the decorated method."""
127
 
            from . import trace
 
122
            from bzrlib import trace
128
123
            if callable.__name__ == '__init__':
129
124
                symbol = "%s.%s" % (self.__class__.__module__,
130
125
                                    self.__class__.__name__,
135
130
                                       callable.__name__
136
131
                                       )
137
132
            trace.mutter_callsite(4, "Deprecated method called")
138
 
            warn(deprecation_version %
139
 
                 symbol, DeprecationWarning, stacklevel=2)
 
133
            warn(deprecation_version % symbol, DeprecationWarning, stacklevel=2)
140
134
            return callable(self, *args, **kwargs)
141
135
        _populate_decorated(callable, deprecation_version, "method",
142
136
                            decorated_method)
160
154
    # def __init__(self, bad, other)
161
155
    # def __init__(self, **kwargs)
162
156
    # RBC 20060116
163
 
    return parameter_value is not DEPRECATED_PARAMETER
 
157
    return not parameter_value is DEPRECATED_PARAMETER
164
158
 
165
159
 
166
160
def _decorate_docstring(callable, deprecation_version, label,
172
166
    if len(docstring_lines) == 0:
173
167
        decorated_callable.__doc__ = deprecation_version % ("This " + label)
174
168
    elif len(docstring_lines) == 1:
175
 
        decorated_callable.__doc__ = (
176
 
            callable.__doc__ + "\n" + "\n" +
177
 
            deprecation_version % ("This " + label) + "\n")
 
169
        decorated_callable.__doc__ = (callable.__doc__
 
170
                                    + "\n"
 
171
                                    + "\n"
 
172
                                    + deprecation_version % ("This " + label)
 
173
                                    + "\n")
178
174
    else:
179
175
        spaces = len(docstring_lines[-1])
180
176
        new_doc = callable.__doc__
213
209
    is_deprecated = True
214
210
 
215
211
    def __init__(self,
216
 
                 deprecation_version,
217
 
                 variable_name,
218
 
                 initial_value,
219
 
                 advice,
220
 
                 ):
 
212
        deprecation_version,
 
213
        variable_name,
 
214
        initial_value,
 
215
        advice,
 
216
        ):
221
217
        """Create a dict that warns when read or modified.
222
218
 
223
219
        :param deprecation_version: string for the warning format to raise,
300
296
    :param error_only: Only match an 'error' filter
301
297
    :return: True if a filter is found, False otherwise
302
298
    """
 
299
    import warnings
303
300
    for filter in warnings.filters:
304
301
        if issubclass(DeprecationWarning, filter[2]):
305
302
            # This filter will effect DeprecationWarning
308
305
    return False
309
306
 
310
307
 
311
 
def _remove_filter_callable(filter):
312
 
    """Build and returns a callable removing filter from the warnings.
313
 
 
314
 
    :param filter: The filter to remove (can be None).
315
 
 
316
 
    :return: A callable that will remove filter from warnings.filters.
317
 
    """
318
 
    def cleanup():
319
 
        if filter:
320
 
            try:
321
 
                warnings.filters.remove(filter)
322
 
            except (ValueError, IndexError):
323
 
                pass
324
 
    return cleanup
325
 
 
326
 
 
327
308
def suppress_deprecation_warnings(override=True):
328
309
    """Call this function to suppress all deprecation warnings.
329
310
 
333
314
 
334
315
    :param override: If True, always set the ignore, if False, only set the
335
316
        ignore if there isn't already a filter.
336
 
 
337
 
    :return: A callable to remove the new warnings this added.
338
317
    """
 
318
    import warnings
339
319
    if not override and _check_for_filter(error_only=False):
340
320
        # If there is already a filter effecting suppress_deprecation_warnings,
341
321
        # then skip it.
342
 
        filter = None
343
 
    else:
344
 
        warnings.filterwarnings('ignore', category=DeprecationWarning)
345
 
        filter = warnings.filters[0]
346
 
    return _remove_filter_callable(filter)
 
322
        return
 
323
    warnings.filterwarnings('ignore', category=DeprecationWarning)
347
324
 
348
325
 
349
326
def activate_deprecation_warnings(override=True):
360
337
    :param override: If False, only add a filter if there isn't an error filter
361
338
        already. (This slightly differs from suppress_deprecation_warnings, in
362
339
        because it always overrides everything but -Werror).
363
 
 
364
 
    :return: A callable to remove the new warnings this added.
365
340
    """
 
341
    import warnings
366
342
    if not override and _check_for_filter(error_only=True):
367
343
        # DeprecationWarnings are already turned into errors, don't downgrade
368
344
        # them to 'default'.
369
 
        filter = None
370
 
    else:
371
 
        warnings.filterwarnings('default', category=DeprecationWarning)
372
 
        filter = warnings.filters[0]
373
 
    return _remove_filter_callable(filter)
 
345
        return
 
346
    warnings.filterwarnings('default', category=DeprecationWarning)