[Qemu-devel] [PATCH] iotests: handle TypeError for Python3 in test 242

Andrey Shinkevich posted 1 patch 5 years, 1 month ago
Test docker-clang@ubuntu failed
Test asan failed
Test docker-mingw@fedora passed
Test checkpatch passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/1550519997-253534-1-git-send-email-andrey.shinkevich@virtuozzo.com
Maintainers: Kevin Wolf <kwolf@redhat.com>, Max Reitz <mreitz@redhat.com>
There is a newer version of this series
tests/qemu-iotests/242 | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
[Qemu-devel] [PATCH] iotests: handle TypeError for Python3 in test 242
Posted by Andrey Shinkevich 5 years, 1 month ago
To write one byte to disk, Python2 may use 'chr' type.
In Python3, conversion to 'byte' type is required.

Signed-off-by: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com>
---
 tests/qemu-iotests/242 | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/tests/qemu-iotests/242 b/tests/qemu-iotests/242
index 16c65ed..6b1f7b8 100755
--- a/tests/qemu-iotests/242
+++ b/tests/qemu-iotests/242
@@ -65,9 +65,14 @@ def toggle_flag(offset):
     with open(disk, "r+b") as f:
         f.seek(offset, 0)
         c = f.read(1)
-        toggled = chr(ord(c) ^ bitmap_flag_unknown)
+        toggled = ord(c) ^ bitmap_flag_unknown
         f.seek(-1, 1)
-        f.write(toggled)
+        try:
+            # python2
+            f.write(chr(toggled))
+        except TypeError:
+            # python3
+            f.write(bytes([toggled]))
 
 
 qemu_img_create('-f', iotests.imgfmt, disk, '1M')
-- 
1.8.3.1

Re: [Qemu-devel] [PATCH] iotests: handle TypeError for Python3 in test 242
Posted by Eric Blake 5 years, 1 month ago
[adding Eduardo for some python 2-vs-3 advice]

On 2/18/19 1:59 PM, Andrey Shinkevich wrote:
> To write one byte to disk, Python2 may use 'chr' type.
> In Python3, conversion to 'byte' type is required.
> 
> Signed-off-by: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com>
> ---
>  tests/qemu-iotests/242 | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
> 
> diff --git a/tests/qemu-iotests/242 b/tests/qemu-iotests/242
> index 16c65ed..6b1f7b8 100755
> --- a/tests/qemu-iotests/242
> +++ b/tests/qemu-iotests/242
> @@ -65,9 +65,14 @@ def toggle_flag(offset):
>      with open(disk, "r+b") as f:
>          f.seek(offset, 0)
>          c = f.read(1)
> -        toggled = chr(ord(c) ^ bitmap_flag_unknown)
> +        toggled = ord(c) ^ bitmap_flag_unknown
>          f.seek(-1, 1)
> -        f.write(toggled)
> +        try:
> +            # python2
> +            f.write(chr(toggled))
> +        except TypeError:
> +            # python3
> +            f.write(bytes([toggled]))

Looks like it works, but I'm not enough of a python expert to know if
there is a more Pythonic elegant approach.

If someone else picks it up before my next NBD pull request,
Acked-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org

Re: [Qemu-devel] [PATCH] iotests: handle TypeError for Python3 in test 242
Posted by Philippe Mathieu-Daudé 5 years, 1 month ago
On 2/18/19 9:05 PM, Eric Blake wrote:
> [adding Eduardo for some python 2-vs-3 advice]

And Cleber.

> 
> On 2/18/19 1:59 PM, Andrey Shinkevich wrote:
>> To write one byte to disk, Python2 may use 'chr' type.
>> In Python3, conversion to 'byte' type is required.
>>
>> Signed-off-by: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com>
>> ---
>>  tests/qemu-iotests/242 | 9 +++++++--
>>  1 file changed, 7 insertions(+), 2 deletions(-)
>>
>> diff --git a/tests/qemu-iotests/242 b/tests/qemu-iotests/242
>> index 16c65ed..6b1f7b8 100755
>> --- a/tests/qemu-iotests/242
>> +++ b/tests/qemu-iotests/242
>> @@ -65,9 +65,14 @@ def toggle_flag(offset):
>>      with open(disk, "r+b") as f:
>>          f.seek(offset, 0)
>>          c = f.read(1)
>> -        toggled = chr(ord(c) ^ bitmap_flag_unknown)
>> +        toggled = ord(c) ^ bitmap_flag_unknown
>>          f.seek(-1, 1)
>> -        f.write(toggled)
>> +        try:
>> +            # python2
>> +            f.write(chr(toggled))
>> +        except TypeError:
>> +            # python3
>> +            f.write(bytes([toggled]))
> 
> Looks like it works, but I'm not enough of a python expert to know if
> there is a more Pythonic elegant approach.
> 
> If someone else picks it up before my next NBD pull request,
> Acked-by: Eric Blake <eblake@redhat.com>
> 

Re: [Qemu-devel] [PATCH] iotests: handle TypeError for Python3 in test 242
Posted by Cleber Rosa 5 years, 1 month ago

On 2/18/19 4:25 PM, Philippe Mathieu-Daudé wrote:
> On 2/18/19 9:05 PM, Eric Blake wrote:
>> [adding Eduardo for some python 2-vs-3 advice]
> 
> And Cleber.
> 
>>
>> On 2/18/19 1:59 PM, Andrey Shinkevich wrote:
>>> To write one byte to disk, Python2 may use 'chr' type.
>>> In Python3, conversion to 'byte' type is required.
>>>
>>> Signed-off-by: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com>
>>> ---
>>>  tests/qemu-iotests/242 | 9 +++++++--
>>>  1 file changed, 7 insertions(+), 2 deletions(-)
>>>
>>> diff --git a/tests/qemu-iotests/242 b/tests/qemu-iotests/242
>>> index 16c65ed..6b1f7b8 100755
>>> --- a/tests/qemu-iotests/242
>>> +++ b/tests/qemu-iotests/242
>>> @@ -65,9 +65,14 @@ def toggle_flag(offset):
>>>      with open(disk, "r+b") as f:
>>>          f.seek(offset, 0)
>>>          c = f.read(1)
>>> -        toggled = chr(ord(c) ^ bitmap_flag_unknown)
>>> +        toggled = ord(c) ^ bitmap_flag_unknown
>>>          f.seek(-1, 1)
>>> -        f.write(toggled)
>>> +        try:
>>> +            # python2
>>> +            f.write(chr(toggled))
>>> +        except TypeError:
>>> +            # python3
>>> +            f.write(bytes([toggled]))
>>
>> Looks like it works, but I'm not enough of a python expert to know if
>> there is a more Pythonic elegant approach.
>>

Well, there's no way around the fact that bytes in Python 3 are very
different from bytes in Python 2 (just another name for a string).

What I'd recommend here is to not base the type on the exception, but
choose it depending on the Python version.  Something like:

if sys.version_info.major == 2:
   f.write(chr(toggled))
else:
   f.write(bytes([toggled])]

This is cheaper than raising/catching exceptions, it's self documenting,
and follows the pattern on other tests.

Regards,
- Cleber.

>> If someone else picks it up before my next NBD pull request,
>> Acked-by: Eric Blake <eblake@redhat.com>
>>

Re: [Qemu-devel] [PATCH] iotests: handle TypeError for Python3 in test 242
Posted by Andrey Shinkevich 5 years, 1 month ago

On 22/02/2019 03:17, Cleber Rosa wrote:
> 
> 
> On 2/18/19 4:25 PM, Philippe Mathieu-Daudé wrote:
>> On 2/18/19 9:05 PM, Eric Blake wrote:
>>> [adding Eduardo for some python 2-vs-3 advice]
>>
>> And Cleber.
>>
>>>
>>> On 2/18/19 1:59 PM, Andrey Shinkevich wrote:
>>>> To write one byte to disk, Python2 may use 'chr' type.
>>>> In Python3, conversion to 'byte' type is required.
>>>>
>>>> Signed-off-by: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com>
>>>> ---
>>>>   tests/qemu-iotests/242 | 9 +++++++--
>>>>   1 file changed, 7 insertions(+), 2 deletions(-)
>>>>
>>>> diff --git a/tests/qemu-iotests/242 b/tests/qemu-iotests/242
>>>> index 16c65ed..6b1f7b8 100755
>>>> --- a/tests/qemu-iotests/242
>>>> +++ b/tests/qemu-iotests/242
>>>> @@ -65,9 +65,14 @@ def toggle_flag(offset):
>>>>       with open(disk, "r+b") as f:
>>>>           f.seek(offset, 0)
>>>>           c = f.read(1)
>>>> -        toggled = chr(ord(c) ^ bitmap_flag_unknown)
>>>> +        toggled = ord(c) ^ bitmap_flag_unknown
>>>>           f.seek(-1, 1)
>>>> -        f.write(toggled)
>>>> +        try:
>>>> +            # python2
>>>> +            f.write(chr(toggled))
>>>> +        except TypeError:
>>>> +            # python3
>>>> +            f.write(bytes([toggled]))
>>>
>>> Looks like it works, but I'm not enough of a python expert to know if
>>> there is a more Pythonic elegant approach.
>>>
> 
> Well, there's no way around the fact that bytes in Python 3 are very
> different from bytes in Python 2 (just another name for a string).
> 
> What I'd recommend here is to not base the type on the exception, but
> choose it depending on the Python version.  Something like:
> 
> if sys.version_info.major == 2:
>     f.write(chr(toggled))
> else:
>     f.write(bytes([toggled])]
> 
> This is cheaper than raising/catching exceptions, it's self documenting,
> and follows the pattern on other tests.
> 
> Regards,
> - Cleber.
> 

Thank you very much, Cleber.
I would like to write it down as that is in iotests.py :
if sys.version_info.major >= 3:
     ...

>>> If someone else picks it up before my next NBD pull request,
>>> Acked-by: Eric Blake <eblake@redhat.com>
>>>

-- 
With the best regards,
Andrey Shinkevich

Re: [Qemu-devel] [PATCH] iotests: handle TypeError for Python3 in test 242
Posted by Vladimir Sementsov-Ogievskiy 5 years, 1 month ago
18.02.2019 22:59, Andrey Shinkevich wrote:
> To write one byte to disk, Python2 may use 'chr' type.
> In Python3, conversion to 'byte' type is required.
> 
> Signed-off-by: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com>

Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>

Forget to say to Andrey that we should add

Reported-by: Kevin Wolf <kwolf@redhat.com>

> ---
>   tests/qemu-iotests/242 | 9 +++++++--
>   1 file changed, 7 insertions(+), 2 deletions(-)
> 
> diff --git a/tests/qemu-iotests/242 b/tests/qemu-iotests/242
> index 16c65ed..6b1f7b8 100755
> --- a/tests/qemu-iotests/242
> +++ b/tests/qemu-iotests/242
> @@ -65,9 +65,14 @@ def toggle_flag(offset):
>       with open(disk, "r+b") as f:
>           f.seek(offset, 0)
>           c = f.read(1)
> -        toggled = chr(ord(c) ^ bitmap_flag_unknown)
> +        toggled = ord(c) ^ bitmap_flag_unknown
>           f.seek(-1, 1)
> -        f.write(toggled)
> +        try:
> +            # python2
> +            f.write(chr(toggled))
> +        except TypeError:
> +            # python3
> +            f.write(bytes([toggled]))
>   
>   
>   qemu_img_create('-f', iotests.imgfmt, disk, '1M')
> 


-- 
Best regards,
Vladimir