Newer
Older
#include <Python.h>
#include "binary_c_python.h"
/*
* binary_c/PYTHON API interface functions
*
* Remember: variables must be passed by references
* (i.e. as pointers).
*
* See apitest.py for an example of how to use these functions.
*
* See also
* http://www-h.eng.cam.ac.uk/help/tpl/languages/mixinglanguages.html
*/
/* list of variables used in the Py<>C interface */
/************************************************************/
/*
* function prototypes : these are the functions
* called by PYTHON code, without the trailing underscore.
*/
/************************************************************/
static char module_docstring[] MAYBE_UNUSED =
"This module is a python wrapper around binary_c";
#ifdef __DEPRECATED
static char create_binary_docstring[] =
"Allocate memory for a binary";
#endif
static char run_binary_docstring[] =
"Run one binary using binary_c";
static char run_binary_with_logdocstring[] =
"Run one binary using binary_c and allow the logfile to be written. Do not use for populations!";
static char run_binary_custom_loggingdocstring[] =
"TODO";
static char new_binary_system_docstring[] =
"Return an object containing a binary, ready for evolution";
static char function_prototype_docstring[] =
"The prototype for a binary_c python function";
static char return_arglines_docstring[] =
"Return the default args for a binary_c system";
static char return_help_info_docstring[] =
"Return the help info for a given parameter";
static struct libbinary_c_store_t *store = NULL;
#ifdef __DEPRECATED
static PyObject* binary_c_create_binary(PyObject *self, PyObject *args);
#endif
static PyObject* binary_c_run_binary(PyObject *self, PyObject *args);
static PyObject* binary_c_run_binary_with_logfile(PyObject *self, PyObject *args);
static PyObject* binary_c_run_binary_custom_logging(PyObject *self, PyObject *args);
static PyObject* binary_c_function_prototype(PyObject *self, PyObject *args);
static PyObject* binary_c_new_binary_system(PyObject *self, PyObject *args);
static PyObject* binary_c_return_arglines(PyObject *self, PyObject *args);
static PyObject* binary_c_return_help_info(PyObject *self, PyObject *args);
/*
* Python 3 interface is described at
*
* http://scipy-lectures.org/advanced/interfacing_with_c/interfacing_with_c.html
*/
static PyMethodDef module_methods[] = {
#ifdef __DEPRECATED
{"create_binary",
binary_c_create_binary,
METH_VARARGS,
create_binary_docstring
},
#endif
{"function_prototype", binary_c_function_prototype, METH_VARARGS, function_prototype_docstring},
{"new_system", binary_c_new_binary_system, METH_VARARGS, new_binary_system_docstring},
{"run_binary", binary_c_run_binary, METH_VARARGS, run_binary_docstring},
{"run_binary_with_logfile", binary_c_run_binary_with_logfile, METH_VARARGS, run_binary_with_logdocstring},
{"run_binary_custom_logging", binary_c_run_binary_custom_logging, METH_VARARGS, run_binary_custom_loggingdocstring},
{"return_arglines", binary_c_return_arglines, METH_VARARGS, return_arglines_docstring},
{"return_help", binary_c_return_help_info, METH_VARARGS, return_help_info_docstring},
{NULL, NULL, 0, NULL}
};
#if PY_MAJOR_VERSION >= 3
/* Python 3+ */
David Hendriks
committed
static struct PyModuleDef Py_binary_c_python_api =
{
PyModuleDef_HEAD_INIT,
"binary_c", /* name of module */
"binary_c docs", /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
module_methods
};
David Hendriks
committed
PyMODINIT_FUNC PyInit_binary_c_python_api(void)
{
David Hendriks
committed
return PyModule_Create(&Py_binary_c_python_api);
}
#else
/*
* Python pre-V3
*
* NOT TESTED THOROUGHLY!
*/
PyMODINIT_FUNC initbinary_c(void)
{
David Hendriks
committed
PyObject *m = Py_InitModule3("binary_c_python_api", module_methods, module_docstring);
if(m == NULL)
return;
}
#endif // Python version check
#ifdef __DEPRECATED
static PyObject* binary_c_create_binary(PyObject *self, PyObject *args)
{
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
double var1, var2;
char * empty_str = "";
int i;
const int N = 1;
/* Parse the input tuple */
if(!PyArg_ParseTuple(args, "dd", &var1, &var2))
return NULL;
/* Binary structures */
struct libbinary_c_stardata_t *stardata[N];
struct libbinary_c_store_t *store = NULL;
/* Allocate memory for binaries */
for(i=0;i<N;i++){
stardata[i] = NULL;
binary_c_new_system(&stardata[i], NULL, NULL, &store, &empty_str, -1);
}
/* Return the evolved binary */
PyObject *ret = Py_BuildValue("");
return ret;
}
#endif //__DEPRECATED
static PyObject* binary_c_new_binary_system(PyObject *self, PyObject *args)
{
/* Binary structures */
struct libbinary_c_stardata_t *stardata;
/* Allocate memory for binaries */
char * empty_str = "";
stardata = NULL;
binary_c_new_system(&stardata, NULL, NULL, &store, &empty_str, -1);
/* Return an object containing the stardata */
PyObject *ret = Py_BuildValue("");
return ret;
}
static PyObject* binary_c_function_prototype(PyObject *self, PyObject *args)
{
// This function is an very bare example of how a function would look like.
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
double var1, var2;
/* Parse the input tuple */
if(!PyArg_ParseTuple(args, "dd", &var1, &var2))
{
return NULL;
}
else
{
/* Return the evolved binary */
PyObject *ret = Py_BuildValue("");
return ret;
}
}
static PyObject* binary_c_run_binary(PyObject *self, PyObject *args)
{
/* Parse the input tuple */
char *argstring;
if(!PyArg_ParseTuple(args, "s", &argstring))
{
return NULL;
}
else
{
char * buffer;
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 * 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;
}
}
static PyObject* binary_c_run_binary_custom_logging(PyObject *self, PyObject *args)
{
/* Parse the input tuple */
char *argstring;
David Hendriks
committed
long int func_memaddr;
David Hendriks
committed
if(!PyArg_ParseTuple(args, "sl", &argstring, &func_memaddr))
{
return NULL;
}
else
{
char * buffer;
char * error_buffer;
size_t nbytes;
int out MAYBE_UNUSED = run_binary_custom_logging(argstring,
David Hendriks
committed
func_memaddr,
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
&buffer,
&error_buffer,
&nbytes);
/* copy the buffer to a python string */
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;
}
}
static PyObject* binary_c_run_binary_with_logfile(PyObject *self, PyObject *args)
{
/* Parse the input tuple */
char *argstring;
if(!PyArg_ParseTuple(args, "s", &argstring))
{
return NULL;
}
else
{
char * buffer;
char * error_buffer;
size_t nbytes;
int out MAYBE_UNUSED = run_binary_with_logfile(argstring,
&buffer,
&error_buffer,
&nbytes);
/* copy the buffer to a python string */
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;
}
}
static PyObject* binary_c_return_arglines(PyObject *self, PyObject *args)
{
char * buffer;
char * error_buffer;
size_t nbytes;
int out MAYBE_UNUSED = return_arglines(&buffer,
&error_buffer,
&nbytes);
/* copy the buffer to a python string */
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;
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
}
static PyObject* binary_c_return_help_info(PyObject *self, PyObject *args)
{
/* Parse the input tuple */
char *argstring;
if(!PyArg_ParseTuple(args, "s", &argstring))
{
return NULL;
}
else
{
char * buffer;
char * error_buffer;
size_t nbytes;
int out MAYBE_UNUSED = return_help_info(argstring,
&buffer,
&error_buffer,
&nbytes);
/* copy the buffer to a python string */
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);
return return_string;
}