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.shinkev...@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 <ebl...@redhat.com> >>> -- With the best regards, Andrey Shinkevich