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 <liming....@intel.com>
Cc: Yonghong Zhu <yonghong....@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Jaben Carsey <jaben.car...@intel.com>
---
 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.<BR>
+# Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
 # 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.<BR>
+# Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>
 # 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

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel

Reply via email to