[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Libvir] python/libvir.html example needs adjustment


Trying a variant of this example from python/libvir.html:

  import libvirt
  import sys

  conn = libvirt.openReadOnly(None)
  if conn == None:
      print 'Failed to open connection to the hypervisor'

  dom0 = conn.lookupByName("Domain-0")
  if dom0 == None:
      print 'Failed to find the main domain'

  print "Domain 0: id %d running %s" % (dom0.ID(), dom0.OSType())
  print dom0.info()

I found that conn.lookupByName throws an exception when the specified
name is not found.  If the example is just out of date, it should use
try:/except: instead of comparing dom0 to None, shouldn't it?

You can demonstrate it like this:

  $ python -c 'import libvirt; libvirt.openReadOnly(None).lookupByName("x")'
  libvir: Xen Daemon error : GET operation failed: No such domain x
  Traceback (most recent call last):
    File "<string>", line 1, in ?
    File "/usr/lib/python2.4/site-packages/libvirt.py", line 213, in lookupByName
      if ret is None:raise libvirtError('virDomainLookupByName() failed')
  libvirt.libvirtError: virDomainLookupByName() failed
  [Exit 1]


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]