From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=134.134.136.100; helo=mga07.intel.com; envelope-from=bob.c.feng@intel.com; receiver=edk2-devel-bounces@lists.01.org Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) (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 771B82118A5B3; Thu, 1 Nov 2018 17:39:16 -0700 (PDT) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 01 Nov 2018 17:39:16 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.54,454,1534834800"; d="scan'208";a="83401122" Received: from fmsmsx104.amr.corp.intel.com ([10.18.124.202]) by fmsmga008.fm.intel.com with ESMTP; 01 Nov 2018 17:39:16 -0700 Received: from fmsmsx120.amr.corp.intel.com (10.18.124.208) by fmsmsx104.amr.corp.intel.com (10.18.124.202) with Microsoft SMTP Server (TLS) id 14.3.408.0; Thu, 1 Nov 2018 17:39:16 -0700 Received: from shsmsx103.ccr.corp.intel.com (10.239.4.69) by fmsmsx120.amr.corp.intel.com (10.18.124.208) with Microsoft SMTP Server (TLS) id 14.3.408.0; Thu, 1 Nov 2018 17:39:15 -0700 Received: from shsmsx101.ccr.corp.intel.com ([169.254.1.102]) by SHSMSX103.ccr.corp.intel.com ([169.254.4.161]) with mapi id 14.03.0415.000; Fri, 2 Nov 2018 08:39:13 +0800 From: "Feng, Bob C" To: "Johnson, Michael" , edk2-devel , "edk2-devel@lists.01.org" CC: "Gao, Liming" Thread-Topic: [edk2] [PATCH v1 1/1] BaseTools: Explicitly close files after readlines Thread-Index: AQHUcjvKCT0OhhFRE0S+xTRHPrZuZqU7nnlQ Date: Fri, 2 Nov 2018 00:39:13 +0000 Message-ID: <08650203BA1BD64D8AD9B6D5D74A85D15FFF422A@SHSMSX101.ccr.corp.intel.com> References: <20181101233520.908-1-michael.johnson@intel.com> In-Reply-To: <20181101233520.908-1-michael.johnson@intel.com> Accept-Language: zh-CN, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-titus-metadata-40: eyJDYXRlZ29yeUxhYmVscyI6IiIsIk1ldGFkYXRhIjp7Im5zIjoiaHR0cDpcL1wvd3d3LnRpdHVzLmNvbVwvbnNcL0ludGVsMyIsImlkIjoiZjJlNzVmM2YtODdkZC00M2QwLWJkODEtZjkwZGE5ZmZjODQ4IiwicHJvcHMiOlt7Im4iOiJDVFBDbGFzc2lmaWNhdGlvbiIsInZhbHMiOlt7InZhbHVlIjoiQ1RQX05UIn1dfV19LCJTdWJqZWN0TGFiZWxzIjpbXSwiVE1DVmVyc2lvbiI6IjE3LjEwLjE4MDQuNDkiLCJUcnVzdGVkTGFiZWxIYXNoIjoiQlJWaElUUmFYZUhcL1NOWGN0WGwxYjlqXC9yRmhmWkRqb012YUVZQ1lJV1djVjBZUm5VM3hOWGMyYnNIVUphMUdHIn0= x-ctpclassification: CTP_NT dlp-product: dlpe-windows dlp-version: 11.0.400.15 dlp-reaction: no-action x-originating-ip: [10.239.127.40] MIME-Version: 1.0 Subject: Re: [PATCH v1 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 00:39:17 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Hi Michael, For the file operation, I would suggest to use with statement. with stateme= nt will automatically close the file after the nested block of code. The ad= vantage of using a with statement is that it is guaranteed to close the fil= e no matter how the nested block exits. For example try: with open(self._FilePath, "r", 0) as File FileLinesList =3D File.readlines() except BaseException: EdkLogger.error("build", AUTOGEN_ERROR, 'File %s is opened= failed.' % self._FilePath) Thanks, Bob -----Original Message----- From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org]=20 Sent: Friday, November 2, 2018 7:35 AM To: edk2-devel@lists.01.org Cc: Gao, Liming Subject: [edk2] [PATCH v1 1/1] BaseTools: Explicitly close files after read= lines 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 | 4 +++- BaseTools/S= ource/Python/Workspace/MetaFileParser.py | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/BaseTools/Source/Python/AutoGen/InfSectionParser.py b/BaseTool= s/Source/Python/AutoGen/InfSectionParser.py index d985089738..b186984101 100644 --- a/BaseTools/Source/Python/AutoGen/InfSectionParser.py +++ b/BaseTools/Source/Python/AutoGen/InfSectionParser.py @@ -34,7 +34,9 @@ class InfSectionParser(): SectionData =3D [] =20 try: - FileLinesList =3D open(self._FilePath, "r", 0).readlines() + File =3D open(self._FilePath, "r", 0) + FileLinesList =3D File.readlines() + File.close() except BaseException: EdkLogger.error("build", AUTOGEN_ERROR, 'File %s is opened fai= led.' % self._FilePath) =20 diff --git a/BaseTools/Source/Python/Workspace/MetaFileParser.py b/BaseTool= s/Source/Python/Workspace/MetaFileParser.py index 804a4aa5cb..9a795315d1 100644 --- a/BaseTools/Source/Python/Workspace/MetaFileParser.py +++ b/BaseTools/Source/Python/Workspace/MetaFileParser.py @@ -537,7 +537,9 @@ class InfParser(MetaFileParser): NmakeLine =3D '' Content =3D '' try: - Content =3D open(str(self.MetaFile), 'r').readlines() + File =3D open(str(self.MetaFile), 'r') + Content =3D File.readlines() + File.close () except: EdkLogger.error("Parser", FILE_READ_FAILURE, ExtraData=3Dself.= MetaFile) =20 @@ -917,7 +919,9 @@ class DscParser(MetaFileParser): def Start(self): Content =3D '' try: - Content =3D open(str(self.MetaFile), 'r').readlines() + File =3D open(str(self.MetaFile), 'r') + Content =3D File.readlines() + File.close() except: EdkLogger.error("Parser", FILE_READ_FAILURE, ExtraData=3Dself.= MetaFile) =20 @@ -1429,7 +1433,9 @@ class DscParser(MetaFileParser): self._SubsectionType =3D MODEL_UNKNOWN =20 def __RetrievePcdValue(self): - Content =3D open(str(self.MetaFile), 'r').readlines() + File =3D open(str(self.MetaFile), 'r') + Content =3D File.readlines() + File.close() GlobalData.gPlatformOtherPcds['DSCFILE'] =3D str(self.MetaFile) for PcdType in (MODEL_PCD_PATCHABLE_IN_MODULE, MODEL_PCD_DYNAMIC_D= EFAULT, MODEL_PCD_DYNAMIC_HII, MODEL_PCD_DYNAMIC_VPD, MODEL_PCD_DYNAMIC_EX_DEFAUL= T, MODEL_PCD_DYNAMIC_EX_HII, @@ -1727,7 +1733,9 @@ class DecParser(MetaFile= Parser): def Start(self): Content =3D '' try: - Content =3D open(str(self.MetaFile), 'r').readlines() + File =3D open(str(self.MetaFile), 'r') + Content =3D File.readlines() + File.close() except: EdkLogger.error("Parser", FILE_READ_FAILURE, ExtraData=3Dself.= MetaFile) =20 -- 2.18.0.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel