[edk2-devel] [PATCH v2] BaseTools: Fix Python3 encoding issue in TestTools

Philippe Mathieu-Daudé posted 1 patch 4 years, 4 months ago
Failed in applying to current master (apply log)
BaseTools/Tests/TestTools.py | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
[edk2-devel] [PATCH v2] BaseTools: Fix Python3 encoding issue in TestTools
Posted by Philippe Mathieu-Daudé 4 years, 4 months ago
Under Centos 7.7 we get:

  Build environment: \
    Linux-3.10.0-1062.7.1.el7.x86_64-x86_64-with-centos-7.7.1908-Core
  [...]
  ======================================================================
  ERROR: testRandomDataCycles (TianoCompress.Tests)
  ----------------------------------------------------------------------
  Traceback (most recent call last):
    File "edk2/BaseTools/Tests/TianoCompress.py", line 60, \
        in testRandomDataCycles
      self.compressionTestCycle(data)
    File "edk2/BaseTools/Tests/TianoCompress.py", line 46, \
        in compressionTestCycle
      start = self.ReadTmpFile('input')
    File "edk2/BaseTools/Tests/TestTools.py", line 139, in ReadTmpFile
      data = f.read()
    File "/usr/lib64/python3.6/encodings/ascii.py", line 26, in decode
      return codecs.ascii_decode(input, self.errors)[0]
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 3: \
    ordinal not in range(128)

  ----------------------------------------------------------------------

Fix by using the 'io' module (per [1]), and specifying the UTF-8 encoding.

[1] https://mail.python.org/pipermail/python-list/2015-March/687124.html
Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Signed-off-by: Philippe Mathieu-Daude <philmd@redhat.com>
---
v2: Use io, wrap commit message lines per CheckPatch
---
 BaseTools/Tests/TestTools.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/BaseTools/Tests/TestTools.py b/BaseTools/Tests/TestTools.py
index 1099fd4eeaea..8aaad3609669 100644
--- a/BaseTools/Tests/TestTools.py
+++ b/BaseTools/Tests/TestTools.py
@@ -11,6 +11,7 @@ from __future__ import print_function
 # Import Modules
 #
 import base64
+import io
 import os
 import os.path
 import random
@@ -18,7 +19,6 @@ import shutil
 import subprocess
 import sys
 import unittest
-import codecs
 
 TestsDir = os.path.realpath(os.path.split(sys.argv[0])[0])
 BaseToolsDir = os.path.realpath(os.path.join(TestsDir, '..'))
@@ -135,7 +135,7 @@ class BaseToolsTest(unittest.TestCase):
         return open(os.path.join(self.testDir, fileName), mode)
 
     def ReadTmpFile(self, fileName):
-        f = open(self.GetTmpFilePath(fileName), 'r')
+        f = io.open(self.GetTmpFilePath(fileName), 'r', encoding='utf-8')
         data = f.read()
         f.close()
         return data
@@ -145,7 +145,7 @@ class BaseToolsTest(unittest.TestCase):
             with open(self.GetTmpFilePath(fileName), 'wb') as f:
                 f.write(data)
         else:
-            with codecs.open(self.GetTmpFilePath(fileName), 'w', encoding='utf-8') as f:
+            with io.open(self.GetTmpFilePath(fileName), 'w', encoding='utf-8') as f:
                 f.write(data)
 
     def GenRandomFileData(self, fileName, minlen = None, maxlen = None):
-- 
2.21.0


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.

View/Reply Online (#51797): https://edk2.groups.io/g/devel/message/51797
Mute This Topic: https://groups.io/mt/67154217/1787277
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub  [importer@patchew.org]
-=-=-=-=-=-=-=-=-=-=-=-