Sha256: c2cc9186ad79646005278ce4bfaa005b12dc9451bae576eca491e36620db2234

Contents?: true

Size: 1.99 KB

Versions: 53

Compression:

Stored size: 1.99 KB

Contents

"""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

Version data entries

53 entries across 53 versions & 1 rubygems

Version Path
libv8-8.4.255.0.1 vendor/depot_tools/third_party/coverage/bytecode.py
libv8-8.4.255.0 vendor/depot_tools/third_party/coverage/bytecode.py
libv8-7.8.279.23.0beta1 vendor/depot_tools/third_party/coverage/bytecode.py
libv8-7.4.288.28.0beta1 vendor/depot_tools/third_party/coverage/bytecode.py
libv8-7.3.492.27.3beta1 vendor/depot_tools/third_party/coverage/bytecode.py
libv8-7.3.492.27.1 vendor/depot_tools/third_party/coverage/bytecode.py
libv8-7.3.492.27.0 vendor/depot_tools/third_party/coverage/bytecode.py
libv8-7.3.492.27.0beta1 vendor/depot_tools/third_party/coverage/bytecode.py
libv8-6.7.288.46.1 vendor/depot_tools/third_party/coverage/bytecode.py
libv8-6.7.288.46.0 vendor/depot_tools/third_party/coverage/bytecode.py
libv8-6.7.288.46.1beta0 vendor/depot_tools/third_party/coverage/bytecode.py
libv8-6.3.292.48.1 vendor/depot_tools/third_party/coverage/bytecode.py
libv8-6.3.292.48.0 vendor/depot_tools/third_party/coverage/bytecode.py
libv8-6.3.292.48.0beta2 vendor/depot_tools/third_party/coverage/bytecode.py
libv8-6.3.292.48.0beta1 vendor/depot_tools/third_party/coverage/bytecode.py
libv8-6.2.414.42.1 vendor/depot_tools/third_party/coverage/bytecode.py
libv8-6.2.414.42.0 vendor/depot_tools/third_party/coverage/bytecode.py
libv8-6.2.414.42.0beta1 vendor/depot_tools/third_party/coverage/bytecode.py
libv8-6.0.286.54.3 vendor/depot_tools/third_party/coverage/bytecode.py
libv8-6.0.286.54.2 vendor/depot_tools/third_party/coverage/bytecode.py