From 2cac52000a6a96a583af72e289a4296c596047d5 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Fri, 22 Apr 2011 21:36:41 +0100 Subject: [PATCH] python: Implement new event API. This implements set_event_callback and delete_event_callback so that Python programs can use the new event mechanism. --- generator/.depend | 6 +- generator/generator_python.ml | 42 ++++++++++++ python/guestfs-py-byhand.c | 155 ++++++++++++++++++++++++++++++++++++++++++ python/guestfs-py.h | 2 + python/t/400-events.py | 58 ++++++++++++++++ 5 files changed, 261 insertions(+), 2 deletions(-) create mode 100644 python/t/400-events.py diff --git a/generator/.depend b/generator/.depend index 74db81e..b2963f2 100644 --- a/generator/.depend +++ b/generator/.depend @@ -86,10 +86,12 @@ generator_perl.cmx: generator_utils.cmx generator_types.cmx \ generator_actions.cmx generator_python.cmo: generator_utils.cmi generator_types.cmo \ generator_structs.cmi generator_pr.cmi generator_optgroups.cmo \ - generator_docstrings.cmo generator_c.cmo generator_actions.cmi + generator_events.cmo generator_docstrings.cmo generator_c.cmo \ + generator_actions.cmi generator_python.cmx: generator_utils.cmx generator_types.cmx \ generator_structs.cmx generator_pr.cmx generator_optgroups.cmx \ - generator_docstrings.cmx generator_c.cmx generator_actions.cmx + generator_events.cmx generator_docstrings.cmx generator_c.cmx \ + generator_actions.cmx generator_ruby.cmo: generator_utils.cmi generator_types.cmo \ generator_structs.cmi generator_pr.cmi generator_optgroups.cmo \ generator_events.cmo generator_docstrings.cmo generator_c.cmo \ diff --git a/generator/generator_python.ml b/generator/generator_python.ml index f3a2a94..ffd63da 100644 --- a/generator/generator_python.ml +++ b/generator/generator_python.ml @@ -28,6 +28,7 @@ open Generator_optgroups open Generator_actions open Generator_structs open Generator_c +open Generator_events (* Generate Python C module. *) let rec generate_python_c () = @@ -455,6 +456,10 @@ free_strings (char **argv) pr "static PyMethodDef methods[] = {\n"; pr " { (char *) \"create\", py_guestfs_create, METH_VARARGS, NULL },\n"; pr " { (char *) \"close\", py_guestfs_close, METH_VARARGS, NULL },\n"; + pr " { (char *) \"set_event_callback\",\n"; + pr " py_guestfs_set_event_callback, METH_VARARGS, NULL },\n"; + pr " { (char *) \"delete_event_callback\",\n"; + pr " py_guestfs_delete_event_callback, METH_VARARGS, NULL },\n"; List.iter ( fun (name, _, _, _, _, _, _) -> pr " { (char *) \"%s\", py_guestfs_%s, METH_VARARGS, NULL },\n" @@ -531,6 +536,15 @@ logvols = g.lvs () import libguestfsmod +"; + + List.iter ( + fun (name, bitmask) -> + pr "EVENT_%s = 0x%x\n" (String.uppercase name) bitmask + ) events; + pr "\n"; + + pr "\ class GuestFS: \"\"\"Instances of this class are libguestfs API handles.\"\"\" @@ -541,6 +555,34 @@ class GuestFS: def __del__ (self): libguestfsmod.close (self._o) + def set_event_callback (self, cb, event_bitmask): + u\"\"\"Register an event callback. + + Register \"cb\" as a callback function for all of the + events in \"event_bitmask\". \"event_bitmask\" should be + one or more \"guestfs.EVENT_*\" flags logically or'd together. + + This function returns an event handle which can be used + to delete the callback (see \"delete_event_callback\"). + + The callback function receives 4 parameters: + + cb (event, event_handle, buf, array) + + \"event\" is one of the \"EVENT_*\" flags. \"buf\" is a + message buffer (only for some types of events). \"array\" + is an array of integers (only for some types of events). + + You should read the documentation for + \"guestfs_set_event_callback\" in guestfs(3) before using + this function. + \"\"\" + return libguestfsmod.set_event_callback (self._o, cb, event_bitmask) + + def delete_event_callback (self, event_handle): + u\"\"\"Delete an event callback.\"\"\" + libguestfsmod.delete_event_callback (self._o, event_handle) + "; List.iter ( diff --git a/python/guestfs-py-byhand.c b/python/guestfs-py-byhand.c index f454a7e..af9686f 100644 --- a/python/guestfs-py-byhand.c +++ b/python/guestfs-py-byhand.c @@ -29,6 +29,8 @@ #include "guestfs-py.h" +static PyObject **get_all_event_callbacks (guestfs_h *g, size_t *len_rtn); + PyObject * py_guestfs_create (PyObject *self, PyObject *args) { @@ -53,17 +55,170 @@ py_guestfs_close (PyObject *self, PyObject *args) PyThreadState *py_save = NULL; PyObject *py_g; guestfs_h *g; + size_t i, len; + PyObject **callbacks; if (!PyArg_ParseTuple (args, (char *) "O:guestfs_close", &py_g)) return NULL; g = get_handle (py_g); + /* As in the OCaml bindings, there is a hard to solve case where the + * caller can delete a callback from within the callback, resulting + * in a double-free here. XXX + */ + callbacks = get_all_event_callbacks (g, &len); + if (PyEval_ThreadsInitialized ()) py_save = PyEval_SaveThread (); guestfs_close (g); if (PyEval_ThreadsInitialized ()) PyEval_RestoreThread (py_save); + for (i = 0; i < len; ++i) + Py_XDECREF (callbacks[i]); + Py_INCREF (Py_None); return Py_None; } + +/* http://docs.python.org/release/2.5.2/ext/callingPython.html */ +static void +py_guestfs_event_callback_wrapper (guestfs_h *g, + void *callback, + uint64_t event, + int event_handle, + int flags, + const char *buf, size_t buf_len, + const uint64_t *array, size_t array_len) +{ + PyObject *py_callback = callback; + PyObject *py_array; + PyObject *args; + PyObject *a; + size_t i; + PyObject *py_r; + + py_array = PyList_New (array_len); + for (i = 0; i < array_len; ++i) { + a = PyLong_FromLongLong (array[i]); + PyList_SET_ITEM (py_array, i, a); + } + + /* XXX As with Perl we don't pass the guestfs_h handle here. */ + args = Py_BuildValue ("(Kis#O)", + (unsigned PY_LONG_LONG) event, event_handle, + buf, buf_len, py_array); + + py_r = PyEval_CallObject (py_callback, args); + Py_DECREF (args); + + if (py_r != NULL) + Py_DECREF (py_r); + else + /* Callback threw an exception: print it. */ + PyErr_PrintEx (0); +} + +PyObject * +py_guestfs_set_event_callback (PyObject *self, PyObject *args) +{ + PyObject *py_g; + guestfs_h *g; + PyObject *py_callback; + unsigned PY_LONG_LONG events; + int eh; + PyObject *py_eh; + char key[64]; + + if (!PyArg_ParseTuple (args, (char *) "OOK:guestfs_set_event_callback", + &py_g, &py_callback, &events)) + return NULL; + + if (!PyCallable_Check (py_callback)) { + PyErr_SetString (PyExc_TypeError, + "callback parameter is not callable " + "(eg. lambda or function)"); + return NULL; + } + + g = get_handle (py_g); + + eh = guestfs_set_event_callback (g, py_guestfs_event_callback_wrapper, + events, 0, py_callback); + if (eh == -1) { + PyErr_SetString (PyExc_RuntimeError, guestfs_last_error (g)); + return NULL; + } + + /* Increase the refcount for this callback since we are storing it + * in the opaque C libguestfs handle. We need to remember that we + * did this, so we can decrease the refcount for all undeleted + * callbacks left around at close time (see py_guestfs_close). + */ + Py_XINCREF (py_callback); + + snprintf (key, sizeof key, "_python_event_%d", eh); + guestfs_set_private (g, key, py_callback); + + py_eh = PyInt_FromLong ((long) eh); + return py_eh; +} + +PyObject * +py_guestfs_delete_event_callback (PyObject *self, PyObject *args) +{ + PyObject *py_g; + guestfs_h *g; + int eh; + PyObject *py_callback; + char key[64]; + + if (!PyArg_ParseTuple (args, (char *) "Oi:guestfs_delete_event_callback", + &py_g, &eh)) + return NULL; + g = get_handle (py_g); + + snprintf (key, sizeof key, "_python_event_%d", eh); + py_callback = guestfs_get_private (g, key); + if (py_callback) { + Py_XDECREF (py_callback); + guestfs_set_private (g, key, NULL); + guestfs_delete_event_callback (g, eh); + } + + Py_INCREF (Py_None); + return Py_None; +} + +static PyObject ** +get_all_event_callbacks (guestfs_h *g, size_t *len_rtn) +{ + PyObject **r; + size_t i; + const char *key; + PyObject *cb; + + /* Count the length of the array that will be needed. */ + *len_rtn = 0; + cb = guestfs_first_private (g, &key); + while (cb != NULL) { + if (strncmp (key, "_python_event_", strlen ("_python_event_")) == 0) + (*len_rtn)++; + cb = guestfs_next_private (g, &key); + } + + /* Copy them into the return array. */ + r = guestfs_safe_malloc (g, sizeof (PyObject *) * (*len_rtn)); + + i = 0; + cb = guestfs_first_private (g, &key); + while (cb != NULL) { + if (strncmp (key, "_python_event_", strlen ("_python_event_")) == 0) { + r[i] = cb; + i++; + } + cb = guestfs_next_private (g, &key); + } + + return r; +} diff --git a/python/guestfs-py.h b/python/guestfs-py.h index 6b7e05f..29e84ba 100644 --- a/python/guestfs-py.h +++ b/python/guestfs-py.h @@ -63,5 +63,7 @@ put_handle (guestfs_h *g) extern PyObject *py_guestfs_create (PyObject *self, PyObject *args); extern PyObject *py_guestfs_close (PyObject *self, PyObject *args); +extern PyObject *py_guestfs_set_event_callback (PyObject *self, PyObject *args); +extern PyObject *py_guestfs_delete_event_callback (PyObject *self, PyObject *args); #endif /* guestfs_py_h */ diff --git a/python/t/400-events.py b/python/t/400-events.py new file mode 100644 index 0000000..1c29b00 --- /dev/null +++ b/python/t/400-events.py @@ -0,0 +1,58 @@ +# libguestfs Python bindings +# Copyright (C) 2011 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +import os +import guestfs + +g = guestfs.GuestFS() + +def log_callback (ev,eh,buf,array): + if ev == guestfs.EVENT_APPLIANCE: + buf = buf.rstrip() + + # Log what happened. + print ("python event logged: event=0x%x eh=%d buf='%s' array=%s" % + (ev, eh, buf, array)) + +close_invoked = 0 + +def close_callback (ev, eh, buf, array): + global close_invoked + close_invoked += 1 + log_callback (ev, eh, buf, array) + +# Register an event callback for all log messages. +events = guestfs.EVENT_APPLIANCE | guestfs.EVENT_LIBRARY | guestfs.EVENT_TRACE +g.set_event_callback (log_callback, events) + +# Register a callback for the close event. +g.set_event_callback (close_callback, guestfs.EVENT_CLOSE) + +# Now make sure we see some messages. +g.set_trace (1) +g.set_verbose (1) + +# Do some stuff. +g.add_drive_ro ("/dev/null") +g.set_autosync (1) + +# Close the handle. The close callback should be invoked. +if close_invoked != 0: + raise "Error: close_invoked should be 0" +del g +if close_invoked != 1: + raise "Error: close_invoked should be 1" -- 1.8.3.1