~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/decorators.py

  • Committer: Patch Queue Manager
  • Date: 2016-01-31 13:36:59 UTC
  • mfrom: (6613.1.5 1538480-match-hostname)
  • Revision ID: pqm@pqm.ubuntu.com-20160131133659-ouy92ee2wlv9xz8m
(vila) Use ssl.match_hostname instead of our own. (Vincent Ladeuil)

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