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>
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. */
15 #include "typewrappers.h"
16 #include "libvirt-utils.h"
19 typedef void(*PyCapsule_Destructor
)(void *, void *);
23 libvirt_buildPyObject(void *cobj
,
25 PyCapsule_Destructor destr
)
30 ret
= PyCapsule_New(cobj
, name
, destr
);
32 ret
= PyCObject_FromVoidPtrAndDesc(cobj
, (void *) name
, destr
);
33 #endif /* _TEST_CAPSULE */
39 libvirt_intWrap(int val
)
42 #if PY_MAJOR_VERSION > 2
43 ret
= PyLong_FromLong((long) val
);
45 ret
= PyInt_FromLong((long) val
);
51 libvirt_uintWrap(uint val
)
54 #if PY_MAJOR_VERSION > 2
55 ret
= PyLong_FromLong((long) val
);
57 ret
= PyInt_FromLong((long) val
);
63 libvirt_longWrap(long val
)
66 ret
= PyLong_FromLong(val
);
71 libvirt_ulongWrap(unsigned long val
)
74 ret
= PyLong_FromLong(val
);
79 libvirt_longlongWrap(long long val
)
82 ret
= PyLong_FromLongLong(val
);
87 libvirt_ulonglongWrap(unsigned long long val
)
90 ret
= PyLong_FromUnsignedLongLong(val
);
95 libvirt_charPtrSizeWrap(char *str
, Py_ssize_t size
)
102 #if PY_MAJOR_VERSION > 2
103 ret
= PyBytes_FromStringAndSize(str
, size
);
105 ret
= PyString_FromStringAndSize(str
, size
);
111 libvirt_charPtrWrap(char *str
)
118 #if PY_MAJOR_VERSION > 2
119 ret
= PyUnicode_FromString(str
);
121 ret
= PyString_FromString(str
);
127 libvirt_constcharPtrWrap(const char *str
)
134 #if PY_MAJOR_VERSION > 2
135 ret
= PyUnicode_FromString(str
);
137 ret
= PyString_FromString(str
);
143 libvirt_boolWrap(int val
)
152 libvirt_intUnwrap(PyObject
*obj
,
158 PyErr_SetString(PyExc_TypeError
, "unexpected type");
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
);
169 long_val
= PyInt_AsLong(obj
);
171 if ((long_val
== -1) && PyErr_Occurred())
174 #if LONG_MAX != INT_MAX
175 if (long_val
>= INT_MIN
&& long_val
<= INT_MAX
) {
178 PyErr_SetString(PyExc_OverflowError
,
179 "Python int too large to convert to C int");
189 libvirt_uintUnwrap(PyObject
*obj
,
195 PyErr_SetString(PyExc_TypeError
, "unexpected type");
199 #if PY_MAJOR_VERSION > 2
200 long_val
= PyLong_AsLong(obj
);
202 long_val
= PyInt_AsLong(obj
);
204 if ((long_val
== -1) && PyErr_Occurred())
207 if (long_val
>= 0 && long_val
<= UINT_MAX
) {
210 PyErr_SetString(PyExc_OverflowError
,
211 "Python int too large to convert to C unsigned int");
218 libvirt_longUnwrap(PyObject
*obj
,
224 PyErr_SetString(PyExc_TypeError
, "unexpected type");
228 long_val
= PyLong_AsLong(obj
);
229 if ((long_val
== -1) && PyErr_Occurred())
237 libvirt_ulongUnwrap(PyObject
*obj
,
243 PyErr_SetString(PyExc_TypeError
, "unexpected type");
247 long_val
= PyLong_AsLong(obj
);
248 if ((long_val
== -1) && PyErr_Occurred())
254 PyErr_SetString(PyExc_OverflowError
,
255 "negative Python int cannot be converted to C unsigned long");
262 libvirt_longlongUnwrap(PyObject
*obj
,
265 long long llong_val
= -1;
268 PyErr_SetString(PyExc_TypeError
, "unexpected type");
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
))
278 if (PyLong_Check(obj
))
280 llong_val
= PyLong_AsLongLong(obj
);
282 PyErr_SetString(PyExc_TypeError
, "an integer is required");
284 if ((llong_val
== -1) && PyErr_Occurred())
292 libvirt_ulonglongUnwrap(PyObject
*obj
,
293 unsigned long long *val
)
295 unsigned long long ullong_val
= -1;
298 PyErr_SetString(PyExc_TypeError
, "unexpected type");
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
);
309 PyErr_SetString(PyExc_OverflowError
,
310 "negative Python int cannot be converted to C unsigned long long");
312 ullong_val
= llong_val
;
313 } else if (PyLong_Check(obj
)) {
315 if (PyLong_Check(obj
)) {
317 ullong_val
= PyLong_AsUnsignedLongLong(obj
);
319 PyErr_SetString(PyExc_TypeError
, "an integer is required");
322 if ((ullong_val
== (unsigned long long)-1) && PyErr_Occurred())
330 libvirt_doubleUnwrap(PyObject
*obj
,
336 PyErr_SetString(PyExc_TypeError
, "unexpected type");
340 double_val
= PyFloat_AsDouble(obj
);
341 if ((double_val
== -1) && PyErr_Occurred())
349 libvirt_boolUnwrap(PyObject
*obj
,
355 PyErr_SetString(PyExc_TypeError
, "unexpected type");
359 if ((ret
= PyObject_IsTrue(obj
)) < 0)
367 libvirt_charPtrUnwrap(PyObject
*obj
,
370 #if PY_MAJOR_VERSION > 2
376 PyErr_SetString(PyExc_TypeError
, "unexpected type");
380 #if PY_MAJOR_VERSION > 2
381 if (!(bytes
= PyUnicode_AsUTF8String(obj
)))
383 ret
= PyBytes_AsString(bytes
);
385 ret
= PyString_AsString(obj
);
392 #if PY_MAJOR_VERSION > 2
395 return ret
&& *str
? 0 : -1;
399 libvirt_charPtrSizeUnwrap(PyObject
*obj
,
404 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION <= 4
410 PyErr_SetString(PyExc_TypeError
, "unexpected type");
414 #if PY_MAJOR_VERSION > 2
415 ret
= PyBytes_AsStringAndSize(obj
, str
, size
);
417 # if PY_MINOR_VERSION <= 4
418 ret
= PyString_AsStringAndSize(obj
, str
, &isize
);
421 ret
= PyString_AsStringAndSize(obj
, str
, size
);
429 libvirt_virDomainPtrWrap(virDomainPtr node
)
437 ret
= libvirt_buildPyObject(node
, "virDomainPtr", NULL
);
442 libvirt_virNetworkPtrWrap(virNetworkPtr node
)
450 ret
= libvirt_buildPyObject(node
, "virNetworkPtr", NULL
);
455 libvirt_virInterfacePtrWrap(virInterfacePtr node
)
463 ret
= libvirt_buildPyObject(node
, "virInterfacePtr", NULL
);
468 libvirt_virStoragePoolPtrWrap(virStoragePoolPtr node
)
476 ret
= libvirt_buildPyObject(node
, "virStoragePoolPtr", NULL
);
481 libvirt_virStorageVolPtrWrap(virStorageVolPtr node
)
489 ret
= libvirt_buildPyObject(node
, "virStorageVolPtr", NULL
);
494 libvirt_virConnectPtrWrap(virConnectPtr node
)
502 ret
= libvirt_buildPyObject(node
, "virConnectPtr", NULL
);
507 libvirt_virNodeDevicePtrWrap(virNodeDevicePtr node
)
515 ret
= libvirt_buildPyObject(node
, "virNodeDevicePtr", NULL
);
520 libvirt_virSecretPtrWrap(virSecretPtr node
)
528 ret
= libvirt_buildPyObject(node
, "virSecretPtr", NULL
);
533 libvirt_virNWFilterPtrWrap(virNWFilterPtr node
)
541 ret
= libvirt_buildPyObject(node
, "virNWFilterPtr", NULL
);
546 libvirt_virNWFilterBindingPtrWrap(virNWFilterBindingPtr node
)
554 ret
= libvirt_buildPyObject(node
, "virNWFilterBindingPtr", NULL
);
559 libvirt_virStreamPtrWrap(virStreamPtr node
)
567 ret
= libvirt_buildPyObject(node
, "virStreamPtr", NULL
);
572 libvirt_virDomainCheckpointPtrWrap(virDomainCheckpointPtr node
)
580 ret
= libvirt_buildPyObject(node
, "virDomainCheckpointPtr", NULL
);
585 libvirt_virDomainSnapshotPtrWrap(virDomainSnapshotPtr node
)
593 ret
= libvirt_buildPyObject(node
, "virDomainSnapshotPtr", NULL
);
598 libvirt_virEventHandleCallbackWrap(virEventHandleCallback node
)
603 printf("%s: WARNING - Wrapping None\n", __func__
);
607 ret
= libvirt_buildPyObject(node
, "virEventHandleCallback", NULL
);
612 libvirt_virEventTimeoutCallbackWrap(virEventTimeoutCallback node
)
617 printf("%s: WARNING - Wrapping None\n", __func__
);
621 ret
= libvirt_buildPyObject(node
, "virEventTimeoutCallback", NULL
);
626 libvirt_virFreeCallbackWrap(virFreeCallback node
)
634 ret
= libvirt_buildPyObject(node
, "virFreeCallback", NULL
);
639 libvirt_virVoidPtrWrap(void* node
)
647 ret
= libvirt_buildPyObject(node
, "void*", NULL
);