DebugClients/Python/coverage/bytecode.py

changeset 3497
7f51ab29a1a2
parent 790
2c0ea0163ef4
child 3499
f2d4b02c7e88
--- a/DebugClients/Python/coverage/bytecode.py	Fri Apr 11 18:37:22 2014 +0200
+++ b/DebugClients/Python/coverage/bytecode.py	Thu Apr 10 23:02:20 2014 +0200
@@ -1,14 +1,25 @@
 """Bytecode manipulation for coverage.py"""
 
-import opcode, sys, types
+import opcode, types
+
+from .backward import byte_to_int
 
 class ByteCode(object):
     """A single bytecode."""
     def __init__(self):
+        # The offset of this bytecode in the code object.
         self.offset = -1
+
+        # The opcode, defined in the `opcode` module.
         self.op = -1
+
+        # The argument, a small integer, whose meaning depends on the opcode.
         self.arg = -1
+
+        # The offset in the code object of the next bytecode.
         self.next_offset = -1
+
+        # The offset to jump to.
         self.jump_to = -1
 
 
@@ -18,44 +29,34 @@
     Returns `ByteCode` objects.
 
     """
+    # pylint: disable=R0924
     def __init__(self, code):
         self.code = code
-        self.offset = 0
 
-    if sys.hexversion > 0x03000000:
-        def __getitem__(self, i):
-            return self.code[i]
-    else:
-        def __getitem__(self, i):
-            return ord(self.code[i])
+    def __getitem__(self, i):
+        return byte_to_int(self.code[i])
 
     def __iter__(self):
-        return self
-
-    def __next__(self):
-        if self.offset >= len(self.code):
-            raise StopIteration
-
-        bc = ByteCode()
-        bc.op = self[self.offset]
-        bc.offset = self.offset
+        offset = 0
+        while offset < len(self.code):
+            bc = ByteCode()
+            bc.op = self[offset]
+            bc.offset = offset
 
-        next_offset = self.offset+1
-        if bc.op >= opcode.HAVE_ARGUMENT:
-            bc.arg = self[self.offset+1] + 256*self[self.offset+2]
-            next_offset += 2
+            next_offset = offset+1
+            if bc.op >= opcode.HAVE_ARGUMENT:
+                bc.arg = self[offset+1] + 256*self[offset+2]
+                next_offset += 2
 
-            label = -1
-            if bc.op in opcode.hasjrel:
-                label = next_offset + bc.arg
-            elif bc.op in opcode.hasjabs:
-                label = bc.arg
-            bc.jump_to = label
+                label = -1
+                if bc.op in opcode.hasjrel:
+                    label = next_offset + bc.arg
+                elif bc.op in opcode.hasjabs:
+                    label = bc.arg
+                bc.jump_to = label
 
-        bc.next_offset = self.offset = next_offset
-        return bc
-
-    next = __next__     # Py2k uses an old-style non-dunder name.
+            bc.next_offset = offset = next_offset
+            yield bc
 
 
 class CodeObjects(object):
@@ -64,21 +65,11 @@
         self.stack = [code]
 
     def __iter__(self):
-        return self
-
-    def __next__(self):
-        if self.stack:
+        while self.stack:
             # We're going to return the code object on the stack, but first
             # push its children for later returning.
             code = self.stack.pop()
             for c in code.co_consts:
                 if isinstance(c, types.CodeType):
                     self.stack.append(c)
-            return code
-
-        raise StopIteration
-
-    next = __next__
-
-#
-# eflag: FileType = Python2
+            yield code

eric ide

mercurial