|
| 1 | +#include "Python.h" |
| 2 | +#include <dlfcn.h> |
| 3 | +#include <stdio.h> |
| 4 | + |
| 5 | +#ifdef SUFFIX |
| 6 | + #define openblas_get_config openblas_get_config64_ |
| 7 | +#endif |
| 8 | + |
| 9 | +extern const char * openblas_get_config(); |
| 10 | + |
| 11 | +PyObject * |
| 12 | +get_config(PyObject *self, PyObject *args) { |
| 13 | + const char * config = openblas_get_config(); |
| 14 | + return PyUnicode_FromString(config); |
| 15 | +} |
| 16 | + |
| 17 | +PyObject* |
| 18 | +open_so(PyObject *self, PyObject *args) { |
| 19 | + const char *utf8 = PyUnicode_AsUTF8(args); |
| 20 | + if (utf8 == NULL) { |
| 21 | + return NULL; |
| 22 | + } |
| 23 | + void *handle = dlopen(utf8, RTLD_GLOBAL | RTLD_NOW); |
| 24 | + if (handle == NULL) { |
| 25 | + PyErr_SetString(PyExc_ValueError, "Could not open SO"); |
| 26 | + return NULL; |
| 27 | + } |
| 28 | + Py_RETURN_TRUE; |
| 29 | +} |
| 30 | + |
| 31 | +static PyMethodDef InitMethods[] = { |
| 32 | + {"get_config", get_config, METH_NOARGS, |
| 33 | + "Return openblas_get_config(), see https://github.com/xianyi/OpenBLAS/wiki/OpenBLAS-Extensions"}, |
| 34 | + {"open_so", open_so, METH_O, |
| 35 | + "Use dlopen to load the shared object, which must exist"}, |
| 36 | + {NULL, NULL, 0, NULL} /* Sentinel */ |
| 37 | +}; |
| 38 | + |
| 39 | +static struct PyModuleDef initmodule = { |
| 40 | + PyModuleDef_HEAD_INIT, |
| 41 | + "_init_openblas", /* name of module */ |
| 42 | + NULL, /* module documentation, may be NULL */ |
| 43 | + -1, /* size of per-interpreter state of the module, |
| 44 | + or -1 if the module keeps state in global variables. */ |
| 45 | + InitMethods |
| 46 | +}; |
| 47 | + |
| 48 | +PyMODINIT_FUNC |
| 49 | +PyInit__init_openblas(void) |
| 50 | +{ |
| 51 | + PyObject *m; |
| 52 | + |
| 53 | + m = PyModule_Create(&initmodule); |
| 54 | + if (m == NULL) |
| 55 | + return NULL; |
| 56 | + |
| 57 | + return m; |
| 58 | +} |
0 commit comments