From a7f0a3a272a847fef76950a173945fc9ad83f574 Mon Sep 17 00:00:00 2001 From: Philipp Hahn Date: Mon, 20 Apr 2020 15:01:11 +0200 Subject: [PATCH] doc/python: Update to Python 3 MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Convert the simple example to Python 3 syntax: - print() is a function - do not use bare except - libvirt.open*() does not return None but raises an exception The referenced source for the example was removed with 5bb2a245abbde4c0a407f631660e2f2c81bc4c02 Reviewed-by: Daniel P. Berrangé Signed-off-by: Philipp Hahn --- docs/python.html.in | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/python.html.in b/docs/python.html.in index e6e8cfade9..0f804da8c3 100644 --- a/docs/python.html.in +++ b/docs/python.html.in @@ -38,24 +38,24 @@ specificities in their argument conversions:

is replaced by virDomain::info() which returns a list of
  1. state: one of the state values (virDomainState)
  2. maxMemory: the maximum memory used by the domain
  3. memory: the current amount of memory used by the domain
  4. nbVirtCPU: the number of virtual CPU
  5. cpuTime: the time used by the domain in nanoseconds
-

So let's look at a simple example inspired from the basic.py -test found in python/tests/ in the source tree:

+

So let's look at a simple example:

import libvirt
 import sys
 
-conn = libvirt.openReadOnly(None)
-if conn == None:
-    print 'Failed to open connection to the hypervisor'
+try:
+    conn = libvirt.openReadOnly(None)
+except libvirt.libvirtError:
+    print('Failed to open connection to the hypervisor')
     sys.exit(1)
 
 try:
     dom0 = conn.lookupByName("Domain-0")
-except:
-    print 'Failed to find the main domain'
+except libvirt.libvirtError:
+    print('Failed to find the main domain')
     sys.exit(1)
 
-print "Domain 0: id %d running %s" % (dom0.ID(), dom0.OSType())
-print dom0.info()
+print("Domain 0: id %d running %s" % (dom0.ID(), dom0.OSType())) +print(dom0.info())

There is not much to comment about it, it really is a straight mapping from the C API, the only points to notice are:

    -- 2.47.2