~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/symbol_versioning.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-08-31 10:16:33 UTC
  • mfrom: (5398.1.1 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20100831101633-ta0o6c60gy2pc0as
(vila) Cleanup warnings.filters after running selftest (Vincent
        Ladeuil)

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