Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Unified Diff: third_party/pycoverage/coverage/bytecode.py

Issue 727003004: Add python coverage module to third_party (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/pycoverage/coverage/backward.py ('k') | third_party/pycoverage/coverage/cmdline.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/pycoverage/coverage/bytecode.py
diff --git a/third_party/pycoverage/coverage/bytecode.py b/third_party/pycoverage/coverage/bytecode.py
new file mode 100644
index 0000000000000000000000000000000000000000..85360638528e6bece33b1f5a1e4dd08e5c2ac3d0
--- /dev/null
+++ b/third_party/pycoverage/coverage/bytecode.py
@@ -0,0 +1,75 @@
+"""Bytecode manipulation for coverage.py"""
+
+import opcode, types
+
+from coverage.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
+
+
+class ByteCodes(object):
+ """Iterator over byte codes in `code`.
+
+ Returns `ByteCode` objects.
+
+ """
+ # pylint: disable=R0924
+ def __init__(self, code):
+ self.code = code
+
+ def __getitem__(self, i):
+ return byte_to_int(self.code[i])
+
+ def __iter__(self):
+ offset = 0
+ while offset < len(self.code):
+ bc = ByteCode()
+ bc.op = self[offset]
+ bc.offset = offset
+
+ 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
+
+ bc.next_offset = offset = next_offset
+ yield bc
+
+
+class CodeObjects(object):
+ """Iterate over all the code objects in `code`."""
+ def __init__(self, code):
+ self.stack = [code]
+
+ def __iter__(self):
+ 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)
+ yield code
« no previous file with comments | « third_party/pycoverage/coverage/backward.py ('k') | third_party/pycoverage/coverage/cmdline.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698