~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/symbol_versioning.py

  • Committer: Andrew Bennetts
  • Date: 2010-08-31 07:12:18 UTC
  • mto: This revision was merged to the branch mainline in revision 5401.
  • Revision ID: andrew.bennetts@canonical.com-20100831071218-4kjieu3ejqcdmdom
Use has_id rather than __contains__; expand NEWS entry; add What's New entry.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
           'warn',
30
30
           ]
31
31
 
32
 
 
33
 
import warnings
34
 
# Import the 'warn' symbol so bzrlib can call it even if we redefine it
35
32
from warnings import warn
36
33
 
37
34
import bzrlib
299
296
    :param error_only: Only match an 'error' filter
300
297
    :return: True if a filter is found, False otherwise
301
298
    """
 
299
    import warnings
302
300
    for filter in warnings.filters:
303
301
        if issubclass(DeprecationWarning, filter[2]):
304
302
            # This filter will effect DeprecationWarning
307
305
    return False
308
306
 
309
307
 
310
 
def _remove_filter_callable(filter):
311
 
    """Build and returns a callable removing filter from the warnings.
312
 
 
313
 
    :param filter: The filter to remove (can be None).
314
 
 
315
 
    :return: A callable that will remove filter from warnings.filters.
316
 
    """
317
 
    def cleanup():
318
 
        if filter:
319
 
            warnings.filters.remove(filter)
320
 
    return cleanup
321
 
 
322
 
 
323
308
def suppress_deprecation_warnings(override=True):
324
309
    """Call this function to suppress all deprecation warnings.
325
310
 
329
314
 
330
315
    :param override: If True, always set the ignore, if False, only set the
331
316
        ignore if there isn't already a filter.
332
 
 
333
317
    :return: A callable to remove the new warnings this added.
334
318
    """
 
319
    import warnings
335
320
    if not override and _check_for_filter(error_only=False):
336
321
        # If there is already a filter effecting suppress_deprecation_warnings,
337
322
        # then skip it.
338
 
        filter = None
339
 
    else:
340
 
        warnings.filterwarnings('ignore', category=DeprecationWarning)
341
 
        filter = warnings.filters[0]
342
 
    return _remove_filter_callable(filter)
 
323
        return
 
324
    warnings.filterwarnings('ignore', category=DeprecationWarning)
 
325
    filter = warnings.filters[0]
 
326
    def cleanup():
 
327
        warnings.filters.remove(filter)
 
328
    return cleanup
343
329
 
344
330
 
345
331
def activate_deprecation_warnings(override=True):
356
342
    :param override: If False, only add a filter if there isn't an error filter
357
343
        already. (This slightly differs from suppress_deprecation_warnings, in
358
344
        because it always overrides everything but -Werror).
359
 
 
360
 
    :return: A callable to remove the new warnings this added.
361
345
    """
 
346
    import warnings
362
347
    if not override and _check_for_filter(error_only=True):
363
348
        # DeprecationWarnings are already turned into errors, don't downgrade
364
349
        # them to 'default'.
365
 
        filter = None
366
 
    else:
367
 
        warnings.filterwarnings('default', category=DeprecationWarning)
368
 
        filter = warnings.filters[0]
369
 
    return _remove_filter_callable(filter)
 
350
        return
 
351
    warnings.filterwarnings('default', category=DeprecationWarning)