'Wrap the basic ``_writedata`` method to restore the ``.data``
attribute to the uncompressed image data in the case of an exception.'

def _writedata(self, fileobj):

    try:
        return super(CompImageHDU, self)._writedata(fileobj)
    finally:
        if hasattr(self, '_imagedata'):
            self.__dict__['data'] = self._imagedata
            del self._imagedata
        else:
            del self.data
