/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: John Arbash Meinel
  • Date: 2008-06-05 16:27:16 UTC
  • mfrom: (3475 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3476.
  • Revision ID: john@arbash-meinel.com-20080605162716-a3hn238tnctbfd8j
merge bzr.dev, resolve NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
"""
22
22
 
23
23
__all__ = ['deprecated_function',
 
24
           'deprecated_in',
24
25
           'deprecated_list',
25
26
           'deprecated_method',
26
27
           'DEPRECATED_PARAMETER',
27
28
           'deprecated_passed',
28
 
           'warn', 'set_warning_method', 'zero_seven',
 
29
           'set_warning_method',
 
30
           'warn',
 
31
           'zero_seven',
29
32
           'zero_eight',
30
33
           'zero_nine',
31
34
           'zero_ten',
45
48
           'one_one',
46
49
           'one_two',
47
50
           'one_three',
 
51
           'one_four',
 
52
           'one_five',
 
53
           'one_six',
48
54
           ]
49
55
 
50
56
from warnings import warn
51
57
 
 
58
import bzrlib
 
59
 
52
60
 
53
61
DEPRECATED_PARAMETER = "A deprecated parameter marker."
54
62
zero_seven = "%s was deprecated in version 0.7."
71
79
one_one = "%s was deprecated in version 1.1."
72
80
one_two = "%s was deprecated in version 1.2."
73
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
 
74
96
 
75
97
def set_warning_method(method):
76
98
    """Set the warning method to be used by this module.
169
191
    # we cannot just forward to a new method name.I.e. in the following
170
192
    # examples we would want to have callers that pass any value to 'bad' be
171
193
    # given a warning - because we have applied:
172
 
    # @deprecated_parameter('bad', zero_seven)
 
194
    # @deprecated_parameter('bad', deprecated_in((1, 5, 0))
173
195
    #
174
196
    # def __init__(self, bad=None)
175
197
    # def __init__(self, bad, other)
237
259
        ):
238
260
        """Create a dict that warns when read or modified.
239
261
 
240
 
        :param deprecation_version: something like zero_nine
 
262
        :param deprecation_version: string for the warning format to raise,
 
263
            typically from deprecated_in()
241
264
        :param initial_value: The contents of the dict
242
265
        :param variable_name: This allows better warnings to be printed
243
266
        :param advice: String of advice on what callers should do instead 
262
285
                    initial_value, extra=None):
263
286
    """Create a list that warns when modified
264
287
 
265
 
    :param deprecation_version: something like zero_nine
 
288
    :param deprecation_version: string for the warning format to raise,
 
289
        typically from deprecated_in()
266
290
    :param initial_value: The contents of the list
267
291
    :param variable_name: This allows better warnings to be printed
268
292
    :param extra: Extra info to print when printing a warning
307
331
                return self._warn_deprecated(list.pop)
308
332
 
309
333
    return _DeprecatedList(initial_value)
 
334
 
 
335
 
 
336
def _check_for_filter(error_only):
 
337
    """Check if there is already a filter for deprecation warnings.
 
338
    
 
339
    :param error_only: Only match an 'error' filter
 
340
    :return: True if a filter is found, False otherwise
 
341
    """
 
342
    import warnings
 
343
    for filter in warnings.filters:
 
344
        if issubclass(DeprecationWarning, filter[2]):
 
345
            # This filter will effect DeprecationWarning
 
346
            if not error_only or filter[0] == 'error':
 
347
                return True
 
348
    return False
 
349
 
 
350
 
 
351
def suppress_deprecation_warnings(override=True):
 
352
    """Call this function to suppress all deprecation warnings.
 
353
 
 
354
    When this is a final release version, we don't want to annoy users with
 
355
    lots of deprecation warnings. We only want the deprecation warnings when
 
356
    running a dev or release candidate.
 
357
 
 
358
    :param override: If True, always set the ignore, if False, only set the
 
359
        ignore if there isn't already a filter.
 
360
    """
 
361
    import warnings
 
362
    if not override and _check_for_filter(error_only=False):
 
363
        # If there is already a filter effecting suppress_deprecation_warnings,
 
364
        # then skip it.
 
365
        return
 
366
    warnings.filterwarnings('ignore', category=DeprecationWarning)
 
367
 
 
368
 
 
369
def activate_deprecation_warnings(override=True):
 
370
    """Call this function to activate deprecation warnings.
 
371
 
 
372
    When running in a 'final' release we suppress deprecation warnings.
 
373
    However, the test suite wants to see them. So when running selftest, we
 
374
    re-enable the deprecation warnings.
 
375
 
 
376
    Note: warnings that have already been issued under 'ignore' will not be
 
377
    reported after this point. The 'warnings' module has already marked them as
 
378
    handled, so they don't get issued again.
 
379
 
 
380
    :param override: If False, only add a filter if there isn't an error filter
 
381
        already. (This slightly differs from suppress_deprecation_warnings, in
 
382
        because it always overrides everything but -Werror).
 
383
    """
 
384
    import warnings
 
385
    if not override and _check_for_filter(error_only=True):
 
386
        # DeprecationWarnings are already turned into errors, don't downgrade
 
387
        # them to 'default'.
 
388
        return
 
389
    warnings.filterwarnings('default', category=DeprecationWarning)