ECC-845: Initial attempt. Use of codes_close_file()

This commit is contained in:
Shahram Najm 2018-12-07 16:25:10 +00:00
parent 6a072cba32
commit 5d9cbb7758
3 changed files with 26 additions and 34 deletions

View File

@ -49,8 +49,8 @@ class CodesFile(io.FileIO):
"""Close all open messages, release file handle and close file."""
while self.open_messages:
self.open_messages.pop().close()
eccodes.codes_close_file(self.file_handle.fileno(), self.name)
self.file_handle.close()
eccodes.codes_close_file(self.file_handle)
#self.file_handle.close()
def __len__(self):
"""Return total number of messages in file."""

View File

@ -1273,23 +1273,7 @@ static FILE* retrieve_file_info(int fd)
//printf("\t result=NULL\n");
return NULL;
}
#if 0
static int clear_file_info(int fd)
{
//printf("clear_file_info: fd=%d\n",fd);
file_info_cache_t* curr = file_info_cache;
while(curr) {
if (curr->file_descriptor==fd) {
curr->file_descriptor=-1;
curr->file_pointer=NULL;
/*TODO: Should delete this node */
return GRIB_SUCCESS;
}
curr=curr->next;
}
return GRIB_INVALID_FILE;
}
#endif
static int clear_file_info(int fd)
{
//printf("clear_file_info: fd=%d\n",fd);
@ -1302,7 +1286,7 @@ static int clear_file_info(int fd)
//Fix previous node's 'next' to skip over the removed node
prev->next = curr->next;
}
//printf("\t Deleting entry curr (%d,%p)\n", curr->file_descriptor,curr->file_pointer);
//printf("clear_file_info:\t Deleting entry curr (%d,%p)\n", curr->file_descriptor,curr->file_pointer);
free(curr);
return GRIB_SUCCESS;
}
@ -1310,14 +1294,14 @@ static int clear_file_info(int fd)
return GRIB_INVALID_FILE;
}
#if DEBUG
static void dump_file_info()
static void dump_file_info(const char* title)
{
int i=1;
file_info_cache_t* curr = file_info_cache;
if(!curr) printf("dump_file_info: EMPTY\n");
else printf("dump_file_info:\n");
if(!curr) printf("%s: dump_file_info: EMPTY\n", title);
else printf("%s: dump_file_info:\n", title);
while(curr) {
printf("\t %d: fd=%d fp=%p\n", i++,curr->file_descriptor,curr->file_pointer);
printf("%s:\t %d: fd=%d fp=%p\n", title, i++,curr->file_descriptor,curr->file_pointer);
curr=curr->next;
}
}
@ -1326,8 +1310,12 @@ static void dump_file_info()
int codes_c_close_file(int fd, char* fname)
{
FILE* fp = retrieve_file_info(fd);
//printf("codes_c_close_file: looking for %d\n", fd);
if (fp) {
clear_file_info(fd);
//dump_file_info("codes_c_close_file: clearing cache");
//} else {
//printf("codes_c_close_file: %d NOT IN CACHE\n", fd);
}
return GRIB_SUCCESS;
}
@ -1444,17 +1432,17 @@ int grib_c_new_from_file(FILE* f, int fd, char* fname, int* gid, int headers_onl
printf("C grib_c_new_from_file: f->fileno=%d (fd=%d)\n", f->_fileno, fd);
printf("C grib_c_new_from_file: fn=%s\n", fname);
#endif
/*dump_file_info();*/
//dump_file_info("grib_c_new_from_file start");
if(f){
FILE* p = retrieve_file_info(fd);
if (p) {
/*printf("C. using CACHED value from store...%p\n",p);*/
//printf("C. using CACHED value from store...%p\n",p);
h=grib_new_from_file(0,p,headers_only,&err);//use cached value
} else {
/*printf("C. using value from ARGS...%p\n",f);*/
//printf("C. using value from ARGS...%p\n",f);
h=grib_new_from_file(0,f,headers_only,&err);//use FILE pointer passed in
store_file_info(fd, f); //store it for next time
/*dump_file_info();*/
//dump_file_info("grib_c_new_from_file First time called");
}
if(h){
@ -1463,9 +1451,9 @@ int grib_c_new_from_file(FILE* f, int fd, char* fname, int* gid, int headers_onl
} else {
*gid=-1;
if (err == GRIB_SUCCESS) {
/*printf("C grib_c_new_from_file: GRIB_END_OF_FILE\n");*/
//printf("C grib_c_new_from_file: GRIB_END_OF_FILE\n");
clear_file_info(fd);
/*dump_file_info();*/
//dump_file_info("grib_c_new_from_file EOF");
return GRIB_END_OF_FILE; //TODO: remove element from cache
} else {
/* A real error occurred */

View File

@ -297,7 +297,7 @@ def grib_new_from_file(fileobj, headers_only=False):
"""
fd = fileobj.fileno()
fn = fileobj.name
#print('Python grib_new_from_file: ', fd,' ', fn)
#print('Python gribapi.py grib_new_from_file: ', fd,' ', fn)
err, gribid = _internal.grib_c_new_from_file(fileobj, fd, fn, 0, headers_only)
if err:
if err == _internal.GRIB_END_OF_FILE:
@ -308,9 +308,13 @@ def grib_new_from_file(fileobj, headers_only=False):
return gribid
@require(fd=int, fname=str)
def codes_close_file(fd, fname):
err = _internal.codes_c_close_file(fd, fname)
@require(fileobj=file)
def codes_close_file(fileobj):
# The client must call this BEFORE calling close() on the file object
# so we can remove the entry in our cache
err = _internal.codes_c_close_file(fileobj.fileno(), fileobj.name)
# Note: it is safe calling close() here as subsequent calls will have no effect
fileobj.close()
GRIB_CHECK(err)