BaseTools: Fix old python2 idioms

Based on "futurize -f lib2to3.fixes.fix_idioms"

* Change some type comparisons to isinstance() calls:
    type(x) == T -> isinstance(x, T)
    type(x) is T -> isinstance(x, T)
    type(x) != T -> not isinstance(x, T)
    type(x) is not T -> not isinstance(x, T)

* Change "while 1:" into "while True:".

* Change both

    v = list(EXPR)
    v.sort()
    foo(v)

and the more general

    v = EXPR
    v.sort()
    foo(v)

into

    v = sorted(EXPR)
    foo(v)

Contributed-under: TianoCore Contribution Agreement 1.1
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Signed-off-by: Gary Lin <glin@suse.com>
Reviewed-by: Yonghong Zhu <yonghong.zhu@intel.com>
This commit is contained in:
Gary Lin
2018-06-25 18:31:35 +08:00
committed by Yonghong Zhu
parent 2617a73c36
commit 0d1f5b2b5d
31 changed files with 93 additions and 111 deletions

View File

@ -123,8 +123,7 @@ def GenPcd(Package, Content):
if Pcd.GetSupModuleList():
Statement += GenDecTailComment(Pcd.GetSupModuleList())
ArchList = Pcd.GetSupArchList()
ArchList.sort()
ArchList = sorted(Pcd.GetSupArchList())
SortedArch = ' '.join(ArchList)
if SortedArch in NewSectionDict:
NewSectionDict[SortedArch] = \
@ -205,8 +204,7 @@ def GenGuidProtocolPpi(Package, Content):
#
if Guid.GetSupModuleList():
Statement += GenDecTailComment(Guid.GetSupModuleList())
ArchList = Guid.GetSupArchList()
ArchList.sort()
ArchList = sorted(Guid.GetSupArchList())
SortedArch = ' '.join(ArchList)
if SortedArch in NewSectionDict:
NewSectionDict[SortedArch] = \
@ -246,8 +244,7 @@ def GenGuidProtocolPpi(Package, Content):
#
if Protocol.GetSupModuleList():
Statement += GenDecTailComment(Protocol.GetSupModuleList())
ArchList = Protocol.GetSupArchList()
ArchList.sort()
ArchList = sorted(Protocol.GetSupArchList())
SortedArch = ' '.join(ArchList)
if SortedArch in NewSectionDict:
NewSectionDict[SortedArch] = \
@ -287,8 +284,7 @@ def GenGuidProtocolPpi(Package, Content):
#
if Ppi.GetSupModuleList():
Statement += GenDecTailComment(Ppi.GetSupModuleList())
ArchList = Ppi.GetSupArchList()
ArchList.sort()
ArchList = sorted(Ppi.GetSupArchList())
SortedArch = ' '.join(ArchList)
if SortedArch in NewSectionDict:
NewSectionDict[SortedArch] = \
@ -463,8 +459,7 @@ def PackageToDec(Package, DistHeader = None):
if LibraryClass.GetSupModuleList():
Statement += \
GenDecTailComment(LibraryClass.GetSupModuleList())
ArchList = LibraryClass.GetSupArchList()
ArchList.sort()
ArchList = sorted(LibraryClass.GetSupArchList())
SortedArch = ' '.join(ArchList)
if SortedArch in NewSectionDict:
NewSectionDict[SortedArch] = \

View File

@ -493,8 +493,7 @@ def GenPackages(ModuleObject):
Statement += RelaPath.replace('\\', '/')
if FFE:
Statement += '|' + FFE
ArchList = PackageDependency.GetSupArchList()
ArchList.sort()
ArchList = sorted(PackageDependency.GetSupArchList())
SortedArch = ' '.join(ArchList)
if SortedArch in NewSectionDict:
NewSectionDict[SortedArch] = NewSectionDict[SortedArch] + [Statement]
@ -513,8 +512,7 @@ def GenSources(ModuleObject):
SourceFile = Source.GetSourceFile()
Family = Source.GetFamily()
FeatureFlag = Source.GetFeatureFlag()
SupArchList = Source.GetSupArchList()
SupArchList.sort()
SupArchList = sorted(Source.GetSupArchList())
SortedArch = ' '.join(SupArchList)
Statement = GenSourceStatement(ConvertPath(SourceFile), Family, FeatureFlag)
if SortedArch in NewSectionDict:
@ -722,8 +720,7 @@ def GenGuidSections(GuidObjList):
#
# merge duplicate items
#
ArchList = Guid.GetSupArchList()
ArchList.sort()
ArchList = sorted(Guid.GetSupArchList())
SortedArch = ' '.join(ArchList)
if (Statement, SortedArch) in GuidDict:
PreviousComment = GuidDict[Statement, SortedArch]
@ -782,8 +779,7 @@ def GenProtocolPPiSections(ObjList, IsProtocol):
#
# merge duplicate items
#
ArchList = Object.GetSupArchList()
ArchList.sort()
ArchList = sorted(Object.GetSupArchList())
SortedArch = ' '.join(ArchList)
if (Statement, SortedArch) in Dict:
PreviousComment = Dict[Statement, SortedArch]
@ -857,8 +853,7 @@ def GenPcdSections(ModuleObject):
#
# Merge duplicate entries
#
ArchList = Pcd.GetSupArchList()
ArchList.sort()
ArchList = sorted(Pcd.GetSupArchList())
SortedArch = ' '.join(ArchList)
if (Statement, SortedArch) in Dict:
PreviousComment = Dict[Statement, SortedArch]
@ -1025,8 +1020,7 @@ def GenSpecialSections(ObjectList, SectionName, UserExtensionsContent=''):
if CommentStr and not CommentStr.endswith('\n#\n'):
CommentStr = CommentStr + '#\n'
NewStateMent = CommentStr + Statement
SupArch = Obj.GetSupArchList()
SupArch.sort()
SupArch = sorted(Obj.GetSupArchList())
SortedArch = ' '.join(SupArch)
if SortedArch in NewSectionDict:
NewSectionDict[SortedArch] = NewSectionDict[SortedArch] + [NewStateMent]
@ -1104,8 +1098,7 @@ def GenBinaries(ModuleObject):
FileName = ConvertPath(FileNameObj.GetFilename())
FileType = FileNameObj.GetFileType()
FFE = FileNameObj.GetFeatureFlag()
ArchList = FileNameObj.GetSupArchList()
ArchList.sort()
ArchList = sorted(FileNameObj.GetSupArchList())
SortedArch = ' '.join(ArchList)
Key = (FileName, FileType, FFE, SortedArch)
if Key in BinariesDict:

View File

@ -514,7 +514,7 @@ class PathClass(object):
# Check whether PathClass are the same
#
def __eq__(self, Other):
if type(Other) == type(self):
if isinstance(Other, type(self)):
return self.Path == Other.Path
else:
return self.Path == str(Other)
@ -819,11 +819,11 @@ def ConvertArchList(ArchList):
if not ArchList:
return NewArchList
if type(ArchList) == list:
if isinstance(ArchList, list):
for Arch in ArchList:
Arch = Arch.upper()
NewArchList.append(Arch)
elif type(ArchList) == str:
elif isinstance(ArchList, str):
ArchList = ArchList.upper()
NewArchList.append(ArchList)

View File

@ -341,7 +341,7 @@ def IsValidCFormatGuid(Guid):
#
# Index may out of bound
#
if type(List[Index]) != type(1) or \
if not isinstance(List[Index], type(1)) or \
len(Value) > List[Index] or len(Value) < 3:
return False

View File

@ -651,7 +651,7 @@ def ConvertToSqlString2(String):
# @param Split: split character
#
def GetStringOfList(List, Split=' '):
if type(List) != type([]):
if not isinstance(List, type([])):
return List
Str = ''
for Item in List:

View File

@ -40,7 +40,7 @@ def CreateXmlElement(Name, String, NodeList, AttributeList):
Element.appendChild(Doc.createTextNode(String))
for Item in NodeList:
if type(Item) == type([]):
if isinstance(Item, type([])):
Key = Item[0]
Value = Item[1]
if Key != '' and Key is not None and Value != '' and Value is not None:

View File

@ -409,8 +409,7 @@ class DecPomAlignment(PackageObject):
#
PackagePath = os.path.split(self.GetFullPath())[0]
IncludePathList = \
[os.path.normpath(Path) + sep for Path in IncludesDict.keys()]
IncludePathList.sort()
sorted([os.path.normpath(Path) + sep for Path in IncludesDict.keys()])
#
# get a non-overlap set of include path, IncludePathList should be

View File

@ -611,8 +611,7 @@ class InfPomAlignment(ModuleObject):
SourceFile = Item.GetSourceFileName()
Family = Item.GetFamily()
FeatureFlag = Item.GetFeatureFlagExp()
SupArchList = ConvertArchList(Item.GetSupArchList())
SupArchList.sort()
SupArchList = sorted(ConvertArchList(Item.GetSupArchList()))
Source = SourceFileObject()
Source.SetSourceFile(SourceFile)
Source.SetFamily(Family)

View File

@ -194,8 +194,7 @@ def GenBinaryData(BinaryData, BinaryObj, BinariesDict, AsBuildIns, BinaryFileObj
# can be used for the attribute.
# If both not have VALID_ARCHITECTURE comment and no architecturie specified, then keep it empty.
#
SupArchList = ConvertArchList(ItemObj.GetSupArchList())
SupArchList.sort()
SupArchList = sorted(ConvertArchList(ItemObj.GetSupArchList()))
if len(SupArchList) == 1 and SupArchList[0] == 'COMMON':
if not (len(OriSupArchList) == 1 or OriSupArchList[0] == 'COMMON'):
SupArchList = OriSupArchList