[llvm] r350326 - Python compat - iterator protocol

Serge Guelton via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 3 07:43:15 PST 2019


Author: serge_sans_paille
Date: Thu Jan  3 07:43:14 2019
New Revision: 350326

URL: http://llvm.org/viewvc/llvm-project?rev=350326&view=rev
Log:
Python compat - iterator protocol

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

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

Modified:
    llvm/trunk/bindings/python/llvm/core.py
    llvm/trunk/utils/gdb-scripts/prettyprinters.py

Modified: llvm/trunk/bindings/python/llvm/core.py
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/bindings/python/llvm/core.py?rev=350326&r1=350325&r2=350326&view=diff
==============================================================================
--- llvm/trunk/bindings/python/llvm/core.py (original)
+++ llvm/trunk/bindings/python/llvm/core.py Thu Jan  3 07:43:14 2019
@@ -19,6 +19,8 @@ from ctypes import byref
 from ctypes import c_char_p
 from ctypes import c_uint
 
+import sys
+
 __all__ = [
     "lib",
     "Enums",
@@ -236,7 +238,7 @@ class Module(LLVMObject):
         def __iter__(self):
             return self
         
-        def next(self):
+        def __next__(self):
             if not isinstance(self.function, Function):
                 raise StopIteration("")
             result = self.function
@@ -245,7 +247,10 @@ class Module(LLVMObject):
             else:
                 self.function = self.function.next
             return result
-    
+
+        if sys.version_info.major == 2:
+            next = __next__
+
     def __iter__(self):
         return Module.__function_iterator(self)
 
@@ -304,7 +309,7 @@ class Function(Value):
         def __iter__(self):
             return self
         
-        def next(self):
+        def __next__(self):
             if not isinstance(self.bb, BasicBlock):
                 raise StopIteration("")
             result = self.bb
@@ -313,6 +318,9 @@ class Function(Value):
             else:
                 self.bb = self.bb.next
             return result
+
+        if sys.version_info.major == 2:
+            next = __next__
     
     def __iter__(self):
         return Function.__bb_iterator(self)
@@ -381,7 +389,7 @@ class BasicBlock(LLVMObject):
         def __iter__(self):
             return self
         
-        def next(self):
+        def __next__(self):
             if not isinstance(self.inst, Instruction):
                 raise StopIteration("")
             result = self.inst
@@ -390,7 +398,10 @@ class BasicBlock(LLVMObject):
             else:
                 self.inst = self.inst.next
             return result
-    
+
+        if sys.version_info.major == 2:
+            next = __next__
+
     def __iter__(self):
         return BasicBlock.__inst_iterator(self)
 

Modified: llvm/trunk/utils/gdb-scripts/prettyprinters.py
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/gdb-scripts/prettyprinters.py?rev=350326&r1=350325&r2=350326&view=diff
==============================================================================
--- llvm/trunk/utils/gdb-scripts/prettyprinters.py (original)
+++ llvm/trunk/utils/gdb-scripts/prettyprinters.py Thu Jan  3 07:43:14 2019
@@ -1,4 +1,5 @@
 from __future__ import print_function
+import sys
 
 import gdb.printing
 
@@ -6,9 +7,9 @@ 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
@@ -70,7 +71,7 @@ class ArrayRefPrinter:
     def __iter__(self):
       return self
 
-    def next(self):
+    def __next__(self):
       if self.cur == self.end:
         raise StopIteration
       count = self.count
@@ -79,13 +80,12 @@ class ArrayRefPrinter:
       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'])
@@ -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)):
         self.cur = self.cur + 1
 
-    def next(self):
+    def __next__(self):
       if self.cur == self.end:
         raise StopIteration
       cur = self.cur
@@ -182,7 +182,8 @@ class DenseMapPrinter:
         self.first = False
       return 'x', v
 
-    __next__ = next
+    if sys.version_info.major == 2:
+        next = __next__
 
   def __init__(self, val):
     self.val = val




More information about the llvm-commits mailing list