1 | /* \file python_macros.h
|
---|
2 | * \brief: macros used for the python bindings
|
---|
3 | */
|
---|
4 |
|
---|
5 | #ifndef _PY_WRAPPER_MACROS_H_
|
---|
6 | #define _PY_WRAPPER_MACROS_H_
|
---|
7 |
|
---|
8 | #ifdef HAVE_CONFIG_H
|
---|
9 | #include <config.h>
|
---|
10 | #else
|
---|
11 | #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
|
---|
12 | #endif
|
---|
13 |
|
---|
14 | #ifdef _HAVE_PYTHON_
|
---|
15 | /* MODULEBOOT/MODULEEND {{{*/
|
---|
16 |
|
---|
17 | /*The following macros hide the error exception handling in a matlab module. Just put
|
---|
18 | * MODULEBOOT(); and MODULEEND(); at the beginning and end of a module, and c++ exceptions
|
---|
19 | * will be trapped*/
|
---|
20 | #define MODULEBOOT(); \
|
---|
21 | PyObject *output = PyTuple_New(NLHS); \
|
---|
22 | int nrhs = (int)PyTuple_Size(args); \
|
---|
23 | if(!output) return NULL;\
|
---|
24 | try{ \
|
---|
25 | IssmComm::SetComm();
|
---|
26 |
|
---|
27 | #define MODULEEND(); }\
|
---|
28 | catch(ErrorException &exception){\
|
---|
29 | PyErr_SetString(PyExc_TypeError,exception.PythonReport()); \
|
---|
30 | return NULL;\
|
---|
31 | } \
|
---|
32 | catch (exception &e){\
|
---|
33 | PyErr_SetString(PyExc_TypeError,exprintf("Standard exception: %s\n",e.what()));\
|
---|
34 | return NULL;\
|
---|
35 | }\
|
---|
36 | catch(...){\
|
---|
37 | PyErr_SetString(PyExc_TypeError,"An unexpected error occurred");\
|
---|
38 | return NULL;\
|
---|
39 | }\
|
---|
40 | return output;
|
---|
41 | //}}}
|
---|
42 | #if _PYTHON_MAJOR_ >=3
|
---|
43 | /* WRAPPER 3.2 {{{*/
|
---|
44 | #define WRAPPER(modulename,...) \
|
---|
45 | \
|
---|
46 | static PyObject* modulename(PyObject* self,PyObject* args);\
|
---|
47 | static PyMethodDef modulename##_funcs[] = {\
|
---|
48 | {#modulename, (PyCFunction)modulename, METH_VARARGS, ""},\
|
---|
49 | {NULL,NULL,0,NULL}\
|
---|
50 | };\
|
---|
51 | \
|
---|
52 | static struct PyModuleDef modulename##module= {\
|
---|
53 | PyModuleDef_HEAD_INIT,\
|
---|
54 | #modulename, /* name of module */\
|
---|
55 | NULL, /* module documentation, may be NULL */\
|
---|
56 | -1, /* size of per-interpreter state of the module,\
|
---|
57 | or -1 if the module keeps state in global variables. */\
|
---|
58 | modulename##_funcs\
|
---|
59 | };\
|
---|
60 | \
|
---|
61 | PyMODINIT_FUNC PyInit_##modulename(void){\
|
---|
62 | \
|
---|
63 | import_array();\
|
---|
64 | return PyModule_Create(&modulename##module);\
|
---|
65 | }\
|
---|
66 | \
|
---|
67 | static PyObject* modulename(PyObject* self,PyObject* args)
|
---|
68 | /*}}}*/
|
---|
69 | #else
|
---|
70 | /* WRAPPER 2.7 {{{*/
|
---|
71 | #define WRAPPER(modulename,...) \
|
---|
72 | \
|
---|
73 | static PyObject* modulename(PyObject* self,PyObject* args);\
|
---|
74 | static PyMethodDef modulename##_funcs[] = {\
|
---|
75 | {#modulename, (PyCFunction)modulename, METH_VARARGS, ""},\
|
---|
76 | {NULL,NULL,0,NULL}\
|
---|
77 | };\
|
---|
78 | \
|
---|
79 | PyMODINIT_FUNC init##modulename(void){\
|
---|
80 | \
|
---|
81 | import_array();\
|
---|
82 | (void) Py_InitModule(#modulename, modulename##_funcs);\
|
---|
83 | }\
|
---|
84 | \
|
---|
85 | static PyObject* modulename(PyObject* self,PyObject* args)
|
---|
86 | /*}}}*/
|
---|
87 | #endif
|
---|
88 | /* CHECKARGUMENTS {{{*/
|
---|
89 | #define CHECKARGUMENTS(NLHS,NRHS,functionpointer) CheckNumPythonArguments(args, NRHS,functionpointer)
|
---|
90 | /*}}}*/
|
---|
91 | #endif
|
---|
92 | #endif
|
---|