All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] iotests: handle TypeError for Python3 in test 242
@ 2019-02-18 19:59 Andrey Shinkevich
  2019-02-18 20:05 ` Eric Blake
  2019-02-19 10:30 ` Vladimir Sementsov-Ogievskiy
  0 siblings, 2 replies; 6+ messages in thread
From: Andrey Shinkevich @ 2019-02-18 19:59 UTC (permalink / raw)
  To: qemu-devel, qemu-block
  Cc: eblake, mreitz, kwolf, den, vsementsov, andrey.shinkevich

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

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] [PATCH] iotests: handle TypeError for Python3 in test 242
  2019-02-18 19:59 [Qemu-devel] [PATCH] iotests: handle TypeError for Python3 in test 242 Andrey Shinkevich
@ 2019-02-18 20:05 ` Eric Blake
  2019-02-18 21:25   ` Philippe Mathieu-Daudé
  2019-02-19 10:30 ` Vladimir Sementsov-Ogievskiy
  1 sibling, 1 reply; 6+ messages in thread
From: Eric Blake @ 2019-02-18 20:05 UTC (permalink / raw)
  To: Andrey Shinkevich, qemu-devel, qemu-block
  Cc: mreitz, kwolf, den, vsementsov, Eduardo Habkost

[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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] [PATCH] iotests: handle TypeError for Python3 in test 242
  2019-02-18 20:05 ` Eric Blake
@ 2019-02-18 21:25   ` Philippe Mathieu-Daudé
  2019-02-22  0:17     ` Cleber Rosa
  0 siblings, 1 reply; 6+ messages in thread
From: Philippe Mathieu-Daudé @ 2019-02-18 21:25 UTC (permalink / raw)
  To: Eric Blake, Andrey Shinkevich, qemu-devel, qemu-block, Cleber Rosa
  Cc: kwolf, den, vsementsov, Eduardo Habkost, mreitz

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>
> 

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] [PATCH] iotests: handle TypeError for Python3 in test 242
  2019-02-18 19:59 [Qemu-devel] [PATCH] iotests: handle TypeError for Python3 in test 242 Andrey Shinkevich
  2019-02-18 20:05 ` Eric Blake
@ 2019-02-19 10:30 ` Vladimir Sementsov-Ogievskiy
  1 sibling, 0 replies; 6+ messages in thread
From: Vladimir Sementsov-Ogievskiy @ 2019-02-19 10:30 UTC (permalink / raw)
  To: Andrey Shinkevich, qemu-devel, qemu-block
  Cc: eblake, mreitz, kwolf, Denis Lunev

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] [PATCH] iotests: handle TypeError for Python3 in test 242
  2019-02-18 21:25   ` Philippe Mathieu-Daudé
@ 2019-02-22  0:17     ` Cleber Rosa
  2019-02-22  9:48       ` Andrey Shinkevich
  0 siblings, 1 reply; 6+ messages in thread
From: Cleber Rosa @ 2019-02-22  0:17 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé,
	Eric Blake, Andrey Shinkevich, qemu-devel, qemu-block
  Cc: kwolf, den, vsementsov, Eduardo Habkost, mreitz



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>
>>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Qemu-devel] [PATCH] iotests: handle TypeError for Python3 in test 242
  2019-02-22  0:17     ` Cleber Rosa
@ 2019-02-22  9:48       ` Andrey Shinkevich
  0 siblings, 0 replies; 6+ messages in thread
From: Andrey Shinkevich @ 2019-02-22  9:48 UTC (permalink / raw)
  To: Cleber Rosa, Philippe Mathieu-Daudé,
	Eric Blake, qemu-devel, qemu-block
  Cc: kwolf, Denis Lunev, Vladimir Sementsov-Ogievskiy,
	Eduardo Habkost, mreitz



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


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-02-22  9:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-18 19:59 [Qemu-devel] [PATCH] iotests: handle TypeError for Python3 in test 242 Andrey Shinkevich
2019-02-18 20:05 ` Eric Blake
2019-02-18 21:25   ` Philippe Mathieu-Daudé
2019-02-22  0:17     ` Cleber Rosa
2019-02-22  9:48       ` Andrey Shinkevich
2019-02-19 10:30 ` Vladimir Sementsov-Ogievskiy

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.