Debian Codegraph @

typewrappers.c

raw symbols SCIP
1 /*
2 * types.c: converter functions between the internal representation
3 * and the Python objects
4 *
5 * Copyright (C) 2005-2019 Red Hat, Inc.
6 *
7 * Daniel Veillard <[email protected]>
8 */
9
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
14
15 #include <Python.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include "typewrappers.h"
19 #include "libvirt-utils.h"
20
21 static PyObject *
22 libvirt_buildPyObject(void *cobj,
23 const char *name,
24 PyCapsule_Destructor destr)
25 {
26 return PyCapsule_New(cobj, name, destr);
27 }
28
29 PyObject *
30 libvirt_intWrap(int val)
31 {
32 return PyLong_FromLong(val);
33 }
34
35 PyObject *
36 libvirt_uintWrap(unsigned int val)
37 {
38 return PyLong_FromUnsignedLong(val);
39 }
40
41 PyObject *
42 libvirt_longWrap(long val)
43 {
44 return PyLong_FromLong(val);
45 }
46
47 PyObject *
48 libvirt_ulongWrap(unsigned long val)
49 {
50 return PyLong_FromLong(val);
51 }
52
53 PyObject *
54 libvirt_longlongWrap(long long val)
55 {
56 return PyLong_FromLongLong(val);
57 }
58
59 PyObject *
60 libvirt_ulonglongWrap(unsigned long long val)
61 {
62 return PyLong_FromUnsignedLongLong(val);
63 }
64
65 PyObject *
66 libvirt_charPtrSizeWrap(char *str, Py_ssize_t size)
67 {
68 if (str == NULL) {
69 return VIR_PY_NONE;
70 }
71 return PyBytes_FromStringAndSize(str, size);
72 }
73
74 PyObject *
75 libvirt_charPtrWrap(char *str)
76 {
77 if (str == NULL) {
78 return VIR_PY_NONE;
79 }
80 return PyUnicode_FromString(str);
81 }
82
83 PyObject *
84 libvirt_constcharPtrWrap(const char *str)
85 {
86 if (str == NULL) {
87 return VIR_PY_NONE;
88 }
89 return PyUnicode_FromString(str);
90 }
91
92 PyObject *
93 libvirt_boolWrap(int val)
94 {
95 if (val)
96 Py_RETURN_TRUE;
97 else
98 Py_RETURN_FALSE;
99 }
100
101 int
102 libvirt_intUnwrap(PyObject *obj,
103 int *val)
104 {
105 long long_val;
106
107 if (!obj) {
108 PyErr_SetString(PyExc_TypeError, "unexpected type");
109 return -1;
110 }
111
112 /* If obj is type of PyInt_Type, PyInt_AsLong converts it
113 * to C long type directly. If it is of PyLong_Type, PyInt_AsLong
114 * will call PyLong_AsLong() to deal with it automatically.
115 */
116 long_val = PyLong_AsLong(obj);
117 if ((long_val == -1) && PyErr_Occurred())
118 return -1;
119
120 #if LONG_MAX != INT_MAX
121 if (long_val >= INT_MIN && long_val <= INT_MAX) {
122 *val = long_val;
123 } else {
124 PyErr_SetString(PyExc_OverflowError,
125 "Python int too large to convert to C int");
126 return -1;
127 }
128 #else
129 *val = long_val;
130 #endif
131 return 0;
132 }
133
134 int
135 libvirt_uintUnwrap(PyObject *obj,
136 unsigned int *val)
137 {
138 unsigned long long_val;
139
140 if (!obj) {
141 PyErr_SetString(PyExc_TypeError, "unexpected type");
142 return -1;
143 }
144
145 long_val = PyLong_AsUnsignedLong(obj);
146 if ((long_val == (unsigned long)-1) && PyErr_Occurred())
147 return -1;
148
149 if (long_val <= UINT_MAX) {
150 *val = long_val;
151 } else {
152 PyErr_SetString(PyExc_OverflowError,
153 "Python int too large to convert to C unsigned int");
154 return -1;
155 }
156 return 0;
157 }
158
159 int
160 libvirt_longUnwrap(PyObject *obj,
161 long *val)
162 {
163 long long_val;
164
165 if (!obj) {
166 PyErr_SetString(PyExc_TypeError, "unexpected type");
167 return -1;
168 }
169
170 long_val = PyLong_AsLong(obj);
171 if ((long_val == -1) && PyErr_Occurred())
172 return -1;
173
174 *val = long_val;
175 return 0;
176 }
177
178 int
179 libvirt_ulongUnwrap(PyObject *obj,
180 unsigned long *val)
181 {
182 long long_val;
183
184 if (!obj) {
185 PyErr_SetString(PyExc_TypeError, "unexpected type");
186 return -1;
187 }
188
189 long_val = PyLong_AsLong(obj);
190 if ((long_val == -1) && PyErr_Occurred())
191 return -1;
192
193 if (long_val >= 0) {
194 *val = long_val;
195 } else {
196 PyErr_SetString(PyExc_OverflowError,
197 "negative Python int cannot be converted to C unsigned long");
198 return -1;
199 }
200 return 0;
201 }
202
203 int
204 libvirt_longlongUnwrap(PyObject *obj,
205 long long *val)
206 {
207 long long llong_val = -1;
208
209 if (!obj) {
210 PyErr_SetString(PyExc_TypeError, "unexpected type");
211 return -1;
212 }
213
214 if (PyLong_Check(obj))
215 llong_val = PyLong_AsLongLong(obj);
216 else
217 PyErr_SetString(PyExc_TypeError, "an integer is required");
218
219 if ((llong_val == -1) && PyErr_Occurred())
220 return -1;
221
222 *val = llong_val;
223 return 0;
224 }
225
226 int
227 libvirt_ulonglongUnwrap(PyObject *obj,
228 unsigned long long *val)
229 {
230 unsigned long long ullong_val = -1;
231
232 if (!obj) {
233 PyErr_SetString(PyExc_TypeError, "unexpected type");
234 return -1;
235 }
236
237 if (PyLong_Check(obj)) {
238 ullong_val = PyLong_AsUnsignedLongLong(obj);
239 } else {
240 PyErr_SetString(PyExc_TypeError, "an integer is required");
241 }
242
243 if ((ullong_val == (unsigned long long)-1) && PyErr_Occurred())
244 return -1;
245
246 *val = ullong_val;
247 return 0;
248 }
249
250 int
251 libvirt_doubleUnwrap(PyObject *obj,
252 double *val)
253 {
254 double double_val;
255
256 if (!obj) {
257 PyErr_SetString(PyExc_TypeError, "unexpected type");
258 return -1;
259 }
260
261 double_val = PyFloat_AsDouble(obj);
262 if ((double_val == -1) && PyErr_Occurred())
263 return -1;
264
265 *val = double_val;
266 return 0;
267 }
268
269 int
270 libvirt_boolUnwrap(PyObject *obj,
271 bool *val)
272 {
273 int ret;
274
275 if (!obj) {
276 PyErr_SetString(PyExc_TypeError, "unexpected type");
277 return -1;
278 }
279
280 if ((ret = PyObject_IsTrue(obj)) < 0)
281 return ret;
282
283 *val = ret > 0;
284 return 0;
285 }
286
287 int
288 libvirt_charPtrUnwrap(PyObject *obj,
289 char **str)
290 {
291 PyObject *bytes;
292 const char *ret;
293 *str = NULL;
294 if (!obj) {
295 PyErr_SetString(PyExc_TypeError, "unexpected type");
296 return -1;
297 }
298
299 if (!(bytes = PyUnicode_AsUTF8String(obj)))
300 return -1;
301 ret = PyBytes_AsString(bytes);
302 if (ret) {
303 *str = strdup(ret);
304 if (!*str)
305 PyErr_NoMemory();
306 }
307 Py_DECREF(bytes);
308 return ret && *str ? 0 : -1;
309 }
310
311 int
312 libvirt_charPtrSizeUnwrap(PyObject *obj,
313 char **str,
314 Py_ssize_t *size)
315 {
316 *str = NULL;
317 *size = 0;
318 if (!obj) {
319 PyErr_SetString(PyExc_TypeError, "unexpected type");
320 return -1;
321 }
322
323 return PyBytes_AsStringAndSize(obj, str, size);
324 }
325
326 PyObject *
327 libvirt_virDomainPtrWrap(virDomainPtr node)
328 {
329 PyObject *ret;
330
331 if (node == NULL) {
332 return VIR_PY_NONE;
333 }
334
335 ret = libvirt_buildPyObject(node, "virDomainPtr", NULL);
336 return ret;
337 }
338
339 PyObject *
340 libvirt_virNetworkPtrWrap(virNetworkPtr node)
341 {
342 PyObject *ret;
343
344 if (node == NULL) {
345 return VIR_PY_NONE;
346 }
347
348 ret = libvirt_buildPyObject(node, "virNetworkPtr", NULL);
349 return ret;
350 }
351
352 PyObject *
353 libvirt_virNetworkPortPtrWrap(virNetworkPortPtr node)
354 {
355 PyObject *ret;
356
357 if (node == NULL) {
358 return VIR_PY_NONE;
359 }
360
361 ret = libvirt_buildPyObject(node, "virNetworkPortPtr", NULL);
362 return ret;
363 }
364
365 PyObject *
366 libvirt_virInterfacePtrWrap(virInterfacePtr node)
367 {
368 PyObject *ret;
369
370 if (node == NULL) {
371 return VIR_PY_NONE;
372 }
373
374 ret = libvirt_buildPyObject(node, "virInterfacePtr", NULL);
375 return ret;
376 }
377
378 PyObject *
379 libvirt_virStoragePoolPtrWrap(virStoragePoolPtr node)
380 {
381 PyObject *ret;
382
383 if (node == NULL) {
384 return VIR_PY_NONE;
385 }
386
387 ret = libvirt_buildPyObject(node, "virStoragePoolPtr", NULL);
388 return ret;
389 }
390
391 PyObject *
392 libvirt_virStorageVolPtrWrap(virStorageVolPtr node)
393 {
394 PyObject *ret;
395
396 if (node == NULL) {
397 return VIR_PY_NONE;
398 }
399
400 ret = libvirt_buildPyObject(node, "virStorageVolPtr", NULL);
401 return ret;
402 }
403
404 PyObject *
405 libvirt_virConnectPtrWrap(virConnectPtr node)
406 {
407 PyObject *ret;
408
409 if (node == NULL) {
410 return VIR_PY_NONE;
411 }
412
413 ret = libvirt_buildPyObject(node, "virConnectPtr", NULL);
414 return ret;
415 }
416
417 PyObject *
418 libvirt_virNodeDevicePtrWrap(virNodeDevicePtr node)
419 {
420 PyObject *ret;
421
422 if (node == NULL) {
423 return VIR_PY_NONE;
424 }
425
426 ret = libvirt_buildPyObject(node, "virNodeDevicePtr", NULL);
427 return ret;
428 }
429
430 PyObject *
431 libvirt_virSecretPtrWrap(virSecretPtr node)
432 {
433 PyObject *ret;
434
435 if (node == NULL) {
436 return VIR_PY_NONE;
437 }
438
439 ret = libvirt_buildPyObject(node, "virSecretPtr", NULL);
440 return ret;
441 }
442
443 PyObject *
444 libvirt_virNWFilterPtrWrap(virNWFilterPtr node)
445 {
446 PyObject *ret;
447
448 if (node == NULL) {
449 return VIR_PY_NONE;
450 }
451
452 ret = libvirt_buildPyObject(node, "virNWFilterPtr", NULL);
453 return ret;
454 }
455
456 PyObject *
457 libvirt_virNWFilterBindingPtrWrap(virNWFilterBindingPtr node)
458 {
459 PyObject *ret;
460
461 if (node == NULL) {
462 return VIR_PY_NONE;
463 }
464
465 ret = libvirt_buildPyObject(node, "virNWFilterBindingPtr", NULL);
466 return ret;
467 }
468
469 PyObject *
470 libvirt_virStreamPtrWrap(virStreamPtr node)
471 {
472 PyObject *ret;
473
474 if (node == NULL) {
475 return VIR_PY_NONE;
476 }
477
478 ret = libvirt_buildPyObject(node, "virStreamPtr", NULL);
479 return ret;
480 }
481
482 PyObject *
483 libvirt_virDomainCheckpointPtrWrap(virDomainCheckpointPtr node)
484 {
485 PyObject *ret;
486
487 if (node == NULL) {
488 return VIR_PY_NONE;
489 }
490
491 ret = libvirt_buildPyObject(node, "virDomainCheckpointPtr", NULL);
492 return ret;
493 }
494
495 PyObject *
496 libvirt_virDomainSnapshotPtrWrap(virDomainSnapshotPtr node)
497 {
498 PyObject *ret;
499
500 if (node == NULL) {
501 return VIR_PY_NONE;
502 }
503
504 ret = libvirt_buildPyObject(node, "virDomainSnapshotPtr", NULL);
505 return ret;
506 }
507
508 PyObject *
509 libvirt_virEventHandleCallbackWrap(virEventHandleCallback node)
510 {
511 PyObject *ret;
512
513 if (node == NULL) {
514 printf("%s: WARNING - Wrapping None\n", __func__);
515 return VIR_PY_NONE;
516 }
517
518 ret = libvirt_buildPyObject(node, "virEventHandleCallback", NULL);
519 return ret;
520 }
521
522 PyObject *
523 libvirt_virEventTimeoutCallbackWrap(virEventTimeoutCallback node)
524 {
525 PyObject *ret;
526
527 if (node == NULL) {
528 printf("%s: WARNING - Wrapping None\n", __func__);
529 return VIR_PY_NONE;
530 }
531
532 ret = libvirt_buildPyObject(node, "virEventTimeoutCallback", NULL);
533 return ret;
534 }
535
536 PyObject *
537 libvirt_virFreeCallbackWrap(virFreeCallback node)
538 {
539 PyObject *ret;
540
541 if (node == NULL) {
542 return VIR_PY_NONE;
543 }
544
545 ret = libvirt_buildPyObject(node, "virFreeCallback", NULL);
546 return ret;
547 }
548
549 PyObject *
550 libvirt_virVoidPtrWrap(void* node)
551 {
552 PyObject *ret;
553
554 if (node == NULL) {
555 return VIR_PY_NONE;
556 }
557
558 ret = libvirt_buildPyObject(node, "void*", NULL);
559 return ret;
560 }