From b3e94a06172113991f28a1eff096255c65702a0c Mon Sep 17 00:00:00 2001 From: Zhijux Fan Date: Fri, 7 Dec 2018 12:34:44 +0800 Subject: [PATCH] BaseTools: change the Division Operator 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: Bob Feng Cc: Liming Gao Cc: Yonghong Zhu Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Zhiju.Fan Tested-by: Laszlo Ersek Tested-by: Ard Biesheuvel Reviewed-by: Liming Gao Reviewed-by: Bob Feng --- BaseTools/Source/Python/AutoGen/AutoGen.py | 2 +- BaseTools/Source/Python/AutoGen/GenC.py | 8 ++++---- BaseTools/Source/Python/BPDG/GenVpd.py | 8 ++++---- BaseTools/Source/Python/Common/Expression.py | 9 ++++++++- BaseTools/Source/Python/Common/Misc.py | 6 +++--- BaseTools/Source/Python/GenFds/DataSection.py | 4 ++-- BaseTools/Source/Python/GenFds/EfiSection.py | 4 ++-- .../Source/Python/GenFds/FfsInfStatement.py | 8 ++++---- BaseTools/Source/Python/GenFds/Fv.py | 4 ++-- .../Source/Python/GenFds/FvImageSection.py | 10 +++++----- BaseTools/Source/Python/GenFds/GenFds.py | 2 +- BaseTools/Source/Python/GenFds/Region.py | 4 ++-- .../Python/PatchPcdValue/PatchPcdValue.py | 4 ++-- BaseTools/Source/Python/build/build.py | 18 +++++++++--------- 14 files changed, 49 insertions(+), 42 deletions(-) diff --git a/BaseTools/Source/Python/AutoGen/AutoGen.py b/BaseTools/Source/Python/AutoGen/AutoGen.py index f9ce17cf77..5f0da5a815 100644 --- a/BaseTools/Source/Python/AutoGen/AutoGen.py +++ b/BaseTools/Source/Python/AutoGen/AutoGen.py @@ -3814,7 +3814,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 915ba2e235..e46942a3e2 100644 --- a/BaseTools/Source/Python/AutoGen/GenC.py +++ b/BaseTools/Source/Python/AutoGen/GenC.py @@ -1053,7 +1053,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: @@ -1063,7 +1063,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. @@ -1906,7 +1906,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: @@ -1915,7 +1915,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 71fcfc5ac3..b4a2dd25a2 100644 --- a/BaseTools/Source/Python/BPDG/GenVpd.py +++ b/BaseTools/Source/Python/BPDG/GenVpd.py @@ -431,7 +431,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): @@ -509,7 +509,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 @@ -573,7 +573,7 @@ class GenVPD : # Not been fixed if eachUnfixedPcd.PcdOffset == TAB_STAR : 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 @@ -627,7 +627,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 19ea13b7fb..0c7e25b445 100644 --- a/BaseTools/Source/Python/Common/Expression.py +++ b/BaseTools/Source/Python/Common/Expression.py @@ -439,6 +439,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: @@ -912,7 +919,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 d23a075f43..bd7c2812e8 100644 --- a/BaseTools/Source/Python/Common/Misc.py +++ b/BaseTools/Source/Python/Common/Misc.py @@ -1025,7 +1025,7 @@ def ParseFieldValue (Value): if "{CODE(" in Value: return Value, len(Value.split(",")) 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() @@ -1146,12 +1146,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 28f9b931ca..989e33c43f 100644 --- a/BaseTools/Source/Python/GenFds/DataSection.py +++ b/BaseTools/Source/Python/GenFds/DataSection.py @@ -88,9 +88,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 0be176ec8a..dfb2470874 100644 --- a/BaseTools/Source/Python/GenFds/EfiSection.py +++ b/BaseTools/Source/Python/GenFds/EfiSection.py @@ -248,9 +248,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 4dda3cf787..a7298a6daf 100644 --- a/BaseTools/Source/Python/GenFds/FfsInfStatement.py +++ b/BaseTools/Source/Python/GenFds/FfsInfStatement.py @@ -772,9 +772,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') @@ -814,9 +814,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 bd5c259348..b141d44dc4 100644 --- a/BaseTools/Source/Python/GenFds/Fv.py +++ b/BaseTools/Source/Python/GenFds/Fv.py @@ -222,9 +222,9 @@ class FV (object): #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 7f277ddef2..535b86ab5e 100644 --- a/BaseTools/Source/Python/GenFds/FvImageSection.py +++ b/BaseTools/Source/Python/GenFds/FvImageSection.py @@ -71,7 +71,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 @@ -87,9 +87,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) @@ -129,9 +129,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 f1ce527f88..2efb2edd9a 100644 --- a/BaseTools/Source/Python/GenFds/GenFds.py +++ b/BaseTools/Source/Python/GenFds/GenFds.py @@ -751,7 +751,7 @@ class GenFds(object): 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 acc9dea413..83363276d2 100644 --- a/BaseTools/Source/Python/GenFds/Region.py +++ b/BaseTools/Source/Python/GenFds/Region.py @@ -300,7 +300,7 @@ class Region(object): 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: @@ -309,7 +309,7 @@ class Region(object): 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..0be5eba492 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"'): @@ -148,7 +148,7 @@ def PatchBinaryFile(FileName, ValueOffset, TypeName, ValueString, MaxSize=0): if Index + 2 >= ValueLength: break # - # Set string value one by one + # Set string value one by one/ 0x100 # ByteList[ValueOffset + Index] = ord(ByteString) Index = Index + 2 diff --git a/BaseTools/Source/Python/build/build.py b/BaseTools/Source/Python/build/build.py index 94074b89b4..139a1dfe29 100644 --- a/BaseTools/Source/Python/build/build.py +++ b/BaseTools/Source/Python/build/build.py @@ -664,7 +664,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 # @@ -1582,21 +1582,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 -- 2.39.2