## Import Modules\r
#\r
from __future__ import print_function\r
+from __future__ import absolute_import\r
from Common.GlobalData import *\r
from CommonDataClass.Exceptions import BadExpression\r
from CommonDataClass.Exceptions import WrnExpression\r
-from Misc import GuidStringToGuidStructureString, ParseFieldValue, IsFieldValueAnArray\r
+from .Misc import GuidStringToGuidStructureString, ParseFieldValue, IsFieldValueAnArray\r
import Common.EdkLogger as EdkLogger\r
import copy\r
from Common.DataType import *\r
+import sys\r
\r
ERR_STRING_EXPR = 'This operator cannot be used in string expression: [%s].'\r
ERR_SNYTAX = 'Syntax error, the rest of expression cannot be evaluated: [%s].'\r
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
- type(0L) : 0,\r
+ # For python2 long type\r
+ type(sys.maxsize + 1) : 0,\r
type('') : 1,\r
type(True) : 2\r
}\r
\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
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
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
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
\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
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
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
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
# 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
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
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
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
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
raise BadExpression('Type %s PCD Value Size is Larger than 8 byte' % self.PcdType)\r
else:\r
try:\r
- TmpValue = long(PcdValue)\r
+ TmpValue = int(PcdValue)\r
TmpList = []\r
if TmpValue.bit_length() == 0:\r
PcdValue = '{0x00}'\r
# 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
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