[Libvir] [PATCH] Add Python binding for virGetVersion

This patch adds a Python binding for the virGetVersion call (called libvirt.getVersion). $ python Python 2.4.4 (#1, Oct 23 2006, 13:58:18) [GCC 4.1.1 20061011 (Red Hat 4.1.1-30)] on linux2 Type "help", "copyright", "credits" or "license" for more information.
import libvirt; libvirt.getVersion (None); (2002, 3000001) libvirt.getVersion ("QEMU"); (2002, 2002)
libvirt.getVersion ("not_here");
It also gives you a way to detect if a particular driver is compiled in, which I guess could be quite useful: libvir: error : no support for hypervisor Traceback (most recent call last): File "<stdin>", line 1, in ? File "/home/rjones/local/lib/python2.4/site-packages/libvirt.py", line 95, in getVersion if ret is None: raise libvirtError ("virGetVersion() failed") libvirt.libvirtError: virGetVersion() failed no support for hypervisor Rich. -- Emerging Technologies, Red Hat - http://et.redhat.com/~rjones/ Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SL4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 03798903

On Wed, May 23, 2007 at 03:58:02PM +0100, Richard W.M. Jones wrote:
This patch adds a Python binding for the virGetVersion call (called libvirt.getVersion).
Looks good to me. Dan. -- |=- Red Hat, Engineering, Emerging Technologies, Boston. +1 978 392 2496 -=| |=- Perl modules: http://search.cpan.org/~danberr/ -=| |=- Projects: http://freshmeat.net/~danielpb/ -=| |=- GnuPG: 7D3B9505 F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 -=|

Daniel P. Berrange wrote:
On Wed, May 23, 2007 at 03:58:02PM +0100, Richard W.M. Jones wrote:
This patch adds a Python binding for the virGetVersion call (called libvirt.getVersion).
Looks good to me.
Actually, it contains a subtle error. Improved patch coming up ... Rich. -- Emerging Technologies, Red Hat - http://et.redhat.com/~rjones/ Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SL4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 03798903

Richard W.M. Jones wrote:
Daniel P. Berrange wrote:
On Wed, May 23, 2007 at 03:58:02PM +0100, Richard W.M. Jones wrote:
This patch adds a Python binding for the virGetVersion call (called libvirt.getVersion).
Looks good to me.
Actually, it contains a subtle error. Improved patch coming up ...
import libvirt; libvirt.getVersion (None); (2002, 0) libvirt.getVersion ("Xen"); (2002, 3000001) libvirt.getVersion ("QEMU"); (2002, 2002) libvirt.getVersion ("not_here");
The error was in the handling of the case where you pass None to the function. Previously that would pass a NULL type and non-NULL typeVer to the underlying C function, which the underlying C function would interpret as a request to get the version of the Xen driver. Bad idea if libvirt is compiled --without-xen. The new version treats None as meaning that we just want to get the version of the main library. The semantics of the new version are described in the Python documentation: + """Return a tuple containing the version of the library and + a version of the driver with the given name. + + If the name passed is None then only the first element of the + tuple (library version) is meaningful. The second element will + be zero. + + If the name passed refers to a non-existent driver, then you + will get the exception 'no support for hypervisor'. + + Versions are 1000000*major + 1000*minor + release.""" and shown in the example below: $ python Python 2.4.4 (#1, Oct 23 2006, 13:58:18) [GCC 4.1.1 20061011 (Red Hat 4.1.1-30)] on linux2 Type "help", "copyright", "credits" or "license" for more information. libvir: error : no support for hypervisor Traceback (most recent call last): File "<stdin>", line 1, in ? File "/home/rjones/local/lib/python2.4/site-packages/libvirt.py", line 102, in getVersion if ret is None: raise libvirtError ("virGetVersion() failed") libvirt.libvirtError: virGetVersion() failed no support for hypervisor Rich. -- Emerging Technologies, Red Hat - http://et.redhat.com/~rjones/ Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SL4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 03798903

Hey, So, the semantics for virGetVersion() are: - virGetVersion(&ver, NULL, NULL) returns the libvirt version only - virGetVersion(&ver, NULL, &xenVer) returns the libvirt version and the xen version - virGetVersion(&ver, "foo", &fooVer) returns the libvirt version and the version of the "foo" driver Now, your python version: - virGetVersion() isn't allowed - virGetVersion(None) returns the libvirt version only - virGetVersion("foo") returns the libvirt version and the version of the "foo" driver, or throws an exception if the driver isn't available I'd suggest: - virGetVersion() returns the libvirt version only, perhaps as a single integer - virGetVersion(None) returns the libvirt version and the Xen version, or throws an exception if xen isn't available - virGetVersion("foo") returns the libvirt version and the version of the "foo" driver, or throws an exception if the driver isn't available i.e. static PyObject * libvirt_virGetVersion (PyObject *self ATTRIBUTE_UNUSED, PyObject *args) { char *type = NULL; unsigned long libVer, typeVer = 0; int noTypeVer = 0; int c_retval; if (PyTuple_Size(args) == 0) noTypeVer = 1 else if (!PyArg_ParseTuple (args, (char *) "z", &type)) return NULL; LIBVIRT_BEGIN_ALLOW_THREADS; if (noTypeVer) c_retval = virGetVersion (&libVer, NULL, NULL); else c_retval = virGetVersion (&libVer, type, &typeVer); LIBVIRT_END_ALLOW_THREADS; if (c_retval == -1) { Py_INCREF(Py_None); return (Py_None); } if (noTypeVer) return PyInt_FromLong(libVer); else return Py_BuildValue ((char *) "kk", libVer, typeVer); } Cheers, Mark.

Thanks Mark, Here's a patch which does that. Example usage:
import libvirt; libvirt.getVersion (); 2002 libvirt.getVersion ("QEMU"); (2002, 2002) libvirt.getVersion ("Test"); (2002, 2002) libvirt.getVersion ("Xen"); (2002, 3000001) libvirt.getVersion (None); 2002
Rich. -- Emerging Technologies, Red Hat - http://et.redhat.com/~rjones/ Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SL4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 03798903

BTW, it doesn't seem to make any sense at all to have virGetVersion be case sensitive. Fixed in the trivial patch attached. Rich. -- Emerging Technologies, Red Hat - http://et.redhat.com/~rjones/ Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SL4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 03798903

On Tue, 2007-05-29 at 12:43 +0100, Richard W.M. Jones wrote:
libvirt.getVersion ("Xen"); (2002, 3000001) libvirt.getVersion (None); 2002
I don't think it's a big deal, but I was suggesting these two should be equivalent - i.e. None == "Xen". That's why the code I posted explicitly checked for a zero length arg list - if you used "|z" with PyArg_ParseTuple(), you wouldn't be able[1] to distinguish between no args and None. Either that, or just don't allow None as an argument at all - i.e. pass "|s" to PyArg_ParseTuple(). Cheers, Mark. [1] - barring doing something ugly like: char *type = (char *) -1; parse(args, "|z", &type); if (type == (char *) -1) return libVer; else if (type == NULL) return getVer("xen"); else return getVer(type);
participants (3)
-
Daniel P. Berrange
-
Mark McLoughlin
-
Richard W.M. Jones