2021-07-19 17:58:50 +02:00
|
|
|
%module pytrexio
|
|
|
|
/* Define SWIGWORDSIZE in order to properly align long integers on 64-bit system */
|
|
|
|
#define SWIGWORDSIZE64
|
|
|
|
%{
|
2021-07-26 18:49:26 +02:00
|
|
|
#define SWIG_FILE_WITH_INIT
|
2021-07-19 17:58:50 +02:00
|
|
|
/* Include the headers in the wrapper code */
|
|
|
|
#include "trexio_s.h"
|
2021-07-20 14:22:54 +02:00
|
|
|
#include "trexio.h"
|
2021-07-19 17:58:50 +02:00
|
|
|
%}
|
|
|
|
|
|
|
|
/* Include stdint to recognize types from stdint.h */
|
|
|
|
%include <stdint.i>
|
2021-08-06 13:04:29 +02:00
|
|
|
|
|
|
|
/* NOTE:
|
|
|
|
carrays was useful before numpy.i was introduced.
|
|
|
|
For Python interface it's better to use numpy arrays instead of carrays, because the latter are less python-ic.
|
|
|
|
On the other hand, carrays might be more portable to other target languages.
|
|
|
|
// Include carrays to work with C pointers to arrays
|
2021-07-19 17:58:50 +02:00
|
|
|
%include "carrays.i"
|
2021-08-06 13:04:29 +02:00
|
|
|
// Include classes that correspond to integer and float arrays
|
2021-07-19 17:58:50 +02:00
|
|
|
%array_class(double, doubleArray);
|
|
|
|
%array_class(float, floatArray);
|
|
|
|
%array_class(int32_t, int32Array);
|
|
|
|
%array_class(int64_t, int64Array);
|
2021-08-06 13:04:29 +02:00
|
|
|
*/
|
|
|
|
|
2021-07-19 17:58:50 +02:00
|
|
|
/* Include typemaps to play with input/output re-casting
|
|
|
|
Useful when working with C pointers
|
|
|
|
*/
|
|
|
|
%include typemaps.i
|
|
|
|
/* Redefine the int32_t* and int64_t* num to be output
|
|
|
|
Useful for TREXIO read_num functions where the
|
|
|
|
num variable is modified by address
|
|
|
|
*/
|
|
|
|
%apply int *OUTPUT { int32_t* const num};
|
|
|
|
%apply int *OUTPUT { int64_t* const num};
|
2021-08-11 15:01:36 +02:00
|
|
|
|
2021-08-06 13:04:29 +02:00
|
|
|
/* Does not work for arrays (SIGSEGV) */
|
|
|
|
|
2021-07-26 18:49:26 +02:00
|
|
|
/* This enables access to trexio_[...]_read_dset_str_low set of functions
|
|
|
|
in order to return one long string with TREXIO_DELIM delimeter as 2-nd argument of output tuple
|
|
|
|
*/
|
|
|
|
%include <cstring.i>
|
2021-08-11 15:01:36 +02:00
|
|
|
/* This enables read of long strings with TREXIO_DELIM delimeters that can be further converted into an array of string */
|
2021-08-06 13:04:29 +02:00
|
|
|
%cstring_bounded_output(char* dset_out, 4096);
|
2021-08-11 15:01:36 +02:00
|
|
|
/* This enables read of single string attributes with pre-defined max_str_len
|
|
|
|
for Python we pre-define max_str_len = PYTREXIO_MAX_STR_LENGTH everywhere for simplicity
|
|
|
|
*/
|
|
|
|
%cstring_output_maxsize(char* const str_out, const uint32_t max_str_len);
|
2021-07-26 18:49:26 +02:00
|
|
|
|
|
|
|
/* [WIP] TREXIO back ends and exit codes can be redefined in the SWIG target language
|
|
|
|
using %ignore and further #define statements (instead of disabling the type cast in the trexio.h file)
|
2021-07-19 17:58:50 +02:00
|
|
|
*/
|
2021-07-20 10:42:25 +02:00
|
|
|
/*
|
2021-07-20 12:31:21 +02:00
|
|
|
%ignore TREXIO_HDF5; // Ignore a macro in the header file
|
|
|
|
%ignore TREXIO_TEST; // Ignore a macro in the header file
|
|
|
|
#define TREXIO_HDF5 0
|
|
|
|
#define TREXIO_TEXT 0
|
2021-07-20 10:42:25 +02:00
|
|
|
*/
|
2021-07-28 15:59:29 +02:00
|
|
|
|
2021-08-06 13:04:29 +02:00
|
|
|
/* This is an attempt to make SWIG treat double * dset_out|_in, int64_t dim_out|_in pattern
|
2021-07-28 15:59:29 +02:00
|
|
|
as a special case in order to return the NumPy array to Python from C pointer to array
|
|
|
|
provided by trexio_read_safe_[dset_num] function.
|
|
|
|
NOTE: numpy.i is currently not part of SWIG but included in the numpy distribution (under numpy/tools/swig/numpy.i)
|
2021-08-06 13:04:29 +02:00
|
|
|
This means that the interface file have to be provided to SWIG during compilation either by
|
2021-07-28 15:59:29 +02:00
|
|
|
copying it to the local working directory or by providing -l/path/to/numpy.i flag upon SWIG compilation
|
|
|
|
*/
|
|
|
|
%include "numpy.i"
|
|
|
|
|
|
|
|
%init %{
|
|
|
|
import_array();
|
|
|
|
%}
|
|
|
|
|
2021-08-06 15:24:11 +02:00
|
|
|
/* Typemaps below change the type of numpy array dimensions from int to int64_t */
|
|
|
|
%numpy_typemaps(double, NPY_DOUBLE, int64_t)
|
|
|
|
%numpy_typemaps(int32_t, NPY_INT, int64_t)
|
2021-08-06 13:04:29 +02:00
|
|
|
/* Enable write|read_safe functions to convert numpy arrays from/to double arrays */
|
2021-08-06 15:24:11 +02:00
|
|
|
%apply (double* ARGOUT_ARRAY1, int64_t DIM1) {(double * const dset_out, const int64_t dim_out)};
|
|
|
|
%apply (double* IN_ARRAY1, int64_t DIM1) {(const double * dset_in, const int64_t dim_in)};
|
2021-08-06 13:04:29 +02:00
|
|
|
/* Enable write|read_safe functions to convert numpy arrays from/to int32 arrays */
|
2021-08-06 15:24:11 +02:00
|
|
|
%apply (int32_t* ARGOUT_ARRAY1, int64_t DIM1) {(int32_t * const dset_out, const int64_t dim_out)};
|
|
|
|
%apply (int32_t* IN_ARRAY1, int64_t DIM1) {(const int32_t * dset_in, const int64_t dim_in)};
|
2021-07-28 15:59:29 +02:00
|
|
|
|
2021-07-26 18:49:26 +02:00
|
|
|
/* This tells SWIG to treat char ** dset_in pattern as a special case
|
|
|
|
Enables access to trexio_[...]_write_dset_str set of functions directly, i.e.
|
|
|
|
by converting input list of strings from Python into char ** of C
|
|
|
|
*/
|
|
|
|
%typemap(in) char ** dset_in {
|
2021-07-19 17:58:50 +02:00
|
|
|
/* Check if is a list */
|
|
|
|
if (PyList_Check($input)) {
|
|
|
|
int size = PyList_Size($input);
|
|
|
|
Py_ssize_t i = 0;
|
|
|
|
$1 = (char **) malloc((size+1)*sizeof(char *));
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
PyObject *o = PyList_GetItem($input, i);
|
|
|
|
if (PyUnicode_Check(o)) {
|
|
|
|
$1[i] = PyUnicode_AsUTF8(PyList_GetItem($input,i));
|
|
|
|
} else {
|
|
|
|
PyErr_Format(PyExc_TypeError, "list must contain strings. %d/%d element was not string.", i, size);
|
|
|
|
free($1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$1[i] = 0;
|
|
|
|
} else {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "not a list");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* This cleans up the char ** array we malloc-ed before */
|
2021-07-26 18:49:26 +02:00
|
|
|
%typemap(freearg) char ** dset_in {
|
2021-07-19 17:58:50 +02:00
|
|
|
free((char *) $1);
|
|
|
|
}
|
2021-07-26 18:49:26 +02:00
|
|
|
|
2021-07-19 17:58:50 +02:00
|
|
|
/* Parse the header files to generate wrappers */
|
|
|
|
%include "trexio_s.h"
|
2021-07-20 14:22:54 +02:00
|
|
|
%include "trexio.h"
|
2021-08-06 13:04:29 +02:00
|
|
|
|