/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

  • Committer: Martin Pool
  • Date: 2009-12-14 06:06:59 UTC
  • mfrom: (4889 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4891.
  • Revision ID: mbp@sourcefrog.net-20091214060659-1ucv8hpnky0cbnaj
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
18
__all__ = ['needs_read_lock',
22
22
           ]
23
23
 
24
24
 
 
25
import sys
 
26
 
 
27
from bzrlib import trace
 
28
 
 
29
 
25
30
def _get_parameters(func):
26
31
    """Recreate the parameters for a function using introspection.
27
32
 
68
73
 
69
74
    This decorator can be applied to methods of any class with lock_read() and
70
75
    unlock() methods.
71
 
    
 
76
 
72
77
    Typical usage:
73
 
        
 
78
 
74
79
    class Branch(...):
75
80
        @needs_read_lock
76
81
        def branch_method(self, ...):
89
94
def %(name)s_read_locked(%(params)s):
90
95
    self.lock_read()
91
96
    try:
92
 
        return unbound(%(passed_params)s)
93
 
    finally:
 
97
        result = unbound(%(passed_params)s)
 
98
    except:
 
99
        import sys
 
100
        exc_info = sys.exc_info()
 
101
        try:
 
102
            self.unlock()
 
103
        finally:
 
104
            raise exc_info[0], exc_info[1], exc_info[2]
 
105
    else:
94
106
        self.unlock()
 
107
        return result
95
108
read_locked = %(name)s_read_locked
96
109
"""
97
110
    params, passed_params = _get_parameters(unbound)
113
126
 
114
127
    This decorator can be applied to methods of any class with lock_read() and
115
128
    unlock() methods.
116
 
    
 
129
 
117
130
    Typical usage:
118
 
        
 
131
 
119
132
    class Branch(...):
120
133
        @needs_read_lock
121
134
        def branch_method(self, ...):
124
137
    def read_locked(self, *args, **kwargs):
125
138
        self.lock_read()
126
139
        try:
127
 
            return unbound(self, *args, **kwargs)
128
 
        finally:
 
140
            result = unbound(self, *args, **kwargs)
 
141
        except:
 
142
            import sys
 
143
            exc_info = sys.exc_info()
 
144
            try:
 
145
                self.unlock()
 
146
            finally:
 
147
                raise exc_info[0], exc_info[1], exc_info[2]
 
148
        else:
129
149
            self.unlock()
 
150
            return result
130
151
    read_locked.__doc__ = unbound.__doc__
131
152
    read_locked.__name__ = unbound.__name__
132
153
    return read_locked
138
159
def %(name)s_write_locked(%(params)s):
139
160
    self.lock_write()
140
161
    try:
141
 
        return unbound(%(passed_params)s)
142
 
    finally:
 
162
        result = unbound(%(passed_params)s)
 
163
    except:
 
164
        import sys
 
165
        exc_info = sys.exc_info()
 
166
        try:
 
167
            self.unlock()
 
168
        finally:
 
169
            raise exc_info[0], exc_info[1], exc_info[2]
 
170
    else:
143
171
        self.unlock()
 
172
        return result
144
173
write_locked = %(name)s_write_locked
145
174
"""
146
175
    params, passed_params = _get_parameters(unbound)
162
191
    def write_locked(self, *args, **kwargs):
163
192
        self.lock_write()
164
193
        try:
165
 
            return unbound(self, *args, **kwargs)
166
 
        finally:
 
194
            result = unbound(self, *args, **kwargs)
 
195
        except:
 
196
            exc_info = sys.exc_info()
 
197
            try:
 
198
                self.unlock()
 
199
            finally:
 
200
                raise exc_info[0], exc_info[1], exc_info[2]
 
201
        else:
167
202
            self.unlock()
 
203
            return result
168
204
    write_locked.__doc__ = unbound.__doc__
169
205
    write_locked.__name__ = unbound.__name__
170
206
    return write_locked
171
207
 
172
208
 
 
209
def only_raises(*errors):
 
210
    """Make a decorator that will only allow the given error classes to be
 
211
    raised.  All other errors will be logged and then discarded.
 
212
 
 
213
    Typical use is something like::
 
214
 
 
215
        @only_raises(LockNotHeld, LockBroken)
 
216
        def unlock(self):
 
217
            # etc
 
218
    """
 
219
    def decorator(unbound):
 
220
        def wrapped(*args, **kwargs):
 
221
            try:
 
222
                return unbound(*args, **kwargs)
 
223
            except errors:
 
224
                raise
 
225
            except:
 
226
                trace.mutter('Error suppressed by only_raises:')
 
227
                trace.log_exception_quietly()
 
228
        wrapped.__doc__ = unbound.__doc__
 
229
        wrapped.__name__ = unbound.__name__
 
230
        return wrapped
 
231
    return decorator
 
232
 
 
233
 
173
234
# Default is more functionality, 'bzr' the commandline will request fast
174
235
# versions.
175
236
needs_read_lock = _pretty_needs_read_lock