public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Fan, ZhijuX" <zhijux.fan@intel.com>
To: "edk2-devel@lists.01.org" <edk2-devel@lists.01.org>
Cc: "Gao, Liming" <liming.gao@intel.com>,
	"Feng, Bob C" <bob.c.feng@intel.com>
Subject: [PATCH] BaseTools:Trim will trig exception when input asl UTF8 format file
Date: Fri, 22 Mar 2019 07:55:02 +0000	[thread overview]
Message-ID: <FAD0D7E0AE0FA54D987F6E72435CAFD50AF366CB@SHSMSX101.ccr.corp.intel.com> (raw)

BZ:https://bugzilla.tianocore.org/show_bug.cgi?id=1641
The command trim --asl-file -o test.i UTF8.asl will trig the exception.
Trim tool should report error message for unsupported UTF8 file instead
of the exception.

Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Zhiju.Fan <zhijux.fan@intel.com>
---
 BaseTools/Source/Python/Trim/Trim.py | 40 +++++++++++++++---------------------
 1 file changed, 17 insertions(+), 23 deletions(-)

diff --git a/BaseTools/Source/Python/Trim/Trim.py b/BaseTools/Source/Python/Trim/Trim.py
index 825ed3e5d5..13d537f5aa 100644
--- a/BaseTools/Source/Python/Trim/Trim.py
+++ b/BaseTools/Source/Python/Trim/Trim.py
@@ -18,7 +18,7 @@ import Common.LongFilePathOs as os
 import sys
 import re
 from io import BytesIO
-
+import codecs
 from optparse import OptionParser
 from optparse import make_option
 from Common.BuildToolError import *
@@ -141,14 +141,11 @@ gIncludedAslFile = []
 def TrimPreprocessedFile(Source, Target, ConvertHex, TrimLong):
     CreateDirectory(os.path.dirname(Target))
     try:
-        f = open (Source, 'r')
+        with codecs.open(Source, "r", encoding="utf-8") as File:
+            Lines = File.readlines()
     except:
         EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)
 
-    # read whole file
-    Lines = f.readlines()
-    f.close()
-
     PreprocessedFile = ""
     InjectedFile = ""
     LineIndexOfOriginalFile = None
@@ -245,11 +242,10 @@ def TrimPreprocessedFile(Source, Target, ConvertHex, TrimLong):
 
     # save to file
     try:
-        f = open (Target, 'w')
+        with codecs.open(Target, 'w', encoding="utf-8") as File:
+            File.writelines(Lines)
     except:
         EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)
-    f.writelines(NewLines)
-    f.close()
 
 ## Trim preprocessed VFR file
 #
@@ -263,12 +259,11 @@ def TrimPreprocessedVfr(Source, Target):
     CreateDirectory(os.path.dirname(Target))
 
     try:
-        f = open (Source, 'r')
+        with codecs.open(Source, "r", encoding="utf-8") as File:
+            Lines = File.readlines()
     except:
         EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)
     # read whole file
-    Lines = f.readlines()
-    f.close()
 
     FoundTypedef = False
     Brace = 0
@@ -312,11 +307,10 @@ def TrimPreprocessedVfr(Source, Target):
 
     # save all lines trimmed
     try:
-        f = open (Target, 'w')
+        with codecs.open(Target, 'w', encoding="utf-8") as File:
+            File.writelines(Lines)
     except:
         EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)
-    f.writelines(Lines)
-    f.close()
 
 ## Read the content  ASL file, including ASL included, recursively
 #
@@ -342,7 +336,8 @@ def DoInclude(Source, Indent='', IncludePathList=[], LocalSearchPath=None):
         for IncludePath in SearchPathList:
             IncludeFile = os.path.join(IncludePath, Source)
             if os.path.isfile(IncludeFile):
-                F = open(IncludeFile, "r")
+                with codecs.open(IncludeFile, "r", encoding="utf-8") as File:
+                    F = File.readlines()
                 break
         else:
             EdkLogger.error("Trim", "Failed to find include file %s" % Source)
@@ -377,7 +372,6 @@ def DoInclude(Source, Indent='', IncludePathList=[], LocalSearchPath=None):
         NewFileContent.append("\n")
 
     gIncludedAslFile.pop()
-    F.close()
 
     return NewFileContent
 
@@ -409,7 +403,9 @@ def TrimAslFile(Source, Target, IncludePathFile):
     if IncludePathFile:
         try:
             LineNum = 0
-            for Line in open(IncludePathFile, 'r'):
+            with codecs.open(IncludePathFile, 'r', encoding="utf-8") as File:
+                FileLines = File.readlines()
+            for Line in FileLines:
                 LineNum += 1
                 if Line.startswith("/I") or Line.startswith ("-I"):
                     IncludePathList.append(Line[2:].strip())
@@ -427,13 +423,11 @@ def TrimAslFile(Source, Target, IncludePathFile):
 
     # save all lines trimmed
     try:
-        f = open (Target, 'w')
+        with codecs.open(Target, 'w', encoding="utf-8") as File:
+            File.writelines(Lines)
     except:
         EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)
 
-    f.writelines(Lines)
-    f.close()
-
 def GenerateVfrBinSec(ModuleName, DebugDir, OutputFile):
     VfrNameList = []
     if os.path.isdir(DebugDir):
@@ -453,7 +447,7 @@ def GenerateVfrBinSec(ModuleName, DebugDir, OutputFile):
         return
 
     try:
-        fInputfile = open(OutputFile, "wb+", 0)
+        fInputfile = open(OutputFile, "wb+")
     except:
         EdkLogger.error("Trim", FILE_OPEN_FAILURE, "File open failed for %s" %OutputFile, None)
 
-- 
2.14.1.windows.1



                 reply	other threads:[~2019-03-22  7:55 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=FAD0D7E0AE0FA54D987F6E72435CAFD50AF366CB@SHSMSX101.ccr.corp.intel.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox