[libvirt] [PATCH python] libvirtaio: Fix compat with python 3.7

Cole Robinson crobinso at redhat.com
Mon Jun 25 18:35:28 UTC 2018


In python 3.7, async is now a keyword, so this throws a syntax error:

  File "/usr/lib64/python3.7/site-packages/libvirtaio.py", line 49
    from asyncio import async as ensure_future
                            ^
  SyntaxError: invalid syntax

Switch to getattr trickery to accomplish the same goal

Signed-off-by: Cole Robinson <crobinso at redhat.com>
---
 libvirtaio.py | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

If we don't care about python3 < 3.4.4 compat, we can just do

  from asyncio import ensure_future

diff --git a/libvirtaio.py b/libvirtaio.py
index 1c432dd..9d517e6 100644
--- a/libvirtaio.py
+++ b/libvirtaio.py
@@ -43,10 +43,12 @@ import warnings
 
 import libvirt
 
-try:
-    from asyncio import ensure_future
-except ImportError:
-    from asyncio import async as ensure_future
+# python < 3.4.4: we want 'async'
+# python >= 3.4.4 < 3.7, we want 'ensure_future'
+# python >= 3.7, 'async' is a reserved keyword, so we can't import it
+ensure_future = getattr(asyncio, "ensure_future", None)
+if not ensure_future:
+    ensure_future = getattr(asyncio, "async")
 
 
 class Callback(object):
-- 
2.17.1




More information about the libvir-list mailing list