diff options
author | Yunhua Feng <yunhuax.feng@intel.com> | 2018-07-27 16:02:05 +0800 |
---|---|---|
committer | Yonghong Zhu <yonghong.zhu@intel.com> | 2018-10-13 09:50:45 +0800 |
commit | a09f4c91f785e36f0987aa3a6d7656ba51e6aeda (patch) | |
tree | 0a644e4b34f4fceb65d10465dbb6688fcddddfa5 /BaseTools/Source/Python/Common/Misc.py | |
parent | fe3991d63552f2eb25ff728a81a6976a3b1f652b (diff) | |
download | edk2-a09f4c91f785e36f0987aa3a6d7656ba51e6aeda.tar.gz edk2-a09f4c91f785e36f0987aa3a6d7656ba51e6aeda.tar.bz2 edk2-a09f4c91f785e36f0987aa3a6d7656ba51e6aeda.zip |
BaseTools: fix the open file's read and write bugs
Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Yunhua Feng <yunhuax.feng@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
Diffstat (limited to 'BaseTools/Source/Python/Common/Misc.py')
-rw-r--r-- | BaseTools/Source/Python/Common/Misc.py | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/BaseTools/Source/Python/Common/Misc.py b/BaseTools/Source/Python/Common/Misc.py index b91f06b19e..1d62a8b56b 100644 --- a/BaseTools/Source/Python/Common/Misc.py +++ b/BaseTools/Source/Python/Common/Misc.py @@ -459,8 +459,14 @@ def SaveFileOnChange(File, Content, IsBinaryFile=True): if os.path.exists(File):
try:
- if Content == open(File, "rb").read():
- return False
+ if isinstance(Content, bytes):
+ with open(File, "rb") as f:
+ if Content == f.read():
+ return False
+ else:
+ with open(File, "r") as f:
+ if Content == f.read():
+ return False
except:
EdkLogger.error(None, FILE_OPEN_FAILURE, ExtraData=File)
@@ -480,13 +486,19 @@ def SaveFileOnChange(File, Content, IsBinaryFile=True): if not SaveFileToDisk(File, Content):
EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData=File)
except:
- Fd = open(File, "wb")
- Fd.write(Content)
- Fd.close()
+ if isinstance(Content, bytes):
+ with open(File, "wb") as Fd:
+ Fd.write(Content)
+ else:
+ with open(File, "w") as Fd:
+ Fd.write(Content)
else:
- Fd = open(File, "wb")
- Fd.write(Content)
- Fd.close()
+ if isinstance(Content, bytes):
+ with open(File, "wb") as Fd:
+ Fd.write(Content)
+ else:
+ with open(File, "w") as Fd:
+ Fd.write(Content)
except IOError as X:
EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X)
|