Reliable destruction

Benjamin Niemann pink at odahoda.de
Thu Aug 4 13:13:50 EDT 2005


Pierre-Eric.Melchy at uni-konstanz.de wrote:

> Hello Benjamin,
> 
> What would happen if an exception was thrown in the middle of setup()?
> tearDown could not handle this case without having a list of the
> objects already constructed (Or I would have to rely on the automatic
> call to __del__, if it is reliable).

class measurement:
    def __init__(self):
        self.powerSupply = None
        ...

    def setup(self):
        self.powerSupply=apparate.PowerSupply()
        ...

    def tearDown(self):
        if self.powerSupply is not None:
            try:
                self.powerSupply.tearDown()
            except:
                # Exception in powerSupply.tearDown() should not stop
                # the following tearDown()s from being executed
                traceback.print_exc()

        ...


> There is still some problem:
> Imagine a communication error in run() which would cause del to fail on
> the instrument.

Not really sure, if I understand what you mean? Does my tearDown() above
covers this?

> Anyway, I think this case is still more difficult to handle.

Reliable, fail-safe software *is* hard to design and implement, that's for
sure..
Be happy that it's just a power supply that could overheat and not the core
of a nuclear power plant.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/



More information about the Python-list mailing list