Ice Sheet System Model  4.18
Code documentation
wrapper_macros.h
Go to the documentation of this file.
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 python 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.WrapperReport()); \
30  return NULL;\
31  } \
32  catch (exception &e){\
33  PyErr_SetString(PyExc_TypeError,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 static PyObject* modulename(PyObject* self,PyObject* args);\
46 static PyMethodDef modulename##_funcs[] = {\
47  {#modulename, (PyCFunction)modulename, METH_VARARGS, ""},\
48  {NULL,NULL,0,NULL}\
49 };\
50 static struct PyModuleDef modulename##module= {\
51  PyModuleDef_HEAD_INIT,\
52  #modulename, /* name of module */\
53  NULL, /* module documentation, may be NULL */\
54  -1, /* size of per-interpreter state of the module,\
55  or -1 if the module keeps state in global variables. */\
56  modulename##_funcs\
57 };\
58 PyMODINIT_FUNC PyInit_##modulename(void){\
59  import_array();\
60  return PyModule_Create(&modulename##module);\
61 }\
62 static PyObject* modulename(PyObject* self,PyObject* args)
63 /*}}}*/
64 #else
65 /* WRAPPER 2.7 {{{*/
66 #define WRAPPER(modulename,...) \
67 static PyObject* modulename(PyObject* self,PyObject* args);\
68 static PyMethodDef modulename##_funcs[] = {\
69  {#modulename, (PyCFunction)modulename, METH_VARARGS, ""},\
70  {NULL,NULL,0,NULL}\
71 };\
72 PyMODINIT_FUNC init##modulename(void){\
73  import_array();\
74  (void) Py_InitModule(#modulename, modulename##_funcs);\
75 }\
76 static PyObject* modulename(PyObject* self,PyObject* args)
77 /*}}}*/
78 #endif
79 /* CHECKARGUMENTS {{{*/
80 #define CHECKARGUMENTS(NLHS,NRHS,functionpointer) CheckNumPythonArguments(args, NRHS,functionpointer)
81 /*}}}*/
82 #endif
83 #endif