Python compat - iterator protocol

In Python2 next() is used wile it's __next__ in Python3.

Differential Revision: https://reviews.llvm.org/D56250

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@350326 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Serge Guelton 2019-01-03 15:43:14 +00:00
parent 39c11273d1
commit 19b8509e55
2 changed files with 26 additions and 14 deletions

View File

@ -19,6 +19,8 @@ from ctypes import byref
from ctypes import c_char_p from ctypes import c_char_p
from ctypes import c_uint from ctypes import c_uint
import sys
__all__ = [ __all__ = [
"lib", "lib",
"Enums", "Enums",
@ -236,7 +238,7 @@ class Module(LLVMObject):
def __iter__(self): def __iter__(self):
return self return self
def next(self): def __next__(self):
if not isinstance(self.function, Function): if not isinstance(self.function, Function):
raise StopIteration("") raise StopIteration("")
result = self.function result = self.function
@ -246,6 +248,9 @@ class Module(LLVMObject):
self.function = self.function.next self.function = self.function.next
return result return result
if sys.version_info.major == 2:
next = __next__
def __iter__(self): def __iter__(self):
return Module.__function_iterator(self) return Module.__function_iterator(self)
@ -304,7 +309,7 @@ class Function(Value):
def __iter__(self): def __iter__(self):
return self return self
def next(self): def __next__(self):
if not isinstance(self.bb, BasicBlock): if not isinstance(self.bb, BasicBlock):
raise StopIteration("") raise StopIteration("")
result = self.bb result = self.bb
@ -314,6 +319,9 @@ class Function(Value):
self.bb = self.bb.next self.bb = self.bb.next
return result return result
if sys.version_info.major == 2:
next = __next__
def __iter__(self): def __iter__(self):
return Function.__bb_iterator(self) return Function.__bb_iterator(self)
@ -381,7 +389,7 @@ class BasicBlock(LLVMObject):
def __iter__(self): def __iter__(self):
return self return self
def next(self): def __next__(self):
if not isinstance(self.inst, Instruction): if not isinstance(self.inst, Instruction):
raise StopIteration("") raise StopIteration("")
result = self.inst result = self.inst
@ -391,6 +399,9 @@ class BasicBlock(LLVMObject):
self.inst = self.inst.next self.inst = self.inst.next
return result return result
if sys.version_info.major == 2:
next = __next__
def __iter__(self): def __iter__(self):
return BasicBlock.__inst_iterator(self) return BasicBlock.__inst_iterator(self)

View File

@ -1,4 +1,5 @@
from __future__ import print_function from __future__ import print_function
import sys
import gdb.printing import gdb.printing
@ -6,7 +7,7 @@ class Iterator:
def __iter__(self): def __iter__(self):
return self return self
# Python 2 compatibility if sys.version_info.major == 2:
def next(self): def next(self):
return self.__next__() return self.__next__()
@ -70,7 +71,7 @@ class ArrayRefPrinter:
def __iter__(self): def __iter__(self):
return self return self
def next(self): def __next__(self):
if self.cur == self.end: if self.cur == self.end:
raise StopIteration raise StopIteration
count = self.count count = self.count
@ -79,13 +80,12 @@ class ArrayRefPrinter:
self.cur = self.cur + 1 self.cur = self.cur + 1
return '[%d]' % count, cur.dereference() return '[%d]' % count, cur.dereference()
__next__ = next if sys.version_info.major == 2:
next = __next__
def __init__(self, val): def __init__(self, val):
self.val = val self.val = val
__next__ = next
def children(self): def children(self):
data = self.val['Data'] data = self.val['Data']
return self._iterator(data, data + self.val['Length']) return self._iterator(data, data + self.val['Length'])
@ -169,7 +169,7 @@ class DenseMapPrinter:
while self.cur != self.end and (is_equal(self.cur.dereference()['first'], empty) or is_equal(self.cur.dereference()['first'], tombstone)): while self.cur != self.end and (is_equal(self.cur.dereference()['first'], empty) or is_equal(self.cur.dereference()['first'], tombstone)):
self.cur = self.cur + 1 self.cur = self.cur + 1
def next(self): def __next__(self):
if self.cur == self.end: if self.cur == self.end:
raise StopIteration raise StopIteration
cur = self.cur cur = self.cur
@ -182,7 +182,8 @@ class DenseMapPrinter:
self.first = False self.first = False
return 'x', v return 'x', v
__next__ = next if sys.version_info.major == 2:
next = __next__
def __init__(self, val): def __init__(self, val):
self.val = val self.val = val