]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Common/Expression.py
BaseTools: Fix old python2 idioms
[mirror_edk2.git] / BaseTools / Source / Python / Common / Expression.py
index 7b04dcdb36ccf4172a018dff37d8baccbaeef43b..e1a2c155b7f3d94afa62997413d8a1f6f6b43480 100644 (file)
@@ -12,6 +12,7 @@
 \r
 ## Import Modules\r
 #\r
+from __future__ import print_function\r
 from Common.GlobalData import *\r
 from CommonDataClass.Exceptions import BadExpression\r
 from CommonDataClass.Exceptions import WrnExpression\r
@@ -244,12 +245,12 @@ class ValueExpression(BaseExpression):
         WrnExp = None\r
 \r
         if Operator not in {"==", "!=", ">=", "<=", ">", "<", "in", "not in"} and \\r
-            (type(Oprand1) == type('') or type(Oprand2) == type('')):\r
+            (isinstance(Oprand1, type('')) or isinstance(Oprand2, type(''))):\r
             raise BadExpression(ERR_STRING_EXPR % Operator)\r
         if Operator in {'in', 'not in'}:\r
-            if type(Oprand1) != type(''):\r
+            if not isinstance(Oprand1, type('')):\r
                 Oprand1 = IntToStr(Oprand1)\r
-            if type(Oprand2) != type(''):\r
+            if not isinstance(Oprand2, type('')):\r
                 Oprand2 = IntToStr(Oprand2)\r
         TypeDict = {\r
             type(0)  : 0,\r
@@ -260,18 +261,18 @@ class ValueExpression(BaseExpression):
 \r
         EvalStr = ''\r
         if Operator in {"!", "NOT", "not"}:\r
-            if type(Oprand1) == type(''):\r
+            if isinstance(Oprand1, type('')):\r
                 raise BadExpression(ERR_STRING_EXPR % Operator)\r
             EvalStr = 'not Oprand1'\r
         elif Operator in {"~"}:\r
-            if type(Oprand1) == type(''):\r
+            if isinstance(Oprand1, type('')):\r
                 raise BadExpression(ERR_STRING_EXPR % Operator)\r
             EvalStr = '~ Oprand1'\r
         else:\r
             if Operator in {"+", "-"} and (type(True) in {type(Oprand1), type(Oprand2)}):\r
                 # Boolean in '+'/'-' will be evaluated but raise warning\r
                 WrnExp = WrnExpression(WRN_BOOL_EXPR)\r
-            elif type('') in {type(Oprand1), type(Oprand2)} and type(Oprand1)!= type(Oprand2):\r
+            elif type('') in {type(Oprand1), type(Oprand2)} and not isinstance(Oprand1, type(Oprand2)):\r
                 # == between string and number/boolean will always return False, != return True\r
                 if Operator == "==":\r
                     WrnExp = WrnExpression(WRN_EQCMP_STR_OTHERS)\r
@@ -292,11 +293,11 @@ class ValueExpression(BaseExpression):
                     pass\r
                 else:\r
                     raise BadExpression(ERR_EXPR_TYPE)\r
-            if type(Oprand1) == type('') and type(Oprand2) == type(''):\r
+            if isinstance(Oprand1, type('')) and isinstance(Oprand2, type('')):\r
                 if (Oprand1.startswith('L"') and not Oprand2.startswith('L"')) or \\r
                     (not Oprand1.startswith('L"') and Oprand2.startswith('L"')):\r
                     raise BadExpression(ERR_STRING_CMP % (Oprand1, Operator, Oprand2))\r
-            if 'in' in Operator and type(Oprand2) == type(''):\r
+            if 'in' in Operator and isinstance(Oprand2, type('')):\r
                 Oprand2 = Oprand2.split()\r
             EvalStr = 'Oprand1 ' + Operator + ' Oprand2'\r
 \r
@@ -324,7 +325,7 @@ class ValueExpression(BaseExpression):
     def __init__(self, Expression, SymbolTable={}):\r
         super(ValueExpression, self).__init__(self, Expression, SymbolTable)\r
         self._NoProcess = False\r
-        if type(Expression) != type(''):\r
+        if not isinstance(Expression, type('')):\r
             self._Expr = Expression\r
             self._NoProcess = True\r
             return\r
@@ -372,7 +373,7 @@ class ValueExpression(BaseExpression):
                 Token = self._GetToken()\r
             except BadExpression:\r
                 pass\r
-            if type(Token) == type('') and Token.startswith('{') and Token.endswith('}') and self._Idx >= self._Len:\r
+            if isinstance(Token, type('')) and Token.startswith('{') and Token.endswith('}') and self._Idx >= self._Len:\r
                 return self._Expr\r
 \r
             self._Idx = 0\r
@@ -380,7 +381,7 @@ class ValueExpression(BaseExpression):
 \r
         Val = self._ConExpr()\r
         RealVal = Val\r
-        if type(Val) == type(''):\r
+        if isinstance(Val, type('')):\r
             if Val == 'L""':\r
                 Val = False\r
             elif not Val:\r
@@ -568,7 +569,7 @@ class ValueExpression(BaseExpression):
         IsArray = IsGuid = False\r
         if len(Token.split(',')) == 11 and len(Token.split(',{')) == 2 \\r
             and len(Token.split('},')) == 1:\r
-            HexLen = [11,6,6,5,4,4,4,4,4,4,6]\r
+            HexLen = [11, 6, 6, 5, 4, 4, 4, 4, 4, 4, 6]\r
             HexList= Token.split(',')\r
             if HexList[3].startswith('{') and \\r
                 not [Index for Index, Hex in enumerate(HexList) if len(Hex) > HexLen[Index]]:\r
@@ -639,7 +640,7 @@ class ValueExpression(BaseExpression):
                 Ex.Pcd = self._Token\r
                 raise Ex\r
             self._Token = ValueExpression(self._Symb[self._Token], self._Symb)(True, self._Depth+1)\r
-            if type(self._Token) != type(''):\r
+            if not isinstance(self._Token, type('')):\r
                 self._LiteralToken = hex(self._Token)\r
                 return\r
 \r
@@ -734,7 +735,7 @@ class ValueExpression(BaseExpression):
                 if Ch == ')':\r
                     TmpValue = self._Expr[Idx :self._Idx - 1]\r
                     TmpValue = ValueExpression(TmpValue)(True)\r
-                    TmpValue = '0x%x' % int(TmpValue) if type(TmpValue) != type('') else TmpValue\r
+                    TmpValue = '0x%x' % int(TmpValue) if not isinstance(TmpValue, type('')) else TmpValue\r
                     break\r
             self._Token, Size = ParseFieldValue(Prefix + '(' + TmpValue + ')')\r
             return  self._Token\r
@@ -764,7 +765,7 @@ class ValueExpression(BaseExpression):
     # Parse operator\r
     def _GetOperator(self):\r
         self.__SkipWS()\r
-        LegalOpLst = ['&&', '||', '!=', '==', '>=', '<='] + self.NonLetterOpLst + ['?',':']\r
+        LegalOpLst = ['&&', '||', '!=', '==', '>=', '<='] + self.NonLetterOpLst + ['?', ':']\r
 \r
         self._Token = ''\r
         Expr = self._Expr[self._Idx:]\r
@@ -823,7 +824,7 @@ class ValueExpressionEx(ValueExpression):
                 PcdValue = PcdValue.strip()\r
                 if PcdValue.startswith('{') and PcdValue.endswith('}'):\r
                     PcdValue = SplitPcdValueString(PcdValue[1:-1])\r
-                if type(PcdValue) == type([]):\r
+                if isinstance(PcdValue, type([])):\r
                     TmpValue = 0\r
                     Size = 0\r
                     ValueType = ''\r
@@ -841,7 +842,7 @@ class ValueExpressionEx(ValueExpression):
                         elif Item.startswith(TAB_UINT64):\r
                             ItemSize = 8\r
                             ValueType = TAB_UINT64\r
-                        elif Item[0] in {'"',"'",'L'}:\r
+                        elif Item[0] in {'"', "'", 'L'}:\r
                             ItemSize = 0\r
                             ValueType = TAB_VOID\r
                         else:\r
@@ -862,7 +863,7 @@ class ValueExpressionEx(ValueExpression):
                         else:\r
                             ItemValue = ParseFieldValue(Item)[0]\r
 \r
-                        if type(ItemValue) == type(''):\r
+                        if isinstance(ItemValue, type('')):\r
                             ItemValue = int(ItemValue, 0)\r
 \r
                         TmpValue = (ItemValue << (Size * 8)) | TmpValue\r
@@ -872,7 +873,7 @@ class ValueExpressionEx(ValueExpression):
                         TmpValue, Size = ParseFieldValue(PcdValue)\r
                     except BadExpression as Value:\r
                         raise BadExpression("Type: %s, Value: %s, %s" % (self.PcdType, PcdValue, Value))\r
-                if type(TmpValue) == type(''):\r
+                if isinstance(TmpValue, type('')):\r
                     try:\r
                         TmpValue = int(TmpValue)\r
                     except:\r
@@ -945,7 +946,7 @@ class ValueExpressionEx(ValueExpression):
                             # replace each offset, except errors\r
                             for Offset in OffsetList:\r
                                 try:\r
-                                    Item = Item.replace('OFFSET_OF({})'.format(Offset),LabelDict[Offset])\r
+                                    Item = Item.replace('OFFSET_OF({})'.format(Offset), LabelDict[Offset])\r
                                 except:\r
                                     raise BadExpression('%s not defined' % Offset)\r
 \r
@@ -995,10 +996,10 @@ class ValueExpressionEx(ValueExpression):
                                     TmpValue = ValueExpressionEx(Item, ValueType, self._Symb)(True)\r
                                 else:\r
                                     TmpValue = ValueExpressionEx(Item, self.PcdType, self._Symb)(True)\r
-                                Item = '0x%x' % TmpValue if type(TmpValue) != type('') else TmpValue\r
+                                Item = '0x%x' % TmpValue if not isinstance(TmpValue, type('')) else TmpValue\r
                                 if ItemSize == 0:\r
                                     ItemValue, ItemSize = ParseFieldValue(Item)\r
-                                    if Item[0] not in {'"','L','{'} and ItemSize > 1:\r
+                                    if Item[0] not in {'"', 'L', '{'} and ItemSize > 1:\r
                                         raise BadExpression("Byte  array number %s should less than 0xFF." % Item)\r
                                 else:\r
                                     ItemValue = ParseFieldValue(Item)[0]\r
@@ -1028,10 +1029,10 @@ if __name__ == '__main__':
         if input in 'qQ':\r
             break\r
         try:\r
-            print ValueExpression(input)(True)\r
-            print ValueExpression(input)(False)\r
+            print(ValueExpression(input)(True))\r
+            print(ValueExpression(input)(False))\r
         except WrnExpression as Ex:\r
-            print Ex.result\r
-            print str(Ex)\r
+            print(Ex.result)\r
+            print(str(Ex))\r
         except Exception as Ex:\r
-            print str(Ex)\r
+            print(str(Ex))\r