From nobody Mon Feb 9 18:46:33 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail(p=none dis=none) header.from=intel.com Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1529528917248817.0409127511074; Wed, 20 Jun 2018 14:08:37 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 42368211BED4E; Wed, 20 Jun 2018 14:08:31 -0700 (PDT) Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) (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 B4F18211BD61B for ; Wed, 20 Jun 2018 14:08:29 -0700 (PDT) Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Jun 2018 14:08:29 -0700 Received: from jcarsey-desk1.amr.corp.intel.com ([10.7.159.144]) by orsmga004.jf.intel.com with ESMTP; 20 Jun 2018 14:08:29 -0700 X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=134.134.136.65; helo=mga03.intel.com; envelope-from=jaben.carsey@intel.com; receiver=edk2-devel@lists.01.org X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,249,1526367600"; d="scan'208";a="209784367" From: Jaben Carsey To: edk2-devel@lists.01.org Date: Wed, 20 Jun 2018 14:08:07 -0700 Message-Id: <738f79eca577bdf64e92e6496f24d62f97a5a04c.1529528784.git.jaben.carsey@intel.com> X-Mailer: git-send-email 2.16.2.windows.1 In-Reply-To: References: In-Reply-To: References: Subject: [edk2] [PATCH v1 01/11] BaseTools: decorate base classes to prevent instantiation X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Liming Gao MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" use python's ABC (abstract base class) to raise type errors if we instantia= te classes we designed to be used only as base classes for other classes. Cc: Liming Gao Cc: Yonghong Zhu Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jaben Carsey --- BaseTools/Source/Python/AutoGen/AutoGen.py | 4 ++++ BaseTools/Source/Python/AutoGen/GenMake.py | 4 ++++ BaseTools/Source/Python/AutoGen/ValidCheckingInfoObject.py | 4 ++++ BaseTools/Source/Python/Common/Expression.py | 4 ++++ BaseTools/Source/Python/Common/VariableAttributes.py | 6 +++++- BaseTools/Source/Python/Ecc/MetaFileWorkspace/MetaFileParser.py | 4 ++++ BaseTools/Source/Python/Table/Table.py | 6 +++++- BaseTools/Source/Python/Workspace/BuildClassObject.py | 7 +++++++ BaseTools/Source/Python/Workspace/MetaFileParser.py | 4 ++++ 9 files changed, 41 insertions(+), 2 deletions(-) diff --git a/BaseTools/Source/Python/AutoGen/AutoGen.py b/BaseTools/Source/= Python/AutoGen/AutoGen.py index ed0be3bc74f9..b7dd086e28a8 100644 --- a/BaseTools/Source/Python/AutoGen/AutoGen.py +++ b/BaseTools/Source/Python/AutoGen/AutoGen.py @@ -51,6 +51,7 @@ from GenVar import VariableMgr,var_info from collections import OrderedDict from collections import defaultdict from Workspace.WorkspaceCommon import OrderedListDict +from abc import ABCMeta, abstractmethod =20 ## Regular expression for splitting Dependency Expression string into toke= ns gDepexTokenPattern =3D re.compile("(\(|\)|\w+| \S+\.inf)") @@ -201,6 +202,9 @@ class AutoGen(object): cls.__ObjectCache[Key] =3D super(AutoGen, cls).__new__(cls) return cls.__ObjectCache[Key] =20 + __metaclass__ =3D ABCMeta + # prevent this class from being accidentally instantiated + @abstractmethod def __init__ (self, Workspace, MetaFile, Target, Toolchain, Arch, *arg= s, **kwargs): super(AutoGen, self).__init__(self, Workspace, MetaFile, Target, T= oolchain, Arch, *args, **kwargs) =20 diff --git a/BaseTools/Source/Python/AutoGen/GenMake.py b/BaseTools/Source/= Python/AutoGen/GenMake.py index 8541372159a2..b7717fc47e47 100644 --- a/BaseTools/Source/Python/AutoGen/GenMake.py +++ b/BaseTools/Source/Python/AutoGen/GenMake.py @@ -26,6 +26,7 @@ from Common.StringUtils import * from BuildEngine import * import Common.GlobalData as GlobalData from collections import OrderedDict +from abc import ABCMeta, abstractmethod =20 ## Regular expression for finding header file inclusions gIncludePattern =3D re.compile(r"^[ \t]*#?[ \t]*include(?:[ \t]*(?:\\(?:\r= \n|\r|\n))*[ \t]*)*(?:\(?[\"<]?[ \t]*)([-\w.\\/() \t]+)(?:[ \t]*[\">]?\)?)"= , re.MULTILINE | re.UNICODE | re.IGNORECASE) @@ -171,6 +172,9 @@ class BuildFile(object): # # @param AutoGenObject Object of AutoGen class # + __metaclass__ =3D ABCMeta + # prevent this class from being accidentally instantiated + @abstractmethod def __init__(self, AutoGenObject): self._AutoGenObject =3D AutoGenObject self._FileType =3D gMakeType diff --git a/BaseTools/Source/Python/AutoGen/ValidCheckingInfoObject.py b/B= aseTools/Source/Python/AutoGen/ValidCheckingInfoObject.py index 64d4965e9662..e2b4795129ef 100644 --- a/BaseTools/Source/Python/AutoGen/ValidCheckingInfoObject.py +++ b/BaseTools/Source/Python/AutoGen/ValidCheckingInfoObject.py @@ -20,6 +20,7 @@ from Common.Misc import * from StringIO import StringIO from struct import pack from Common.DataType import * +from abc import ABCMeta, abstractmethod =20 class VAR_CHECK_PCD_VARIABLE_TAB_CONTAINER(object): def __init__(self): @@ -222,6 +223,9 @@ class VAR_CHECK_PCD_VARIABLE_TAB(object): =20 =20 class VAR_CHECK_PCD_VALID_OBJ(object): + __metaclass__ =3D ABCMeta + # prevent this class from being accidentally instantiated + @abstractmethod def __init__(self, VarOffset, data, PcdDataType): self.Type =3D 1 self.Length =3D 0 # Length include this header diff --git a/BaseTools/Source/Python/Common/Expression.py b/BaseTools/Sourc= e/Python/Common/Expression.py index 9e9d9fdc02e7..9fa07c6add16 100644 --- a/BaseTools/Source/Python/Common/Expression.py +++ b/BaseTools/Source/Python/Common/Expression.py @@ -19,6 +19,7 @@ from Misc import GuidStringToGuidStructureString, ParseFi= eldValue, IsFieldValueA import Common.EdkLogger as EdkLogger import copy from Common.DataType import * +from abc import ABCMeta, abstractmethod =20 ERR_STRING_EXPR =3D 'This operator cannot be used in string expres= sion: [%s].' ERR_SNYTAX =3D 'Syntax error, the rest of expression cannot b= e evaluated: [%s].' @@ -202,6 +203,9 @@ def IntToStr(Value): SupportedInMacroList =3D ['TARGET', 'TOOL_CHAIN_TAG', 'ARCH', 'FAMILY'] =20 class BaseExpression(object): + __metaclass__ =3D ABCMeta + # prevent this class from being accidentally instantiated + @abstractmethod def __init__(self, *args, **kwargs): super(BaseExpression, self).__init__() =20 diff --git a/BaseTools/Source/Python/Common/VariableAttributes.py b/BaseToo= ls/Source/Python/Common/VariableAttributes.py index a2e22ca0409c..72f64fff3864 100644 --- a/BaseTools/Source/Python/Common/VariableAttributes.py +++ b/BaseTools/Source/Python/Common/VariableAttributes.py @@ -3,7 +3,7 @@ # This file is used to handle the variable attributes and property informa= tion # # -# Copyright (c) 2015, Intel Corporation. All rights reserved.
+# Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
# This program and the accompanying materials # are licensed and made available under the terms and conditions of the BS= D License # which accompanies this distribution. The full text of the license may b= e found at @@ -12,6 +12,7 @@ # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMP= LIED. # +from abc import ABCMeta, abstractmethod =20 class VariableAttributes(object): EFI_VARIABLE_NON_VOLATILE =3D 0x00000001 @@ -25,6 +26,9 @@ class VariableAttributes(object): "RO":VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY } =20 + __metaclass__ =3D ABCMeta + # prevent this class from being accidentally instantiated + @abstractmethod def __init__(self): pass =20 diff --git a/BaseTools/Source/Python/Ecc/MetaFileWorkspace/MetaFileParser.p= y b/BaseTools/Source/Python/Ecc/MetaFileWorkspace/MetaFileParser.py index 3749f6a2699e..c2e60016926d 100644 --- a/BaseTools/Source/Python/Ecc/MetaFileWorkspace/MetaFileParser.py +++ b/BaseTools/Source/Python/Ecc/MetaFileWorkspace/MetaFileParser.py @@ -35,6 +35,7 @@ from MetaFileTable import MetaFileStorage from GenFds.FdfParser import FdfParser from Common.LongFilePathSupport import OpenLongFilePath as open from Common.LongFilePathSupport import CodecOpenLongFilePath +from abc import ABCMeta, abstractmethod =20 ## A decorator used to parse macro definition def ParseMacro(Parser): @@ -146,6 +147,9 @@ class MetaFileParser(object): # @param Owner Owner ID (for sub-section parsing) # @param From ID from which the data comes (for !INC= LUDE directive) # + __metaclass__ =3D ABCMeta + # prevent this class from being accidentally instantiated + @abstractmethod def __init__(self, FilePath, FileType, Table, Owner=3D-1, From=3D-1): self._Table =3D Table self._RawTable =3D Table diff --git a/BaseTools/Source/Python/Table/Table.py b/BaseTools/Source/Pyth= on/Table/Table.py index c311df91c2ec..46bc92ea8377 100644 --- a/BaseTools/Source/Python/Table/Table.py +++ b/BaseTools/Source/Python/Table/Table.py @@ -1,7 +1,7 @@ ## @file # This file is used to create/update/query/erase a common table # -# Copyright (c) 2008, Intel Corporation. All rights reserved.
+# Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.
# This program and the accompanying materials # are licensed and made available under the terms and conditions of the BS= D License # which accompanies this distribution. The full text of the license may b= e found at @@ -15,6 +15,7 @@ # Import Modules # import Common.EdkLogger as EdkLogger +from abc import ABCMeta, abstractmethod =20 ## TableFile # @@ -26,6 +27,9 @@ import Common.EdkLogger as EdkLogger # @param TableName: Name of the table # class Table(object): + __metaclass__ =3D ABCMeta + # prevent this class from being accidentally instantiated + @abstractmethod def __init__(self, Cursor): self.Cur =3D Cursor self.Table =3D '' diff --git a/BaseTools/Source/Python/Workspace/BuildClassObject.py b/BaseTo= ols/Source/Python/Workspace/BuildClassObject.py index 209315d901b2..5f34e8e0bc69 100644 --- a/BaseTools/Source/Python/Workspace/BuildClassObject.py +++ b/BaseTools/Source/Python/Workspace/BuildClassObject.py @@ -18,6 +18,7 @@ from Common.Misc import RealPath2 from Common.BuildToolError import * from Common.DataType import * import collections +from abc import ABCMeta, abstractmethod =20 ## PcdClassObject # @@ -381,6 +382,9 @@ class ModuleBuildClassObject(object): # { [(PcdCName, PcdGuidCName)] : PcdClassObject} # class PackageBuildClassObject(object): + __metaclass__ =3D ABCMeta + # prevent this class from being accidentally instantiated + @abstractmethod def __init__(self): self.MetaFile =3D '' self.PackageName =3D '' @@ -451,6 +455,9 @@ class PackageBuildClassObject(object): # { [BuildOptionKey] : BuildOptionValue } # class PlatformBuildClassObject(object): + __metaclass__ =3D ABCMeta + # prevent this class from being accidentally instantiated + @abstractmethod def __init__(self): self.MetaFile =3D '' self.PlatformName =3D '' diff --git a/BaseTools/Source/Python/Workspace/MetaFileParser.py b/BaseTool= s/Source/Python/Workspace/MetaFileParser.py index a2ded0c845ae..38bc469b0f3d 100644 --- a/BaseTools/Source/Python/Workspace/MetaFileParser.py +++ b/BaseTools/Source/Python/Workspace/MetaFileParser.py @@ -34,6 +34,7 @@ from Common.LongFilePathSupport import OpenLongFilePath a= s open from collections import defaultdict from MetaFileTable import MetaFileStorage from MetaFileCommentParser import CheckInfComment +from abc import ABCMeta, abstractmethod =20 ## RegEx for finding file versions hexVersionPattern =3D re.compile(r'0[xX][\da-f-A-F]{5,8}') @@ -154,6 +155,9 @@ class MetaFileParser(object): # @param Owner Owner ID (for sub-section parsing) # @param From ID from which the data comes (for !INC= LUDE directive) # + __metaclass__ =3D ABCMeta + # prevent this class from being accidentally instantiated + @abstractmethod def __init__(self, FilePath, FileType, Arch, Table, Owner=3D -1, From= =3D -1): self._Table =3D Table self._RawTable =3D Table --=20 2.16.2.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel