/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: 2017-06-10 01:35:53 UTC
  • mto: (6670.4.8 move-bzr)
  • mto: This revision was merged to the branch mainline in revision 6681.
  • Revision ID: jelmer@jelmer.uk-20170610013553-560y7mn3su4pp763
Fix remaining tests.

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):
76
81
        symbol = "%s.%s" % (a_callable.__module__,
77
82
                            a_callable.__name__)
78
83
    else:
79
 
        symbol = "%s.%s.%s" % (a_callable.im_class.__module__,
80
 
                               a_callable.im_class.__name__,
 
84
        symbol = "%s.%s.%s" % (a_callable.__self__.__class__.__module__,
 
85
                               a_callable.__self__.__class__.__name__,
81
86
                               a_callable.__name__
82
87
                               )
83
88
    return deprecation_version % symbol
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
102
                DeprecationWarning, stacklevel=2)
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__,
296
301
    :param error_only: Only match an 'error' filter
297
302
    :return: True if a filter is found, False otherwise
298
303
    """
299
 
    import warnings
300
304
    for filter in warnings.filters:
301
305
        if issubclass(DeprecationWarning, filter[2]):
302
306
            # This filter will effect DeprecationWarning
305
309
    return False
306
310
 
307
311
 
 
312
def _remove_filter_callable(filter):
 
313
    """Build and returns a callable removing filter from the warnings.
 
314
 
 
315
    :param filter: The filter to remove (can be None).
 
316
 
 
317
    :return: A callable that will remove filter from warnings.filters.
 
318
    """
 
319
    def cleanup():
 
320
        if filter:
 
321
            warnings.filters.remove(filter)
 
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)