~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/decorators.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-09-01 08:02:42 UTC
  • mfrom: (5390.3.3 faster-revert-593560)
  • Revision ID: pqm@pqm.ubuntu.com-20100901080242-esg62ody4frwmy66
(spiv) Avoid repeatedly calling self.target.all_file_ids() in
 InterTree.iter_changes. (Andrew Bennetts)

Show diffs side-by-side

added added

removed removed

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