diff --git a/Makefile b/Makefile index c2ae2b26511e0355fef167a53eacb002db0f1d2a..f04d68ef2c502ea63e73fd81d395eecb92e9b8d6 100644 --- a/Makefile +++ b/Makefile @@ -11,11 +11,12 @@ CC := gcc LD := gcc PROGRAM := binary_c_python_api MAKE := /usr/bin/make -LIBS := -lbinary_c `$(BINARY_C)/binary_c-config --libs` +LIBS := -lbinary_c $(shell $(BINARY_C)/binary_c-config --libs) +#`$(BINARY_C)/binary_c-config --libdirs_list` C_SRC := binary_c_python_api.c OBJECTS := $(C_SRC:.c=.o) OBJ_FLAGS := -c -CFLAGS := -fPIC `$(BINARY_C)/binary_c-config --flags` -I$(BINARY_C)/src/ -I$(BINARY_C)/src/API +CFLAGS := -fPIC $(shell $(BINARY_C)/binary_c-config --flags) -I$(BINARY_C)/src/ -I$(BINARY_C)/src/API SO_FLAGS := -shared -o SO_NAME := libbinary_c_api.so diff --git a/README.md b/README.md index 8b8ccdff5ddb04e907f7aad3967c22507c20c1fd..98ab80fe34b1093817757a896feb58e43dfbf38f 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Based on a original work by Jeff Andrews (can be found in old_solution/ directory) updated and extended for Python3 by Robert Izzard, David hendriks -**THIS CODE IS VERY EXPERIMENTAL AND PROBABLY WILL NOT WORK** +Warning : THIS CODE IS EXPERIMENTAL! r.izzard@surrey.ac.uk http://personal.ph.surrey.ac.uk/~ri0005/binary_c.html diff --git a/binary_c_python.c b/binary_c_python.c index c4103e769fc48c7592eef903ccc3b17c14c771a6..c5cf65d475530899a6cde21276a8d222e5621fc3 100644 --- a/binary_c_python.c +++ b/binary_c_python.c @@ -184,14 +184,31 @@ static PyObject* binary_c_run_binary(PyObject *self, PyObject *args) else { char * buffer; - int nbytes; + char * error_buffer; + size_t nbytes; int out MAYBE_UNUSED = run_binary(argstring, &buffer, + &error_buffer, &nbytes); /* copy the buffer to a python string */ - PyObject * ret = Py_BuildValue("s", buffer); - free(buffer); - return ret; + PyObject * return_string = Py_BuildValue("s", buffer); + PyObject * return_error_string MAYBE_UNUSED = Py_BuildValue("s", error_buffer); + + if(error_buffer != NULL && strlen(error_buffer)>0) + { + fprintf(stderr, + "Error in binary_c run : %s\n", + error_buffer); + } + + Safe_free(buffer); + Safe_free(error_buffer); + + /* + * TODO + * return the return_error_string as well! + */ + return return_string; } } diff --git a/binary_c_python.h b/binary_c_python.h index 0122aa7885538b422f0ffe8c29420b33c94c16f6..29fbf724745e3165dbb0d1eb08dda734f8601e96 100644 --- a/binary_c_python.h +++ b/binary_c_python.h @@ -10,8 +10,9 @@ /* Binary_c's python API prototypes */ int run_binary (char * argstring, - char ** outstring, - int * nbytes); + char ** const outstring, + char ** const errorstring, + size_t * const nbytes); int run_binary_with_log (char * argstring, char ** outstring, diff --git a/binary_c_python_api.c b/binary_c_python_api.c index 511516859613aa2d1f1ae5584164f09f6355dba3..166aa6ee72d19b08477bf7532b481c66b37378c9 100644 --- a/binary_c_python_api.c +++ b/binary_c_python_api.c @@ -50,7 +50,7 @@ int stdoutwas; int main(int argc, char * argv[]) { - char * argstring = MALLOC(sizeof(char) * (size_t)STRING_LENGTH); + char * argstring = Malloc(sizeof(char) * (size_t)STRING_LENGTH); snprintf(argstring, STRING_LENGTH, "binary_c M_1 %g M_2 %g separation %g orbital_period %g metallicity %g max_evolution_time %g\n", @@ -61,10 +61,12 @@ int main(int argc, 0.02, 15000.0); - char * buffer ; - int nbytes; + char * buffer; + char * error_buffer; + size_t nbytes; int out = run_binary(argstring, &buffer, + &error_buffer, &nbytes); printf("output (binary_c returned %d)\n%s\n",out,buffer); @@ -76,8 +78,9 @@ int main(int argc, int run_binary(char * argstring, - char ** buffer, - int * nbytes) + char ** const buffer, + char ** const error_buffer, + size_t * const nbytes) { /* memory for N binary systems */ struct libbinary_c_stardata_t *stardata; @@ -91,6 +94,8 @@ int run_binary(char * argstring, &store, &argstring, -1); + printf("have new stardata %p\n",stardata);fflush(NULL); + /* disable logging */ snprintf(stardata->preferences->log_filename, STRING_LENGTH-1, @@ -101,23 +106,26 @@ int run_binary(char * argstring, "%s", "/dev/null"); /* output to strings */ - stardata->preferences->internal_buffering = 2; - stardata->preferences->internal_buffering_compression = 0; + stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE; stardata->preferences->batchmode = BATCHMODE_LIBRARY; /* do binary evolution */ binary_c_evolve_for_dt(stardata, stardata->model.max_evolution_time); - + /* get buffer pointer */ binary_c_buffer_info(stardata,buffer,nbytes); - + + /* get error buffer pointer */ + binary_c_error_buffer(stardata,error_buffer); + /* set raw_buffer_size = -1 to prevent it being freed */ stardata->tmpstore->raw_buffer_size = -1; /* free stardata (except the buffer) */ - binary_c_free_memory(&stardata,TRUE,TRUE,FALSE); + binary_c_free_memory(&stardata,TRUE,TRUE,FALSE,FALSE); binary_c_free_store_contents(store); + return 0; } diff --git a/old_solution/binary_c_api.c b/old_solution/binary_c_api.c index afd31aa6473fab49237764cce1211885802f42bd..2361e2fdae25bd826a8fc9effa65d6d0663d9762 100644 --- a/old_solution/binary_c_api.c +++ b/old_solution/binary_c_api.c @@ -105,7 +105,6 @@ int run_binary(char * argstring, "%s", "/dev/null"); stardata->preferences->internal_buffering = 2; - stardata->preferences->internal_buffering_compression = 0; stardata->preferences->batchmode = BATCHMODE_LIBRARY; binary_c_evolve_for_dt(stardata, diff --git a/setup.py b/setup.py index 0969ba532482209705f3a60fe43ad315dc664c22..e5e1062595eb77ca48c98e97833dbbcb8522fea3 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ import sys binary_c_config = os.environ['BINARY_C']+'/binary_c-config' binary_c_incdirs = subprocess.run([binary_c_config,'incdirs_list'],stdout=subprocess.PIPE).stdout.decode('utf-8').split() -binary_c_libdirs = subprocess.run([binary_c_config,'libdirs'],stdout=subprocess.PIPE).stdout.decode('utf-8').split() +binary_c_libdirs = subprocess.run([binary_c_config,'libdirs_list'],stdout=subprocess.PIPE).stdout.decode('utf-8').split() binary_c_cflags = subprocess.run([binary_c_config,'cflags'],stdout=subprocess.PIPE).stdout.decode('utf-8').split() binary_c_libs = subprocess.run([binary_c_config,'libs_list'],stdout=subprocess.PIPE).stdout.decode('utf-8').split()