1 /* libguestfs python bindings
2 * Copyright (C) 2009-2011 Red Hat Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 /* This file contains a small number of functions that are written by
20 * hand. The majority of the bindings are generated (see
30 #include "guestfs-py.h"
32 static PyObject **get_all_event_callbacks (guestfs_h *g, size_t *len_rtn);
35 py_guestfs_create (PyObject *self, PyObject *args)
39 g = guestfs_create ();
41 PyErr_SetString (PyExc_RuntimeError,
42 "guestfs.create: failed to allocate handle");
45 guestfs_set_error_handler (g, NULL, NULL);
46 /* This can return NULL, but in that case put_handle will have
47 * set the Python error string.
49 return put_handle (g);
53 py_guestfs_close (PyObject *self, PyObject *args)
55 PyThreadState *py_save = NULL;
61 if (!PyArg_ParseTuple (args, (char *) "O:guestfs_close", &py_g))
63 g = get_handle (py_g);
65 /* As in the OCaml bindings, there is a hard to solve case where the
66 * caller can delete a callback from within the callback, resulting
67 * in a double-free here. XXX
69 callbacks = get_all_event_callbacks (g, &len);
71 if (PyEval_ThreadsInitialized ())
72 py_save = PyEval_SaveThread ();
74 if (PyEval_ThreadsInitialized ())
75 PyEval_RestoreThread (py_save);
77 for (i = 0; i < len; ++i)
78 Py_XDECREF (callbacks[i]);
84 /* http://docs.python.org/release/2.5.2/ext/callingPython.html */
86 py_guestfs_event_callback_wrapper (guestfs_h *g,
91 const char *buf, size_t buf_len,
92 const uint64_t *array, size_t array_len)
94 PyGILState_STATE py_save = PyGILState_UNLOCKED;
95 PyObject *py_callback = callback;
102 py_array = PyList_New (array_len);
103 for (i = 0; i < array_len; ++i) {
104 a = PyLong_FromLongLong (array[i]);
105 PyList_SET_ITEM (py_array, i, a);
108 /* XXX As with Perl we don't pass the guestfs_h handle here. */
109 args = Py_BuildValue ("(Kis#O)",
110 (unsigned PY_LONG_LONG) event, event_handle,
111 buf, buf_len, py_array);
113 if (PyEval_ThreadsInitialized ())
114 py_save = PyGILState_Ensure ();
116 py_r = PyEval_CallObject (py_callback, args);
118 if (PyEval_ThreadsInitialized ())
119 PyGILState_Release (py_save);
126 /* Callback threw an exception: print it. */
131 py_guestfs_set_event_callback (PyObject *self, PyObject *args)
135 PyObject *py_callback;
136 unsigned PY_LONG_LONG events;
141 if (!PyArg_ParseTuple (args, (char *) "OOK:guestfs_set_event_callback",
142 &py_g, &py_callback, &events))
145 if (!PyCallable_Check (py_callback)) {
146 PyErr_SetString (PyExc_TypeError,
147 "callback parameter is not callable "
148 "(eg. lambda or function)");
152 g = get_handle (py_g);
154 eh = guestfs_set_event_callback (g, py_guestfs_event_callback_wrapper,
155 events, 0, py_callback);
157 PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g));
161 /* Increase the refcount for this callback since we are storing it
162 * in the opaque C libguestfs handle. We need to remember that we
163 * did this, so we can decrease the refcount for all undeleted
164 * callbacks left around at close time (see py_guestfs_close).
166 Py_XINCREF (py_callback);
168 snprintf (key, sizeof key, "_python_event_%d", eh);
169 guestfs_set_private (g, key, py_callback);
171 py_eh = PyLong_FromLong ((long) eh);
176 py_guestfs_delete_event_callback (PyObject *self, PyObject *args)
181 PyObject *py_callback;
184 if (!PyArg_ParseTuple (args, (char *) "Oi:guestfs_delete_event_callback",
187 g = get_handle (py_g);
189 snprintf (key, sizeof key, "_python_event_%d", eh);
190 py_callback = guestfs_get_private (g, key);
192 Py_XDECREF (py_callback);
193 guestfs_set_private (g, key, NULL);
194 guestfs_delete_event_callback (g, eh);
202 get_all_event_callbacks (guestfs_h *g, size_t *len_rtn)
209 /* Count the length of the array that will be needed. */
211 cb = guestfs_first_private (g, &key);
213 if (strncmp (key, "_python_event_", strlen ("_python_event_")) == 0)
215 cb = guestfs_next_private (g, &key);
218 /* Copy them into the return array. */
219 r = guestfs_safe_malloc (g, sizeof (PyObject *) * (*len_rtn));
222 cb = guestfs_first_private (g, &key);
224 if (strncmp (key, "_python_event_", strlen ("_python_event_")) == 0) {
228 cb = guestfs_next_private (g, &key);