Propagate libvirt errors back with python exceptions
[libvirt-python/ericb.git] / libvirt_wrap.h
blob906245aeee1b3ab29d0a4d43ef9fb82ebd5e0dc7
1 /*
2 * libvirt_wrap.h: type wrappers for libvir python bindings
4 * Copyright (C) 2005 Red Hat, Inc.
6 * Daniel Veillard <veillard@redhat.com>
7 */
9 #include <Python.h>
10 #include <libvirt/libvirt.h>
11 #include <libvirt/virterror.h>
13 #ifdef __GNUC__
14 #ifdef ATTRIBUTE_UNUSED
15 #undef ATTRIBUTE_UNUSED
16 #endif
17 #ifndef ATTRIBUTE_UNUSED
18 #define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
19 #endif /* ATTRIBUTE_UNUSED */
20 #else
21 #define ATTRIBUTE_UNUSED
22 #endif
24 #define PyvirConnect_Get(v) (((v) == Py_None) ? NULL : \
25 (((PyvirConnect_Object *)(v))->obj))
27 typedef struct {
28 PyObject_HEAD
29 virConnectPtr obj;
30 } PyvirConnect_Object;
33 #define PyvirDomain_Get(v) (((v) == Py_None) ? NULL : \
34 (((PyvirDomain_Object *)(v))->obj))
36 typedef struct {
37 PyObject_HEAD
38 virDomainPtr obj;
39 } PyvirDomain_Object;
42 PyObject * libvirt_intWrap(int val);
43 PyObject * libvirt_longWrap(long val);
44 PyObject * libvirt_longlongWrap(long long val);
45 PyObject * libvirt_charPtrWrap(char *str);
46 PyObject * libvirt_constcharPtrWrap(const char *str);
47 PyObject * libvirt_charPtrConstWrap(const char *str);
48 PyObject * libvirt_virConnectPtrWrap(virConnectPtr node);
49 PyObject * libvirt_virDomainPtrWrap(virDomainPtr node);
52 /* Provide simple macro statement wrappers (adapted from GLib, in turn from Perl):
53 * LIBVIRT_STMT_START { statements; } LIBVIRT_STMT_END;
54 * can be used as a single statement, as in
55 * if (x) LIBVIRT_STMT_START { ... } LIBVIRT_STMT_END; else ...
57 * When GCC is compiling C code in non-ANSI mode, it will use the
58 * compiler __extension__ to wrap the statements wihin `({' and '})' braces.
59 * When compiling on platforms where configure has defined
60 * HAVE_DOWHILE_MACROS, statements will be wrapped with `do' and `while (0)'.
61 * For any other platforms (SunOS4 is known to have this issue), wrap the
62 * statements with `if (1)' and `else (void) 0'.
64 #if !(defined (LIBVIRT_STMT_START) && defined (LIBVIRT_STMT_END))
65 # if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
66 # define LIBVIRT_STMT_START (void) __extension__ (
67 # define LIBVIRT_STMT_END )
68 # else /* !(__GNUC__ && !__STRICT_ANSI__ && !__cplusplus) */
69 # if defined (HAVE_DOWHILE_MACROS)
70 # define LIBVIRT_STMT_START do
71 # define LIBVIRT_STMT_END while (0)
72 # else /* !HAVE_DOWHILE_MACROS */
73 # define LIBVIRT_STMT_START if (1)
74 # define LIBVIRT_STMT_END else (void) 0
75 # endif /* !HAVE_DOWHILE_MACROS */
76 # endif /* !(__GNUC__ && !__STRICT_ANSI__ && !__cplusplus) */
77 #endif
79 #define LIBVIRT_BEGIN_ALLOW_THREADS \
80 LIBVIRT_STMT_START { \
81 PyThreadState *_save = NULL; \
82 if (PyEval_ThreadsInitialized()) \
83 _save = PyEval_SaveThread();
85 #define LIBVIRT_END_ALLOW_THREADS \
86 if (PyEval_ThreadsInitialized()) \
87 PyEval_RestoreThread(_save); \
88 } LIBVIRT_STMT_END
90 #define LIBVIRT_ENSURE_THREAD_STATE \
91 LIBVIRT_STMT_START { \
92 PyGILState_STATE _save; \
93 if (PyEval_ThreadsInitialized()) \
94 _save = PyGILState_Ensure();
96 #define LIBVIRT_RELEASE_THREAD_STATE \
97 if (PyEval_ThreadsInitialized()) \
98 PyGILState_Release(_save); \
99 } LIBVIRT_STMT_END