/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/decorators.py

Merge bzr.dev, update to use new hooks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
def _get_parameters(func):
31
31
    """Recreate the parameters for a function using introspection.
32
32
 
33
 
    :return: (function_params, calling_params)
 
33
    :return: (function_params, calling_params, default_values)
34
34
        function_params: is a string representing the parameters of the
35
35
            function. (such as "a, b, c=None, d=1")
36
36
            This is used in the function declaration.
37
37
        calling_params: is another string representing how you would call the
38
38
            function with the correct parameters. (such as "a, b, c=c, d=d")
39
 
            Assuming you sued function_params in the function declaration, this
 
39
            Assuming you used function_params in the function declaration, this
40
40
            is the parameters to put in the function call.
 
41
        default_values_block: a dict with the default values to be passed as
 
42
            the scope for the 'exec' statement.
41
43
 
42
44
        For example:
43
45
 
49
51
    # it globally.
50
52
    import inspect
51
53
    args, varargs, varkw, defaults = inspect.getargspec(func)
 
54
    defaults_dict = {}
 
55
    def formatvalue(value):
 
56
        default_name = '__default_%d' % len(defaults_dict)
 
57
        defaults_dict[default_name] = value
 
58
        return '=' + default_name
52
59
    formatted = inspect.formatargspec(args, varargs=varargs,
53
60
                                      varkw=varkw,
54
 
                                      defaults=defaults)
 
61
                                      defaults=defaults,
 
62
                                      formatvalue=formatvalue)
55
63
    if defaults is None:
56
64
        args_passed = args
57
65
    else:
65
73
        args_passed.append('**' + varkw)
66
74
    args_passed = ', '.join(args_passed)
67
75
 
68
 
    return formatted[1:-1], args_passed
 
76
    return formatted[1:-1], args_passed, defaults_dict
69
77
 
70
78
 
71
79
def _pretty_needs_read_lock(unbound):
101
109
        try:
102
110
            self.unlock()
103
111
        finally:
104
 
            raise exc_info[0], exc_info[1], exc_info[2]
 
112
            try:
 
113
                raise exc_info[0], exc_info[1], exc_info[2]
 
114
            finally:
 
115
                del exc_info
105
116
    else:
106
117
        self.unlock()
107
118
        return result
108
119
read_locked = %(name)s_read_locked
109
120
"""
110
 
    params, passed_params = _get_parameters(unbound)
 
121
    params, passed_params, defaults_dict = _get_parameters(unbound)
111
122
    variables = {'name':unbound.__name__,
112
123
                 'params':params,
113
124
                 'passed_params':passed_params,
114
125
                }
115
126
    func_def = template % variables
116
127
 
117
 
    exec func_def in locals()
 
128
    scope = dict(defaults_dict)
 
129
    scope['unbound'] = unbound
 
130
    exec func_def in scope
 
131
    read_locked = scope['read_locked']
118
132
 
119
133
    read_locked.__doc__ = unbound.__doc__
120
134
    read_locked.__name__ = unbound.__name__
144
158
            try:
145
159
                self.unlock()
146
160
            finally:
147
 
                raise exc_info[0], exc_info[1], exc_info[2]
 
161
                try:
 
162
                    raise exc_info[0], exc_info[1], exc_info[2]
 
163
                finally:
 
164
                    del exc_info
148
165
        else:
149
166
            self.unlock()
150
167
            return result
166
183
        try:
167
184
            self.unlock()
168
185
        finally:
169
 
            raise exc_info[0], exc_info[1], exc_info[2]
 
186
            try:
 
187
                raise exc_info[0], exc_info[1], exc_info[2]
 
188
            finally:
 
189
                del exc_info
170
190
    else:
171
191
        self.unlock()
172
192
        return result
173
193
write_locked = %(name)s_write_locked
174
194
"""
175
 
    params, passed_params = _get_parameters(unbound)
 
195
    params, passed_params, defaults_dict = _get_parameters(unbound)
176
196
    variables = {'name':unbound.__name__,
177
197
                 'params':params,
178
198
                 'passed_params':passed_params,
179
199
                }
180
200
    func_def = template % variables
181
201
 
182
 
    exec func_def in locals()
 
202
    scope = dict(defaults_dict)
 
203
    scope['unbound'] = unbound
 
204
    exec func_def in scope
 
205
    write_locked = scope['write_locked']
183
206
 
184
207
    write_locked.__doc__ = unbound.__doc__
185
208
    write_locked.__name__ = unbound.__name__
197
220
            try:
198
221
                self.unlock()
199
222
            finally:
200
 
                raise exc_info[0], exc_info[1], exc_info[2]
 
223
                try:
 
224
                    raise exc_info[0], exc_info[1], exc_info[2]
 
225
                finally:
 
226
                    del exc_info
201
227
        else:
202
228
            self.unlock()
203
229
            return result