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.""" """Close all open messages, release file handle and close file."""
while self.open_messages: while self.open_messages:
self.open_messages.pop().close() self.open_messages.pop().close()
eccodes.codes_close_file(self.file_handle.fileno(), self.name) eccodes.codes_close_file(self.file_handle)
self.file_handle.close() #self.file_handle.close()
def __len__(self): def __len__(self):
"""Return total number of messages in file.""" """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"); //printf("\t result=NULL\n");
return NULL; 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) static int clear_file_info(int fd)
{ {
//printf("clear_file_info: fd=%d\n",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 //Fix previous node's 'next' to skip over the removed node
prev->next = curr->next; 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); free(curr);
return GRIB_SUCCESS; return GRIB_SUCCESS;
} }
@ -1310,14 +1294,14 @@ static int clear_file_info(int fd)
return GRIB_INVALID_FILE; return GRIB_INVALID_FILE;
} }
#if DEBUG #if DEBUG
static void dump_file_info() static void dump_file_info(const char* title)
{ {
int i=1; int i=1;
file_info_cache_t* curr = file_info_cache; file_info_cache_t* curr = file_info_cache;
if(!curr) printf("dump_file_info: EMPTY\n"); if(!curr) printf("%s: dump_file_info: EMPTY\n", title);
else printf("dump_file_info:\n"); else printf("%s: dump_file_info:\n", title);
while(curr) { 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; curr=curr->next;
} }
} }
@ -1326,8 +1310,12 @@ static void dump_file_info()
int codes_c_close_file(int fd, char* fname) int codes_c_close_file(int fd, char* fname)
{ {
FILE* fp = retrieve_file_info(fd); FILE* fp = retrieve_file_info(fd);
//printf("codes_c_close_file: looking for %d\n", fd);
if (fp) { if (fp) {
clear_file_info(fd); 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; 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: f->fileno=%d (fd=%d)\n", f->_fileno, fd);
printf("C grib_c_new_from_file: fn=%s\n", fname); printf("C grib_c_new_from_file: fn=%s\n", fname);
#endif #endif
/*dump_file_info();*/ //dump_file_info("grib_c_new_from_file start");
if(f){ if(f){
FILE* p = retrieve_file_info(fd); FILE* p = retrieve_file_info(fd);
if (p) { 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 h=grib_new_from_file(0,p,headers_only,&err);//use cached value
} else { } 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 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 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){ if(h){
@ -1463,9 +1451,9 @@ int grib_c_new_from_file(FILE* f, int fd, char* fname, int* gid, int headers_onl
} else { } else {
*gid=-1; *gid=-1;
if (err == GRIB_SUCCESS) { 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); 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 return GRIB_END_OF_FILE; //TODO: remove element from cache
} else { } else {
/* A real error occurred */ /* A real error occurred */

View File

@ -297,7 +297,7 @@ def grib_new_from_file(fileobj, headers_only=False):
""" """
fd = fileobj.fileno() fd = fileobj.fileno()
fn = fileobj.name 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) err, gribid = _internal.grib_c_new_from_file(fileobj, fd, fn, 0, headers_only)
if err: if err:
if err == _internal.GRIB_END_OF_FILE: if err == _internal.GRIB_END_OF_FILE:
@ -308,9 +308,13 @@ def grib_new_from_file(fileobj, headers_only=False):
return gribid return gribid
@require(fd=int, fname=str) @require(fileobj=file)
def codes_close_file(fd, fname): def codes_close_file(fileobj):
err = _internal.codes_c_close_file(fd, fname) # 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) GRIB_CHECK(err)