~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/symbol_versioning.py

Optimize Tree._iter_changes with specific file_ids

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 by Canonical Ltd
 
1
# Copyright (C) 2006 Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
21
21
"""
22
22
 
23
23
__all__ = ['deprecated_function',
 
24
           'deprecated_list',
24
25
           'deprecated_method',
25
26
           'DEPRECATED_PARAMETER',
26
27
           'deprecated_passed',
27
28
           'warn', 'set_warning_method', 'zero_seven',
28
29
           'zero_eight',
29
30
           'zero_nine',
 
31
           'zero_ten',
 
32
           'zero_eleven',
 
33
           'zero_twelve',
 
34
           'zero_thirteen',
 
35
           'zero_fourteen',
30
36
           ]
31
37
 
32
38
from warnings import warn
36
42
zero_seven = "%s was deprecated in version 0.7."
37
43
zero_eight = "%s was deprecated in version 0.8."
38
44
zero_nine = "%s was deprecated in version 0.9."
 
45
zero_ten = "%s was deprecated in version 0.10."
 
46
zero_eleven = "%s was deprecated in version 0.11."
 
47
zero_twelve = "%s was deprecated in version 0.12."
 
48
zero_thirteen = "%s was deprecated in version 0.13."
 
49
zero_fourteen = "%s was deprecated in version 0.14."
39
50
 
40
51
 
41
52
def set_warning_method(method):
52
63
# add that on top of the primitives, once we have all three written
53
64
# - RBC 20050105
54
65
 
 
66
 
 
67
def deprecation_string(a_callable, deprecation_version):
 
68
    """Generate an automatic deprecation string for a_callable.
 
69
 
 
70
    :param a_callable: The callable to substitute into deprecation_version.
 
71
    :param deprecation_version: A deprecation format warning string. This should
 
72
        have a single %s operator in it. a_callable will be turned into a nice
 
73
        python symbol and then substituted into deprecation_version.
 
74
    """
 
75
    if getattr(a_callable, 'im_class', None) is None:
 
76
        symbol = "%s.%s" % (a_callable.__module__,
 
77
                            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
    return deprecation_version % symbol
 
84
 
 
85
 
55
86
def deprecated_function(deprecation_version):
56
87
    """Decorate a function so that use of it will trigger a warning."""
57
88
 
60
91
        
61
92
        def decorated_function(*args, **kwargs):
62
93
            """This is the decorated function."""
63
 
            symbol = "%s.%s" % (callable.__module__, 
64
 
                                callable.__name__
65
 
                                )
66
 
            warn(deprecation_version % symbol, DeprecationWarning, stacklevel=2)
 
94
            warn(deprecation_string(callable, deprecation_version),
 
95
                DeprecationWarning, stacklevel=2)
67
96
            return callable(*args, **kwargs)
68
97
        _populate_decorated(callable, deprecation_version, "function",
69
98
                            decorated_function)
82
111
        
83
112
        def decorated_method(self, *args, **kwargs):
84
113
            """This is the decorated method."""
85
 
            symbol = "%s.%s.%s" % (self.__class__.__module__, 
 
114
            symbol = "%s.%s.%s" % (self.__class__.__module__,
86
115
                                   self.__class__.__name__,
87
116
                                   callable.__name__
88
117
                                   )
145
174
    decorated_callable.__module__ = callable.__module__
146
175
    decorated_callable.__name__ = callable.__name__
147
176
    decorated_callable.is_deprecated = True
 
177
 
 
178
 
 
179
def deprecated_list(deprecation_version, variable_name,
 
180
                    initial_value, extra=None):
 
181
    """Create a list that warns when modified
 
182
 
 
183
    :param deprecation_version: something like zero_nine
 
184
    :param initial_value: The contents of the list
 
185
    :param variable_name: This allows better warnings to be printed
 
186
    :param extra: Extra info to print when printing a warning
 
187
    """
 
188
 
 
189
    subst_text = 'Modifying %s' % (variable_name,)
 
190
    msg = deprecation_version % (subst_text,)
 
191
    if extra:
 
192
        msg += ' ' + extra
 
193
 
 
194
    class _DeprecatedList(list):
 
195
        __doc__ = list.__doc__ + msg
 
196
 
 
197
        is_deprecated = True
 
198
 
 
199
        def _warn_deprecated(self, func, *args, **kwargs):
 
200
            warn(msg, DeprecationWarning, stacklevel=3)
 
201
            return func(self, *args, **kwargs)
 
202
            
 
203
        def append(self, obj):
 
204
            """appending to %s is deprecated""" % (variable_name,)
 
205
            return self._warn_deprecated(list.append, obj)
 
206
 
 
207
        def insert(self, index, obj):
 
208
            """inserting to %s is deprecated""" % (variable_name,)
 
209
            return self._warn_deprecated(list.insert, index, obj)
 
210
 
 
211
        def extend(self, iterable):
 
212
            """extending %s is deprecated""" % (variable_name,)
 
213
            return self._warn_deprecated(list.extend, iterable)
 
214
 
 
215
        def remove(self, value):
 
216
            """removing from %s is deprecated""" % (variable_name,)
 
217
            return self._warn_deprecated(list.remove, value)
 
218
 
 
219
        def pop(self, index=None):
 
220
            """pop'ing from from %s is deprecated""" % (variable_name,)
 
221
            if index:
 
222
                return self._warn_deprecated(list.pop, index)
 
223
            else:
 
224
                # Can't pass None
 
225
                return self._warn_deprecated(list.pop)
 
226
 
 
227
    return _DeprecatedList(initial_value)