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>
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. */
17 #include "typewrappers.h"
22 typedef void(*PyCapsule_Destructor
)(void *, void *);
26 libvirt_buildPyObject(void *cobj
,
28 PyCapsule_Destructor destr
)
33 ret
= PyCapsule_New(cobj
, name
, destr
);
35 ret
= PyCObject_FromVoidPtrAndDesc(cobj
, (void *) name
, destr
);
36 #endif /* _TEST_CAPSULE */
42 libvirt_intWrap(int val
)
45 ret
= PyInt_FromLong((long) val
);
50 libvirt_longWrap(long val
)
53 ret
= PyInt_FromLong(val
);
58 libvirt_ulongWrap(unsigned long val
)
61 ret
= PyLong_FromLong(val
);
66 libvirt_longlongWrap(long long val
)
69 ret
= PyLong_FromUnsignedLongLong((unsigned long long) val
);
74 libvirt_ulonglongWrap(unsigned long long val
)
77 ret
= PyLong_FromUnsignedLongLong(val
);
82 libvirt_charPtrSizeWrap(char *str
, Py_ssize_t size
)
90 ret
= PyString_FromStringAndSize(str
, size
);
96 libvirt_charPtrWrap(char *str
)
104 ret
= PyString_FromString(str
);
110 libvirt_constcharPtrWrap(const char *str
)
118 ret
= PyString_FromString(str
);
123 libvirt_intUnwrap(PyObject
*obj
, int *val
)
128 PyErr_SetString(PyExc_TypeError
, "unexpected type");
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())
140 #if LONG_MAX != INT_MAX
141 if (long_val
>= INT_MIN
&& long_val
<= INT_MAX
) {
144 PyErr_SetString(PyExc_OverflowError
,
145 "Python int too large to convert to C int");
155 libvirt_uintUnwrap(PyObject
*obj
, unsigned int *val
)
160 PyErr_SetString(PyExc_TypeError
, "unexpected type");
164 long_val
= PyInt_AsLong(obj
);
165 if ((long_val
== -1) && PyErr_Occurred())
168 if (long_val
>= 0 && long_val
<= UINT_MAX
) {
171 PyErr_SetString(PyExc_OverflowError
,
172 "Python int too large to convert to C unsigned int");
179 libvirt_longUnwrap(PyObject
*obj
, long *val
)
184 PyErr_SetString(PyExc_TypeError
, "unexpected type");
188 long_val
= PyInt_AsLong(obj
);
189 if ((long_val
== -1) && PyErr_Occurred())
197 libvirt_ulongUnwrap(PyObject
*obj
, unsigned long *val
)
202 PyErr_SetString(PyExc_TypeError
, "unexpected type");
206 long_val
= PyInt_AsLong(obj
);
207 if ((long_val
== -1) && PyErr_Occurred())
213 PyErr_SetString(PyExc_OverflowError
,
214 "negative Python int cannot be converted to C unsigned long");
221 libvirt_longlongUnwrap(PyObject
*obj
, long long *val
)
223 long long llong_val
= -1;
226 PyErr_SetString(PyExc_TypeError
, "unexpected type");
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
);
236 PyErr_SetString(PyExc_TypeError
, "an integer is required");
238 if ((llong_val
== -1) && PyErr_Occurred())
246 libvirt_ulonglongUnwrap(PyObject
*obj
, unsigned long long *val
)
248 unsigned long long ullong_val
= -1;
252 PyErr_SetString(PyExc_TypeError
, "unexpected type");
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
);
262 PyErr_SetString(PyExc_OverflowError
,
263 "negative Python int cannot be converted to C unsigned long long");
265 ullong_val
= llong_val
;
266 } else if (PyLong_Check(obj
)) {
267 ullong_val
= PyLong_AsUnsignedLongLong(obj
);
269 PyErr_SetString(PyExc_TypeError
, "an integer is required");
272 if ((ullong_val
== -1) && PyErr_Occurred())
280 libvirt_doubleUnwrap(PyObject
*obj
, double *val
)
285 PyErr_SetString(PyExc_TypeError
, "unexpected type");
289 double_val
= PyFloat_AsDouble(obj
);
290 if ((double_val
== -1) && PyErr_Occurred())
298 libvirt_boolUnwrap(PyObject
*obj
, bool *val
)
303 PyErr_SetString(PyExc_TypeError
, "unexpected type");
307 if ((ret
= PyObject_IsTrue(obj
)) < 0)
315 libvirt_virDomainPtrWrap(virDomainPtr node
)
324 ret
= libvirt_buildPyObject(node
, "virDomainPtr", NULL
);
329 libvirt_virNetworkPtrWrap(virNetworkPtr node
)
338 ret
= libvirt_buildPyObject(node
, "virNetworkPtr", NULL
);
343 libvirt_virInterfacePtrWrap(virInterfacePtr node
)
352 ret
= libvirt_buildPyObject(node
, "virInterfacePtr", NULL
);
357 libvirt_virStoragePoolPtrWrap(virStoragePoolPtr node
)
366 ret
= libvirt_buildPyObject(node
, "virStoragePoolPtr", NULL
);
371 libvirt_virStorageVolPtrWrap(virStorageVolPtr node
)
380 ret
= libvirt_buildPyObject(node
, "virStorageVolPtr", NULL
);
385 libvirt_virConnectPtrWrap(virConnectPtr node
)
394 ret
= libvirt_buildPyObject(node
, "virConnectPtr", NULL
);
399 libvirt_virNodeDevicePtrWrap(virNodeDevicePtr node
)
408 ret
= libvirt_buildPyObject(node
, "virNodeDevicePtr", NULL
);
413 libvirt_virSecretPtrWrap(virSecretPtr node
)
422 ret
= libvirt_buildPyObject(node
, "virSecretPtr", NULL
);
427 libvirt_virNWFilterPtrWrap(virNWFilterPtr node
)
436 ret
= libvirt_buildPyObject(node
, "virNWFilterPtr", NULL
);
441 libvirt_virStreamPtrWrap(virStreamPtr node
)
450 ret
= libvirt_buildPyObject(node
, "virStreamPtr", NULL
);
455 libvirt_virDomainSnapshotPtrWrap(virDomainSnapshotPtr node
)
464 ret
= libvirt_buildPyObject(node
, "virDomainSnapshotPtr", NULL
);
469 libvirt_virEventHandleCallbackWrap(virEventHandleCallback node
)
475 printf("%s: WARNING - Wrapping None\n", __func__
);
479 ret
= libvirt_buildPyObject(node
, "virEventHandleCallback", NULL
);
484 libvirt_virEventTimeoutCallbackWrap(virEventTimeoutCallback node
)
489 printf("%s: WARNING - Wrapping None\n", __func__
);
494 ret
= libvirt_buildPyObject(node
, "virEventTimeoutCallback", NULL
);
499 libvirt_virFreeCallbackWrap(virFreeCallback node
)
508 ret
= libvirt_buildPyObject(node
, "virFreeCallback", NULL
);
513 libvirt_virVoidPtrWrap(void* node
)
522 ret
= libvirt_buildPyObject(node
, "void*", NULL
);