/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: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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',
31
33
 
32
34
 
33
35
import warnings
34
 
# Import the 'warn' symbol so breezy can call it even if we redefine it
 
36
# Import the 'warn' symbol so bzrlib can call it even if we redefine it
35
37
from warnings import warn
36
38
 
37
 
import breezy
 
39
import bzrlib
38
40
 
39
41
 
40
42
DEPRECATED_PARAMETER = "A deprecated parameter marker."
47
49
    '%s was deprecated in version 1.4.0.'
48
50
    """
49
51
    return ("%%s was deprecated in version %s."
50
 
            % breezy._format_version_tuple(version_tuple))
 
52
            % bzrlib._format_version_tuple(version_tuple))
51
53
 
52
54
 
53
55
def set_warning_method(method):
69
71
    """Generate an automatic deprecation string for a_callable.
70
72
 
71
73
    :param a_callable: The callable to substitute 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.
 
74
    :param deprecation_version: A deprecation format warning string. This should
 
75
        have a single %s operator in it. a_callable will be turned into a nice
 
76
        python symbol and then substituted into deprecation_version.
75
77
    """
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:
 
78
    # We also want to handle old-style classes, in particular exception, and
 
79
    # they don't have an im_class attribute.
 
80
    if getattr(a_callable, 'im_class', None) is None:
84
81
        symbol = "%s.%s" % (a_callable.__module__,
85
82
                            a_callable.__name__)
 
83
    else:
 
84
        symbol = "%s.%s.%s" % (a_callable.im_class.__module__,
 
85
                               a_callable.im_class.__name__,
 
86
                               a_callable.__name__
 
87
                               )
86
88
    return deprecation_version % symbol
87
89
 
88
90
 
94
96
 
95
97
        def decorated_function(*args, **kwargs):
96
98
            """This is the decorated function."""
97
 
            from . import trace
 
99
            from bzrlib import trace
98
100
            trace.mutter_callsite(4, "Deprecated function called")
99
101
            warn(deprecation_string(callable, deprecation_version),
100
 
                 DeprecationWarning, stacklevel=2)
 
102
                DeprecationWarning, stacklevel=2)
101
103
            return callable(*args, **kwargs)
102
104
        _populate_decorated(callable, deprecation_version, "function",
103
105
                            decorated_function)
122
124
 
123
125
        def decorated_method(self, *args, **kwargs):
124
126
            """This is the decorated method."""
125
 
            from . import trace
 
127
            from bzrlib import trace
126
128
            if callable.__name__ == '__init__':
127
129
                symbol = "%s.%s" % (self.__class__.__module__,
128
130
                                    self.__class__.__name__,
133
135
                                       callable.__name__
134
136
                                       )
135
137
            trace.mutter_callsite(4, "Deprecated method called")
136
 
            warn(deprecation_version %
137
 
                 symbol, DeprecationWarning, stacklevel=2)
 
138
            warn(deprecation_version % symbol, DeprecationWarning, stacklevel=2)
138
139
            return callable(self, *args, **kwargs)
139
140
        _populate_decorated(callable, deprecation_version, "method",
140
141
                            decorated_method)
158
159
    # def __init__(self, bad, other)
159
160
    # def __init__(self, **kwargs)
160
161
    # RBC 20060116
161
 
    return parameter_value is not DEPRECATED_PARAMETER
 
162
    return not parameter_value is DEPRECATED_PARAMETER
162
163
 
163
164
 
164
165
def _decorate_docstring(callable, deprecation_version, label,
170
171
    if len(docstring_lines) == 0:
171
172
        decorated_callable.__doc__ = deprecation_version % ("This " + label)
172
173
    elif len(docstring_lines) == 1:
173
 
        decorated_callable.__doc__ = (
174
 
            callable.__doc__ + "\n" + "\n" +
175
 
            deprecation_version % ("This " + label) + "\n")
 
174
        decorated_callable.__doc__ = (callable.__doc__
 
175
                                    + "\n"
 
176
                                    + "\n"
 
177
                                    + deprecation_version % ("This " + label)
 
178
                                    + "\n")
176
179
    else:
177
180
        spaces = len(docstring_lines[-1])
178
181
        new_doc = callable.__doc__
211
214
    is_deprecated = True
212
215
 
213
216
    def __init__(self,
214
 
                 deprecation_version,
215
 
                 variable_name,
216
 
                 initial_value,
217
 
                 advice,
218
 
                 ):
 
217
        deprecation_version,
 
218
        variable_name,
 
219
        initial_value,
 
220
        advice,
 
221
        ):
219
222
        """Create a dict that warns when read or modified.
220
223
 
221
224
        :param deprecation_version: string for the warning format to raise,
315
318
    """
316
319
    def cleanup():
317
320
        if filter:
318
 
            try:
319
 
                warnings.filters.remove(filter)
320
 
            except (ValueError, IndexError):
321
 
                pass
 
321
            warnings.filters.remove(filter)
322
322
    return cleanup
323
323
 
324
324