Skip to content

Commit

Permalink
Python compat - iterator protocol
Browse files Browse the repository at this point in the history
In Python2 next() is used wile it's __next__ in Python3.

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

llvm-svn: 350326
  • Loading branch information
serge-sans-paille-qb committed Jan 3, 2019
1 parent 9ff5001 commit dd84c9d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
21 changes: 16 additions & 5 deletions llvm/bindings/python/llvm/core.py
Expand Up @@ -19,6 +19,8 @@
from ctypes import c_char_p
from ctypes import c_uint

import sys

__all__ = [
"lib",
"Enums",
Expand Down Expand Up @@ -236,7 +238,7 @@ def __init__(self, module, reverse=False):
def __iter__(self):
return self

def next(self):
def __next__(self):
if not isinstance(self.function, Function):
raise StopIteration("")
result = self.function
Expand All @@ -245,7 +247,10 @@ def next(self):
else:
self.function = self.function.next
return result


if sys.version_info.major == 2:
next = __next__

def __iter__(self):
return Module.__function_iterator(self)

Expand Down Expand Up @@ -304,7 +309,7 @@ def __init__(self, function, reverse=False):
def __iter__(self):
return self

def next(self):
def __next__(self):
if not isinstance(self.bb, BasicBlock):
raise StopIteration("")
result = self.bb
Expand All @@ -313,6 +318,9 @@ def next(self):
else:
self.bb = self.bb.next
return result

if sys.version_info.major == 2:
next = __next__

def __iter__(self):
return Function.__bb_iterator(self)
Expand Down Expand Up @@ -381,7 +389,7 @@ def __init__(self, bb, reverse=False):
def __iter__(self):
return self

def next(self):
def __next__(self):
if not isinstance(self.inst, Instruction):
raise StopIteration("")
result = self.inst
Expand All @@ -390,7 +398,10 @@ def next(self):
else:
self.inst = self.inst.next
return result


if sys.version_info.major == 2:
next = __next__

def __iter__(self):
return BasicBlock.__inst_iterator(self)

Expand Down
19 changes: 10 additions & 9 deletions llvm/utils/gdb-scripts/prettyprinters.py
@@ -1,14 +1,15 @@
from __future__ import print_function
import sys

import gdb.printing

class Iterator:
def __iter__(self):
return self

# Python 2 compatibility
def next(self):
return self.__next__()
if sys.version_info.major == 2:
def next(self):
return self.__next__()

def children(self):
return self
Expand Down Expand Up @@ -70,7 +71,7 @@ def __init__(self, begin, end):
def __iter__(self):
return self

def next(self):
def __next__(self):
if self.cur == self.end:
raise StopIteration
count = self.count
Expand All @@ -79,13 +80,12 @@ def next(self):
self.cur = self.cur + 1
return '[%d]' % count, cur.dereference()

__next__ = next
if sys.version_info.major == 2:
next = __next__

def __init__(self, val):
self.val = val

__next__ = next

def children(self):
data = self.val['Data']
return self._iterator(data, data + self.val['Length'])
Expand Down Expand Up @@ -169,7 +169,7 @@ def advancePastEmptyBuckets(self):
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

def next(self):
def __next__(self):
if self.cur == self.end:
raise StopIteration
cur = self.cur
Expand All @@ -182,7 +182,8 @@ def next(self):
self.first = False
return 'x', v

__next__ = next
if sys.version_info.major == 2:
next = __next__

def __init__(self, val):
self.val = val
Expand Down

0 comments on commit dd84c9d

Please sign in to comment.