typewrappers.h 6.7 KB
Newer Older
1
/*
2
 * libvirt_wrap.h: type wrappers for libvir python bindings
3
 *
E
Eric Blake 已提交
4
 * Copyright (C) 2005, 2011-2012 Red Hat, Inc.
5 6 7 8 9
 *
 * Daniel Veillard <veillard@redhat.com>
 */

#include <Python.h>
10 11
#include "libvirt/libvirt.h"
#include "libvirt/virterror.h"
12 13

#ifdef __GNUC__
14 15 16 17 18 19
# ifdef ATTRIBUTE_UNUSED
#  undef ATTRIBUTE_UNUSED
# endif
# ifndef ATTRIBUTE_UNUSED
#  define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
# endif /* ATTRIBUTE_UNUSED */
20
#else
21
# define ATTRIBUTE_UNUSED
22 23
#endif

24
/* Work around really old python.  */
25
#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
26 27 28
typedef ssize_t Py_ssize_t;
#endif

29
#define PyvirConnect_Get(v) (((v) == Py_None) ? NULL : \
30
        (((PyvirConnect_Object *)(v))->obj))
31 32 33 34 35 36 37 38

typedef struct {
    PyObject_HEAD
    virConnectPtr obj;
} PyvirConnect_Object;


#define PyvirDomain_Get(v) (((v) == Py_None) ? NULL : \
39
        (((PyvirDomain_Object *)(v))->obj))
40 41 42 43 44 45 46

typedef struct {
    PyObject_HEAD
    virDomainPtr obj;
} PyvirDomain_Object;


47
#define PyvirNetwork_Get(v) (((v) == Py_None) ? NULL : \
48
        (((PyvirNetwork_Object *)(v))->obj))
49 50 51 52 53 54 55

typedef struct {
    PyObject_HEAD
    virNetworkPtr obj;
} PyvirNetwork_Object;


56 57 58 59 60 61 62 63 64
#define PyvirInterface_Get(v) (((v) == Py_None) ? NULL : \
        (((PyvirInterface_Object *)(v))->obj))

typedef struct {
    PyObject_HEAD
    virInterfacePtr obj;
} PyvirInterface_Object;


65
#define PyvirStoragePool_Get(v) (((v) == Py_None) ? NULL : \
66
        (((PyvirStoragePool_Object *)(v))->obj))
67 68 69 70 71 72 73 74

typedef struct {
    PyObject_HEAD
    virStoragePoolPtr obj;
} PyvirStoragePool_Object;


#define PyvirStorageVol_Get(v) (((v) == Py_None) ? NULL : \
75
        (((PyvirStorageVol_Object *)(v))->obj))
76 77 78 79 80 81

typedef struct {
    PyObject_HEAD
    virStorageVolPtr obj;
} PyvirStorageVol_Object;

82 83 84 85 86 87 88 89 90

#define PyvirNodeDevice_Get(v) (((v) == Py_None) ? NULL : \
        (((PyvirNodeDevice_Object *)(v))->obj))

typedef struct {
    PyObject_HEAD
    virNodeDevicePtr obj;
} PyvirNodeDevice_Object;

91 92 93 94 95 96 97 98
#define PyvirSecret_Get(v) (((v) == Py_None) ? NULL : \
        (((PyvirSecret_Object *)(v))->obj))

typedef struct {
    PyObject_HEAD
    virSecretPtr obj;
} PyvirSecret_Object;

99 100 101 102 103 104 105 106
#define PyvirNWFilter_Get(v) (((v) == Py_None) ? NULL : \
        (((PyvirNWFilter_Object *)(v))->obj))

typedef struct {
    PyObject_HEAD
    virNWFilterPtr obj;
} PyvirNWFilter_Object;

107

108 109 110 111 112 113 114 115 116
#define PyvirStream_Get(v) (((v) == Py_None) ? NULL : \
        (((PyvirStream_Object *)(v))->obj))

typedef struct {
    PyObject_HEAD
    virStreamPtr obj;
} PyvirStream_Object;


C
Chris Lalancette 已提交
117 118 119 120 121 122 123 124 125
#define PyvirDomainSnapshot_Get(v) (((v) == Py_None) ? NULL : \
        (((PyvirDomainSnapshot_Object *)(v))->obj))

typedef struct {
    PyObject_HEAD
    virDomainSnapshotPtr obj;
} PyvirDomainSnapshot_Object;


126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
#define PyvirEventHandleCallback_Get(v) (((v) == Py_None) ? NULL : \
        (((PyvirEventHandleCallback_Object *)(v))->obj))

typedef struct {
    PyObject_HEAD
    virEventHandleCallback obj;
} PyvirEventHandleCallback_Object;

#define PyvirEventTimeoutCallback_Get(v) (((v) == Py_None) ? NULL : \
        (((PyvirEventTimeoutCallback_Object *)(v))->obj))

typedef struct {
    PyObject_HEAD
    virEventTimeoutCallback obj;
} PyvirEventTimeoutCallback_Object;

142 143 144 145 146 147 148 149
#define PyvirFreeCallback_Get(v) (((v) == Py_None) ? NULL : \
        (((PyvirFreeCallback_Object *)(v))->obj))

typedef struct {
    PyObject_HEAD
    virFreeCallback obj;
} PyvirFreeCallback_Object;

150 151 152 153 154 155 156
#define PyvirVoidPtr_Get(v) (((v) == Py_None) ? NULL : \
        (((PyvirVoidPtr_Object *)(v))->obj))

typedef struct {
    PyObject_HEAD
    void* obj;
} PyvirVoidPtr_Object;
157

158 159
PyObject * libvirt_intWrap(int val);
PyObject * libvirt_longWrap(long val);
160
PyObject * libvirt_ulongWrap(unsigned long val);
161
PyObject * libvirt_longlongWrap(long long val);
162
PyObject * libvirt_ulonglongWrap(unsigned long long val);
163
PyObject * libvirt_charPtrWrap(char *str);
164
PyObject * libvirt_charPtrSizeWrap(char *str, Py_ssize_t size);
165 166 167
PyObject * libvirt_constcharPtrWrap(const char *str);
PyObject * libvirt_virConnectPtrWrap(virConnectPtr node);
PyObject * libvirt_virDomainPtrWrap(virDomainPtr node);
168
PyObject * libvirt_virNetworkPtrWrap(virNetworkPtr node);
169
PyObject * libvirt_virInterfacePtrWrap(virInterfacePtr node);
170 171
PyObject * libvirt_virStoragePoolPtrWrap(virStoragePoolPtr node);
PyObject * libvirt_virStorageVolPtrWrap(virStorageVolPtr node);
172 173
PyObject * libvirt_virEventHandleCallbackWrap(virEventHandleCallback node);
PyObject * libvirt_virEventTimeoutCallbackWrap(virEventTimeoutCallback node);
174
PyObject * libvirt_virFreeCallbackWrap(virFreeCallback node);
175
PyObject * libvirt_virVoidPtrWrap(void* node);
176
PyObject * libvirt_virNodeDevicePtrWrap(virNodeDevicePtr node);
177
PyObject * libvirt_virSecretPtrWrap(virSecretPtr node);
178
PyObject * libvirt_virNWFilterPtrWrap(virNWFilterPtr node);
179
PyObject * libvirt_virStreamPtrWrap(virStreamPtr node);
C
Chris Lalancette 已提交
180
PyObject * libvirt_virDomainSnapshotPtrWrap(virDomainSnapshotPtr node);
181

182 183 184 185 186 187 188

/* Provide simple macro statement wrappers (adapted from GLib, in turn from Perl):
 *  LIBVIRT_STMT_START { statements; } LIBVIRT_STMT_END;
 *  can be used as a single statement, as in
 *  if (x) LIBVIRT_STMT_START { ... } LIBVIRT_STMT_END; else ...
 *
 *  When GCC is compiling C code in non-ANSI mode, it will use the
189
 *  compiler __extension__ to wrap the statements within `({' and '})' braces.
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
 *  When compiling on platforms where configure has defined
 *  HAVE_DOWHILE_MACROS, statements will be wrapped with `do' and `while (0)'.
 *  For any other platforms (SunOS4 is known to have this issue), wrap the
 *  statements with `if (1)' and `else (void) 0'.
 */
#if !(defined (LIBVIRT_STMT_START) && defined (LIBVIRT_STMT_END))
# if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
#  define LIBVIRT_STMT_START (void) __extension__ (
#  define LIBVIRT_STMT_END )
# else /* !(__GNUC__ && !__STRICT_ANSI__ && !__cplusplus) */
#  if defined (HAVE_DOWHILE_MACROS)
#   define LIBVIRT_STMT_START do
#   define LIBVIRT_STMT_END while (0)
#  else /* !HAVE_DOWHILE_MACROS */
#   define LIBVIRT_STMT_START if (1)
#   define LIBVIRT_STMT_END else (void) 0
#  endif /* !HAVE_DOWHILE_MACROS */
# endif /* !(__GNUC__ && !__STRICT_ANSI__ && !__cplusplus) */
#endif

#define LIBVIRT_BEGIN_ALLOW_THREADS			\
  LIBVIRT_STMT_START {					\
    PyThreadState *_save = NULL;			\
    if (PyEval_ThreadsInitialized())			\
      _save = PyEval_SaveThread();

#define LIBVIRT_END_ALLOW_THREADS                           \
  if (PyEval_ThreadsInitialized())			    \
    PyEval_RestoreThread(_save);			    \
    } LIBVIRT_STMT_END

#define LIBVIRT_ENSURE_THREAD_STATE			\
  LIBVIRT_STMT_START {					\
223
    PyGILState_STATE _save = PyGILState_UNLOCKED;	\
224 225 226 227 228 229 230
    if (PyEval_ThreadsInitialized())			\
      _save = PyGILState_Ensure();

#define LIBVIRT_RELEASE_THREAD_STATE                           \
  if (PyEval_ThreadsInitialized())			       \
    PyGILState_Release(_save);				       \
  } LIBVIRT_STMT_END