def close(self, cond=True): """Close file handle given that filehandle exists (return True), otherwise do nothing (return False). If cond is False, othing is done (made in order to avoid ifs i callers) """ if not cond: return True if self._fhandle: ier = _cxtgeo.xtg_fclose(self._fhandle) if ier != 0: raise RuntimeError("Could not close C file") logger.debug("File is now closed %s", self._fhandle) if self._tmpfile: try: os.remove(self._tmpfile) except Exception as ex: # pylint: disable=W0703 logger.error( "Could not remove tempfile for some reason: %s", ex) return True return False
def cfclose(self, strict=True): """ Close SWIG C file handle by keeping track of _cfhandlecount Return True if cfhandle is really closed. """ logger.info("Request for closing SWIG fhandle no: %s", self._cfhandlecount) if self._cfhandle is None or self._cfhandlecount == 0: if strict: raise RuntimeError("Ask to close a nonexisting C file handle") self._cfhandle = None self._cfhandlecount = 0 return True if self._cfhandlecount > 1 or self._cfhandlecount == 0: self._cfhandlecount -= 1 logger.info("Remaining SWIG cfhandles: %s, do not close...", self._cfhandlecount) return False if self._memstream and self._cfhandle and "w" in self._mode: # this assures that the file pointer is in the end of the current filehandle npos = _cxtgeo.xtg_ftell(self._cfhandle) buf = bytes(npos) ier = _cxtgeo.xtg_get_fbuffer(self._cfhandle, buf) if ier == 0: self._file.write(buf) # write to bytesIO instance _cxtgeo.xtg_fflush(self._cfhandle) else: raise RuntimeError( "Could not write stream for unknown reasons") ier = _cxtgeo.xtg_fclose(self._cfhandle) if ier != 0: raise RuntimeError("Could not close C file, code {}".format(ier)) logger.info("File is now closed for C io: %s", self.name) if self._tmpfile: try: os.remove(self._tmpfile) except Exception as ex: # pylint: disable=W0703 logger.error("Could not remove tempfile for some reason: %s", ex) self._cfhandle = None self._cfhandlecount = 0 logger.info("Remaining SWIG cfhandles: %s, return is True", self._cfhandlecount) return True
def close(self, cond=True): """Close file handle given that filehandle exists (return True), otherwise do nothing (return False). If cond is False, nothing is done (made in order to avoid ifs in callers) """ logger.info("Close SWIG fhandle...") if not cond: return True if self._memstream and self._fhandle and "w" in self._mode: # this assumes that the file pointer is in the end of the current filehandle npos = _cxtgeo.xtg_ftell(self._fhandle) buf = bytes(npos) ier = _cxtgeo.xtg_get_fbuffer(self._fhandle, buf) if ier == 0: self._file.write(buf) # write to bytesIO instance _cxtgeo.xtg_fflush(self._fhandle) else: raise RuntimeError( "Could not write stream for unknown reasons") if self._fhandle: ier = _cxtgeo.xtg_fclose(self._fhandle) if ier != 0: raise RuntimeError( "Could not close C file, code {}".format(ier)) logger.debug("File is now closed %s", self._fhandle) if self._tmpfile: try: os.remove(self._tmpfile) except Exception as ex: # pylint: disable=W0703 logger.error( "Could not remove tempfile for some reason: %s", ex) return True return False