\r
## 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
}\r
try:\r
Val = eval(EvalStr, {}, Dict)\r
- except Exception, Excpt:\r
+ except Exception as Excpt:\r
raise BadExpression(str(Excpt))\r
\r
if Operator in {'and', 'or'}:\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
continue\r
try:\r
Val = self.Eval(Op, Val, EvalFunc())\r
- except WrnExpression, Warn:\r
+ except WrnExpression as Warn:\r
self._WarnExcept = Warn\r
Val = Warn.result\r
return Val\r
Op += ' ' + self._Token\r
try:\r
Val = self.Eval(Op, Val, self._RelExpr())\r
- except WrnExpression, Warn:\r
+ except WrnExpression as Warn:\r
self._WarnExcept = Warn\r
Val = Warn.result\r
return Val\r
Val = self._UnaryExpr()\r
try:\r
return self.Eval('not', Val)\r
- except WrnExpression, Warn:\r
+ except WrnExpression as Warn:\r
self._WarnExcept = Warn\r
return Warn.result\r
if self._IsOperator({"~"}):\r
Val = self._UnaryExpr()\r
try:\r
return self.Eval('~', Val)\r
- except WrnExpression, Warn:\r
+ except WrnExpression as Warn:\r
self._WarnExcept = Warn\r
return Warn.result\r
return self._IdenExpr()\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
elif self.PcdType in TAB_PCD_NUMERIC_TYPES and (PcdValue.startswith("'") or \\r
PcdValue.startswith('"') or PcdValue.startswith("L'") or PcdValue.startswith('L"') or PcdValue.startswith('{')):\r
raise BadExpression\r
- except WrnExpression, Value:\r
+ except WrnExpression as Value:\r
PcdValue = Value.result\r
- except BadExpression, Value:\r
+ except BadExpression as Value:\r
if self.PcdType in TAB_PCD_NUMERIC_TYPES:\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
tmpValue = int(Item, 0)\r
if tmpValue > 255:\r
raise BadExpression("Byte array number %s should less than 0xFF." % Item)\r
- except BadExpression, Value:\r
+ except BadExpression as Value:\r
raise BadExpression(Value)\r
except ValueError:\r
pass\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
else:\r
try:\r
TmpValue, Size = ParseFieldValue(PcdValue)\r
- except BadExpression, Value:\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
if input in 'qQ':\r
break\r
try:\r
- print ValueExpression(input)(True)\r
- print ValueExpression(input)(False)\r
- except WrnExpression, Ex:\r
- print Ex.result\r
- print str(Ex)\r
- except Exception, Ex:\r
- print str(Ex)\r
+ print(ValueExpression(input)(True))\r
+ print(ValueExpression(input)(False))\r
+ except WrnExpression as Ex:\r
+ print(Ex.result)\r
+ print(str(Ex))\r
+ except Exception as Ex:\r
+ print(str(Ex))\r