54
53
trace.mutter('Cleanup failed:')
55
54
trace.log_exception_quietly()
56
55
if 'cleanup' in debug.debug_flags:
57
trace.warning('brz: warning: Cleanup failed: %s', exc)
56
trace.warning('bzr: warning: Cleanup failed: %s', exc)
60
59
def _run_cleanup(func, *args, **kwargs):
79
78
_run_cleanup(func, *args, **kwargs)
82
class ObjectWithCleanups(object):
83
"""A mixin for objects that hold a cleanup list.
85
Subclass or client code can call add_cleanup and then later `cleanup_now`.
88
self.cleanups = deque()
90
def add_cleanup(self, cleanup_func, *args, **kwargs):
91
"""Add a cleanup to run.
93
Cleanups may be added at any time.
94
Cleanups will be executed in LIFO order.
96
self.cleanups.appendleft((cleanup_func, args, kwargs))
98
def cleanup_now(self):
99
_run_cleanups(self.cleanups)
100
self.cleanups.clear()
103
class OperationWithCleanups(ObjectWithCleanups):
81
class OperationWithCleanups(object):
104
82
"""A way to run some code with a dynamic cleanup list.
106
84
This provides a way to add cleanups while the function-with-cleanups is
126
104
def __init__(self, func):
127
super(OperationWithCleanups, self).__init__()
106
self.cleanups = deque()
108
def add_cleanup(self, cleanup_func, *args, **kwargs):
109
"""Add a cleanup to run.
111
Cleanups may be added at any time before or during the execution of
112
self.func. Cleanups will be executed in LIFO order.
114
self.cleanups.appendleft((cleanup_func, args, kwargs))
130
116
def run(self, *args, **kwargs):
131
117
return _do_with_cleanups(
135
121
return _do_with_cleanups(
136
122
self.cleanups, self.func, *args, **kwargs)
124
def cleanup_now(self):
125
_run_cleanups(self.cleanups)
126
self.cleanups.clear()
139
129
def _do_with_cleanups(cleanup_funcs, func, *args, **kwargs):
140
130
"""Run `func`, then call all the cleanup_funcs.
161
151
Unike `_run_cleanup`, `_do_with_cleanups` can propagate an exception from a
162
152
cleanup, but only if there is no exception from func.
154
# As correct as Python 2.4 allows.
165
156
result = func(*args, **kwargs)
167
158
# We have an exception from func already, so suppress cleanup errors.
168
159
_run_cleanups(cleanup_funcs)
170
# No exception from func, so allow first cleanup error to propgate.
171
pending_cleanups = iter(cleanup_funcs)
173
for cleanup, c_args, c_kwargs in pending_cleanups:
174
cleanup(*c_args, **c_kwargs)
176
# Still run the remaining cleanups but suppress any further errors.
177
_run_cleanups(pending_cleanups)
179
# No error, so we can return the result
162
# No exception from func, so allow the first exception from
163
# cleanup_funcs to propagate if one occurs (but only after running all
166
for cleanup, c_args, c_kwargs in cleanup_funcs:
167
# XXX: Hmm, if KeyboardInterrupt arrives at exactly this line, we
168
# won't run all cleanups... perhaps we should temporarily install a
172
cleanup(*c_args, **c_kwargs)
174
# This is the first cleanup to fail, so remember its
176
exc_info = sys.exc_info()
178
# We already have an exception to propagate, so log any errors
179
# but don't propagate them.
180
_run_cleanup(cleanup, *c_args, **kwargs)
181
if exc_info is not None:
182
raise exc_info[0], exc_info[1], exc_info[2]
183
# No error, so we can return the result