diff --git a/addons/document/content_index.py b/addons/document/content_index.py index df2b12b5456..6d49a29873f 100644 --- a/addons/document/content_index.py +++ b/addons/document/content_index.py @@ -71,8 +71,10 @@ class indexer(object): pass fp = open(realfile,'rb') - content2 = fp.read() - fp.close() + try: + content2 = fp.read() + finally: + fp.close() # The not-handled exception may be raised here return self._doIndexContent(content2) @@ -164,8 +166,10 @@ class contentIndex(object): os.close(fd) fp = Popen(['file','-b','--mime',fname], shell=False, stdout=PIPE).stdout - result = fp.read() - fp.close() + try: + result = fp.read() + finally: + fp.close() mime2 = result.split(';')[0] self.__logger.debug('File gave us: %s', mime2) # Note that the temporary file still exists now. diff --git a/addons/document/document_storage.py b/addons/document/document_storage.py index 0ecc01941b4..467a1d7ff31 100644 --- a/addons/document/document_storage.py +++ b/addons/document/document_storage.py @@ -541,9 +541,11 @@ class document_storage(osv.osv): try: store_fname = self.__get_random_fname(path) fname = os.path.join(path, store_fname) - fp = file(fname, 'wb') - fp.write(data) - fp.close() + fp = open(fname, 'wb') + try: + fp.write(data) + finally: + fp.close() self._doclog.debug( "Saved data to %s" % fname) filesize = len(data) # os.stat(fname).st_size @@ -568,9 +570,11 @@ class document_storage(osv.osv): try: path, npath = self.__prepare_realpath(cr, file_node, ira, boo.path, do_create=True) fname = os.path.join(path, npath[-1]) - fp = file(fname,'wb') - fp.write(data) - fp.close() + fp = open(fname,'wb') + try: + fp.write(data) + finally: + fp.close() self._doclog.debug("Saved data to %s", fname) filesize = len(data) # os.stat(fname).st_size store_fname = os.path.join(*npath)