[PATCH v2] doc/python: Update to Python 3

Philipp Hahn posted 1 patch 3 years, 12 months ago
Test syntax-check failed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/a080aa447281ed32121f31822b7c6fa0d34804c3.1587387652.git.hahn@univention.de
docs/python.html.in | 18 +++++++++---------
1 file changed, 9 insertions(+), 9 deletions(-)
[PATCH v2] doc/python: Update to Python 3
Posted by Philipp Hahn 3 years, 12 months ago
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

Signed-off-by: Philipp Hahn <hahn@univention.de>
---
 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:</p>
     is replaced by <code>virDomain::info()</code> which returns a list of
     <ol><li>state: one of the state values (virDomainState)</li><li>maxMemory: the maximum memory used by the domain</li><li>memory: the current amount of memory used by the domain</li><li>nbVirtCPU: the number of virtual CPU</li><li>cpuTime: the time used by the domain in nanoseconds</li></ol></li>
     </ul>
-    <p>So let's look at a simple example inspired from the <code>basic.py</code>
-test found in <code>python/tests/</code> in the source tree:</p>
+    <p>So let's look at a simple example:</p>
     <pre>import <span style="color: #0071FF; background-color: #FFFFFF">libvirt</span>
 import sys
 
-conn = <span style="color: #0071FF; background-color: #FFFFFF">libvirt</span>.openReadOnly(None)
-if conn == None:
-    print 'Failed to open connection to the hypervisor'
+try:
+    conn = <span style="color: #0071FF; background-color: #FFFFFF">libvirt</span>.openReadOnly(None)
+except <span style="color: #0071FF; background-color: #FFFFFF">libvirt</span>.libvirtError:
+    print('Failed to open connection to the hypervisor')
     sys.exit(1)
 
 try:
     dom0 = conn.<span style="color: #007F00; background-color: #FFFFFF">lookupByName</span>("Domain-0")
-except:
-    print 'Failed to find the main domain'
+except <span style="color: #0071FF; background-color: #FFFFFF">libvirt</span>.libvirtError:
+    print('Failed to find the main domain')
     sys.exit(1)
 
-print "Domain 0: id %d running %s" % (dom0.<span style="color: #FF0080; background-color: #FFFFFF">ID</span>(), dom0.<span style="color: #FF0080; background-color: #FFFFFF">OSType</span>())
-print dom0.<span style="color: #FF0080; background-color: #FFFFFF">info</span>()</pre>
+print("Domain 0: id %d running %s" % (dom0.<span style="color: #FF0080; background-color: #FFFFFF">ID</span>(), dom0.<span style="color: #FF0080; background-color: #FFFFFF">OSType</span>()))
+print(dom0.<span style="color: #FF0080; background-color: #FFFFFF">info</span>())</pre>
     <p>There is not much to comment about it, it really is a straight mapping
 from the C API, the only points to notice are:</p>
     <ul>
-- 
2.20.1


Re: [PATCH v2] doc/python: Update to Python 3
Posted by Daniel P. Berrangé 3 years, 12 months ago
On Mon, Apr 20, 2020 at 03:01:11PM +0200, Philipp Hahn wrote:
> 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
> 
> Signed-off-by: Philipp Hahn <hahn@univention.de>
> ---
>  docs/python.html.in | 18 +++++++++---------
>  1 file changed, 9 insertions(+), 9 deletions(-)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>

and pushed to git.

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|