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=jaben.carsey@intel.com; receiver=edk2-devel@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 21EC920969673 for ; Mon, 14 May 2018 11:09:25 -0700 (PDT) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 14 May 2018 11:09:23 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,401,1520924400"; d="scan'208";a="55814253" Received: from jcarsey-desk1.amr.corp.intel.com ([10.7.159.144]) by orsmga001.jf.intel.com with ESMTP; 14 May 2018 11:09:23 -0700 From: Jaben Carsey To: edk2-devel@lists.01.org Cc: Liming Gao , Yonghong Zhu Date: Mon, 14 May 2018 11:09:10 -0700 Message-Id: <1d945b8d75cc240968753642344be9909eb265bb.1526321052.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: Mon, 14 May 2018 18:09:25 -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 54f6b1f173b2..619e1e41e32b 100644 --- a/BaseTools/Source/Python/AutoGen/AutoGen.py +++ b/BaseTools/Source/Python/AutoGen/AutoGen.py @@ -47,6 +47,7 @@ import hashlib from GenVar import VariableMgr,var_info from collections import OrderedDict from collections import defaultdict +from abc import ABCMeta, abstractmethod ## Regular expression for splitting Dependency Expression string into tokens gDepexTokenPattern = re.compile("(\(|\)|\w+| \S+\.inf)") @@ -197,6 +198,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 a37350742240..68ec9a817133 100644 --- a/BaseTools/Source/Python/AutoGen/GenMake.py +++ b/BaseTools/Source/Python/AutoGen/GenMake.py @@ -26,6 +26,7 @@ from Common.String 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 4d61cd1cea91..e5c43b629151 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 36843643ed13..21b20bce4018 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