Esempio n. 1
0
    def _bytes_lines(self):
        """Map byte offsets to line numbers in `code`.

        Uses co_lnotab described in Python/compile.c to map byte offsets to
        line numbers.  Produces a sequence: (b0, l0), (b1, l1), ...

        Only byte offsets that correspond to line numbers are included in the
        results.

        """
        # Adapted from dis.py in the standard library.
        byte_increments = bytes_to_ints(self.code.co_lnotab[0::2])
        line_increments = bytes_to_ints(self.code.co_lnotab[1::2])

        last_line_num = None
        line_num = self.code.co_firstlineno
        byte_num = 0
        for byte_incr, line_incr in zip(byte_increments, line_increments):
            if byte_incr:
                if line_num != last_line_num:
                    yield (byte_num, line_num)
                    last_line_num = line_num
                byte_num += byte_incr
            line_num += line_incr
        if line_num != last_line_num:
            yield (byte_num, line_num)
Esempio n. 2
0
    def _line_numbers(self):
        """Yield the line numbers possible in this code object.

        Uses co_lnotab described in Python/compile.c to find the
        line numbers.  Produces a sequence: l0, l1, ...
        """
        if hasattr(self.code, "co_lines"):
            for _, _, line in self.code.co_lines():
                if line is not None:
                    yield line
        else:
            # Adapted from dis.py in the standard library.
            byte_increments = bytes_to_ints(self.code.co_lnotab[0::2])
            line_increments = bytes_to_ints(self.code.co_lnotab[1::2])

            last_line_num = None
            line_num = self.code.co_firstlineno
            byte_num = 0
            for byte_incr, line_incr in zip(byte_increments, line_increments):
                if byte_incr:
                    if line_num != last_line_num:
                        yield line_num
                        last_line_num = line_num
                    byte_num += byte_incr
                if env.PYBEHAVIOR.negative_lnotab and line_incr >= 0x80:
                    line_incr -= 0x100
                line_num += line_incr
            if line_num != last_line_num:
                yield line_num
Esempio n. 3
0
    def _bytes_lines(self):
        """Map byte offsets to line numbers in `code`.

        Uses co_lnotab described in Python/compile.c to map byte offsets to
        line numbers.  Produces a sequence: (b0, l0), (b1, l1), ...

        Only byte offsets that correspond to line numbers are included in the
        results.

        """
        # Adapted from dis.py in the standard library.
        byte_increments = bytes_to_ints(self.code.co_lnotab[0::2])
        line_increments = bytes_to_ints(self.code.co_lnotab[1::2])

        last_line_num = None
        line_num = self.code.co_firstlineno
        byte_num = 0
        for byte_incr, line_incr in zip(byte_increments, line_increments):
            if byte_incr:
                if line_num != last_line_num:
                    yield (byte_num, line_num)
                    last_line_num = line_num
                byte_num += byte_incr
            line_num += line_incr
        if line_num != last_line_num:
            yield (byte_num, line_num)
Esempio n. 4
0
def numbits_union(numbits1, numbits2):
    """Compute the union of two numbits.

    Returns:
        A new numbits, the union of `numbits1` and `numbits2`.
    """
    byte_pairs = zip_longest(bytes_to_ints(numbits1), bytes_to_ints(numbits2), fillvalue=0)
    return _to_blob(binary_bytes(b1 | b2 for b1, b2 in byte_pairs))
Esempio n. 5
0
def numbits_intersection(numbits1, numbits2):
    """Compute the intersection of two numbits.

    Returns:
        A new numbits, the intersection `numbits1` and `numbits2`.
    """
    byte_pairs = zip_longest(bytes_to_ints(numbits1), bytes_to_ints(numbits2), fillvalue=0)
    intersection_bytes = binary_bytes(b1 & b2 for b1, b2 in byte_pairs)
    return _to_blob(intersection_bytes.rstrip(b'\0'))
Esempio n. 6
0
def numbits_any_intersection(numbits1, numbits2):
    """Is there any number that appears in both numbits?

    Determine whether two number sets have a non-empty intersection. This is
    faster than computing the intersection.

    Returns:
        A bool, True if there is any number in both `numbits1` and `numbits2`.
    """
    byte_pairs = zip_longest(bytes_to_ints(numbits1), bytes_to_ints(numbits2), fillvalue=0)
    return any(b1 & b2 for b1, b2 in byte_pairs)
Esempio n. 7
0
def numbits_union(numbits1, numbits2):
    """Compute the union of two numbits.

    Arguments:
        numbits1, numbits2: packed number sets.

    Returns:
        A new numbits, the union of the two number sets.
    """
    byte_pairs = zip_longest(bytes_to_ints(numbits1), bytes_to_ints(numbits2), fillvalue=0)
    return _to_blob(binary_bytes(b1 | b2 for b1, b2 in byte_pairs))
Esempio n. 8
0
    def _bytes_lines(self):
        byte_increments = bytes_to_ints(self.code.co_lnotab[0::2])
        line_increments = bytes_to_ints(self.code.co_lnotab[1::2])
        last_line_num = None
        line_num = self.code.co_firstlineno
        byte_num = 0
        for byte_incr, line_incr in zip(byte_increments, line_increments):
            if byte_incr:
                if line_num != last_line_num:
                    yield (byte_num, line_num)
                    last_line_num = line_num
                byte_num += byte_incr
            line_num += line_incr

        if line_num != last_line_num:
            yield (byte_num, line_num)
Esempio n. 9
0
    def _bytes_lines(self):
        byte_increments = bytes_to_ints(self.code.co_lnotab[0::2])
        line_increments = bytes_to_ints(self.code.co_lnotab[1::2])
        last_line_num = None
        line_num = self.code.co_firstlineno
        byte_num = 0
        for byte_incr, line_incr in zip(byte_increments, line_increments):
            if byte_incr:
                if line_num != last_line_num:
                    yield (byte_num, line_num)
                    last_line_num = line_num
                byte_num += byte_incr
            line_num += line_incr

        if line_num != last_line_num:
            yield (byte_num, line_num)
Esempio n. 10
0
def bitmap_to_nums(bitmap):
    """Convert a bitmap into a list of numbers."""
    nums = []
    for byte_i, byte in enumerate(bytes_to_ints(bitmap)):
        for bit_i in range(8):
            if (byte & (1 << bit_i)):
                nums.append(byte_i * 8 + bit_i)
    return nums
Esempio n. 11
0
def numbits_to_nums(numbits):
    """Convert a numbits into a list of numbers.

    Arguments:
        numbits: a binary blob, the packed number set.

    Returns:
        A list of ints.
    """
    nums = []
    for byte_i, byte in enumerate(bytes_to_ints(numbits)):
        for bit_i in range(8):
            if (byte & (1 << bit_i)):
                nums.append(byte_i * 8 + bit_i)
    return nums
Esempio n. 12
0
def numbits_to_nums(numbits):
    """Convert a numbits into a list of numbers.

    Arguments:
        numbits: a binary blob, the packed number set.

    Returns:
        A list of ints.

    When registered as a SQLite function by :func:`register_sqlite_functions`,
    this returns a string, a JSON-encoded list of ints.

    """
    nums = []
    for byte_i, byte in enumerate(bytes_to_ints(numbits)):
        for bit_i in range(8):
            if (byte & (1 << bit_i)):
                nums.append(byte_i * 8 + bit_i)
    return nums
Esempio n. 13
0
 def test_binary_bytes(self):
     byte_values = [0, 255, 17, 23, 42, 57]
     bb = binary_bytes(byte_values)
     self.assertEqual(len(bb), len(byte_values))
     self.assertEqual(byte_values, list(bytes_to_ints(bb)))
Esempio n. 14
0
 def test_binary_bytes(self):
     byte_values = [0, 255, 17, 23, 42, 57]
     bb = binary_bytes(byte_values)
     self.assertEqual(len(bb), len(byte_values))
     self.assertEqual(byte_values, list(bytes_to_ints(bb)))
Esempio n. 15
0
 def test_binary_bytes(self):
     byte_values = [0, 255, 17, 23, 42, 57]
     bb = binary_bytes(byte_values)
     assert len(bb) == len(byte_values)
     assert byte_values == list(bytes_to_ints(bb))
Esempio n. 16
0
def merge_bitmaps(map1, map2):
    """Merge two bitmaps"""
    byte_pairs = zip_longest(bytes_to_ints(map1), bytes_to_ints(map2), fillvalue=0)
    return binary_bytes(b1 | b2 for b1, b2 in byte_pairs)