Skip to content

Commit dd84c9d

Browse files
committedJan 3, 2019
Python compat - iterator protocol
In Python2 next() is used wile it's __next__ in Python3. Differential Revision: https://reviews.llvm.org/D56250 llvm-svn: 350326
1 parent 9ff5001 commit dd84c9d

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed
 

‎llvm/bindings/python/llvm/core.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from ctypes import c_char_p
2020
from ctypes import c_uint
2121

22+
import sys
23+
2224
__all__ = [
2325
"lib",
2426
"Enums",
@@ -236,7 +238,7 @@ def __init__(self, module, reverse=False):
236238
def __iter__(self):
237239
return self
238240

239-
def next(self):
241+
def __next__(self):
240242
if not isinstance(self.function, Function):
241243
raise StopIteration("")
242244
result = self.function
@@ -245,7 +247,10 @@ def next(self):
245247
else:
246248
self.function = self.function.next
247249
return result
248-
250+
251+
if sys.version_info.major == 2:
252+
next = __next__
253+
249254
def __iter__(self):
250255
return Module.__function_iterator(self)
251256

@@ -304,7 +309,7 @@ def __init__(self, function, reverse=False):
304309
def __iter__(self):
305310
return self
306311

307-
def next(self):
312+
def __next__(self):
308313
if not isinstance(self.bb, BasicBlock):
309314
raise StopIteration("")
310315
result = self.bb
@@ -313,6 +318,9 @@ def next(self):
313318
else:
314319
self.bb = self.bb.next
315320
return result
321+
322+
if sys.version_info.major == 2:
323+
next = __next__
316324

317325
def __iter__(self):
318326
return Function.__bb_iterator(self)
@@ -381,7 +389,7 @@ def __init__(self, bb, reverse=False):
381389
def __iter__(self):
382390
return self
383391

384-
def next(self):
392+
def __next__(self):
385393
if not isinstance(self.inst, Instruction):
386394
raise StopIteration("")
387395
result = self.inst
@@ -390,7 +398,10 @@ def next(self):
390398
else:
391399
self.inst = self.inst.next
392400
return result
393-
401+
402+
if sys.version_info.major == 2:
403+
next = __next__
404+
394405
def __iter__(self):
395406
return BasicBlock.__inst_iterator(self)
396407

‎llvm/utils/gdb-scripts/prettyprinters.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
from __future__ import print_function
2+
import sys
23

34
import gdb.printing
45

56
class Iterator:
67
def __iter__(self):
78
return self
89

9-
# Python 2 compatibility
10-
def next(self):
11-
return self.__next__()
10+
if sys.version_info.major == 2:
11+
def next(self):
12+
return self.__next__()
1213

1314
def children(self):
1415
return self
@@ -70,7 +71,7 @@ def __init__(self, begin, end):
7071
def __iter__(self):
7172
return self
7273

73-
def next(self):
74+
def __next__(self):
7475
if self.cur == self.end:
7576
raise StopIteration
7677
count = self.count
@@ -79,13 +80,12 @@ def next(self):
7980
self.cur = self.cur + 1
8081
return '[%d]' % count, cur.dereference()
8182

82-
__next__ = next
83+
if sys.version_info.major == 2:
84+
next = __next__
8385

8486
def __init__(self, val):
8587
self.val = val
8688

87-
__next__ = next
88-
8989
def children(self):
9090
data = self.val['Data']
9191
return self._iterator(data, data + self.val['Length'])
@@ -169,7 +169,7 @@ def advancePastEmptyBuckets(self):
169169
while self.cur != self.end and (is_equal(self.cur.dereference()['first'], empty) or is_equal(self.cur.dereference()['first'], tombstone)):
170170
self.cur = self.cur + 1
171171

172-
def next(self):
172+
def __next__(self):
173173
if self.cur == self.end:
174174
raise StopIteration
175175
cur = self.cur
@@ -182,7 +182,8 @@ def next(self):
182182
self.first = False
183183
return 'x', v
184184

185-
__next__ = next
185+
if sys.version_info.major == 2:
186+
next = __next__
186187

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

0 commit comments

Comments
 (0)
Please sign in to comment.