qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com>
To: Nir Soffer <nsoffer@redhat.com>, Eduardo Habkost <ehabkost@redhat.com>
Cc: Kevin Wolf <kwolf@redhat.com>,
	Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>,
	qemu-block <qemu-block@nongnu.org>,
	"\"philmd@redhat.com; eblake\"@redhat.com" <"philmd@redhat.com;
	eblake"@redhat.com>, QEMU Developers <qemu-devel@nongnu.org>,
	Max Reitz <mreitz@redhat.com>, Cleber Rosa <crosa@redhat.com>,
	Denis Lunev <den@virtuozzo.com>
Subject: Re: [Qemu-devel] [Qemu-block] [PATCH v2] iotests: handle TypeError for Python3 in test 242
Date: Tue, 26 Feb 2019 10:39:48 +0000	[thread overview]
Message-ID: <a8ca5f99-43d0-610b-ea09-28b2655f5cda@virtuozzo.com> (raw)
In-Reply-To: <CAMRbyysWcfctB8AeSZrQ9x9HRAKut7mWYFqVP=oQ5uVoQgvh0w@mail.gmail.com>



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


  reply	other threads:[~2019-02-26 10:40 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2019-02-26 14:06       ` Eric Blake
2019-02-26 14:08         ` Andrey Shinkevich
2019-02-26 10:44     ` Eduardo Habkost

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a8ca5f99-43d0-610b-ea09-28b2655f5cda@virtuozzo.com \
    --to=andrey.shinkevich@virtuozzo.com \
    --cc="philmd@redhat.com; eblake"@redhat.com \
    --cc=crosa@redhat.com \
    --cc=den@virtuozzo.com \
    --cc=ehabkost@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=nsoffer@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=vsementsov@virtuozzo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).