- while len(self._devices):
- device = self._devices.pop()
- device.destroy()
-
- while len(self._cleanup_jobs):
- job, args = self._cleanup_jobs.pop()
- job(*args)
+ try:
+ while len(self._devices):
+ device = self._devices.pop()
+ device.destroy()
+ finally:
+ # Make sure those are executed even if one of the device.destroy
+ # methods throws exeptions.
+ while len(self._cleanup_jobs):
+ job, args = self._cleanup_jobs.pop()
+ job(*args)