161
161
Unike `_run_cleanup`, `_do_with_cleanups` can propagate an exception from a
162
162
cleanup, but only if there is no exception from func.
164
# As correct as Python 2.4 allows.
166
165
result = func(*args, **kwargs)
168
167
# We have an exception from func already, so suppress cleanup errors.
169
168
_run_cleanups(cleanup_funcs)
172
# No exception from func, so allow the first exception from
173
# cleanup_funcs to propagate if one occurs (but only after running all
176
for cleanup, c_args, c_kwargs in cleanup_funcs:
177
# XXX: Hmm, if KeyboardInterrupt arrives at exactly this line, we
178
# won't run all cleanups... perhaps we should temporarily install a
182
cleanup(*c_args, **c_kwargs)
184
# This is the first cleanup to fail, so remember its
186
exc_info = sys.exc_info()
188
# We already have an exception to propagate, so log any errors
189
# but don't propagate them.
190
_run_cleanup(cleanup, *c_args, **kwargs)
191
if exc_info is not None:
193
raise exc_info[0], exc_info[1], exc_info[2]
196
# No error, so we can return the result
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