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.65; helo=mga03.intel.com; envelope-from=jaben.carsey@intel.com; receiver=edk2-devel@lists.01.org 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) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False 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 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,249,1526367600"; d="scan'208";a="209784367" 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 From: Jaben Carsey To: edk2-devel@lists.01.org Cc: Liming Gao , Yonghong Zhu 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: [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: , X-List-Received-Date: Wed, 20 Jun 2018 21:08:29 -0000 use python's ABC (abstract base class) to raise type errors if we instantiate 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 ## Regular expression for splitting Dependency Expression string into tokens gDepexTokenPattern = re.compile("(\(|\)|\w+| \S+\.inf)") @@ -201,6 +202,9 @@ class AutoGen(object): cls.__ObjectCache[Key] = super(AutoGen, cls).__new__(cls) return cls.__ObjectCache[Key] + __metaclass__ = ABCMeta + # prevent this class from being accidentally instantiated + @abstractmethod def __init__ (self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs): super(AutoGen, self).__init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs) 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 ## Regular expression for finding header file inclusions gIncludePattern = 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__ = ABCMeta + # prevent this class from being accidentally instantiated + @abstractmethod def __init__(self, AutoGenObject): self._AutoGenObject = AutoGenObject self._FileType = gMakeType diff --git a/BaseTools/Source/Python/AutoGen/ValidCheckingInfoObject.py b/BaseTools/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 class VAR_CHECK_PCD_VARIABLE_TAB_CONTAINER(object): def __init__(self): @@ -222,6 +223,9 @@ class VAR_CHECK_PCD_VARIABLE_TAB(object): class VAR_CHECK_PCD_VALID_OBJ(object): + __metaclass__ = ABCMeta + # prevent this class from being accidentally instantiated + @abstractmethod def __init__(self, VarOffset, data, PcdDataType): self.Type = 1 self.Length = 0 # Length include this header diff --git a/BaseTools/Source/Python/Common/Expression.py b/BaseTools/Source/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, ParseFieldValue, IsFieldValueA import Common.EdkLogger as EdkLogger import copy from Common.DataType import * +from abc import ABCMeta, abstractmethod ERR_STRING_EXPR = 'This operator cannot be used in string expression: [%s].' ERR_SNYTAX = 'Syntax error, the rest of expression cannot be evaluated: [%s].' @@ -202,6 +203,9 @@ def IntToStr(Value): SupportedInMacroList = ['TARGET', 'TOOL_CHAIN_TAG', 'ARCH', 'FAMILY'] class BaseExpression(object): + __metaclass__ = ABCMeta + # prevent this class from being accidentally instantiated + @abstractmethod def __init__(self, *args, **kwargs): super(BaseExpression, self).__init__() diff --git a/BaseTools/Source/Python/Common/VariableAttributes.py b/BaseTools/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 information # # -# 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 BSD License # which accompanies this distribution. The full text of the license may be 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 IMPLIED. # +from abc import ABCMeta, abstractmethod class VariableAttributes(object): EFI_VARIABLE_NON_VOLATILE = 0x00000001 @@ -25,6 +26,9 @@ class VariableAttributes(object): "RO":VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY } + __metaclass__ = ABCMeta + # prevent this class from being accidentally instantiated + @abstractmethod def __init__(self): pass diff --git a/BaseTools/Source/Python/Ecc/MetaFileWorkspace/MetaFileParser.py 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 ## 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 !INCLUDE directive) # + __metaclass__ = ABCMeta + # prevent this class from being accidentally instantiated + @abstractmethod def __init__(self, FilePath, FileType, Table, Owner=-1, From=-1): self._Table = Table self._RawTable = Table diff --git a/BaseTools/Source/Python/Table/Table.py b/BaseTools/Source/Python/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 BSD License # which accompanies this distribution. The full text of the license may be found at @@ -15,6 +15,7 @@ # Import Modules # import Common.EdkLogger as EdkLogger +from abc import ABCMeta, abstractmethod ## TableFile # @@ -26,6 +27,9 @@ import Common.EdkLogger as EdkLogger # @param TableName: Name of the table # class Table(object): + __metaclass__ = ABCMeta + # prevent this class from being accidentally instantiated + @abstractmethod def __init__(self, Cursor): self.Cur = Cursor self.Table = '' diff --git a/BaseTools/Source/Python/Workspace/BuildClassObject.py b/BaseTools/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 ## PcdClassObject # @@ -381,6 +382,9 @@ class ModuleBuildClassObject(object): # { [(PcdCName, PcdGuidCName)] : PcdClassObject} # class PackageBuildClassObject(object): + __metaclass__ = ABCMeta + # prevent this class from being accidentally instantiated + @abstractmethod def __init__(self): self.MetaFile = '' self.PackageName = '' @@ -451,6 +455,9 @@ class PackageBuildClassObject(object): # { [BuildOptionKey] : BuildOptionValue } # class PlatformBuildClassObject(object): + __metaclass__ = ABCMeta + # prevent this class from being accidentally instantiated + @abstractmethod def __init__(self): self.MetaFile = '' self.PlatformName = '' diff --git a/BaseTools/Source/Python/Workspace/MetaFileParser.py b/BaseTools/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 as open from collections import defaultdict from MetaFileTable import MetaFileStorage from MetaFileCommentParser import CheckInfComment +from abc import ABCMeta, abstractmethod ## RegEx for finding file versions hexVersionPattern = 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 !INCLUDE directive) # + __metaclass__ = ABCMeta + # prevent this class from being accidentally instantiated + @abstractmethod def __init__(self, FilePath, FileType, Arch, Table, Owner= -1, From= -1): self._Table = Table self._RawTable = Table -- 2.16.2.windows.1