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

Cole Robinson posted 1 patch 5 years, 10 months ago
Failed in applying to current master (apply log)
libvirtaio.py | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
[libvirt] [PATCH python] libvirtaio: Fix compat with python 3.7
Posted by Cole Robinson 5 years, 10 months ago
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@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

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH python] libvirtaio: Fix compat with python 3.7
Posted by Cole Robinson 5 years, 10 months ago
On 06/25/2018 02:35 PM, Cole Robinson wrote:
> 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@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
> 

Also I meant to say, this was prompted by python3.7 rebase in Fedora:

https://lists.fedoraproject.org/archives/list/devel@lists.fedoraproject.org/message/5DOPLN7K7GCI3T6ZMXDBIPJW5Y7ESGYS/

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH python] libvirtaio: Fix compat with python 3.7
Posted by Andrea Bolognani 5 years, 10 months ago
On Mon, 2018-06-25 at 14:35 -0400, Cole Robinson wrote:
> If we don't care about python3 < 3.4.4 compat, we can just do
> 
>   from asyncio import ensure_future

Debian 8, which is still a supported target platform[1], only
has Python 3.4.2.


[1] At least for libvirt; I'm going to assume libvirt-python
    doesn't have its own support policy.
-- 
Andrea Bolognani / Red Hat / Virtualization

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH python] libvirtaio: Fix compat with python 3.7
Posted by Pavel Hrdina 5 years, 10 months ago
On Mon, Jun 25, 2018 at 02:35:28PM -0400, Cole Robinson wrote:
> 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@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'

This is slightly misleading, I would remove the ' < 3.7' part as we
need 'ensure_future' even for that versions.  The following explanation
is good enough.

> +# 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")

Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH python] libvirtaio: Fix compat with python 3.7
Posted by Andrea Bolognani 5 years, 10 months ago
On Mon, 2018-06-25 at 14:35 -0400, Cole Robinson wrote:
[...]
> -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")

Python is not exactly my forte, but the above makes sense to me
and it stood up to some testing across all platforms supported by
libvirt, so

  Reviewed-by: Andrea Bolognani <abologna@redhat.com>

One nit is that the comment above the code doesn't IMHO describe
the situation properly: I would have worded it along the lines of

  # Python < 3.4.4 doesn't have 'ensure_future', so we have to fall
  # back to 'async'; however, since 'async' is a reserved keyword
  # in Python >= 3.7, we can't perform a straightforward import and
  # we have to resort to getattr() instead

I leave it up to you whether or not you want to reword the comment.

-- 
Andrea Bolognani / Red Hat / Virtualization

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH python] libvirtaio: Fix compat with python 3.7
Posted by Cole Robinson 5 years, 10 months ago
On 06/27/2018 07:50 AM, Andrea Bolognani wrote:
> On Mon, 2018-06-25 at 14:35 -0400, Cole Robinson wrote:
> [...]
>> -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")
> 
> Python is not exactly my forte, but the above makes sense to me
> and it stood up to some testing across all platforms supported by
> libvirt, so
> 
>   Reviewed-by: Andrea Bolognani <abologna@redhat.com>
> 
> One nit is that the comment above the code doesn't IMHO describe
> the situation properly: I would have worded it along the lines of
> 
>   # Python < 3.4.4 doesn't have 'ensure_future', so we have to fall
>   # back to 'async'; however, since 'async' is a reserved keyword
>   # in Python >= 3.7, we can't perform a straightforward import and
>   # we have to resort to getattr() instead
> 
> I leave it up to you whether or not you want to reword the comment.
> 

I used your comment and pushed it now

Thanks,
Cole

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list