From: Andrey Shinkevich <andrey.shinkevich@virtuozzo.com>
To: "Cleber Rosa" <crosa@redhat.com>,
"Philippe Mathieu-Daudé" <philmd@redhat.com>,
"Eric Blake" <eblake@redhat.com>,
"qemu-devel@nongnu.org" <qemu-devel@nongnu.org>,
"qemu-block@nongnu.org" <qemu-block@nongnu.org>
Cc: "kwolf@redhat.com" <kwolf@redhat.com>,
Denis Lunev <den@virtuozzo.com>,
Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>,
Eduardo Habkost <ehabkost@redhat.com>,
"mreitz@redhat.com" <mreitz@redhat.com>
Subject: Re: [Qemu-devel] [PATCH] iotests: handle TypeError for Python3 in test 242
Date: Fri, 22 Feb 2019 09:48:03 +0000 [thread overview]
Message-ID: <a05ae653-88bb-cf37-e73c-3ca6595e05b5@virtuozzo.com> (raw)
In-Reply-To: <dbd04ea0-6a60-9817-52b3-a0cc73f1a637@redhat.com>
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
next prev parent reply other threads:[~2019-02-22 9:48 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2019-02-19 10:30 ` Vladimir Sementsov-Ogievskiy
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=a05ae653-88bb-cf37-e73c-3ca6595e05b5@virtuozzo.com \
--to=andrey.shinkevich@virtuozzo.com \
--cc=crosa@redhat.com \
--cc=den@virtuozzo.com \
--cc=eblake@redhat.com \
--cc=ehabkost@redhat.com \
--cc=kwolf@redhat.com \
--cc=mreitz@redhat.com \
--cc=philmd@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).