Post-release version bump to 11.1.0
[libvirt-python.git] / typewrappers.c
blob2f37525fb770801447d07444632e621d339feff3
1 /*
2 * types.c: converter functions between the internal representation
3 * and the Python objects
5 * Copyright (C) 2005-2019 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/python3.x/pyconfig*.h,
12 * which has over 180 autoconf-style HAVE_* definitions. Shame on them. */
13 #undef HAVE_PTHREAD_H
15 #include <stdio.h>
16 #include <string.h>
17 #include "typewrappers.h"
18 #include "libvirt-utils.h"
20 static PyObject *
21 libvirt_buildPyObject(void *cobj,
22 const char *name,
23 PyCapsule_Destructor destr)
25 return PyCapsule_New(cobj, name, destr);
28 PyObject *
29 libvirt_intWrap(int val)
31 return PyLong_FromLong(val);
34 PyObject *
35 libvirt_uintWrap(unsigned int val)
37 return PyLong_FromUnsignedLong(val);
40 PyObject *
41 libvirt_longWrap(long val)
43 return PyLong_FromLong(val);
46 PyObject *
47 libvirt_ulongWrap(unsigned long val)
49 return PyLong_FromLong(val);
52 PyObject *
53 libvirt_longlongWrap(long long val)
55 return PyLong_FromLongLong(val);
58 PyObject *
59 libvirt_ulonglongWrap(unsigned long long val)
61 return PyLong_FromUnsignedLongLong(val);
64 PyObject *
65 libvirt_charPtrSizeWrap(char *str, Py_ssize_t size)
67 if (str == NULL) {
68 return VIR_PY_NONE;
70 return PyBytes_FromStringAndSize(str, size);
73 PyObject *
74 libvirt_charPtrWrap(char *str)
76 if (str == NULL) {
77 return VIR_PY_NONE;
79 return PyUnicode_FromString(str);
82 PyObject *
83 libvirt_constcharPtrWrap(const char *str)
85 if (str == NULL) {
86 return VIR_PY_NONE;
88 return PyUnicode_FromString(str);
91 PyObject *
92 libvirt_boolWrap(int val)
94 if (val)
95 Py_RETURN_TRUE;
96 else
97 Py_RETURN_FALSE;
101 libvirt_intUnwrap(PyObject *obj,
102 int *val)
104 long long_val;
106 if (!obj) {
107 PyErr_SetString(PyExc_TypeError, "unexpected type");
108 return -1;
111 /* If obj is type of PyInt_Type, PyInt_AsLong converts it
112 * to C long type directly. If it is of PyLong_Type, PyInt_AsLong
113 * will call PyLong_AsLong() to deal with it automatically.
115 long_val = PyLong_AsLong(obj);
116 if ((long_val == -1) && PyErr_Occurred())
117 return -1;
119 #if LONG_MAX != INT_MAX
120 if (long_val >= INT_MIN && long_val <= INT_MAX) {
121 *val = long_val;
122 } else {
123 PyErr_SetString(PyExc_OverflowError,
124 "Python int too large to convert to C int");
125 return -1;
127 #else
128 *val = long_val;
129 #endif
130 return 0;
134 libvirt_uintUnwrap(PyObject *obj,
135 unsigned int *val)
137 unsigned long long_val;
139 if (!obj) {
140 PyErr_SetString(PyExc_TypeError, "unexpected type");
141 return -1;
144 long_val = PyLong_AsUnsignedLong(obj);
145 if ((long_val == (unsigned long)-1) && PyErr_Occurred())
146 return -1;
148 if (long_val <= UINT_MAX) {
149 *val = long_val;
150 } else {
151 PyErr_SetString(PyExc_OverflowError,
152 "Python int too large to convert to C unsigned int");
153 return -1;
155 return 0;
159 libvirt_longUnwrap(PyObject *obj,
160 long *val)
162 long long_val;
164 if (!obj) {
165 PyErr_SetString(PyExc_TypeError, "unexpected type");
166 return -1;
169 long_val = PyLong_AsLong(obj);
170 if ((long_val == -1) && PyErr_Occurred())
171 return -1;
173 *val = long_val;
174 return 0;
178 libvirt_ulongUnwrap(PyObject *obj,
179 unsigned long *val)
181 long long_val;
183 if (!obj) {
184 PyErr_SetString(PyExc_TypeError, "unexpected type");
185 return -1;
188 long_val = PyLong_AsLong(obj);
189 if ((long_val == -1) && PyErr_Occurred())
190 return -1;
192 if (long_val >= 0) {
193 *val = long_val;
194 } else {
195 PyErr_SetString(PyExc_OverflowError,
196 "negative Python int cannot be converted to C unsigned long");
197 return -1;
199 return 0;
203 libvirt_longlongUnwrap(PyObject *obj,
204 long long *val)
206 long long llong_val = -1;
208 if (!obj) {
209 PyErr_SetString(PyExc_TypeError, "unexpected type");
210 return -1;
213 if (PyLong_Check(obj))
214 llong_val = PyLong_AsLongLong(obj);
215 else
216 PyErr_SetString(PyExc_TypeError, "an integer is required");
218 if ((llong_val == -1) && PyErr_Occurred())
219 return -1;
221 *val = llong_val;
222 return 0;
226 libvirt_ulonglongUnwrap(PyObject *obj,
227 unsigned long long *val)
229 unsigned long long ullong_val = -1;
231 if (!obj) {
232 PyErr_SetString(PyExc_TypeError, "unexpected type");
233 return -1;
236 if (PyLong_Check(obj)) {
237 ullong_val = PyLong_AsUnsignedLongLong(obj);
238 } else {
239 PyErr_SetString(PyExc_TypeError, "an integer is required");
242 if ((ullong_val == (unsigned long long)-1) && PyErr_Occurred())
243 return -1;
245 *val = ullong_val;
246 return 0;
250 libvirt_doubleUnwrap(PyObject *obj,
251 double *val)
253 double double_val;
255 if (!obj) {
256 PyErr_SetString(PyExc_TypeError, "unexpected type");
257 return -1;
260 double_val = PyFloat_AsDouble(obj);
261 if ((double_val == -1) && PyErr_Occurred())
262 return -1;
264 *val = double_val;
265 return 0;
269 libvirt_boolUnwrap(PyObject *obj,
270 bool *val)
272 int ret;
274 if (!obj) {
275 PyErr_SetString(PyExc_TypeError, "unexpected type");
276 return -1;
279 if ((ret = PyObject_IsTrue(obj)) < 0)
280 return ret;
282 *val = ret > 0;
283 return 0;
287 libvirt_charPtrUnwrap(PyObject *obj,
288 char **str)
290 PyObject *bytes;
291 const char *ret;
292 *str = NULL;
293 if (!obj) {
294 PyErr_SetString(PyExc_TypeError, "unexpected type");
295 return -1;
298 if (!(bytes = PyUnicode_AsUTF8String(obj)))
299 return -1;
300 ret = PyBytes_AsString(bytes);
301 if (ret) {
302 *str = strdup(ret);
303 if (!*str)
304 PyErr_NoMemory();
306 Py_DECREF(bytes);
307 return ret && *str ? 0 : -1;
311 libvirt_charPtrSizeUnwrap(PyObject *obj,
312 char **str,
313 Py_ssize_t *size)
315 *str = NULL;
316 *size = 0;
317 if (!obj) {
318 PyErr_SetString(PyExc_TypeError, "unexpected type");
319 return -1;
322 return PyBytes_AsStringAndSize(obj, str, size);
325 PyObject *
326 libvirt_virDomainPtrWrap(virDomainPtr node)
328 PyObject *ret;
330 if (node == NULL) {
331 return VIR_PY_NONE;
334 ret = libvirt_buildPyObject(node, "virDomainPtr", NULL);
335 return ret;
338 PyObject *
339 libvirt_virNetworkPtrWrap(virNetworkPtr node)
341 PyObject *ret;
343 if (node == NULL) {
344 return VIR_PY_NONE;
347 ret = libvirt_buildPyObject(node, "virNetworkPtr", NULL);
348 return ret;
351 PyObject *
352 libvirt_virNetworkPortPtrWrap(virNetworkPortPtr node)
354 PyObject *ret;
356 if (node == NULL) {
357 return VIR_PY_NONE;
360 ret = libvirt_buildPyObject(node, "virNetworkPortPtr", NULL);
361 return ret;
364 PyObject *
365 libvirt_virInterfacePtrWrap(virInterfacePtr node)
367 PyObject *ret;
369 if (node == NULL) {
370 return VIR_PY_NONE;
373 ret = libvirt_buildPyObject(node, "virInterfacePtr", NULL);
374 return ret;
377 PyObject *
378 libvirt_virStoragePoolPtrWrap(virStoragePoolPtr node)
380 PyObject *ret;
382 if (node == NULL) {
383 return VIR_PY_NONE;
386 ret = libvirt_buildPyObject(node, "virStoragePoolPtr", NULL);
387 return ret;
390 PyObject *
391 libvirt_virStorageVolPtrWrap(virStorageVolPtr node)
393 PyObject *ret;
395 if (node == NULL) {
396 return VIR_PY_NONE;
399 ret = libvirt_buildPyObject(node, "virStorageVolPtr", NULL);
400 return ret;
403 PyObject *
404 libvirt_virConnectPtrWrap(virConnectPtr node)
406 PyObject *ret;
408 if (node == NULL) {
409 return VIR_PY_NONE;
412 ret = libvirt_buildPyObject(node, "virConnectPtr", NULL);
413 return ret;
416 PyObject *
417 libvirt_virNodeDevicePtrWrap(virNodeDevicePtr node)
419 PyObject *ret;
421 if (node == NULL) {
422 return VIR_PY_NONE;
425 ret = libvirt_buildPyObject(node, "virNodeDevicePtr", NULL);
426 return ret;
429 PyObject *
430 libvirt_virSecretPtrWrap(virSecretPtr node)
432 PyObject *ret;
434 if (node == NULL) {
435 return VIR_PY_NONE;
438 ret = libvirt_buildPyObject(node, "virSecretPtr", NULL);
439 return ret;
442 PyObject *
443 libvirt_virNWFilterPtrWrap(virNWFilterPtr node)
445 PyObject *ret;
447 if (node == NULL) {
448 return VIR_PY_NONE;
451 ret = libvirt_buildPyObject(node, "virNWFilterPtr", NULL);
452 return ret;
455 PyObject *
456 libvirt_virNWFilterBindingPtrWrap(virNWFilterBindingPtr node)
458 PyObject *ret;
460 if (node == NULL) {
461 return VIR_PY_NONE;
464 ret = libvirt_buildPyObject(node, "virNWFilterBindingPtr", NULL);
465 return ret;
468 PyObject *
469 libvirt_virStreamPtrWrap(virStreamPtr node)
471 PyObject *ret;
473 if (node == NULL) {
474 return VIR_PY_NONE;
477 ret = libvirt_buildPyObject(node, "virStreamPtr", NULL);
478 return ret;
481 PyObject *
482 libvirt_virDomainCheckpointPtrWrap(virDomainCheckpointPtr node)
484 PyObject *ret;
486 if (node == NULL) {
487 return VIR_PY_NONE;
490 ret = libvirt_buildPyObject(node, "virDomainCheckpointPtr", NULL);
491 return ret;
494 PyObject *
495 libvirt_virDomainSnapshotPtrWrap(virDomainSnapshotPtr node)
497 PyObject *ret;
499 if (node == NULL) {
500 return VIR_PY_NONE;
503 ret = libvirt_buildPyObject(node, "virDomainSnapshotPtr", NULL);
504 return ret;
507 PyObject *
508 libvirt_virEventHandleCallbackWrap(virEventHandleCallback node)
510 PyObject *ret;
512 if (node == NULL) {
513 printf("%s: WARNING - Wrapping None\n", __func__);
514 return VIR_PY_NONE;
517 ret = libvirt_buildPyObject(node, "virEventHandleCallback", NULL);
518 return ret;
521 PyObject *
522 libvirt_virEventTimeoutCallbackWrap(virEventTimeoutCallback node)
524 PyObject *ret;
526 if (node == NULL) {
527 printf("%s: WARNING - Wrapping None\n", __func__);
528 return VIR_PY_NONE;
531 ret = libvirt_buildPyObject(node, "virEventTimeoutCallback", NULL);
532 return ret;
535 PyObject *
536 libvirt_virFreeCallbackWrap(virFreeCallback node)
538 PyObject *ret;
540 if (node == NULL) {
541 return VIR_PY_NONE;
544 ret = libvirt_buildPyObject(node, "virFreeCallback", NULL);
545 return ret;
548 PyObject *
549 libvirt_virVoidPtrWrap(void* node)
551 PyObject *ret;
553 if (node == NULL) {
554 return VIR_PY_NONE;
557 ret = libvirt_buildPyObject(node, "void*", NULL);
558 return ret;