* [Qemu-devel] [PATCH v2] iotests: handle TypeError for Python3 in test 242
@ 2019-02-22 11:26 Andrey Shinkevich
2019-02-22 14:53 ` Eric Blake
` (3 more replies)
0 siblings, 4 replies; 12+ messages in thread
From: Andrey Shinkevich @ 2019-02-22 11:26 UTC (permalink / raw)
To: qemu-devel, qemu-block
Cc: eblake, crosa, mreitz, kwolf, ehabkost, philmd, den, vsementsov,
andrey.shinkevich
The data type for bytes in Python3 differs from the one in Python2.
Those cases should be managed separately.
v1:
In the first version, the TypeError in Python3 was handled as the
exception.
Discussed in the e-mail thread with the Message ID:
<1550519997-253534-1-git-send-email-andrey.shinkevich@virtuozzo.com>
Signed-off-by: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com>
Reported-by: Kevin Wolf <kwolf@redhat.com>
---
tests/qemu-iotests/242 | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/tests/qemu-iotests/242 b/tests/qemu-iotests/242
index 16c65ed..446fbf8 100755
--- a/tests/qemu-iotests/242
+++ b/tests/qemu-iotests/242
@@ -20,6 +20,7 @@
import iotests
import json
+import sys
from iotests import qemu_img_create, qemu_io, qemu_img_pipe, \
file_path, img_info_log, log, filter_qemu_io
@@ -65,9 +66,12 @@ 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)
+ if sys.version_info.major >= 3:
+ f.write(bytes([toggled]))
+ else:
+ f.write(chr(toggled))
qemu_img_create('-f', iotests.imgfmt, disk, '1M')
--
1.8.3.1
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [Qemu-devel] [PATCH v2] iotests: handle TypeError for Python3 in test 242
2019-02-22 11:26 [Qemu-devel] [PATCH v2] iotests: handle TypeError for Python3 in test 242 Andrey Shinkevich
@ 2019-02-22 14:53 ` Eric Blake
2019-02-25 8:51 ` Andrey Shinkevich
2019-02-22 15:20 ` Cleber Rosa
` (2 subsequent siblings)
3 siblings, 1 reply; 12+ messages in thread
From: Eric Blake @ 2019-02-22 14:53 UTC (permalink / raw)
To: Andrey Shinkevich, qemu-devel, qemu-block
Cc: crosa, mreitz, kwolf, ehabkost, philmd, den, vsementsov
On 2/22/19 5:26 AM, Andrey Shinkevich wrote:
> The data type for bytes in Python3 differs from the one in Python2.
> Those cases should be managed separately.
>
> v1:
> In the first version, the TypeError in Python3 was handled as the
> exception.
> Discussed in the e-mail thread with the Message ID:
> <1550519997-253534-1-git-send-email-andrey.shinkevich@virtuozzo.com>
This paragraph belongs...
>
> Signed-off-by: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com>
> Reported-by: Kevin Wolf <kwolf@redhat.com>
> ---
...here, after the --- marker. It is useful to reviewers, but does not
need to land in the long-term git history (a year from now, we won't
care if it was v2 or v10 that landed, nor how it changed from the
versions that didn't land). I can clean it up on staging.
Reviewed-by: Eric Blake <eblake@redhat.com>
Will stage through my NBD tree.
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3226
Virtualization: qemu.org | libvirt.org
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Qemu-devel] [PATCH v2] iotests: handle TypeError for Python3 in test 242
2019-02-22 11:26 [Qemu-devel] [PATCH v2] iotests: handle TypeError for Python3 in test 242 Andrey Shinkevich
2019-02-22 14:53 ` Eric Blake
@ 2019-02-22 15:20 ` Cleber Rosa
2019-02-25 8:51 ` Andrey Shinkevich
2019-02-23 9:19 ` Vladimir Sementsov-Ogievskiy
2019-02-25 20:26 ` Eduardo Habkost
3 siblings, 1 reply; 12+ messages in thread
From: Cleber Rosa @ 2019-02-22 15:20 UTC (permalink / raw)
To: Andrey Shinkevich, qemu-devel, qemu-block
Cc: kwolf, vsementsov, ehabkost, mreitz, den, philmd
On 2/22/19 6:26 AM, Andrey Shinkevich wrote:
> The data type for bytes in Python3 differs from the one in Python2.
> Those cases should be managed separately.
>
> v1:
> In the first version, the TypeError in Python3 was handled as the
> exception.
> Discussed in the e-mail thread with the Message ID:
> <1550519997-253534-1-git-send-email-andrey.shinkevich@virtuozzo.com>
>
> Signed-off-by: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com>
> Reported-by: Kevin Wolf <kwolf@redhat.com>
> ---
> tests/qemu-iotests/242 | 8 ++++++--
> 1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/tests/qemu-iotests/242 b/tests/qemu-iotests/242
> index 16c65ed..446fbf8 100755
> --- a/tests/qemu-iotests/242
> +++ b/tests/qemu-iotests/242
> @@ -20,6 +20,7 @@
>
> import iotests
> import json
> +import sys
> from iotests import qemu_img_create, qemu_io, qemu_img_pipe, \
> file_path, img_info_log, log, filter_qemu_io
>
> @@ -65,9 +66,12 @@ 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)
> + if sys.version_info.major >= 3:
> + f.write(bytes([toggled]))
> + else:
> + f.write(chr(toggled))
>
I originally suggested:
sys.version_info.major == 2:
...
Because this is already present on other tests, and IIRC Max mentioned
using this as an easy to find flag the moment Python 2 support is to be
dropped. But, looking for "sys.version_info.major" is just as
effective, so:
Reviewed-by: Cleber Rosa <crosa@redhat.com>
>
> qemu_img_create('-f', iotests.imgfmt, disk, '1M')
>
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Qemu-devel] [PATCH v2] iotests: handle TypeError for Python3 in test 242
2019-02-22 11:26 [Qemu-devel] [PATCH v2] iotests: handle TypeError for Python3 in test 242 Andrey Shinkevich
2019-02-22 14:53 ` Eric Blake
2019-02-22 15:20 ` Cleber Rosa
@ 2019-02-23 9:19 ` Vladimir Sementsov-Ogievskiy
2019-02-25 20:26 ` Eduardo Habkost
3 siblings, 0 replies; 12+ messages in thread
From: Vladimir Sementsov-Ogievskiy @ 2019-02-23 9:19 UTC (permalink / raw)
To: Andrey Shinkevich, qemu-devel, qemu-block
Cc: eblake, crosa, mreitz, kwolf, ehabkost, philmd, Denis Lunev
22.02.2019 14:26, Andrey Shinkevich wrote:
> The data type for bytes in Python3 differs from the one in Python2.
> Those cases should be managed separately.
>
> v1:
> In the first version, the TypeError in Python3 was handled as the
> exception.
> Discussed in the e-mail thread with the Message ID:
> <1550519997-253534-1-git-send-email-andrey.shinkevich@virtuozzo.com>
>
> Signed-off-by: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com>
> Reported-by: Kevin Wolf <kwolf@redhat.com>
> ---
> tests/qemu-iotests/242 | 8 ++++++--
> 1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/tests/qemu-iotests/242 b/tests/qemu-iotests/242
> index 16c65ed..446fbf8 100755
> --- a/tests/qemu-iotests/242
> +++ b/tests/qemu-iotests/242
> @@ -20,6 +20,7 @@
>
> import iotests
> import json
> +import sys
> from iotests import qemu_img_create, qemu_io, qemu_img_pipe, \
> file_path, img_info_log, log, filter_qemu_io
>
> @@ -65,9 +66,12 @@ 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)
> + if sys.version_info.major >= 3:
> + f.write(bytes([toggled]))
> + else:
> + f.write(chr(toggled))
>
>
> qemu_img_create('-f', iotests.imgfmt, disk, '1M')
>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
--
Best regards,
Vladimir
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Qemu-devel] [PATCH v2] iotests: handle TypeError for Python3 in test 242
2019-02-22 15:20 ` Cleber Rosa
@ 2019-02-25 8:51 ` Andrey Shinkevich
0 siblings, 0 replies; 12+ messages in thread
From: Andrey Shinkevich @ 2019-02-25 8:51 UTC (permalink / raw)
To: Cleber Rosa, qemu-devel, qemu-block
Cc: kwolf, Vladimir Sementsov-Ogievskiy, ehabkost, mreitz,
Denis Lunev, philmd
On 22/02/2019 18:20, Cleber Rosa wrote:
>
>
> On 2/22/19 6:26 AM, Andrey Shinkevich wrote:
>> The data type for bytes in Python3 differs from the one in Python2.
>> Those cases should be managed separately.
>>
>> v1:
>> In the first version, the TypeError in Python3 was handled as the
>> exception.
>> Discussed in the e-mail thread with the Message ID:
>> <1550519997-253534-1-git-send-email-andrey.shinkevich@virtuozzo.com>
>>
>> Signed-off-by: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com>
>> Reported-by: Kevin Wolf <kwolf@redhat.com>
>> ---
>> tests/qemu-iotests/242 | 8 ++++++--
>> 1 file changed, 6 insertions(+), 2 deletions(-)
>>
>> diff --git a/tests/qemu-iotests/242 b/tests/qemu-iotests/242
>> index 16c65ed..446fbf8 100755
>> --- a/tests/qemu-iotests/242
>> +++ b/tests/qemu-iotests/242
>> @@ -20,6 +20,7 @@
>>
>> import iotests
>> import json
>> +import sys
>> from iotests import qemu_img_create, qemu_io, qemu_img_pipe, \
>> file_path, img_info_log, log, filter_qemu_io
>>
>> @@ -65,9 +66,12 @@ 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)
>> + if sys.version_info.major >= 3:
>> + f.write(bytes([toggled]))
>> + else:
>> + f.write(chr(toggled))
>>
>
> I originally suggested:
>
> sys.version_info.major == 2:
> ...
>
> Because this is already present on other tests, and IIRC Max mentioned
> using this as an easy to find flag the moment Python 2 support is to be
> dropped. But, looking for "sys.version_info.major" is just as
> effective, so:
>
> Reviewed-by: Cleber Rosa <crosa@redhat.com>
>
Thank you very much, Cleber. Your review is helpful.
>>
>> qemu_img_create('-f', iotests.imgfmt, disk, '1M')
>>
--
With the best regards,
Andrey Shinkevich
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Qemu-devel] [PATCH v2] iotests: handle TypeError for Python3 in test 242
2019-02-22 14:53 ` Eric Blake
@ 2019-02-25 8:51 ` Andrey Shinkevich
0 siblings, 0 replies; 12+ messages in thread
From: Andrey Shinkevich @ 2019-02-25 8:51 UTC (permalink / raw)
To: Eric Blake, qemu-devel, qemu-block
Cc: crosa, mreitz, kwolf, ehabkost, philmd, Denis Lunev,
Vladimir Sementsov-Ogievskiy
On 22/02/2019 17:53, Eric Blake wrote:
> On 2/22/19 5:26 AM, Andrey Shinkevich wrote:
>> The data type for bytes in Python3 differs from the one in Python2.
>> Those cases should be managed separately.
>>
>> v1:
>> In the first version, the TypeError in Python3 was handled as the
>> exception.
>> Discussed in the e-mail thread with the Message ID:
>> <1550519997-253534-1-git-send-email-andrey.shinkevich@virtuozzo.com>
>
> This paragraph belongs...
>
>>
>> Signed-off-by: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com>
>> Reported-by: Kevin Wolf <kwolf@redhat.com>
>> ---
>
> ...here, after the --- marker. It is useful to reviewers, but does not
> need to land in the long-term git history (a year from now, we won't
> care if it was v2 or v10 that landed, nor how it changed from the
> versions that didn't land). I can clean it up on staging.
>
> Reviewed-by: Eric Blake <eblake@redhat.com>
>
> Will stage through my NBD tree.
>
Yes, please, Eric. Actually, I had missed to put that paragraph bellow
the dashes while composing it as a cover letter. That was my fault.
Acknowledged with thanks. I will take your comment into account for
future.
--
With the best regards,
Andrey Shinkevich
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Qemu-devel] [PATCH v2] iotests: handle TypeError for Python3 in test 242
2019-02-22 11:26 [Qemu-devel] [PATCH v2] iotests: handle TypeError for Python3 in test 242 Andrey Shinkevich
` (2 preceding siblings ...)
2019-02-23 9:19 ` Vladimir Sementsov-Ogievskiy
@ 2019-02-25 20:26 ` Eduardo Habkost
2019-02-26 1:42 ` [Qemu-devel] [Qemu-block] " Nir Soffer
3 siblings, 1 reply; 12+ messages in thread
From: Eduardo Habkost @ 2019-02-25 20:26 UTC (permalink / raw)
To: Andrey Shinkevich
Cc: qemu-devel, qemu-block, eblake, crosa, mreitz, kwolf, philmd,
den, vsementsov
On Fri, Feb 22, 2019 at 02:26:13PM +0300, Andrey Shinkevich wrote:
> The data type for bytes in Python3 differs from the one in Python2.
> Those cases should be managed separately.
>
> v1:
> In the first version, the TypeError in Python3 was handled as the
> exception.
> Discussed in the e-mail thread with the Message ID:
> <1550519997-253534-1-git-send-email-andrey.shinkevich@virtuozzo.com>
>
> Signed-off-by: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com>
> Reported-by: Kevin Wolf <kwolf@redhat.com>
> ---
> tests/qemu-iotests/242 | 8 ++++++--
> 1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/tests/qemu-iotests/242 b/tests/qemu-iotests/242
> index 16c65ed..446fbf8 100755
> --- a/tests/qemu-iotests/242
> +++ b/tests/qemu-iotests/242
> @@ -20,6 +20,7 @@
>
> import iotests
> import json
> +import sys
> from iotests import qemu_img_create, qemu_io, qemu_img_pipe, \
> file_path, img_info_log, log, filter_qemu_io
>
> @@ -65,9 +66,12 @@ 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)
> + if sys.version_info.major >= 3:
> + f.write(bytes([toggled]))
> + else:
> + f.write(chr(toggled))
Pretending we are dealing with text strings and using str/ord is
a python2-specific quirk. I think it would be nice to get rid of
it.
Python 2 has bytearray(), which behaves more similarly to the
bytes type from Python 3. If we use it, we can make the code
more python3-like:
diff --git a/tests/qemu-iotests/242 b/tests/qemu-iotests/242
index 16c65edcd7..7794fd4a70 100755
--- a/tests/qemu-iotests/242
+++ b/tests/qemu-iotests/242
@@ -64,10 +64,12 @@ def write_to_disk(offset, size):
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)
+ # The casts to bytearray() below are only necessary
+ # for Python 2 compatibility
+ c = bytearray(f.read(1))[0]
+ toggled = c ^ bitmap_flag_unknown
f.seek(-1, 1)
- f.write(toggled)
+ f.write(bytearray([toggled]))
qemu_img_create('-f', iotests.imgfmt, disk, '1M')
--
Eduardo
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [Qemu-devel] [Qemu-block] [PATCH v2] iotests: handle TypeError for Python3 in test 242
2019-02-25 20:26 ` Eduardo Habkost
@ 2019-02-26 1:42 ` Nir Soffer
2019-02-26 10:39 ` Andrey Shinkevich
2019-02-26 10:44 ` Eduardo Habkost
0 siblings, 2 replies; 12+ messages in thread
From: Nir Soffer @ 2019-02-26 1:42 UTC (permalink / raw)
To: Eduardo Habkost
Cc: Andrey Shinkevich, Kevin Wolf, Vladimir Sementsov-Ogievskiy,
qemu-block, philmd, QEMU Developers, Max Reitz, Cleber Rosa, den
On Mon, Feb 25, 2019 at 10:36 PM Eduardo Habkost <ehabkost@redhat.com>
wrote:
> On Fri, Feb 22, 2019 at 02:26:13PM +0300, Andrey Shinkevich wrote:
> > The data type for bytes in Python3 differs from the one in Python2.
> > Those cases should be managed separately.
> >
> > v1:
> > In the first version, the TypeError in Python3 was handled as the
> > exception.
> > Discussed in the e-mail thread with the Message ID:
> > <1550519997-253534-1-git-send-email-andrey.shinkevich@virtuozzo.com>
> >
> > Signed-off-by: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com>
> > Reported-by: Kevin Wolf <kwolf@redhat.com>
> > ---
> > tests/qemu-iotests/242 | 8 ++++++--
> > 1 file changed, 6 insertions(+), 2 deletions(-)
> >
> > diff --git a/tests/qemu-iotests/242 b/tests/qemu-iotests/242
> > index 16c65ed..446fbf8 100755
> > --- a/tests/qemu-iotests/242
> > +++ b/tests/qemu-iotests/242
> > @@ -20,6 +20,7 @@
> >
> > import iotests
> > import json
> > +import sys
> > from iotests import qemu_img_create, qemu_io, qemu_img_pipe, \
> > file_path, img_info_log, log, filter_qemu_io
> >
> > @@ -65,9 +66,12 @@ 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)
> > + if sys.version_info.major >= 3:
> > + f.write(bytes([toggled]))
> > + else:
> > + f.write(chr(toggled))
>
> Pretending we are dealing with text strings and using str/ord is
> a python2-specific quirk. I think it would be nice to get rid of
> it.
>
> Python 2 has bytearray(), which behaves more similarly to the
> bytes type from Python 3. If we use it, we can make the code
> more python3-like:
>
> diff --git a/tests/qemu-iotests/242 b/tests/qemu-iotests/242
> index 16c65edcd7..7794fd4a70 100755
> --- a/tests/qemu-iotests/242
> +++ b/tests/qemu-iotests/242
> @@ -64,10 +64,12 @@ def write_to_disk(offset, size):
> 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)
> + # The casts to bytearray() below are only necessary
> + # for Python 2 compatibility
> + c = bytearray(f.read(1))[0]
>
This is simpler and makes the intent of the code more clear:
flag, = struct.unpack("B", f.read(1))
> + toggled = c ^ bitmap_flag_unknown
> f.seek(-1, 1)
> - f.write(toggled)
> + f.write(bytearray([toggled]))
>
For consistency, we can use struct.pack here:
f.write(struct.pack("B", toggled))
Nir
>
>
> qemu_img_create('-f', iotests.imgfmt, disk, '1M')
>
> --
> Eduardo
>
>
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Qemu-devel] [Qemu-block] [PATCH v2] iotests: handle TypeError for Python3 in test 242
2019-02-26 1:42 ` [Qemu-devel] [Qemu-block] " Nir Soffer
@ 2019-02-26 10:39 ` Andrey Shinkevich
2019-02-26 14:06 ` Eric Blake
2019-02-26 10:44 ` Eduardo Habkost
1 sibling, 1 reply; 12+ messages in thread
From: Andrey Shinkevich @ 2019-02-26 10:39 UTC (permalink / raw)
To: Nir Soffer, Eduardo Habkost
Cc: Kevin Wolf, Vladimir Sementsov-Ogievskiy, qemu-block,
philmd@redhat.com; eblake, QEMU Developers, Max Reitz,
Cleber Rosa, Denis Lunev
On 26/02/2019 04:42, Nir Soffer wrote:
> On Mon, Feb 25, 2019 at 10:36 PM Eduardo Habkost <ehabkost@redhat.com
> <mailto:ehabkost@redhat.com>> wrote:
>
> On Fri, Feb 22, 2019 at 02:26:13PM +0300, Andrey Shinkevich wrote:
> > The data type for bytes in Python3 differs from the one in Python2.
> > Those cases should be managed separately.
> >
> > v1:
> > In the first version, the TypeError in Python3 was handled as the
> > exception.
> > Discussed in the e-mail thread with the Message ID:
> >
> <1550519997-253534-1-git-send-email-andrey.shinkevich@virtuozzo.com
> <mailto:1550519997-253534-1-git-send-email-andrey.shinkevich@virtuozzo.com>>
> >
> > Signed-off-by: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com
> <mailto:andrey.shinkevich@virtuozzo.com>>
> > Reported-by: Kevin Wolf <kwolf@redhat.com <mailto:kwolf@redhat.com>>
> > ---
> > tests/qemu-iotests/242 | 8 ++++++--
> > 1 file changed, 6 insertions(+), 2 deletions(-)
> >
> > diff --git a/tests/qemu-iotests/242 b/tests/qemu-iotests/242
> > index 16c65ed..446fbf8 100755
> > --- a/tests/qemu-iotests/242
> > +++ b/tests/qemu-iotests/242
> > @@ -20,6 +20,7 @@
> >
> > import iotests
> > import json
> > +import sys
> > from iotests import qemu_img_create, qemu_io, qemu_img_pipe, \
> > file_path, img_info_log, log, filter_qemu_io
> >
> > @@ -65,9 +66,12 @@ 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)
> > + if sys.version_info.major >= 3:
> > + f.write(bytes([toggled]))
> > + else:
> > + f.write(chr(toggled))
>
> Pretending we are dealing with text strings and using str/ord is
> a python2-specific quirk. I think it would be nice to get rid of
> it.
>
> Python 2 has bytearray(), which behaves more similarly to the
> bytes type from Python 3. If we use it, we can make the code
> more python3-like:
>
> diff --git a/tests/qemu-iotests/242 b/tests/qemu-iotests/242
> index 16c65edcd7..7794fd4a70 100755
> --- a/tests/qemu-iotests/242
> +++ b/tests/qemu-iotests/242
> @@ -64,10 +64,12 @@ def write_to_disk(offset, size):
> 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)
> + # The casts to bytearray() below are only necessary
> + # for Python 2 compatibility
> + c = bytearray(f.read(1))[0]
>
>
> This is simpler and makes the intent of the code more clear:
>
> flag, = struct.unpack("B", f.read(1))
>
> + toggled = c ^ bitmap_flag_unknown
> f.seek(-1, 1)
> - f.write(toggled)
> + f.write(bytearray([toggled]))
>
>
> For consistency, we can use struct.pack here:
>
> f.write(struct.pack("B", toggled))
>
> Nir
>
Thank you all. I am OK with this approach.
Will wait for Eric's response.
>
>
> qemu_img_create('-f', iotests.imgfmt, disk, '1M')
>
> --
> Eduardo
>
--
With the best regards,
Andrey Shinkevich
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Qemu-devel] [Qemu-block] [PATCH v2] iotests: handle TypeError for Python3 in test 242
2019-02-26 1:42 ` [Qemu-devel] [Qemu-block] " Nir Soffer
2019-02-26 10:39 ` Andrey Shinkevich
@ 2019-02-26 10:44 ` Eduardo Habkost
1 sibling, 0 replies; 12+ messages in thread
From: Eduardo Habkost @ 2019-02-26 10:44 UTC (permalink / raw)
To: Nir Soffer
Cc: Andrey Shinkevich, Kevin Wolf, Vladimir Sementsov-Ogievskiy,
qemu-block, philmd, QEMU Developers, Max Reitz, Cleber Rosa, den
On Tue, Feb 26, 2019 at 03:42:11AM +0200, Nir Soffer wrote:
> On Mon, Feb 25, 2019 at 10:36 PM Eduardo Habkost <ehabkost@redhat.com>
[...]
> > - c = f.read(1)
> > - toggled = chr(ord(c) ^ bitmap_flag_unknown)
> > + # The casts to bytearray() below are only necessary
> > + # for Python 2 compatibility
> > + c = bytearray(f.read(1))[0]
> >
>
> This is simpler and makes the intent of the code more clear:
>
> flag, = struct.unpack("B", f.read(1))
>
>
> > + toggled = c ^ bitmap_flag_unknown
> > f.seek(-1, 1)
> > - f.write(toggled)
> > + f.write(bytearray([toggled]))
> >
>
> For consistency, we can use struct.pack here:
>
> f.write(struct.pack("B", toggled))
That's perfect. Thanks for the suggestion!
--
Eduardo
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Qemu-devel] [Qemu-block] [PATCH v2] iotests: handle TypeError for Python3 in test 242
2019-02-26 10:39 ` Andrey Shinkevich
@ 2019-02-26 14:06 ` Eric Blake
2019-02-26 14:08 ` Andrey Shinkevich
0 siblings, 1 reply; 12+ messages in thread
From: Eric Blake @ 2019-02-26 14:06 UTC (permalink / raw)
To: Andrey Shinkevich, Nir Soffer, Eduardo Habkost
Cc: Kevin Wolf, Vladimir Sementsov-Ogievskiy, Denis Lunev,
qemu-block, philmd, QEMU Developers, Max Reitz, Cleber Rosa
On 2/26/19 4:39 AM, Andrey Shinkevich wrote:
>> +++ b/tests/qemu-iotests/242
>> @@ -64,10 +64,12 @@ def write_to_disk(offset, size):
>> 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)
>> + # The casts to bytearray() below are only necessary
>> + # for Python 2 compatibility
>> + c = bytearray(f.read(1))[0]
>>
>>
>> This is simpler and makes the intent of the code more clear:
>>
>> flag, = struct.unpack("B", f.read(1))
>>
>> + toggled = c ^ bitmap_flag_unknown
>> f.seek(-1, 1)
>> - f.write(toggled)
>> + f.write(bytearray([toggled]))
>>
>>
>> For consistency, we can use struct.pack here:
>>
>> f.write(struct.pack("B", toggled))
>>
>> Nir
>>
>
> Thank you all. I am OK with this approach.
> Will wait for Eric's response.
That looks better. Peter hasn't applied my pull request yet, so you have
time to submit a formal v3 (making it easier for me to 'git am' it
rather than reconstruct from this email), and then I will update my pull
request to use this improved version.
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3226
Virtualization: qemu.org | libvirt.org
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [Qemu-devel] [Qemu-block] [PATCH v2] iotests: handle TypeError for Python3 in test 242
2019-02-26 14:06 ` Eric Blake
@ 2019-02-26 14:08 ` Andrey Shinkevich
0 siblings, 0 replies; 12+ messages in thread
From: Andrey Shinkevich @ 2019-02-26 14:08 UTC (permalink / raw)
To: Eric Blake, Nir Soffer, Eduardo Habkost
Cc: Kevin Wolf, Vladimir Sementsov-Ogievskiy, Denis Lunev,
qemu-block, philmd, QEMU Developers, Max Reitz, Cleber Rosa
On 26/02/2019 17:06, Eric Blake wrote:
> On 2/26/19 4:39 AM, Andrey Shinkevich wrote:
>
>>> +++ b/tests/qemu-iotests/242
>>> @@ -64,10 +64,12 @@ def write_to_disk(offset, size):
>>> 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)
>>> + # The casts to bytearray() below are only necessary
>>> + # for Python 2 compatibility
>>> + c = bytearray(f.read(1))[0]
>>>
>>>
>>> This is simpler and makes the intent of the code more clear:
>>>
>>> flag, = struct.unpack("B", f.read(1))
>>>
>>> + toggled = c ^ bitmap_flag_unknown
>>> f.seek(-1, 1)
>>> - f.write(toggled)
>>> + f.write(bytearray([toggled]))
>>>
>>>
>>> For consistency, we can use struct.pack here:
>>>
>>> f.write(struct.pack("B", toggled))
>>>
>>> Nir
>>>
>>
>> Thank you all. I am OK with this approach.
>> Will wait for Eric's response.
>
> That looks better. Peter hasn't applied my pull request yet, so you have
> time to submit a formal v3 (making it easier for me to 'git am' it
> rather than reconstruct from this email), and then I will update my pull
> request to use this improved version.
>
Noted
--
With the best regards,
Andrey Shinkevich
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2019-02-26 14:08 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-22 11:26 [Qemu-devel] [PATCH v2] iotests: handle TypeError for Python3 in test 242 Andrey Shinkevich
2019-02-22 14:53 ` Eric Blake
2019-02-25 8:51 ` Andrey Shinkevich
2019-02-22 15:20 ` Cleber Rosa
2019-02-25 8:51 ` Andrey Shinkevich
2019-02-23 9:19 ` Vladimir Sementsov-Ogievskiy
2019-02-25 20:26 ` Eduardo Habkost
2019-02-26 1:42 ` [Qemu-devel] [Qemu-block] " Nir Soffer
2019-02-26 10:39 ` Andrey Shinkevich
2019-02-26 14:06 ` Eric Blake
2019-02-26 14:08 ` Andrey Shinkevich
2019-02-26 10:44 ` Eduardo Habkost
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.