From: Yunhua Feng Date: Tue, 14 Aug 2018 07:57:47 +0000 (+0800) Subject: BaseTools: change the Division Operator in the expression X-Git-Tag: edk2-stable201903~859 X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=commitdiff_plain;h=2e300969aeff5a863098242ebd95b7d653893262 BaseTools: change the Division Operator in the expression PEP 238 -- Changing the Division Operator x/y to return a reasonable approximation of the mathematical result of the division ("true division") x//y to return the floor ("floor division") Cc: Liming Gao Cc: Yonghong Zhu Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Yunhua Feng Reviewed-by: Liming Gao --- diff --git a/BaseTools/Source/Python/AutoGen/AutoGen.py b/BaseTools/Source/Python/AutoGen/AutoGen.py index ac2c2cda50..2b3f93c3bc 100644 --- a/BaseTools/Source/Python/AutoGen/AutoGen.py +++ b/BaseTools/Source/Python/AutoGen/AutoGen.py @@ -3729,7 +3729,7 @@ class ModuleAutoGen(AutoGen): Padding = '0x00, ' if Unicode: Padding = Padding * 2 - ArraySize = ArraySize / 2 + ArraySize = ArraySize // 2 if ArraySize < (len(PcdValue) + 1): if Pcd.MaxSizeUserSet: EdkLogger.error("build", AUTOGEN_ERROR, diff --git a/BaseTools/Source/Python/AutoGen/GenC.py b/BaseTools/Source/Python/AutoGen/GenC.py index 067d95473f..a21880f317 100644 --- a/BaseTools/Source/Python/AutoGen/GenC.py +++ b/BaseTools/Source/Python/AutoGen/GenC.py @@ -1050,7 +1050,7 @@ def CreateModulePcdCode(Info, AutoGenC, AutoGenH, Pcd): else: NewValue = NewValue + str(ord(Value[Index]) % 0x100) + ', ' if Unicode: - ArraySize = ArraySize / 2 + ArraySize = ArraySize // 2 Value = NewValue + '0 }' if ArraySize < ValueSize: if Pcd.MaxSizeUserSet: @@ -1060,7 +1060,7 @@ def CreateModulePcdCode(Info, AutoGenC, AutoGenH, Pcd): else: ArraySize = Pcd.GetPcdSize() if Unicode: - ArraySize = ArraySize / 2 + ArraySize = ArraySize // 2 Array = '[%d]' % ArraySize # # skip casting for fixed at build since it breaks ARM assembly. @@ -1919,7 +1919,7 @@ def BmpImageDecoder(File, Buffer, PaletteIndex, TransParent): else: ImageBuffer = pack('B', EFI_HII_IIBT_IMAGE_1BIT) ImageBuffer += pack('B', PaletteIndex) - Width = (BmpHeader.biWidth + 7)/8 + Width = (BmpHeader.biWidth + 7)//8 if BmpHeader.bfOffBits > BMP_IMAGE_HEADER_STRUCT.size + 2: PaletteBuffer = Buffer[BMP_IMAGE_HEADER_STRUCT.size + 2 : BmpHeader.bfOffBits] elif BmpHeader.biBitCount == 4: @@ -1928,7 +1928,7 @@ def BmpImageDecoder(File, Buffer, PaletteIndex, TransParent): else: ImageBuffer = pack('B', EFI_HII_IIBT_IMAGE_4BIT) ImageBuffer += pack('B', PaletteIndex) - Width = (BmpHeader.biWidth + 1)/2 + Width = (BmpHeader.biWidth + 1)//2 if BmpHeader.bfOffBits > BMP_IMAGE_HEADER_STRUCT.size + 2: PaletteBuffer = Buffer[BMP_IMAGE_HEADER_STRUCT.size + 2 : BmpHeader.bfOffBits] elif BmpHeader.biBitCount == 8: diff --git a/BaseTools/Source/Python/BPDG/GenVpd.py b/BaseTools/Source/Python/BPDG/GenVpd.py index 28bfde1151..fc47a4b252 100644 --- a/BaseTools/Source/Python/BPDG/GenVpd.py +++ b/BaseTools/Source/Python/BPDG/GenVpd.py @@ -430,7 +430,7 @@ class GenVPD : EdkLogger.error("BPDG", BuildToolError.FORMAT_INVALID, 'The offset value of PCD %s should be %s-byte aligned.' % (PCD.PcdCName, Alignment)) else: if PCD.PcdOccupySize % Alignment != 0: - PCD.PcdOccupySize = (PCD.PcdOccupySize / Alignment + 1) * Alignment + PCD.PcdOccupySize = (PCD.PcdOccupySize // Alignment + 1) * Alignment PackSize = PCD.PcdOccupySize if PCD._IsBoolean(PCD.PcdValue, PCD.PcdSize): @@ -508,7 +508,7 @@ class GenVPD : NowOffset = 0 for Pcd in self.PcdUnknownOffsetList : if NowOffset % Pcd.Alignment != 0: - NowOffset = (NowOffset/ Pcd.Alignment + 1) * Pcd.Alignment + NowOffset = (NowOffset // Pcd.Alignment + 1) * Pcd.Alignment Pcd.PcdBinOffset = NowOffset Pcd.PcdOffset = str(hex(Pcd.PcdBinOffset)) NowOffset += Pcd.PcdOccupySize @@ -572,7 +572,7 @@ class GenVPD : # Not been fixed if eachUnfixedPcd.PcdOffset == '*' : if LastOffset % eachUnfixedPcd.Alignment != 0: - LastOffset = (LastOffset / eachUnfixedPcd.Alignment + 1) * eachUnfixedPcd.Alignment + LastOffset = (LastOffset // eachUnfixedPcd.Alignment + 1) * eachUnfixedPcd.Alignment # The offset un-fixed pcd can write into this free space if needFixPcdSize <= (NowOffset - LastOffset) : # Change the offset value of un-fixed pcd @@ -626,7 +626,7 @@ class GenVPD : NeedFixPcd.PcdBinOffset = LastPcd.PcdBinOffset + LastPcd.PcdOccupySize if NeedFixPcd.PcdBinOffset % NeedFixPcd.Alignment != 0: - NeedFixPcd.PcdBinOffset = (NeedFixPcd.PcdBinOffset / NeedFixPcd.Alignment + 1) * NeedFixPcd.Alignment + NeedFixPcd.PcdBinOffset = (NeedFixPcd.PcdBinOffset // NeedFixPcd.Alignment + 1) * NeedFixPcd.Alignment NeedFixPcd.PcdOffset = str(hex(NeedFixPcd.PcdBinOffset)) diff --git a/BaseTools/Source/Python/Common/Expression.py b/BaseTools/Source/Python/Common/Expression.py index 9a98236a8c..5e4e66a10e 100644 --- a/BaseTools/Source/Python/Common/Expression.py +++ b/BaseTools/Source/Python/Common/Expression.py @@ -425,6 +425,13 @@ class ValueExpression(BaseExpression): else: Val = Val3 continue + # + # PEP 238 -- Changing the Division Operator + # x/y to return a reasonable approximation of the mathematical result of the division ("true division") + # x//y to return the floor ("floor division") + # + if Op == '/': + Op = '//' try: Val = self.Eval(Op, Val, EvalFunc()) except WrnExpression as Warn: @@ -898,7 +905,7 @@ class ValueExpressionEx(ValueExpression): if TmpValue.bit_length() == 0: PcdValue = '{0x00}' else: - for I in range((TmpValue.bit_length() + 7) / 8): + for I in range((TmpValue.bit_length() + 7) // 8): TmpList.append('0x%02x' % ((TmpValue >> I * 8) & 0xff)) PcdValue = '{' + ', '.join(TmpList) + '}' except: diff --git a/BaseTools/Source/Python/Common/Misc.py b/BaseTools/Source/Python/Common/Misc.py index ea0e62245f..71ab6da529 100644 --- a/BaseTools/Source/Python/Common/Misc.py +++ b/BaseTools/Source/Python/Common/Misc.py @@ -1310,7 +1310,7 @@ def ParseDevPathValue (Value): def ParseFieldValue (Value): if isinstance(Value, type(0)): - return Value, (Value.bit_length() + 7) / 8 + return Value, (Value.bit_length() + 7) // 8 if not isinstance(Value, type('')): raise BadExpression('Type %s is %s' %(Value, type(Value))) Value = Value.strip() @@ -1431,12 +1431,12 @@ def ParseFieldValue (Value): raise BadExpression("invalid hex value: %s" % Value) if Value == 0: return 0, 1 - return Value, (Value.bit_length() + 7) / 8 + return Value, (Value.bit_length() + 7) // 8 if Value[0].isdigit(): Value = int(Value, 10) if Value == 0: return 0, 1 - return Value, (Value.bit_length() + 7) / 8 + return Value, (Value.bit_length() + 7) // 8 if Value.lower() == 'true': return 1, 1 if Value.lower() == 'false': diff --git a/BaseTools/Source/Python/GenFds/DataSection.py b/BaseTools/Source/Python/GenFds/DataSection.py index 182ea4f3da..3302c14091 100644 --- a/BaseTools/Source/Python/GenFds/DataSection.py +++ b/BaseTools/Source/Python/GenFds/DataSection.py @@ -87,9 +87,9 @@ class DataSection (DataSectionClassObject): if ImageObj.SectionAlignment < 0x400: self.Alignment = str (ImageObj.SectionAlignment) elif ImageObj.SectionAlignment < 0x100000: - self.Alignment = str (ImageObj.SectionAlignment / 0x400) + 'K' + self.Alignment = str (ImageObj.SectionAlignment // 0x400) + 'K' else: - self.Alignment = str (ImageObj.SectionAlignment / 0x100000) + 'M' + self.Alignment = str (ImageObj.SectionAlignment // 0x100000) + 'M' NoStrip = True if self.SecType in (BINARY_FILE_TYPE_TE, BINARY_FILE_TYPE_PE32): diff --git a/BaseTools/Source/Python/GenFds/EfiSection.py b/BaseTools/Source/Python/GenFds/EfiSection.py index fa2148ec7b..9fe7db3f46 100644 --- a/BaseTools/Source/Python/GenFds/EfiSection.py +++ b/BaseTools/Source/Python/GenFds/EfiSection.py @@ -247,9 +247,9 @@ class EfiSection (EfiSectionClassObject): if ImageObj.SectionAlignment < 0x400: Align = str (ImageObj.SectionAlignment) elif ImageObj.SectionAlignment < 0x100000: - Align = str (ImageObj.SectionAlignment / 0x400) + 'K' + Align = str (ImageObj.SectionAlignment // 0x400) + 'K' else: - Align = str (ImageObj.SectionAlignment / 0x100000) + 'M' + Align = str (ImageObj.SectionAlignment // 0x100000) + 'M' if File[(len(File)-4):] == '.efi': MapFile = File.replace('.efi', '.map') diff --git a/BaseTools/Source/Python/GenFds/FfsInfStatement.py b/BaseTools/Source/Python/GenFds/FfsInfStatement.py index e9dd5bc2c5..9ef03edecc 100644 --- a/BaseTools/Source/Python/GenFds/FfsInfStatement.py +++ b/BaseTools/Source/Python/GenFds/FfsInfStatement.py @@ -770,9 +770,9 @@ class FfsInfStatement(FfsInfStatementClassObject): if ImageObj.SectionAlignment < 0x400: self.Alignment = str (ImageObj.SectionAlignment) elif ImageObj.SectionAlignment < 0x100000: - self.Alignment = str (ImageObj.SectionAlignment / 0x400) + 'K' + self.Alignment = str (ImageObj.SectionAlignment // 0x400) + 'K' else: - self.Alignment = str (ImageObj.SectionAlignment / 0x100000) + 'M' + self.Alignment = str (ImageObj.SectionAlignment // 0x100000) + 'M' if not NoStrip: FileBeforeStrip = os.path.join(self.OutputPath, ModuleName + '.reloc') @@ -812,9 +812,9 @@ class FfsInfStatement(FfsInfStatementClassObject): if ImageObj.SectionAlignment < 0x400: self.Alignment = str (ImageObj.SectionAlignment) elif ImageObj.SectionAlignment < 0x100000: - self.Alignment = str (ImageObj.SectionAlignment / 0x400) + 'K' + self.Alignment = str (ImageObj.SectionAlignment // 0x400) + 'K' else: - self.Alignment = str (ImageObj.SectionAlignment / 0x100000) + 'M' + self.Alignment = str (ImageObj.SectionAlignment // 0x100000) + 'M' if not NoStrip: FileBeforeStrip = os.path.join(self.OutputPath, ModuleName + '.reloc') diff --git a/BaseTools/Source/Python/GenFds/Fv.py b/BaseTools/Source/Python/GenFds/Fv.py index 7653cf692b..3c3cd0b308 100644 --- a/BaseTools/Source/Python/GenFds/Fv.py +++ b/BaseTools/Source/Python/GenFds/Fv.py @@ -212,9 +212,9 @@ class FV (FvClassObject): #The max alignment supported by FFS is 16M. self.FvAlignment = "16M" else: - self.FvAlignment = str(FvAlignmentValue / 0x100000) + "M" + self.FvAlignment = str(FvAlignmentValue // 0x100000) + "M" else: - self.FvAlignment = str(FvAlignmentValue / 0x400) + "K" + self.FvAlignment = str(FvAlignmentValue // 0x400) + "K" else: # FvAlignmentValue is less than 1K self.FvAlignment = str (FvAlignmentValue) diff --git a/BaseTools/Source/Python/GenFds/FvImageSection.py b/BaseTools/Source/Python/GenFds/FvImageSection.py index 5f1b42b078..5cc5b969fd 100644 --- a/BaseTools/Source/Python/GenFds/FvImageSection.py +++ b/BaseTools/Source/Python/GenFds/FvImageSection.py @@ -70,7 +70,7 @@ class FvImageSection(FvImageSectionClassObject): # PI FvHeader is 0x48 byte FvHeaderBuffer = FvFileObj.read(0x48) # FV alignment position. - FvAlignmentValue = 1 << (ord (FvHeaderBuffer[0x2E]) & 0x1F) + FvAlignmentValue = 1 << (FvHeaderBuffer[0x2E] & 0x1F) FvFileObj.close() if FvAlignmentValue > MaxFvAlignment: MaxFvAlignment = FvAlignmentValue @@ -86,9 +86,9 @@ class FvImageSection(FvImageSectionClassObject): if MaxFvAlignment >= 0x1000000: self.Alignment = "16M" else: - self.Alignment = str(MaxFvAlignment / 0x100000) + "M" + self.Alignment = str(MaxFvAlignment // 0x100000) + "M" else: - self.Alignment = str (MaxFvAlignment / 0x400) + "K" + self.Alignment = str (MaxFvAlignment // 0x400) + "K" else: # MaxFvAlignment is less than 1K self.Alignment = str (MaxFvAlignment) @@ -126,9 +126,9 @@ class FvImageSection(FvImageSectionClassObject): if FvAlignmentValue >= 0x1000000: self.Alignment = "16M" else: - self.Alignment = str(FvAlignmentValue / 0x100000) + "M" + self.Alignment = str(FvAlignmentValue // 0x100000) + "M" else: - self.Alignment = str (FvAlignmentValue / 0x400) + "K" + self.Alignment = str (FvAlignmentValue // 0x400) + "K" else: # FvAlignmentValue is less than 1K self.Alignment = str (FvAlignmentValue) diff --git a/BaseTools/Source/Python/GenFds/GenFds.py b/BaseTools/Source/Python/GenFds/GenFds.py index e135139bc1..ed1bd33fdb 100644 --- a/BaseTools/Source/Python/GenFds/GenFds.py +++ b/BaseTools/Source/Python/GenFds/GenFds.py @@ -684,7 +684,7 @@ class GenFds : F.read() length = F.tell() F.seek(4) - TmpStr = unpack('%dh' % ((length - 4) / 2), F.read()) + TmpStr = unpack('%dh' % ((length - 4) // 2), F.read()) Name = ''.join(chr(c) for c in TmpStr[:-1]) else: FileList = [] diff --git a/BaseTools/Source/Python/GenFds/Region.py b/BaseTools/Source/Python/GenFds/Region.py index cd81c6df68..31c65a32a1 100644 --- a/BaseTools/Source/Python/GenFds/Region.py +++ b/BaseTools/Source/Python/GenFds/Region.py @@ -296,7 +296,7 @@ class Region(RegionClassObject): else: # region ended within current blocks if self.Offset + self.Size <= End: - ExpectedList.append((BlockSize, (RemindingSize + BlockSize - 1) / BlockSize)) + ExpectedList.append((BlockSize, (RemindingSize + BlockSize - 1) // BlockSize)) break # region not ended yet else: @@ -305,7 +305,7 @@ class Region(RegionClassObject): UsedBlockNum = BlockNum # region started in middle of current blocks else: - UsedBlockNum = (End - self.Offset) / BlockSize + UsedBlockNum = (End - self.Offset) // BlockSize Start = End ExpectedList.append((BlockSize, UsedBlockNum)) RemindingSize -= BlockSize * UsedBlockNum diff --git a/BaseTools/Source/Python/PatchPcdValue/PatchPcdValue.py b/BaseTools/Source/Python/PatchPcdValue/PatchPcdValue.py index 8e243aea96..fb99302933 100644 --- a/BaseTools/Source/Python/PatchPcdValue/PatchPcdValue.py +++ b/BaseTools/Source/Python/PatchPcdValue/PatchPcdValue.py @@ -133,7 +133,7 @@ def PatchBinaryFile(FileName, ValueOffset, TypeName, ValueString, MaxSize=0): # for Index in range(ValueLength): ByteList[ValueOffset + Index] = ValueNumber % 0x100 - ValueNumber = ValueNumber / 0x100 + ValueNumber = ValueNumber // 0x100 elif TypeName == TAB_VOID: ValueString = SavedStr if ValueString.startswith('L"'): diff --git a/BaseTools/Source/Python/build/build.py b/BaseTools/Source/Python/build/build.py index ad30c652da..1a21395699 100644 --- a/BaseTools/Source/Python/build/build.py +++ b/BaseTools/Source/Python/build/build.py @@ -724,7 +724,7 @@ class PeImageInfo(): self.OutputDir = OutputDir self.DebugDir = DebugDir self.Image = ImageClass - self.Image.Size = (self.Image.Size / 0x1000 + 1) * 0x1000 + self.Image.Size = (self.Image.Size // 0x1000 + 1) * 0x1000 ## The class implementing the EDK2 build process # @@ -1597,7 +1597,7 @@ class Build(): RtModuleList[Module.MetaFile] = ImageInfo #IPF runtime driver needs to be at 2 page alignment. if IsIpfPlatform and ImageInfo.Image.Size % 0x2000 != 0: - ImageInfo.Image.Size = (ImageInfo.Image.Size / 0x2000 + 1) * 0x2000 + ImageInfo.Image.Size = (ImageInfo.Image.Size // 0x2000 + 1) * 0x2000 RtSize += ImageInfo.Image.Size elif Module.ModuleType in [SUP_MODULE_SMM_CORE, SUP_MODULE_DXE_SMM_DRIVER, SUP_MODULE_MM_STANDALONE, SUP_MODULE_MM_CORE_STANDALONE]: SmmModuleList[Module.MetaFile] = ImageInfo @@ -1666,21 +1666,21 @@ class Build(): for PcdInfo in PcdTable: ReturnValue = 0 if PcdInfo[0] == TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_PEI_PAGE_SIZE: - ReturnValue, ErrorInfo = PatchBinaryFile (EfiImage, PcdInfo[1], TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_PEI_PAGE_SIZE_DATA_TYPE, str (PeiSize / 0x1000)) + ReturnValue, ErrorInfo = PatchBinaryFile (EfiImage, PcdInfo[1], TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_PEI_PAGE_SIZE_DATA_TYPE, str (PeiSize // 0x1000)) elif PcdInfo[0] == TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_DXE_PAGE_SIZE: - ReturnValue, ErrorInfo = PatchBinaryFile (EfiImage, PcdInfo[1], TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_DXE_PAGE_SIZE_DATA_TYPE, str (BtSize / 0x1000)) + ReturnValue, ErrorInfo = PatchBinaryFile (EfiImage, PcdInfo[1], TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_DXE_PAGE_SIZE_DATA_TYPE, str (BtSize // 0x1000)) elif PcdInfo[0] == TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_RUNTIME_PAGE_SIZE: - ReturnValue, ErrorInfo = PatchBinaryFile (EfiImage, PcdInfo[1], TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_RUNTIME_PAGE_SIZE_DATA_TYPE, str (RtSize / 0x1000)) + ReturnValue, ErrorInfo = PatchBinaryFile (EfiImage, PcdInfo[1], TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_RUNTIME_PAGE_SIZE_DATA_TYPE, str (RtSize // 0x1000)) elif PcdInfo[0] == TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_SMM_PAGE_SIZE and len (SmmModuleList) > 0: - ReturnValue, ErrorInfo = PatchBinaryFile (EfiImage, PcdInfo[1], TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_SMM_PAGE_SIZE_DATA_TYPE, str (SmmSize / 0x1000)) + ReturnValue, ErrorInfo = PatchBinaryFile (EfiImage, PcdInfo[1], TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_SMM_PAGE_SIZE_DATA_TYPE, str (SmmSize // 0x1000)) if ReturnValue != 0: EdkLogger.error("build", PARAMETER_INVALID, "Patch PCD value failed", ExtraData=ErrorInfo) - MapBuffer.write('PEI_CODE_PAGE_NUMBER = 0x%x\n' % (PeiSize / 0x1000)) - MapBuffer.write('BOOT_CODE_PAGE_NUMBER = 0x%x\n' % (BtSize / 0x1000)) - MapBuffer.write('RUNTIME_CODE_PAGE_NUMBER = 0x%x\n' % (RtSize / 0x1000)) + MapBuffer.write('PEI_CODE_PAGE_NUMBER = 0x%x\n' % (PeiSize // 0x1000)) + MapBuffer.write('BOOT_CODE_PAGE_NUMBER = 0x%x\n' % (BtSize // 0x1000)) + MapBuffer.write('RUNTIME_CODE_PAGE_NUMBER = 0x%x\n' % (RtSize // 0x1000)) if len (SmmModuleList) > 0: - MapBuffer.write('SMM_CODE_PAGE_NUMBER = 0x%x\n' % (SmmSize / 0x1000)) + MapBuffer.write('SMM_CODE_PAGE_NUMBER = 0x%x\n' % (SmmSize // 0x1000)) PeiBaseAddr = TopMemoryAddress - RtSize - BtSize BtBaseAddr = TopMemoryAddress - RtSize