[Libguestfs] [PATCH v3 4/4] python: Implement Pointer ("virDomainPtr", _) (RHBZ#1075164).

Richard W.M. Jones rjones at redhat.com
Thu Dec 11 11:31:43 UTC 2014


This allows the Python binding of guestfs_add_libvirt_dom to work.

There is a regression test to ensure this keeps working.

Note this requires a patched libvirt-python, supporting the
c_pointer() method.
---
 README                  |  3 +++
 generator/python.ml     | 19 +++++++++++--------
 python/run-python-tests |  3 +++
 python/t/910-libvirt.py | 42 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 59 insertions(+), 8 deletions(-)
 create mode 100644 python/t/910-libvirt.py

diff --git a/README b/README
index fc88eb9..9962ae8 100644
--- a/README
+++ b/README
@@ -223,6 +223,9 @@ The full requirements are described below.
 +--------------+-------------+---+-----------------------------------------+
 | Sys::Virt    |             | O | Perl bindings for libvirt.              |
 +--------------+-------------+---+-----------------------------------------+
+| libvirt-python             | O | For testing Python libvirt/libguestfs   |
+|              |             |   | interactions.                           |
++--------------+-------------+---+-----------------------------------------+
 | Win::Hivex   |             | O | Perl bindings for hivex.                |
 +--------------+-------------+---+-----------------------------------------+
 | Pod::Usage   |             | O | Perl module used by tests.              |
diff --git a/generator/python.ml b/generator/python.ml
index 82afb2e..e47195f 100644
--- a/generator/python.ml
+++ b/generator/python.ml
@@ -294,8 +294,8 @@ put_table (char * const * const argv)
         | Int n -> pr "  int %s;\n" n
         | Int64 n -> pr "  long long %s;\n" n
         | Pointer (t, n) ->
-            pr "  long long %s_int64;\n" n;
-            pr "  void * /* %s */ %s;\n" t n
+            pr "  void * /* %s */ %s;\n" t n;
+            pr "  PyObject *%s_long;\n" n
       ) args;
 
       (* Fetch the optional arguments as objects, so we can detect
@@ -324,11 +324,12 @@ put_table (char * const * const argv)
         | StringList _ | DeviceList _ -> pr "O"
         | Bool _ -> pr "i" (* XXX Python has booleans? *)
         | Int _ -> pr "i"
-        | Int64 _ | Pointer _ ->
+        | Int64 _ ->
             (* XXX Whoever thought it was a good idea to
              * emulate C's int/long/long long in Python?
              *)
             pr "L"
+        | Pointer _ -> pr "O"
         | BufferIn _ -> pr "s#"
       ) args;
 
@@ -347,7 +348,7 @@ put_table (char * const * const argv)
         | Bool n -> pr ", &%s" n
         | Int n -> pr ", &%s" n
         | Int64 n -> pr ", &%s" n
-        | Pointer (_, n) -> pr ", &%s_int64" n
+        | Pointer (_, n) -> pr ", &%s_long" n
         | BufferIn n -> pr ", &%s, &%s_size" n n
       ) args;
 
@@ -369,8 +370,8 @@ put_table (char * const * const argv)
         | StringList n | DeviceList n ->
             pr "  %s = get_string_list (py_%s);\n" n n;
             pr "  if (!%s) goto out;\n" n
-        | Pointer (t, n) ->
-            pr "  %s = POINTER_NOT_IMPLEMENTED (\"%s\");\n" n t
+        | Pointer (_, n) ->
+            pr "  %s = PyLong_AsVoidPtr (%s_long);\n" n n
       ) args;
 
       pr "\n";
@@ -798,9 +799,11 @@ class GuestFS(object):
         | Pathname _ | Device _ | Mountable _
         | Dev_or_Path _ | Mountable_or_Path _ | String _ | Key _
         | FileIn _ | FileOut _ | OptString _ | Bool _ | Int _ | Int64 _
-        | BufferIn _ | Pointer _ | GUID _ -> ()
+        | BufferIn _ | GUID _ -> ()
         | StringList n | DeviceList n ->
-            pr "        %s = list (%s)\n" n n
+          pr "        %s = list (%s)\n" n n
+        | Pointer (_, n) ->
+          pr "        %s = %s.c_pointer()\n" n n
       ) args;
       pr "        self._check_not_closed ()\n";
       pr "        r = libguestfsmod.%s (self._o" f.name;
diff --git a/python/run-python-tests b/python/run-python-tests
index af849c7..285bb18 100755
--- a/python/run-python-tests
+++ b/python/run-python-tests
@@ -18,6 +18,9 @@
 
 errors=0
 
+guestsdir="$(cd ../tests/guests && pwd)"
+export guestsdir
+
 for f in $srcdir/t/*.py; do
   $PYTHON $f
   r=$?
diff --git a/python/t/910-libvirt.py b/python/t/910-libvirt.py
new file mode 100644
index 0000000..f39a371
--- /dev/null
+++ b/python/t/910-libvirt.py
@@ -0,0 +1,42 @@
+# libguestfs Python bindings
+# Copyright (C) 2014 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+# The Python bindings for add_libvirt_dom use some internal knowledge
+# of the libvirt-python library.  Ensure this keeps working by testing
+# it.  See: https://bugzilla.redhat.com/show_bug.cgi?id=1075164
+
+from types import *
+import os
+import guestfs
+
+guestsdir = os.environ['guestsdir']
+
+try:
+    import libvirt
+except:
+    print "could not import python-libvirt"
+    exit (77)
+
+conn = libvirt.open ("test:///%s/guests.xml" % guestsdir)
+dom = conn.lookupByName ("blank-disk")
+
+g = guestfs.GuestFS ()
+
+r = g.add_libvirt_dom (dom, readonly=1)
+
+if r != 1:
+    raise "unexpected return value from add_libvirt_dom (%d)" % r
-- 
2.1.0




More information about the Libguestfs mailing list