/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: 2018-09-11 02:50:43 UTC
  • mfrom: (7067.16.7 fix-c-extensions)
  • Revision ID: breezy.the.bot@gmail.com-20180911025043-c4v82nhbajxniw2y
Fix remaining Cython extensions on Python 3.

Merged from https://code.launchpad.net/~jelmer/brz/fix-c-extensions/+merge/353369

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
 
 
41
from .sixish import PY3
36
42
 
37
43
DEPRECATED_PARAMETER = "A deprecated parameter marker."
38
44
 
44
50
    '%s was deprecated in version 1.4.0.'
45
51
    """
46
52
    return ("%%s was deprecated in version %s."
47
 
            % bzrlib._format_version_tuple(version_tuple))
 
53
            % breezy._format_version_tuple(version_tuple))
48
54
 
49
55
 
50
56
def set_warning_method(method):
70
76
        have a single %s operator in it. a_callable will be turned into a nice
71
77
        python symbol and then substituted into deprecation_version.
72
78
    """
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
 
        symbol = "%s.%s" % (a_callable.__module__,
77
 
                            a_callable.__name__)
 
79
    if PY3:
 
80
        func = getattr(a_callable, '__func__', a_callable)
 
81
        symbol ='%s.%s' % (a_callable.__module__,
 
82
                           a_callable.__qualname__)
78
83
    else:
79
 
        symbol = "%s.%s.%s" % (a_callable.im_class.__module__,
80
 
                               a_callable.im_class.__name__,
81
 
                               a_callable.__name__
82
 
                               )
 
84
        if getattr(a_callable, '__self__', None) is None:
 
85
            symbol = "%s.%s" % (a_callable.__module__,
 
86
                                a_callable.__name__)
 
87
        else:
 
88
            symbol = "%s.%s.%s" % (a_callable.__self__.__class__.__module__,
 
89
                                   a_callable.__self__.__class__.__name__,
 
90
                                   a_callable.__name__
 
91
                                   )
83
92
    return deprecation_version % symbol
84
93
 
85
94
 
91
100
 
92
101
        def decorated_function(*args, **kwargs):
93
102
            """This is the decorated function."""
94
 
            from bzrlib import trace
 
103
            from . import trace
95
104
            trace.mutter_callsite(4, "Deprecated function called")
96
105
            warn(deprecation_string(callable, deprecation_version),
97
106
                DeprecationWarning, stacklevel=2)
119
128
 
120
129
        def decorated_method(self, *args, **kwargs):
121
130
            """This is the decorated method."""
122
 
            from bzrlib import trace
 
131
            from . import trace
123
132
            if callable.__name__ == '__init__':
124
133
                symbol = "%s.%s" % (self.__class__.__module__,
125
134
                                    self.__class__.__name__,
296
305
    :param error_only: Only match an 'error' filter
297
306
    :return: True if a filter is found, False otherwise
298
307
    """
299
 
    import warnings
300
308
    for filter in warnings.filters:
301
309
        if issubclass(DeprecationWarning, filter[2]):
302
310
            # This filter will effect DeprecationWarning
305
313
    return False
306
314
 
307
315
 
 
316
def _remove_filter_callable(filter):
 
317
    """Build and returns a callable removing filter from the warnings.
 
318
 
 
319
    :param filter: The filter to remove (can be None).
 
320
 
 
321
    :return: A callable that will remove filter from warnings.filters.
 
322
    """
 
323
    def cleanup():
 
324
        if filter:
 
325
            warnings.filters.remove(filter)
 
326
    return cleanup
 
327
 
 
328
 
308
329
def suppress_deprecation_warnings(override=True):
309
330
    """Call this function to suppress all deprecation warnings.
310
331
 
314
335
 
315
336
    :param override: If True, always set the ignore, if False, only set the
316
337
        ignore if there isn't already a filter.
 
338
 
 
339
    :return: A callable to remove the new warnings this added.
317
340
    """
318
 
    import warnings
319
341
    if not override and _check_for_filter(error_only=False):
320
342
        # If there is already a filter effecting suppress_deprecation_warnings,
321
343
        # then skip it.
322
 
        return
323
 
    warnings.filterwarnings('ignore', category=DeprecationWarning)
 
344
        filter = None
 
345
    else:
 
346
        warnings.filterwarnings('ignore', category=DeprecationWarning)
 
347
        filter = warnings.filters[0]
 
348
    return _remove_filter_callable(filter)
324
349
 
325
350
 
326
351
def activate_deprecation_warnings(override=True):
337
362
    :param override: If False, only add a filter if there isn't an error filter
338
363
        already. (This slightly differs from suppress_deprecation_warnings, in
339
364
        because it always overrides everything but -Werror).
 
365
 
 
366
    :return: A callable to remove the new warnings this added.
340
367
    """
341
 
    import warnings
342
368
    if not override and _check_for_filter(error_only=True):
343
369
        # DeprecationWarnings are already turned into errors, don't downgrade
344
370
        # them to 'default'.
345
 
        return
346
 
    warnings.filterwarnings('default', category=DeprecationWarning)
 
371
        filter = None
 
372
    else:
 
373
        warnings.filterwarnings('default', category=DeprecationWarning)
 
374
        filter = warnings.filters[0]
 
375
    return _remove_filter_callable(filter)