From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.151; helo=mga17.intel.com; envelope-from=michael.johnson@intel.com; receiver=edk2-devel@lists.01.org Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id B7DC421180F4F for ; Thu, 1 Nov 2018 19:02:30 -0700 (PDT) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga107.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 01 Nov 2018 19:02:30 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.54,454,1534834800"; d="scan'208";a="82796085" Received: from clu11-mobl.ccr.corp.intel.com (HELO mjohn4-MOBL.amr.corp.intel.com) ([10.251.14.202]) by fmsmga007.fm.intel.com with ESMTP; 01 Nov 2018 19:02:30 -0700 From: Michael Johnson To: edk2-devel@lists.01.org Cc: mjohn4 , Liming Gao Date: Thu, 1 Nov 2018 19:02:10 -0700 Message-Id: <20181102020210.12320-1-michael.johnson@intel.com> X-Mailer: git-send-email 2.18.0.windows.1 Subject: [PATCH v3 1/1] BaseTools: Explicitly close files after readlines X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 02 Nov 2018 02:02:30 -0000 From: mjohn4 Rework some file open().readlines to open, readlines, close. This prevents excessive file handles being open at the same time, which may be a problem with alternative python environments. Cc: Liming Gao Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Michael Johnson --- BaseTools/Source/Python/AutoGen/InfSectionParser.py | 3 ++- BaseTools/Source/Python/Workspace/MetaFileParser.py | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/BaseTools/Source/Python/AutoGen/InfSectionParser.py b/BaseTools/Source/Python/AutoGen/InfSectionParser.py index d985089738..69f0bc8ee9 100644 --- a/BaseTools/Source/Python/AutoGen/InfSectionParser.py +++ b/BaseTools/Source/Python/AutoGen/InfSectionParser.py @@ -34,7 +34,8 @@ class InfSectionParser(): SectionData = [] try: - FileLinesList = open(self._FilePath, "r", 0).readlines() + with open(self._FilePath, "r", 0) as File: + FileLinesList = File.readlines() except BaseException: EdkLogger.error("build", AUTOGEN_ERROR, 'File %s is opened failed.' % self._FilePath) diff --git a/BaseTools/Source/Python/Workspace/MetaFileParser.py b/BaseTools/Source/Python/Workspace/MetaFileParser.py index 804a4aa5cb..1cbed6e693 100644 --- a/BaseTools/Source/Python/Workspace/MetaFileParser.py +++ b/BaseTools/Source/Python/Workspace/MetaFileParser.py @@ -537,7 +537,8 @@ class InfParser(MetaFileParser): NmakeLine = '' Content = '' try: - Content = open(str(self.MetaFile), 'r').readlines() + with open(str(self.MetaFile), 'r') as File: + Content = File.readlines() except: EdkLogger.error("Parser", FILE_READ_FAILURE, ExtraData=self.MetaFile) @@ -917,7 +918,8 @@ class DscParser(MetaFileParser): def Start(self): Content = '' try: - Content = open(str(self.MetaFile), 'r').readlines() + with open(str(self.MetaFile), 'r') as File: + Content = File.readlines() except: EdkLogger.error("Parser", FILE_READ_FAILURE, ExtraData=self.MetaFile) @@ -1429,7 +1431,12 @@ class DscParser(MetaFileParser): self._SubsectionType = MODEL_UNKNOWN def __RetrievePcdValue(self): - Content = open(str(self.MetaFile), 'r').readlines() + try: + with open(str(self.MetaFile), 'r') as File: + Content = File.readlines() + except: + EdkLogger.error("Parser", FILE_READ_FAILURE, ExtraData=self.MetaFile) + GlobalData.gPlatformOtherPcds['DSCFILE'] = str(self.MetaFile) for PcdType in (MODEL_PCD_PATCHABLE_IN_MODULE, MODEL_PCD_DYNAMIC_DEFAULT, MODEL_PCD_DYNAMIC_HII, MODEL_PCD_DYNAMIC_VPD, MODEL_PCD_DYNAMIC_EX_DEFAULT, MODEL_PCD_DYNAMIC_EX_HII, @@ -1727,7 +1734,8 @@ class DecParser(MetaFileParser): def Start(self): Content = '' try: - Content = open(str(self.MetaFile), 'r').readlines() + with open(str(self.MetaFile), 'r') as File: + Content = File.readlines() except: EdkLogger.error("Parser", FILE_READ_FAILURE, ExtraData=self.MetaFile) -- 2.18.0.windows.1