From: Zhijux Fan <zhijux....@intel.com>

update Test scripts support python2 and python3

Cc: Bob Feng <bob.c.f...@intel.com>
Cc: Liming Gao <liming....@intel.com>
Cc: Yonghong Zhu <yonghong....@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Zhiju.Fan <zhijux....@intel.com>
---
 BaseTools/Tests/CToolsTests.py             |  2 +-
 BaseTools/Tests/CheckUnicodeSourceFiles.py |  6 +++---
 BaseTools/Tests/TestTools.py               | 13 ++++++++-----
 3 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/BaseTools/Tests/CToolsTests.py b/BaseTools/Tests/CToolsTests.py
index ab75d9a7dc..f0de44b141 100644
--- a/BaseTools/Tests/CToolsTests.py
+++ b/BaseTools/Tests/CToolsTests.py
@@ -24,11 +24,11 @@ modules = (
     TianoCompress,
     )
 
 
 def TheTestSuite():
-    suites = map(lambda module: module.TheTestSuite(), modules)
+    suites = list(map(lambda module: module.TheTestSuite(), modules))
     return unittest.TestSuite(suites)
 
 if __name__ == '__main__':
     allTests = TheTestSuite()
     unittest.TextTestRunner().run(allTests)
diff --git a/BaseTools/Tests/CheckUnicodeSourceFiles.py 
b/BaseTools/Tests/CheckUnicodeSourceFiles.py
index 6ae62f180a..c76b2bc20e 100644
--- a/BaseTools/Tests/CheckUnicodeSourceFiles.py
+++ b/BaseTools/Tests/CheckUnicodeSourceFiles.py
@@ -108,11 +108,11 @@ class Tests(TestTools.BaseToolsTest):
         # with the Surrogate Pair code point.
         #
         # This test makes sure that BaseTools rejects these characters
         # if seen in a .uni file.
         #
-        data = codecs.BOM_UTF16_LE + '//\x01\xd8 '
+        data = codecs.BOM_UTF16_LE + b'//\x01\xd8 '
 
         self.CheckFile(encoding=None, shouldPass=False, string=data)
 
     def testValidUtf8File(self):
         self.CheckFile(encoding='utf_8', shouldPass=True)
@@ -159,20 +159,20 @@ class Tests(TestTools.BaseToolsTest):
         # UTF-16 Surrogate Pairs.
         #
         # This test makes sure that BaseTools rejects these characters
         # if seen in a .uni file.
         #
-        data = '\xed\xa0\x81'
+        data = b'\xed\xa0\x81'
 
         self.CheckFile(encoding=None, shouldPass=False, string=data)
 
     def testSurrogatePairUnicodeCharInUtf8FileWithBom(self):
         #
         # Same test as testSurrogatePairUnicodeCharInUtf8File, but add
         # the UTF-8 BOM
         #
-        data = codecs.BOM_UTF8 + '\xed\xa0\x81'
+        data = codecs.BOM_UTF8 + b'\xed\xa0\x81'
 
         self.CheckFile(encoding=None, shouldPass=False, string=data)
 
 TheTestSuite = TestTools.MakeTheTestSuite(locals())
 
diff --git a/BaseTools/Tests/TestTools.py b/BaseTools/Tests/TestTools.py
index e16e993048..4332dcdaac 100644
--- a/BaseTools/Tests/TestTools.py
+++ b/BaseTools/Tests/TestTools.py
@@ -38,11 +38,11 @@ if PythonSourceDir not in sys.path:
     #
     sys.path.append(PythonSourceDir)
 
 def MakeTheTestSuite(localItems):
     tests = []
-    for name, item in localItems.iteritems():
+    for name, item in localItems.items():
         if isinstance(item, type):
             if issubclass(item, unittest.TestCase):
                 tests.append(unittest.TestLoader().loadTestsFromTestCase(item))
             elif issubclass(item, unittest.TestSuite):
                 tests.append(item())
@@ -144,13 +144,16 @@ class BaseToolsTest(unittest.TestCase):
         data = f.read()
         f.close()
         return data
 
     def WriteTmpFile(self, fileName, data):
-        f = open(self.GetTmpFilePath(fileName), 'w')
-        f.write(data)
-        f.close()
+        if isinstance(data, bytes):
+            with open(self.GetTmpFilePath(fileName), 'wb') as f:
+                f.write(data)
+        else:
+            with open(self.GetTmpFilePath(fileName), 'w') as f:
+                f.write(data)
 
     def GenRandomFileData(self, fileName, minlen = None, maxlen = None):
         if maxlen is None: maxlen = minlen
         f = self.OpenTmpFile(fileName, 'w')
         f.write(self.GetRandomString(minlen, maxlen))
@@ -159,11 +162,11 @@ class BaseToolsTest(unittest.TestCase):
     def GetRandomString(self, minlen = None, maxlen = None):
         if minlen is None: minlen = 1024
         if maxlen is None: maxlen = minlen
         return ''.join(
             [chr(random.randint(0, 255))
-             for x in xrange(random.randint(minlen, maxlen))
+             for x in range(random.randint(minlen, maxlen))
             ])
 
     def setUp(self):
         self.savedEnvPath = os.environ['PATH']
         self.savedSysPath = sys.path[:]
-- 
2.20.1.windows.1

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel

Reply via email to