[Qemu-devel] [PATCH 6/9] iotests.py: Add is_str()

Max Reitz posted 9 patches 6 years, 9 months ago
There is a newer version of this series
[Qemu-devel] [PATCH 6/9] iotests.py: Add is_str()
Posted by Max Reitz 6 years, 9 months ago
Strings can have various representations, this helper function checks
whether a given value is in any of them.

Signed-off-by: Max Reitz <mreitz@redhat.com>
---
 tests/qemu-iotests/iotests.py | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py
index f51456fa63..29e4e3306f 100644
--- a/tests/qemu-iotests/iotests.py
+++ b/tests/qemu-iotests/iotests.py
@@ -221,6 +221,12 @@ def image_size(img):
     r = qemu_img_pipe('info', '--output=json', '-f', imgfmt, img)
     return json.loads(r)['virtual-size']
 
+def is_str(val):
+    if sys.version_info.major >= 3:
+        return isinstance(val, str) or isinstance(val, bytes)
+    else:
+        return isinstance(val, str) or isinstance(val, unicode)
+
 test_dir_re = re.compile(r"%s" % test_dir)
 def filter_test_dir(msg):
     return test_dir_re.sub("TEST_DIR", msg)
-- 
2.20.1


Re: [Qemu-devel] [PATCH 6/9] iotests.py: Add is_str()
Posted by John Snow 6 years, 9 months ago

On 1/23/19 9:46 AM, Max Reitz wrote:
> Strings can have various representations, this helper function checks
> whether a given value is in any of them.
> 
> Signed-off-by: Max Reitz <mreitz@redhat.com>
> ---
>  tests/qemu-iotests/iotests.py | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py
> index f51456fa63..29e4e3306f 100644
> --- a/tests/qemu-iotests/iotests.py
> +++ b/tests/qemu-iotests/iotests.py
> @@ -221,6 +221,12 @@ def image_size(img):
>      r = qemu_img_pipe('info', '--output=json', '-f', imgfmt, img)
>      return json.loads(r)['virtual-size']
>  
> +def is_str(val):
> +    if sys.version_info.major >= 3:
> +        return isinstance(val, str) or isinstance(val, bytes)
> +    else:
> +        return isinstance(val, str) or isinstance(val, unicode)
> +

conflating bytes with strings under a function named "is_str()" makes me
nervous, but... I have no concrete objections beyond hypothetically
"What if people take this function name literally?"

*prays for 2.x support to get dropped*

Reviewed-by: John Snow <jsnow@redhat.com>

Re: [Qemu-devel] [PATCH 6/9] iotests.py: Add is_str()
Posted by Max Reitz 6 years, 9 months ago
On 29.01.19 22:35, John Snow wrote:
> 
> 
> On 1/23/19 9:46 AM, Max Reitz wrote:
>> Strings can have various representations, this helper function checks
>> whether a given value is in any of them.
>>
>> Signed-off-by: Max Reitz <mreitz@redhat.com>
>> ---
>>  tests/qemu-iotests/iotests.py | 6 ++++++
>>  1 file changed, 6 insertions(+)
>>
>> diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py
>> index f51456fa63..29e4e3306f 100644
>> --- a/tests/qemu-iotests/iotests.py
>> +++ b/tests/qemu-iotests/iotests.py
>> @@ -221,6 +221,12 @@ def image_size(img):
>>      r = qemu_img_pipe('info', '--output=json', '-f', imgfmt, img)
>>      return json.loads(r)['virtual-size']
>>  
>> +def is_str(val):
>> +    if sys.version_info.major >= 3:
>> +        return isinstance(val, str) or isinstance(val, bytes)
>> +    else:
>> +        return isinstance(val, str) or isinstance(val, unicode)
>> +
> 
> conflating bytes with strings under a function named "is_str()" makes me
> nervous, but... I have no concrete objections beyond hypothetically
> "What if people take this function name literally?"

Hm.  Well, the strings from 2.x are byte strings in 3.x, so.

But then there's this:

$ python2
[...]
>>> 'foo'.replace('f', 'g')
'goo'
>>> u'foo'.replace('f', 'g')
u'goo'

$ python3
[...]
>>> 'foo'.replace('f', 'g')
'goo'
>>> b'foo'.replace('f', 'g')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'

So that just doesn't work for the next patch.  How about I just drop the
bytes test and if at some point someone feels the need to add it, we can
think about it again?

Max

> *prays for 2.x support to get dropped*
> 
> Reviewed-by: John Snow <jsnow@redhat.com>
>