/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 breezy/symbol_versioning.py

  • Committer: Jelmer Vernooij
  • Date: 2018-11-11 04:08:32 UTC
  • mto: (7143.16.20 even-more-cleanups)
  • mto: This revision was merged to the branch mainline in revision 7175.
  • Revision ID: jelmer@jelmer.uk-20181111040832-nsljjynzzwmznf3h
Run autopep8.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007, 2008, 2009 Canonical Ltd
 
1
# Copyright (C) 2006-2010 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
 
22
24
__all__ = ['deprecated_function',
23
25
           'deprecated_in',
24
26
           'deprecated_list',
29
31
           'warn',
30
32
           ]
31
33
 
 
34
 
 
35
import warnings
 
36
# Import the 'warn' symbol so breezy can call it even if we redefine it
32
37
from warnings import warn
33
38
 
34
 
import bzrlib
 
39
import breezy
35
40
 
36
41
 
37
42
DEPRECATED_PARAMETER = "A deprecated parameter marker."
44
49
    '%s was deprecated in version 1.4.0.'
45
50
    """
46
51
    return ("%%s was deprecated in version %s."
47
 
            % bzrlib._format_version_tuple(version_tuple))
 
52
            % breezy._format_version_tuple(version_tuple))
48
53
 
49
54
 
50
55
def set_warning_method(method):
70
75
        have a single %s operator in it. a_callable will be turned into a nice
71
76
        python symbol and then substituted into deprecation_version.
72
77
    """
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__,
 
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:
76
86
        symbol = "%s.%s" % (a_callable.__module__,
77
87
                            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
 
                               )
83
88
    return deprecation_version % symbol
84
89
 
85
90
 
91
96
 
92
97
        def decorated_function(*args, **kwargs):
93
98
            """This is the decorated function."""
94
 
            from bzrlib import trace
 
99
            from . 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)
119
124
 
120
125
        def decorated_method(self, *args, **kwargs):
121
126
            """This is the decorated method."""
122
 
            from bzrlib import trace
 
127
            from . 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__
131
136
                                       )
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)
170
176
                                    + "\n"
171
177
                                    + "\n"
172
178
                                    + deprecation_version % ("This " + label)
173
 
                                    + "\n")
 
179
                                      + "\n")
174
180
    else:
175
181
        spaces = len(docstring_lines[-1])
176
182
        new_doc = callable.__doc__
209
215
    is_deprecated = True
210
216
 
211
217
    def __init__(self,
212
 
        deprecation_version,
213
 
        variable_name,
214
 
        initial_value,
215
 
        advice,
216
 
        ):
 
218
                 deprecation_version,
 
219
                 variable_name,
 
220
                 initial_value,
 
221
                 advice,
 
222
                 ):
217
223
        """Create a dict that warns when read or modified.
218
224
 
219
225
        :param deprecation_version: string for the warning format to raise,
296
302
    :param error_only: Only match an 'error' filter
297
303
    :return: True if a filter is found, False otherwise
298
304
    """
299
 
    import warnings
300
305
    for filter in warnings.filters:
301
306
        if issubclass(DeprecationWarning, filter[2]):
302
307
            # This filter will effect DeprecationWarning
305
310
    return False
306
311
 
307
312
 
 
313
def _remove_filter_callable(filter):
 
314
    """Build and returns a callable removing filter from the warnings.
 
315
 
 
316
    :param filter: The filter to remove (can be None).
 
317
 
 
318
    :return: A callable that will remove filter from warnings.filters.
 
319
    """
 
320
    def cleanup():
 
321
        if filter:
 
322
            warnings.filters.remove(filter)
 
323
    return cleanup
 
324
 
 
325
 
308
326
def suppress_deprecation_warnings(override=True):
309
327
    """Call this function to suppress all deprecation warnings.
310
328
 
314
332
 
315
333
    :param override: If True, always set the ignore, if False, only set the
316
334
        ignore if there isn't already a filter.
 
335
 
 
336
    :return: A callable to remove the new warnings this added.
317
337
    """
318
 
    import warnings
319
338
    if not override and _check_for_filter(error_only=False):
320
339
        # If there is already a filter effecting suppress_deprecation_warnings,
321
340
        # then skip it.
322
 
        return
323
 
    warnings.filterwarnings('ignore', category=DeprecationWarning)
 
341
        filter = None
 
342
    else:
 
343
        warnings.filterwarnings('ignore', category=DeprecationWarning)
 
344
        filter = warnings.filters[0]
 
345
    return _remove_filter_callable(filter)
324
346
 
325
347
 
326
348
def activate_deprecation_warnings(override=True):
337
359
    :param override: If False, only add a filter if there isn't an error filter
338
360
        already. (This slightly differs from suppress_deprecation_warnings, in
339
361
        because it always overrides everything but -Werror).
 
362
 
 
363
    :return: A callable to remove the new warnings this added.
340
364
    """
341
 
    import warnings
342
365
    if not override and _check_for_filter(error_only=True):
343
366
        # DeprecationWarnings are already turned into errors, don't downgrade
344
367
        # them to 'default'.
345
 
        return
346
 
    warnings.filterwarnings('default', category=DeprecationWarning)
 
368
        filter = None
 
369
    else:
 
370
        warnings.filterwarnings('default', category=DeprecationWarning)
 
371
        filter = warnings.filters[0]
 
372
    return _remove_filter_callable(filter)