~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/symbol_versioning.py

New utility function symbol_versioning.deprecation_string. Returns the
formatted string for a callable, deprecation format pair. (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
57
57
# add that on top of the primitives, once we have all three written
58
58
# - RBC 20050105
59
59
 
 
60
 
 
61
def deprecation_string(a_callable, deprecation_version):
 
62
    """Generate an automatic deprecation string for a_callable.
 
63
 
 
64
    :param a_callable: The callable to substitute into deprecation_version.
 
65
    :param deprecation_version: A deprecation format warning string. This should
 
66
        have a single %s operator in it. a_callable will be turned into a nice
 
67
        python symbol and then substituted into deprecation_version.
 
68
    """
 
69
    if getattr(a_callable, 'im_class', None) is None:
 
70
        symbol = "%s.%s" % (a_callable.__module__,
 
71
                            a_callable.__name__)
 
72
    else:
 
73
        symbol = "%s.%s.%s" % (a_callable.im_class.__module__,
 
74
                               a_callable.im_class.__name__,
 
75
                               a_callable.__name__
 
76
                               )
 
77
    return deprecation_version % symbol
 
78
 
 
79
 
60
80
def deprecated_function(deprecation_version):
61
81
    """Decorate a function so that use of it will trigger a warning."""
62
82
 
65
85
        
66
86
        def decorated_function(*args, **kwargs):
67
87
            """This is the decorated function."""
68
 
            symbol = "%s.%s" % (callable.__module__, 
69
 
                                callable.__name__
70
 
                                )
71
 
            warn(deprecation_version % symbol, DeprecationWarning, stacklevel=2)
 
88
            warn(deprecation_string(callable, deprecation_version),
 
89
                DeprecationWarning, stacklevel=2)
72
90
            return callable(*args, **kwargs)
73
91
        _populate_decorated(callable, deprecation_version, "function",
74
92
                            decorated_function)
87
105
        
88
106
        def decorated_method(self, *args, **kwargs):
89
107
            """This is the decorated method."""
90
 
            symbol = "%s.%s.%s" % (self.__class__.__module__, 
 
108
            symbol = "%s.%s.%s" % (self.__class__.__module__,
91
109
                                   self.__class__.__name__,
92
110
                                   callable.__name__
93
111
                                   )