[Qemu-devel] [PATCH for-3.1 v2] python: Use io.StringIO

Philippe Mathieu-Daudé posted 1 patch 5 years, 9 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20180718223628.5062-1-f4bug@amsat.org
Test checkpatch passed
Test docker-mingw@fedora passed
Test docker-quick@centos7 passed
There is a newer version of this series
tests/docker/docker.py        | 5 +----
tests/image-fuzzer/runner.py  | 6 +++---
tests/qemu-iotests/iotests.py | 5 +++--
3 files changed, 7 insertions(+), 9 deletions(-)
[Qemu-devel] [PATCH for-3.1 v2] python: Use io.StringIO
Posted by Philippe Mathieu-Daudé 5 years, 9 months ago
Both Python 2.7 and 3 support the same io.StringIO to
handle unicode strings.

Python 2.6 requires special care, but since 7f2b55443a his
support was removed. Stop caring, drop the ImportError check.

Use the common form to use indistinctly Python 2.7 or 3.

http://python-future.org/compatible_idioms.html#stringio

This fixes running tests on the Fedora Docker image,
which uses Python3 since 356dc290f:

  $ make docker-test-block@fedora
  [...]
  045         [failed, exit status 1] - output mismatch (see 045.out.bad)
  --- /tmp/qemu-test/src/tests/qemu-iotests/045.out       2018-07-17 16:56:18.000000000 +0000
  +++ /tmp/qemu-test/build/tests/qemu-iotests/045.out.bad 2018-07-17 17:19:22.448409007 +0000
  @@ -1,5 +1,6 @@
  -...........
  -----------------------------------------------------------------------
  -Ran 11 tests
  -
  -OK
  +Traceback (most recent call last):
  +  File "045", line 178, in <module>
  +    iotests.main(supported_fmts=['raw'])
  +  File "/tmp/qemu-test/src/tests/qemu-iotests/iotests.py", line 682, in main
  +    import StringIO
  +ModuleNotFoundError: No module named 'StringIO'
  132         [failed, exit status 1] - output mismatch (see 132.out.bad)
  152         [failed, exit status 1] - output mismatch (see 152.out.bad)

  Failures: 045 132 152

Suggested-by: Eduardo Habkost <ehabkost@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 tests/docker/docker.py        | 5 +----
 tests/image-fuzzer/runner.py  | 6 +++---
 tests/qemu-iotests/iotests.py | 5 +++--
 3 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/tests/docker/docker.py b/tests/docker/docker.py
index 69e7130db7..9d53b868db 100755
--- a/tests/docker/docker.py
+++ b/tests/docker/docker.py
@@ -26,10 +26,7 @@ import tempfile
 import re
 import signal
 from tarfile import TarFile, TarInfo
-try:
-    from StringIO import StringIO
-except ImportError:
-    from io import StringIO
+from io import StringIO
 from shutil import copy, rmtree
 from pwd import getpwuid
 from datetime import datetime,timedelta
diff --git a/tests/image-fuzzer/runner.py b/tests/image-fuzzer/runner.py
index 95d84f38f3..4462d84f45 100755
--- a/tests/image-fuzzer/runner.py
+++ b/tests/image-fuzzer/runner.py
@@ -28,7 +28,7 @@ import shutil
 from itertools import count
 import time
 import getopt
-import StringIO
+from io import StringIO
 import resource
 
 try:
@@ -183,7 +183,7 @@ class TestEnv(object):
                                            MAX_BACKING_FILE_SIZE) * (1 << 20)
         cmd = self.qemu_img + ['create', '-f', backing_file_fmt,
                                backing_file_name, str(backing_file_size)]
-        temp_log = StringIO.StringIO()
+        temp_log = StringIO()
         retcode = run_app(temp_log, cmd)
         if retcode == 0:
             temp_log.close()
@@ -240,7 +240,7 @@ class TestEnv(object):
                            "Backing file: %s\n" \
                            % (self.seed, " ".join(current_cmd),
                               self.current_dir, backing_file_name)
-            temp_log = StringIO.StringIO()
+            temp_log = StringIO()
             try:
                 retcode = run_app(temp_log, current_cmd)
             except OSError as e:
diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py
index 4e67fbbe96..c95dd17190 100644
--- a/tests/qemu-iotests/iotests.py
+++ b/tests/qemu-iotests/iotests.py
@@ -679,13 +679,14 @@ def main(supported_fmts=[], supported_oses=['linux'], supported_cache_modes=[],
 
     # We need to filter out the time taken from the output so that qemu-iotest
     # can reliably diff the results against master output.
-    import StringIO
+    from io import StringIO
+
     if debug:
         output = sys.stdout
         verbosity = 2
         sys.argv.remove('-d')
     else:
-        output = StringIO.StringIO()
+        output = StringIO()
 
     logging.basicConfig(level=(logging.DEBUG if debug else logging.WARN))
 
-- 
2.18.0


Re: [Qemu-devel] [PATCH for-3.1 v2] python: Use io.StringIO
Posted by Eduardo Habkost 5 years, 9 months ago
On Wed, Jul 18, 2018 at 07:36:28PM -0300, Philippe Mathieu-Daudé wrote:
> Both Python 2.7 and 3 support the same io.StringIO to
> handle unicode strings.
> 
> Python 2.6 requires special care, but since 7f2b55443a his
> support was removed. Stop caring, drop the ImportError check.
> 
> Use the common form to use indistinctly Python 2.7 or 3.
> 
> http://python-future.org/compatible_idioms.html#stringio
> 
> This fixes running tests on the Fedora Docker image,
> which uses Python3 since 356dc290f:
> 
>   $ make docker-test-block@fedora
>   [...]
>   045         [failed, exit status 1] - output mismatch (see 045.out.bad)
>   --- /tmp/qemu-test/src/tests/qemu-iotests/045.out       2018-07-17 16:56:18.000000000 +0000
>   +++ /tmp/qemu-test/build/tests/qemu-iotests/045.out.bad 2018-07-17 17:19:22.448409007 +0000
>   @@ -1,5 +1,6 @@
>   -...........
>   -----------------------------------------------------------------------
>   -Ran 11 tests
>   -
>   -OK
>   +Traceback (most recent call last):
>   +  File "045", line 178, in <module>
>   +    iotests.main(supported_fmts=['raw'])
>   +  File "/tmp/qemu-test/src/tests/qemu-iotests/iotests.py", line 682, in main
>   +    import StringIO
>   +ModuleNotFoundError: No module named 'StringIO'
>   132         [failed, exit status 1] - output mismatch (see 132.out.bad)
>   152         [failed, exit status 1] - output mismatch (see 152.out.bad)
> 
>   Failures: 045 132 152
> 
> Suggested-by: Eduardo Habkost <ehabkost@redhat.com>
> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
> ---
>  tests/docker/docker.py        | 5 +----
>  tests/image-fuzzer/runner.py  | 6 +++---
>  tests/qemu-iotests/iotests.py | 5 +++--
>  3 files changed, 7 insertions(+), 9 deletions(-)
> 
> diff --git a/tests/docker/docker.py b/tests/docker/docker.py
> index 69e7130db7..9d53b868db 100755
> --- a/tests/docker/docker.py
> +++ b/tests/docker/docker.py
> @@ -26,10 +26,7 @@ import tempfile
>  import re
>  import signal
>  from tarfile import TarFile, TarInfo
> -try:
> -    from StringIO import StringIO
> -except ImportError:
> -    from io import StringIO
> +from io import StringIO

This one should be BytesIO, see:
https://www.mail-archive.com/qemu-devel@nongnu.org/msg545627.html


>  from shutil import copy, rmtree
>  from pwd import getpwuid
>  from datetime import datetime,timedelta
> diff --git a/tests/image-fuzzer/runner.py b/tests/image-fuzzer/runner.py
> index 95d84f38f3..4462d84f45 100755
> --- a/tests/image-fuzzer/runner.py
> +++ b/tests/image-fuzzer/runner.py
> @@ -28,7 +28,7 @@ import shutil
>  from itertools import count
>  import time
>  import getopt
> -import StringIO
> +from io import StringIO
>  import resource
>  
>  try:
> @@ -183,7 +183,7 @@ class TestEnv(object):
>                                             MAX_BACKING_FILE_SIZE) * (1 << 20)
>          cmd = self.qemu_img + ['create', '-f', backing_file_fmt,
>                                 backing_file_name, str(backing_file_size)]
> -        temp_log = StringIO.StringIO()
> +        temp_log = StringIO()
>          retcode = run_app(temp_log, cmd)

I wouldn't touch this until we're sure if temp_log needs to be a
binary file or a text file.


>          if retcode == 0:
>              temp_log.close()
> @@ -240,7 +240,7 @@ class TestEnv(object):
>                             "Backing file: %s\n" \
>                             % (self.seed, " ".join(current_cmd),
>                                self.current_dir, backing_file_name)
> -            temp_log = StringIO.StringIO()
> +            temp_log = StringIO()
>              try:
>                  retcode = run_app(temp_log, current_cmd)
>              except OSError as e:
> diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py
> index 4e67fbbe96..c95dd17190 100644
> --- a/tests/qemu-iotests/iotests.py
> +++ b/tests/qemu-iotests/iotests.py
> @@ -679,13 +679,14 @@ def main(supported_fmts=[], supported_oses=['linux'], supported_cache_modes=[],
>  
>      # We need to filter out the time taken from the output so that qemu-iotest
>      # can reliably diff the results against master output.
> -    import StringIO
> +    from io import StringIO
> +

This one looks correct.


>      if debug:
>          output = sys.stdout
>          verbosity = 2
>          sys.argv.remove('-d')
>      else:
> -        output = StringIO.StringIO()
> +        output = StringIO()
>  
>      logging.basicConfig(level=(logging.DEBUG if debug else logging.WARN))
>  
> -- 
> 2.18.0
> 

-- 
Eduardo