/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: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2020-08-23 01:15:41 UTC
  • mfrom: (7520.1.4 merge-3.1)
  • Revision ID: breezy.the.bot@gmail.com-20200823011541-nv0oh7nzaganx2qy
Merge lp:brz/3.1.

Merged from https://code.launchpad.net/~jelmer/brz/merge-3.1/+merge/389690

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