/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

Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.

This is used to replace various ad hoc implementations of the same logic,
notably the version used in registry's _LazyObjectGetter which had a bug when
getting a module without also getting a member.  And of course, this new
function has unit tests, unlike the replaced code.

This also adds a KnownHooksRegistry subclass to provide a more natural home for
some other logic.

I'm not thrilled about the name of the new module or the new functions, but it's
hard to think of good names for such generic functionality.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007, 2008, 2009 Canonical Ltd
 
1
# Copyright (C) 2006-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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
 
 
333
    :return: A callable to remove the new warnings this added.
317
334
    """
318
 
    import warnings
319
335
    if not override and _check_for_filter(error_only=False):
320
336
        # If there is already a filter effecting suppress_deprecation_warnings,
321
337
        # then skip it.
322
 
        return
323
 
    warnings.filterwarnings('ignore', category=DeprecationWarning)
 
338
        filter = None
 
339
    else:
 
340
        warnings.filterwarnings('ignore', category=DeprecationWarning)
 
341
        filter = warnings.filters[0]
 
342
    return _remove_filter_callable(filter)
324
343
 
325
344
 
326
345
def activate_deprecation_warnings(override=True):
337
356
    :param override: If False, only add a filter if there isn't an error filter
338
357
        already. (This slightly differs from suppress_deprecation_warnings, in
339
358
        because it always overrides everything but -Werror).
 
359
 
 
360
    :return: A callable to remove the new warnings this added.
340
361
    """
341
 
    import warnings
342
362
    if not override and _check_for_filter(error_only=True):
343
363
        # DeprecationWarnings are already turned into errors, don't downgrade
344
364
        # them to 'default'.
345
 
        return
346
 
    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)