Add missing impl of virNetworkListAllPorts
[libvirt-python/ericb.git] / typewrappers.c
blob57107447ce4cf281335ee7ecac766581fe553c8e
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 /* Horrible kludge to work around even more horrible name-space pollution
11 * via Python.h. That file includes /usr/include/python2.5/pyconfig*.h,
12 * which has over 180 autoconf-style HAVE_* definitions. Shame on them. */
13 #undef HAVE_PTHREAD_H
15 #include "typewrappers.h"
16 #include "libvirt-utils.h"
18 #ifndef Py_CAPSULE_H
19 typedef void(*PyCapsule_Destructor)(void *, void *);
20 #endif
22 static PyObject *
23 libvirt_buildPyObject(void *cobj,
24 const char *name,
25 PyCapsule_Destructor destr)
27 PyObject *ret;
29 #ifdef Py_CAPSULE_H
30 ret = PyCapsule_New(cobj, name, destr);
31 #else
32 ret = PyCObject_FromVoidPtrAndDesc(cobj, (void *) name, destr);
33 #endif /* _TEST_CAPSULE */
35 return ret;
38 PyObject *
39 libvirt_intWrap(int val)
41 PyObject *ret;
42 #if PY_MAJOR_VERSION > 2
43 ret = PyLong_FromLong((long) val);
44 #else
45 ret = PyInt_FromLong((long) val);
46 #endif
47 return ret;
50 PyObject *
51 libvirt_uintWrap(uint val)
53 PyObject *ret;
54 #if PY_MAJOR_VERSION > 2
55 ret = PyLong_FromLong((long) val);
56 #else
57 ret = PyInt_FromLong((long) val);
58 #endif
59 return ret;
62 PyObject *
63 libvirt_longWrap(long val)
65 PyObject *ret;
66 ret = PyLong_FromLong(val);
67 return ret;
70 PyObject *
71 libvirt_ulongWrap(unsigned long val)
73 PyObject *ret;
74 ret = PyLong_FromLong(val);
75 return ret;
78 PyObject *
79 libvirt_longlongWrap(long long val)
81 PyObject *ret;
82 ret = PyLong_FromLongLong(val);
83 return ret;
86 PyObject *
87 libvirt_ulonglongWrap(unsigned long long val)
89 PyObject *ret;
90 ret = PyLong_FromUnsignedLongLong(val);
91 return ret;
94 PyObject *
95 libvirt_charPtrSizeWrap(char *str, Py_ssize_t size)
97 PyObject *ret;
99 if (str == NULL) {
100 return VIR_PY_NONE;
102 #if PY_MAJOR_VERSION > 2
103 ret = PyBytes_FromStringAndSize(str, size);
104 #else
105 ret = PyString_FromStringAndSize(str, size);
106 #endif
107 return ret;
110 PyObject *
111 libvirt_charPtrWrap(char *str)
113 PyObject *ret;
115 if (str == NULL) {
116 return VIR_PY_NONE;
118 #if PY_MAJOR_VERSION > 2
119 ret = PyUnicode_FromString(str);
120 #else
121 ret = PyString_FromString(str);
122 #endif
123 return ret;
126 PyObject *
127 libvirt_constcharPtrWrap(const char *str)
129 PyObject *ret;
131 if (str == NULL) {
132 return VIR_PY_NONE;
134 #if PY_MAJOR_VERSION > 2
135 ret = PyUnicode_FromString(str);
136 #else
137 ret = PyString_FromString(str);
138 #endif
139 return ret;
142 PyObject *
143 libvirt_boolWrap(int val)
145 if (val)
146 Py_RETURN_TRUE;
147 else
148 Py_RETURN_FALSE;
152 libvirt_intUnwrap(PyObject *obj,
153 int *val)
155 long long_val;
157 if (!obj) {
158 PyErr_SetString(PyExc_TypeError, "unexpected type");
159 return -1;
162 /* If obj is type of PyInt_Type, PyInt_AsLong converts it
163 * to C long type directly. If it is of PyLong_Type, PyInt_AsLong
164 * will call PyLong_AsLong() to deal with it automatically.
166 #if PY_MAJOR_VERSION > 2
167 long_val = PyLong_AsLong(obj);
168 #else
169 long_val = PyInt_AsLong(obj);
170 #endif
171 if ((long_val == -1) && PyErr_Occurred())
172 return -1;
174 #if LONG_MAX != INT_MAX
175 if (long_val >= INT_MIN && long_val <= INT_MAX) {
176 *val = long_val;
177 } else {
178 PyErr_SetString(PyExc_OverflowError,
179 "Python int too large to convert to C int");
180 return -1;
182 #else
183 *val = long_val;
184 #endif
185 return 0;
189 libvirt_uintUnwrap(PyObject *obj,
190 unsigned int *val)
192 long long_val;
194 if (!obj) {
195 PyErr_SetString(PyExc_TypeError, "unexpected type");
196 return -1;
199 #if PY_MAJOR_VERSION > 2
200 long_val = PyLong_AsLong(obj);
201 #else
202 long_val = PyInt_AsLong(obj);
203 #endif
204 if ((long_val == -1) && PyErr_Occurred())
205 return -1;
207 if (long_val >= 0 && long_val <= UINT_MAX) {
208 *val = long_val;
209 } else {
210 PyErr_SetString(PyExc_OverflowError,
211 "Python int too large to convert to C unsigned int");
212 return -1;
214 return 0;
218 libvirt_longUnwrap(PyObject *obj,
219 long *val)
221 long long_val;
223 if (!obj) {
224 PyErr_SetString(PyExc_TypeError, "unexpected type");
225 return -1;
228 long_val = PyLong_AsLong(obj);
229 if ((long_val == -1) && PyErr_Occurred())
230 return -1;
232 *val = long_val;
233 return 0;
237 libvirt_ulongUnwrap(PyObject *obj,
238 unsigned long *val)
240 long long_val;
242 if (!obj) {
243 PyErr_SetString(PyExc_TypeError, "unexpected type");
244 return -1;
247 long_val = PyLong_AsLong(obj);
248 if ((long_val == -1) && PyErr_Occurred())
249 return -1;
251 if (long_val >= 0) {
252 *val = long_val;
253 } else {
254 PyErr_SetString(PyExc_OverflowError,
255 "negative Python int cannot be converted to C unsigned long");
256 return -1;
258 return 0;
262 libvirt_longlongUnwrap(PyObject *obj,
263 long long *val)
265 long long llong_val = -1;
267 if (!obj) {
268 PyErr_SetString(PyExc_TypeError, "unexpected type");
269 return -1;
272 #if PY_MAJOR_VERSION == 2
273 /* If obj is of PyInt_Type, PyLong_AsLongLong
274 * will call PyInt_AsLong() to handle it automatically.
276 if (PyInt_Check(obj) || PyLong_Check(obj))
277 #else
278 if (PyLong_Check(obj))
279 #endif
280 llong_val = PyLong_AsLongLong(obj);
281 else
282 PyErr_SetString(PyExc_TypeError, "an integer is required");
284 if ((llong_val == -1) && PyErr_Occurred())
285 return -1;
287 *val = llong_val;
288 return 0;
292 libvirt_ulonglongUnwrap(PyObject *obj,
293 unsigned long long *val)
295 unsigned long long ullong_val = -1;
297 if (!obj) {
298 PyErr_SetString(PyExc_TypeError, "unexpected type");
299 return -1;
302 #if PY_MAJOR_VERSION == 2
303 /* The PyLong_AsUnsignedLongLong doesn't check the type of
304 * obj, only accept argument of PyLong_Type, so we check it instead.
306 if (PyInt_Check(obj)) {
307 long long llong_val = PyInt_AsLong(obj);
308 if (llong_val < 0)
309 PyErr_SetString(PyExc_OverflowError,
310 "negative Python int cannot be converted to C unsigned long long");
311 else
312 ullong_val = llong_val;
313 } else if (PyLong_Check(obj)) {
314 #else
315 if (PyLong_Check(obj)) {
316 #endif
317 ullong_val = PyLong_AsUnsignedLongLong(obj);
318 } else {
319 PyErr_SetString(PyExc_TypeError, "an integer is required");
322 if ((ullong_val == (unsigned long long)-1) && PyErr_Occurred())
323 return -1;
325 *val = ullong_val;
326 return 0;
330 libvirt_doubleUnwrap(PyObject *obj,
331 double *val)
333 double double_val;
335 if (!obj) {
336 PyErr_SetString(PyExc_TypeError, "unexpected type");
337 return -1;
340 double_val = PyFloat_AsDouble(obj);
341 if ((double_val == -1) && PyErr_Occurred())
342 return -1;
344 *val = double_val;
345 return 0;
349 libvirt_boolUnwrap(PyObject *obj,
350 bool *val)
352 int ret;
354 if (!obj) {
355 PyErr_SetString(PyExc_TypeError, "unexpected type");
356 return -1;
359 if ((ret = PyObject_IsTrue(obj)) < 0)
360 return ret;
362 *val = ret > 0;
363 return 0;
367 libvirt_charPtrUnwrap(PyObject *obj,
368 char **str)
370 #if PY_MAJOR_VERSION > 2
371 PyObject *bytes;
372 #endif
373 const char *ret;
374 *str = NULL;
375 if (!obj) {
376 PyErr_SetString(PyExc_TypeError, "unexpected type");
377 return -1;
380 #if PY_MAJOR_VERSION > 2
381 if (!(bytes = PyUnicode_AsUTF8String(obj)))
382 return -1;
383 ret = PyBytes_AsString(bytes);
384 #else
385 ret = PyString_AsString(obj);
386 #endif
387 if (ret) {
388 *str = strdup(ret);
389 if (!*str)
390 PyErr_NoMemory();
392 #if PY_MAJOR_VERSION > 2
393 Py_DECREF(bytes);
394 #endif
395 return ret && *str ? 0 : -1;
399 libvirt_charPtrSizeUnwrap(PyObject *obj,
400 char **str,
401 Py_ssize_t *size)
403 int ret;
404 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION <= 4
405 int isize;
406 #endif
407 *str = NULL;
408 *size = 0;
409 if (!obj) {
410 PyErr_SetString(PyExc_TypeError, "unexpected type");
411 return -1;
414 #if PY_MAJOR_VERSION > 2
415 ret = PyBytes_AsStringAndSize(obj, str, size);
416 #else
417 # if PY_MINOR_VERSION <= 4
418 ret = PyString_AsStringAndSize(obj, str, &isize);
419 *size = isize;
420 # else
421 ret = PyString_AsStringAndSize(obj, str, size);
422 # endif
423 #endif
425 return ret;
428 PyObject *
429 libvirt_virDomainPtrWrap(virDomainPtr node)
431 PyObject *ret;
433 if (node == NULL) {
434 return VIR_PY_NONE;
437 ret = libvirt_buildPyObject(node, "virDomainPtr", NULL);
438 return ret;
441 PyObject *
442 libvirt_virNetworkPtrWrap(virNetworkPtr node)
444 PyObject *ret;
446 if (node == NULL) {
447 return VIR_PY_NONE;
450 ret = libvirt_buildPyObject(node, "virNetworkPtr", NULL);
451 return ret;
454 PyObject *
455 libvirt_virNetworkPortPtrWrap(virNetworkPortPtr node)
457 PyObject *ret;
459 if (node == NULL) {
460 return VIR_PY_NONE;
463 ret = libvirt_buildPyObject(node, "virNetworkPortPtr", NULL);
464 return ret;
467 PyObject *
468 libvirt_virInterfacePtrWrap(virInterfacePtr node)
470 PyObject *ret;
472 if (node == NULL) {
473 return VIR_PY_NONE;
476 ret = libvirt_buildPyObject(node, "virInterfacePtr", NULL);
477 return ret;
480 PyObject *
481 libvirt_virStoragePoolPtrWrap(virStoragePoolPtr node)
483 PyObject *ret;
485 if (node == NULL) {
486 return VIR_PY_NONE;
489 ret = libvirt_buildPyObject(node, "virStoragePoolPtr", NULL);
490 return ret;
493 PyObject *
494 libvirt_virStorageVolPtrWrap(virStorageVolPtr node)
496 PyObject *ret;
498 if (node == NULL) {
499 return VIR_PY_NONE;
502 ret = libvirt_buildPyObject(node, "virStorageVolPtr", NULL);
503 return ret;
506 PyObject *
507 libvirt_virConnectPtrWrap(virConnectPtr node)
509 PyObject *ret;
511 if (node == NULL) {
512 return VIR_PY_NONE;
515 ret = libvirt_buildPyObject(node, "virConnectPtr", NULL);
516 return ret;
519 PyObject *
520 libvirt_virNodeDevicePtrWrap(virNodeDevicePtr node)
522 PyObject *ret;
524 if (node == NULL) {
525 return VIR_PY_NONE;
528 ret = libvirt_buildPyObject(node, "virNodeDevicePtr", NULL);
529 return ret;
532 PyObject *
533 libvirt_virSecretPtrWrap(virSecretPtr node)
535 PyObject *ret;
537 if (node == NULL) {
538 return VIR_PY_NONE;
541 ret = libvirt_buildPyObject(node, "virSecretPtr", NULL);
542 return ret;
545 PyObject *
546 libvirt_virNWFilterPtrWrap(virNWFilterPtr node)
548 PyObject *ret;
550 if (node == NULL) {
551 return VIR_PY_NONE;
554 ret = libvirt_buildPyObject(node, "virNWFilterPtr", NULL);
555 return ret;
558 PyObject *
559 libvirt_virNWFilterBindingPtrWrap(virNWFilterBindingPtr node)
561 PyObject *ret;
563 if (node == NULL) {
564 return VIR_PY_NONE;
567 ret = libvirt_buildPyObject(node, "virNWFilterBindingPtr", NULL);
568 return ret;
571 PyObject *
572 libvirt_virStreamPtrWrap(virStreamPtr node)
574 PyObject *ret;
576 if (node == NULL) {
577 return VIR_PY_NONE;
580 ret = libvirt_buildPyObject(node, "virStreamPtr", NULL);
581 return ret;
584 PyObject *
585 libvirt_virDomainSnapshotPtrWrap(virDomainSnapshotPtr node)
587 PyObject *ret;
589 if (node == NULL) {
590 return VIR_PY_NONE;
593 ret = libvirt_buildPyObject(node, "virDomainSnapshotPtr", NULL);
594 return ret;
597 PyObject *
598 libvirt_virEventHandleCallbackWrap(virEventHandleCallback node)
600 PyObject *ret;
602 if (node == NULL) {
603 printf("%s: WARNING - Wrapping None\n", __func__);
604 return VIR_PY_NONE;
607 ret = libvirt_buildPyObject(node, "virEventHandleCallback", NULL);
608 return ret;
611 PyObject *
612 libvirt_virEventTimeoutCallbackWrap(virEventTimeoutCallback node)
614 PyObject *ret;
616 if (node == NULL) {
617 printf("%s: WARNING - Wrapping None\n", __func__);
618 return VIR_PY_NONE;
621 ret = libvirt_buildPyObject(node, "virEventTimeoutCallback", NULL);
622 return ret;
625 PyObject *
626 libvirt_virFreeCallbackWrap(virFreeCallback node)
628 PyObject *ret;
630 if (node == NULL) {
631 return VIR_PY_NONE;
634 ret = libvirt_buildPyObject(node, "virFreeCallback", NULL);
635 return ret;
638 PyObject *
639 libvirt_virVoidPtrWrap(void* node)
641 PyObject *ret;
643 if (node == NULL) {
644 return VIR_PY_NONE;
647 ret = libvirt_buildPyObject(node, "void*", NULL);
648 return ret;