python/libvir.html example needs adjustment

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

 



Hello,

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'
      sys.exit(1)

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

  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]

Jim


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]