viralloc: Report OOM error on failure
[libvirt-python/ericb.git] / typewrappers.c
blob9ba8790c42eca9202b9d709d73f3461d6ecc3d60
1 /*
2 * types.c: converter functions between the internal representation
3 * and the Python objects
5 * Copyright (C) 2005, 2007, 2012 Red Hat, Inc.
7 * Daniel Veillard <veillard@redhat.com>
8 */
10 #include <config.h>
12 /* Horrible kludge to work around even more horrible name-space pollution
13 * via Python.h. That file includes /usr/include/python2.5/pyconfig*.h,
14 * which has over 180 autoconf-style HAVE_* definitions. Shame on them. */
15 #undef HAVE_PTHREAD_H
17 #include "typewrappers.h"
19 #include "viralloc.h"
21 #ifndef Py_CAPSULE_H
22 typedef void(*PyCapsule_Destructor)(void *, void *);
23 #endif
25 static PyObject *
26 libvirt_buildPyObject(void *cobj,
27 const char *name,
28 PyCapsule_Destructor destr)
30 PyObject *ret;
32 #ifdef Py_CAPSULE_H
33 ret = PyCapsule_New(cobj, name, destr);
34 #else
35 ret = PyCObject_FromVoidPtrAndDesc(cobj, (void *) name, destr);
36 #endif /* _TEST_CAPSULE */
38 return ret;
41 PyObject *
42 libvirt_intWrap(int val)
44 PyObject *ret;
45 ret = PyInt_FromLong((long) val);
46 return ret;
49 PyObject *
50 libvirt_longWrap(long val)
52 PyObject *ret;
53 ret = PyInt_FromLong(val);
54 return ret;
57 PyObject *
58 libvirt_ulongWrap(unsigned long val)
60 PyObject *ret;
61 ret = PyLong_FromLong(val);
62 return ret;
65 PyObject *
66 libvirt_longlongWrap(long long val)
68 PyObject *ret;
69 ret = PyLong_FromUnsignedLongLong((unsigned long long) val);
70 return ret;
73 PyObject *
74 libvirt_ulonglongWrap(unsigned long long val)
76 PyObject *ret;
77 ret = PyLong_FromUnsignedLongLong(val);
78 return ret;
81 PyObject *
82 libvirt_charPtrSizeWrap(char *str, Py_ssize_t size)
84 PyObject *ret;
86 if (str == NULL) {
87 Py_INCREF(Py_None);
88 return Py_None;
90 ret = PyString_FromStringAndSize(str, size);
91 VIR_FREE(str);
92 return ret;
95 PyObject *
96 libvirt_charPtrWrap(char *str)
98 PyObject *ret;
100 if (str == NULL) {
101 Py_INCREF(Py_None);
102 return Py_None;
104 ret = PyString_FromString(str);
105 VIR_FREE(str);
106 return ret;
109 PyObject *
110 libvirt_constcharPtrWrap(const char *str)
112 PyObject *ret;
114 if (str == NULL) {
115 Py_INCREF(Py_None);
116 return Py_None;
118 ret = PyString_FromString(str);
119 return ret;
123 libvirt_intUnwrap(PyObject *obj, int *val)
125 long long_val;
127 if (!obj) {
128 PyErr_SetString(PyExc_TypeError, "unexpected type");
129 return -1;
132 /* If obj is type of PyInt_Type, PyInt_AsLong converts it
133 * to C long type directly. If it is of PyLong_Type, PyInt_AsLong
134 * will call PyLong_AsLong() to deal with it automatically.
136 long_val = PyInt_AsLong(obj);
137 if ((long_val == -1) && PyErr_Occurred())
138 return -1;
140 #if LONG_MAX != INT_MAX
141 if (long_val >= INT_MIN && long_val <= INT_MAX) {
142 *val = long_val;
143 } else {
144 PyErr_SetString(PyExc_OverflowError,
145 "Python int too large to convert to C int");
146 return -1;
148 #else
149 *val = long_val;
150 #endif
151 return 0;
155 libvirt_uintUnwrap(PyObject *obj, unsigned int *val)
157 long long_val;
159 if (!obj) {
160 PyErr_SetString(PyExc_TypeError, "unexpected type");
161 return -1;
164 long_val = PyInt_AsLong(obj);
165 if ((long_val == -1) && PyErr_Occurred())
166 return -1;
168 if (long_val >= 0 && long_val <= UINT_MAX) {
169 *val = long_val;
170 } else {
171 PyErr_SetString(PyExc_OverflowError,
172 "Python int too large to convert to C unsigned int");
173 return -1;
175 return 0;
179 libvirt_longUnwrap(PyObject *obj, long *val)
181 long long_val;
183 if (!obj) {
184 PyErr_SetString(PyExc_TypeError, "unexpected type");
185 return -1;
188 long_val = PyInt_AsLong(obj);
189 if ((long_val == -1) && PyErr_Occurred())
190 return -1;
192 *val = long_val;
193 return 0;
197 libvirt_ulongUnwrap(PyObject *obj, unsigned long *val)
199 long long_val;
201 if (!obj) {
202 PyErr_SetString(PyExc_TypeError, "unexpected type");
203 return -1;
206 long_val = PyInt_AsLong(obj);
207 if ((long_val == -1) && PyErr_Occurred())
208 return -1;
210 if (long_val >= 0) {
211 *val = long_val;
212 } else {
213 PyErr_SetString(PyExc_OverflowError,
214 "negative Python int cannot be converted to C unsigned long");
215 return -1;
217 return 0;
221 libvirt_longlongUnwrap(PyObject *obj, long long *val)
223 long long llong_val = -1;
225 if (!obj) {
226 PyErr_SetString(PyExc_TypeError, "unexpected type");
227 return -1;
230 /* If obj is of PyInt_Type, PyLong_AsLongLong
231 * will call PyInt_AsLong() to handle it automatically.
233 if (PyInt_Check(obj) || PyLong_Check(obj))
234 llong_val = PyLong_AsLongLong(obj);
235 else
236 PyErr_SetString(PyExc_TypeError, "an integer is required");
238 if ((llong_val == -1) && PyErr_Occurred())
239 return -1;
241 *val = llong_val;
242 return 0;
246 libvirt_ulonglongUnwrap(PyObject *obj, unsigned long long *val)
248 unsigned long long ullong_val = -1;
249 long long llong_val;
251 if (!obj) {
252 PyErr_SetString(PyExc_TypeError, "unexpected type");
253 return -1;
256 /* The PyLong_AsUnsignedLongLong doesn't check the type of
257 * obj, only accept argument of PyLong_Type, so we check it instead.
259 if (PyInt_Check(obj)) {
260 llong_val = PyInt_AsLong(obj);
261 if (llong_val < 0)
262 PyErr_SetString(PyExc_OverflowError,
263 "negative Python int cannot be converted to C unsigned long long");
264 else
265 ullong_val = llong_val;
266 } else if (PyLong_Check(obj)) {
267 ullong_val = PyLong_AsUnsignedLongLong(obj);
268 } else {
269 PyErr_SetString(PyExc_TypeError, "an integer is required");
272 if ((ullong_val == -1) && PyErr_Occurred())
273 return -1;
275 *val = ullong_val;
276 return 0;
280 libvirt_doubleUnwrap(PyObject *obj, double *val)
282 double double_val;
284 if (!obj) {
285 PyErr_SetString(PyExc_TypeError, "unexpected type");
286 return -1;
289 double_val = PyFloat_AsDouble(obj);
290 if ((double_val == -1) && PyErr_Occurred())
291 return -1;
293 *val = double_val;
294 return 0;
298 libvirt_boolUnwrap(PyObject *obj, bool *val)
300 int ret;
302 if (!obj) {
303 PyErr_SetString(PyExc_TypeError, "unexpected type");
304 return -1;
307 if ((ret = PyObject_IsTrue(obj)) < 0)
308 return ret;
310 *val = ret > 0;
311 return 0;
314 PyObject *
315 libvirt_virDomainPtrWrap(virDomainPtr node)
317 PyObject *ret;
319 if (node == NULL) {
320 Py_INCREF(Py_None);
321 return Py_None;
324 ret = libvirt_buildPyObject(node, "virDomainPtr", NULL);
325 return ret;
328 PyObject *
329 libvirt_virNetworkPtrWrap(virNetworkPtr node)
331 PyObject *ret;
333 if (node == NULL) {
334 Py_INCREF(Py_None);
335 return Py_None;
338 ret = libvirt_buildPyObject(node, "virNetworkPtr", NULL);
339 return ret;
342 PyObject *
343 libvirt_virInterfacePtrWrap(virInterfacePtr node)
345 PyObject *ret;
347 if (node == NULL) {
348 Py_INCREF(Py_None);
349 return Py_None;
352 ret = libvirt_buildPyObject(node, "virInterfacePtr", NULL);
353 return ret;
356 PyObject *
357 libvirt_virStoragePoolPtrWrap(virStoragePoolPtr node)
359 PyObject *ret;
361 if (node == NULL) {
362 Py_INCREF(Py_None);
363 return Py_None;
366 ret = libvirt_buildPyObject(node, "virStoragePoolPtr", NULL);
367 return ret;
370 PyObject *
371 libvirt_virStorageVolPtrWrap(virStorageVolPtr node)
373 PyObject *ret;
375 if (node == NULL) {
376 Py_INCREF(Py_None);
377 return Py_None;
380 ret = libvirt_buildPyObject(node, "virStorageVolPtr", NULL);
381 return ret;
384 PyObject *
385 libvirt_virConnectPtrWrap(virConnectPtr node)
387 PyObject *ret;
389 if (node == NULL) {
390 Py_INCREF(Py_None);
391 return Py_None;
394 ret = libvirt_buildPyObject(node, "virConnectPtr", NULL);
395 return ret;
398 PyObject *
399 libvirt_virNodeDevicePtrWrap(virNodeDevicePtr node)
401 PyObject *ret;
403 if (node == NULL) {
404 Py_INCREF(Py_None);
405 return Py_None;
408 ret = libvirt_buildPyObject(node, "virNodeDevicePtr", NULL);
409 return ret;
412 PyObject *
413 libvirt_virSecretPtrWrap(virSecretPtr node)
415 PyObject *ret;
417 if (node == NULL) {
418 Py_INCREF(Py_None);
419 return Py_None;
422 ret = libvirt_buildPyObject(node, "virSecretPtr", NULL);
423 return ret;
426 PyObject *
427 libvirt_virNWFilterPtrWrap(virNWFilterPtr node)
429 PyObject *ret;
431 if (node == NULL) {
432 Py_INCREF(Py_None);
433 return Py_None;
436 ret = libvirt_buildPyObject(node, "virNWFilterPtr", NULL);
437 return ret;
440 PyObject *
441 libvirt_virStreamPtrWrap(virStreamPtr node)
443 PyObject *ret;
445 if (node == NULL) {
446 Py_INCREF(Py_None);
447 return Py_None;
450 ret = libvirt_buildPyObject(node, "virStreamPtr", NULL);
451 return ret;
454 PyObject *
455 libvirt_virDomainSnapshotPtrWrap(virDomainSnapshotPtr node)
457 PyObject *ret;
459 if (node == NULL) {
460 Py_INCREF(Py_None);
461 return Py_None;
464 ret = libvirt_buildPyObject(node, "virDomainSnapshotPtr", NULL);
465 return ret;
468 PyObject *
469 libvirt_virEventHandleCallbackWrap(virEventHandleCallback node)
471 PyObject *ret;
473 if (node == NULL) {
474 Py_INCREF(Py_None);
475 printf("%s: WARNING - Wrapping None\n", __func__);
476 return Py_None;
479 ret = libvirt_buildPyObject(node, "virEventHandleCallback", NULL);
480 return ret;
483 PyObject *
484 libvirt_virEventTimeoutCallbackWrap(virEventTimeoutCallback node)
486 PyObject *ret;
488 if (node == NULL) {
489 printf("%s: WARNING - Wrapping None\n", __func__);
490 Py_INCREF(Py_None);
491 return Py_None;
494 ret = libvirt_buildPyObject(node, "virEventTimeoutCallback", NULL);
495 return ret;
498 PyObject *
499 libvirt_virFreeCallbackWrap(virFreeCallback node)
501 PyObject *ret;
503 if (node == NULL) {
504 Py_INCREF(Py_None);
505 return Py_None;
508 ret = libvirt_buildPyObject(node, "virFreeCallback", NULL);
509 return ret;
512 PyObject *
513 libvirt_virVoidPtrWrap(void* node)
515 PyObject *ret;
517 if (node == NULL) {
518 Py_INCREF(Py_None);
519 return Py_None;
522 ret = libvirt_buildPyObject(node, "void*", NULL);
523 return ret;