~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/symbol_versioning.py

  • Committer: Vincent Ladeuil
  • Date: 2007-06-06 13:52:02 UTC
  • mto: (2485.8.44 bzr.connection.sharing)
  • mto: This revision was merged to the branch mainline in revision 2646.
  • Revision ID: v.ladeuil+lp@free.fr-20070606135202-mqhxcv6z57uce434
Fix merge multiple connections. Test suite *not* passing (sftp
refactoring pending but unrelated to merge).

* bzrlib/builtins.py:
(cmd_merge.run): Fix the multiple connections bug by reusing the
tramsport used to check for a bundle and keep all other used
transports in possible_transports.
(_merge_helper): Add a possible_transports parameter for
reuse.

* bzrlib/transport/__init__.py:
(Transport._reuse_for): By default, Transports are not reusable.
(ConnectedTransport._reuse_for): ConnectedTransports are reusable
under certain conditions.
(_urlRE): Fix misleading group name.
(_try_transport_factories): Moved after get_transport (another use
case for moved lines). The do_catching_redirections was
incorrectly inserted between get_transport and
_try_transport_factories.

* bzrlib/tests/test_transport.py:
(TestReusedTransports.test_reuse_same_transport)
(TestReusedTransports.test_don_t_reuse_different_transport): Add
more tests.

* bzrlib/merge.py:
(_get_tree, Merger.set_other): Add a possible_transports parameter
for reuse.

* bzrlib/bzrdir.py:
(BzrDir.open_containing): Add a possible_transports parameter for
reuse.

* bzrlib/branch.py:
(Branch.open_containing): Add a possible_transports parameter for
reuse.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007, 2008 Canonical Ltd
 
1
# Copyright (C) 2006, 2007 Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com> and others
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_in',
25
24
           'deprecated_list',
26
25
           'deprecated_method',
27
26
           'DEPRECATED_PARAMETER',
28
27
           'deprecated_passed',
29
 
           'set_warning_method',
30
 
           'warn',
31
 
           'zero_seven',
 
28
           'warn', 'set_warning_method', 'zero_seven',
32
29
           'zero_eight',
33
30
           'zero_nine',
34
31
           'zero_ten',
39
36
           'zero_fifteen',
40
37
           'zero_sixteen',
41
38
           'zero_seventeen',
42
 
           'zero_eighteen',
43
 
           'zero_ninety',
44
 
           'zero_ninetyone',
45
 
           'zero_ninetytwo',
46
 
           'zero_ninetythree',
47
 
           'one_zero',
48
 
           'one_one',
49
 
           'one_two',
50
 
           'one_three',
51
 
           'one_four',
52
 
           'one_five',
53
 
           'one_six',
54
39
           ]
55
40
 
56
41
from warnings import warn
57
42
 
58
 
import bzrlib
59
 
 
60
43
 
61
44
DEPRECATED_PARAMETER = "A deprecated parameter marker."
62
45
zero_seven = "%s was deprecated in version 0.7."
70
53
zero_fifteen = "%s was deprecated in version 0.15."
71
54
zero_sixteen = "%s was deprecated in version 0.16."
72
55
zero_seventeen = "%s was deprecated in version 0.17."
73
 
zero_eighteen = "%s was deprecated in version 0.18."
74
 
zero_ninety = "%s was deprecated in version 0.90."
75
 
zero_ninetyone = "%s was deprecated in version 0.91."
76
 
zero_ninetytwo = "%s was deprecated in version 0.92."
77
 
one_zero = "%s was deprecated in version 1.0."
78
 
zero_ninetythree = one_zero # Maintained for backwards compatibility
79
 
one_one = "%s was deprecated in version 1.1."
80
 
one_two = "%s was deprecated in version 1.2."
81
 
one_three = "%s was deprecated in version 1.3."
82
 
one_four = "%s was deprecated in version 1.4."
83
 
one_five = "%s was deprecated in version 1.5."
84
 
one_six = "%s was deprecated in version 1.6."
85
 
 
86
 
 
87
 
def deprecated_in(version_tuple):
88
 
    """Generate a message that something was deprecated in a release.
89
 
 
90
 
    >>> deprecated_in((1, 4, 0))
91
 
    '%s was deprecated in version 1.4.'
92
 
    """
93
 
    return ("%%s was deprecated in version %s."
94
 
            % bzrlib._format_version_tuple(version_tuple))
95
56
 
96
57
 
97
58
def set_warning_method(method):
117
78
        have a single %s operator in it. a_callable will be turned into a nice
118
79
        python symbol and then substituted into deprecation_version.
119
80
    """
120
 
    # We also want to handle old-style classes, in particular exception, and
121
 
    # they don't have an im_class attribute.
122
81
    if getattr(a_callable, 'im_class', None) is None:
123
82
        symbol = "%s.%s" % (a_callable.__module__,
124
83
                            a_callable.__name__)
138
97
        
139
98
        def decorated_function(*args, **kwargs):
140
99
            """This is the decorated function."""
141
 
            from bzrlib import trace
142
 
            trace.mutter_callsite(4, "Deprecated function called")
143
100
            warn(deprecation_string(callable, deprecation_version),
144
101
                DeprecationWarning, stacklevel=2)
145
102
            return callable(*args, **kwargs)
151
108
 
152
109
def deprecated_method(deprecation_version):
153
110
    """Decorate a method so that use of it will trigger a warning.
154
 
 
155
 
    To deprecate a static or class method, use 
156
 
 
157
 
        @staticmethod
158
 
        @deprecated_function
159
 
        def ...
160
111
    
161
112
    To deprecate an entire class, decorate __init__.
162
113
    """
166
117
        
167
118
        def decorated_method(self, *args, **kwargs):
168
119
            """This is the decorated method."""
169
 
            from bzrlib import trace
170
 
            if callable.__name__ == '__init__':
171
 
                symbol = "%s.%s" % (self.__class__.__module__,
172
 
                                    self.__class__.__name__,
173
 
                                    )
174
 
            else:
175
 
                symbol = "%s.%s.%s" % (self.__class__.__module__,
176
 
                                       self.__class__.__name__,
177
 
                                       callable.__name__
178
 
                                       )
179
 
            trace.mutter_callsite(4, "Deprecated method called")
 
120
            symbol = "%s.%s.%s" % (self.__class__.__module__,
 
121
                                   self.__class__.__name__,
 
122
                                   callable.__name__
 
123
                                   )
180
124
            warn(deprecation_version % symbol, DeprecationWarning, stacklevel=2)
181
125
            return callable(self, *args, **kwargs)
182
126
        _populate_decorated(callable, deprecation_version, "method",
195
139
    # we cannot just forward to a new method name.I.e. in the following
196
140
    # examples we would want to have callers that pass any value to 'bad' be
197
141
    # given a warning - because we have applied:
198
 
    # @deprecated_parameter('bad', deprecated_in((1, 5, 0))
 
142
    # @deprecated_parameter('bad', zero_seven)
199
143
    #
200
144
    # def __init__(self, bad=None)
201
145
    # def __init__(self, bad, other)
263
207
        ):
264
208
        """Create a dict that warns when read or modified.
265
209
 
266
 
        :param deprecation_version: string for the warning format to raise,
267
 
            typically from deprecated_in()
 
210
        :param deprecation_version: something like zero_nine
268
211
        :param initial_value: The contents of the dict
269
212
        :param variable_name: This allows better warnings to be printed
270
213
        :param advice: String of advice on what callers should do instead 
289
232
                    initial_value, extra=None):
290
233
    """Create a list that warns when modified
291
234
 
292
 
    :param deprecation_version: string for the warning format to raise,
293
 
        typically from deprecated_in()
 
235
    :param deprecation_version: something like zero_nine
294
236
    :param initial_value: The contents of the list
295
237
    :param variable_name: This allows better warnings to be printed
296
238
    :param extra: Extra info to print when printing a warning
335
277
                return self._warn_deprecated(list.pop)
336
278
 
337
279
    return _DeprecatedList(initial_value)
338
 
 
339
 
 
340
 
def _check_for_filter(error_only):
341
 
    """Check if there is already a filter for deprecation warnings.
342
 
    
343
 
    :param error_only: Only match an 'error' filter
344
 
    :return: True if a filter is found, False otherwise
345
 
    """
346
 
    import warnings
347
 
    for filter in warnings.filters:
348
 
        if issubclass(DeprecationWarning, filter[2]):
349
 
            # This filter will effect DeprecationWarning
350
 
            if not error_only or filter[0] == 'error':
351
 
                return True
352
 
    return False
353
 
 
354
 
 
355
 
def suppress_deprecation_warnings(override=True):
356
 
    """Call this function to suppress all deprecation warnings.
357
 
 
358
 
    When this is a final release version, we don't want to annoy users with
359
 
    lots of deprecation warnings. We only want the deprecation warnings when
360
 
    running a dev or release candidate.
361
 
 
362
 
    :param override: If True, always set the ignore, if False, only set the
363
 
        ignore if there isn't already a filter.
364
 
    """
365
 
    import warnings
366
 
    if not override and _check_for_filter(error_only=False):
367
 
        # If there is already a filter effecting suppress_deprecation_warnings,
368
 
        # then skip it.
369
 
        return
370
 
    warnings.filterwarnings('ignore', category=DeprecationWarning)
371
 
 
372
 
 
373
 
def activate_deprecation_warnings(override=True):
374
 
    """Call this function to activate deprecation warnings.
375
 
 
376
 
    When running in a 'final' release we suppress deprecation warnings.
377
 
    However, the test suite wants to see them. So when running selftest, we
378
 
    re-enable the deprecation warnings.
379
 
 
380
 
    Note: warnings that have already been issued under 'ignore' will not be
381
 
    reported after this point. The 'warnings' module has already marked them as
382
 
    handled, so they don't get issued again.
383
 
 
384
 
    :param override: If False, only add a filter if there isn't an error filter
385
 
        already. (This slightly differs from suppress_deprecation_warnings, in
386
 
        because it always overrides everything but -Werror).
387
 
    """
388
 
    import warnings
389
 
    if not override and _check_for_filter(error_only=True):
390
 
        # DeprecationWarnings are already turned into errors, don't downgrade
391
 
        # them to 'default'.
392
 
        return
393
 
    warnings.filterwarnings('default', category=DeprecationWarning)