diff mbox series

[U-Boot,v2,5/8] test/py: Encode/decode strings for stdio

Message ID 20170914213450.28754-6-paul.burton@imgtec.com
State Deferred
Delegated to: Tom Rini
Headers show
Series test/py: Fixes for python 3.x | expand

Commit Message

Paul Burton Sept. 14, 2017, 9:34 p.m. UTC
When reading stdin python 3.x will give us byte arrays, and when writing
stdout or stderr it will expect byte arrays. In order to insulate the
rest of the code from this difference, call encode or decode at
appropriate points when reading or writing stdio files. This works fine
on python 2.x too.

Signed-off-by: Paul Burton <paul.burton@imgtec.com>
Reviewed-by: Stephen Warren <swarren@nvidia.com>
---

Changes in v2: None

 test/py/multiplexed_log.py | 4 ++--
 test/py/tests/test_ut.py   | 2 +-
 test/py/u_boot_spawn.py    | 4 ++--
 3 files changed, 5 insertions(+), 5 deletions(-)

Comments

Tom Rini Sept. 22, 2017, 12:41 a.m. UTC | #1
On Thu, Sep 14, 2017 at 02:34:47PM -0700, Paul Burton wrote:

> When reading stdin python 3.x will give us byte arrays, and when writing
> stdout or stderr it will expect byte arrays. In order to insulate the
> rest of the code from this difference, call encode or decode at
> appropriate points when reading or writing stdio files. This works fine
> on python 2.x too.
> 
> Signed-off-by: Paul Burton <paul.burton@imgtec.com>
> Reviewed-by: Stephen Warren <swarren@nvidia.com>

OK, this almost works.  When I add in the following only the vboot test
fails and I can't understand why:

diff --git a/test/py/multiplexed_log.py b/test/py/multiplexed_log.py
index 21bdcb730917..d23bec0b9668 100644
--- a/test/py/multiplexed_log.py
+++ b/test/py/multiplexed_log.py
@@ -326,7 +326,7 @@ $(document).ready(function () {
 
         data = data.replace(chr(13), '')
         data = ''.join((c in self._nonprint) and ('%%%02x' % ord(c)) or
-                       c for c in data)
+                       c for c in data.encode('utf-8'))
         data = cgi.escape(data)
         return data
 
diff --git a/test/py/u_boot_spawn.py b/test/py/u_boot_spawn.py
index 751302a529e8..0490230c83dc 100644
--- a/test/py/u_boot_spawn.py
+++ b/test/py/u_boot_spawn.py
@@ -136,7 +136,7 @@ class Spawn(object):
         """
 
         for pi in range(len(patterns)):
-            if type(patterns[pi]) == type(''):
+            if type(patterns[pi]) != type(re.compile('')):
                 patterns[pi] = re.compile(patterns[pi])
 
         tstart_s = time.time()

Please run 'make tests' on a host with python==python2, thanks!
diff mbox series

Patch

diff --git a/test/py/multiplexed_log.py b/test/py/multiplexed_log.py
index bf926c3e77..21bdcb7309 100644
--- a/test/py/multiplexed_log.py
+++ b/test/py/multiplexed_log.py
@@ -140,11 +140,11 @@  class RunAndLog(object):
             if stdout:
                 if stderr:
                     output += 'stdout:\n'
-                output += stdout
+                output += stdout.decode('utf8')
             if stderr:
                 if stdout:
                     output += 'stderr:\n'
-                output += stderr
+                output += stderr.decode('utf8')
             exit_status = p.returncode
             exception = None
         except subprocess.CalledProcessError as cpe:
diff --git a/test/py/tests/test_ut.py b/test/py/tests/test_ut.py
index 5c25a2d465..1f40e2c2d0 100644
--- a/test/py/tests/test_ut.py
+++ b/test/py/tests/test_ut.py
@@ -14,7 +14,7 @@  def test_ut_dm_init(u_boot_console):
         data = 'this is a test'
         data += '\x00' * ((4 * 1024 * 1024) - len(data))
         with open(fn, 'wb') as fh:
-            fh.write(data)
+            fh.write(data.encode('utf-8'))
 
     fn = u_boot_console.config.source_dir + '/spi.bin'
     if not os.path.exists(fn):
diff --git a/test/py/u_boot_spawn.py b/test/py/u_boot_spawn.py
index 638c5dd31d..751302a529 100644
--- a/test/py/u_boot_spawn.py
+++ b/test/py/u_boot_spawn.py
@@ -114,7 +114,7 @@  class Spawn(object):
             Nothing.
         """
 
-        os.write(self.fd, data)
+        os.write(self.fd, data.encode('utf8'))
 
     def expect(self, patterns):
         """Wait for the sub-process to emit specific data.
@@ -172,7 +172,7 @@  class Spawn(object):
                 events = self.poll.poll(poll_maxwait)
                 if not events:
                     raise Timeout()
-                c = os.read(self.fd, 1024)
+                c = os.read(self.fd, 1024).decode('utf8')
                 if not c:
                     raise EOFError()
                 if self.logfile_read: