OLD | NEW |
(Empty) | |
| 1 import dis |
| 2 import array |
| 3 import collections |
| 4 |
| 5 import six |
| 6 |
| 7 |
| 8 OpArg = collections.namedtuple('OpArg', 'opcode arg') |
| 9 |
| 10 |
| 11 class Bytecode_compat(object): |
| 12 def __init__(self, code): |
| 13 self.code = code |
| 14 |
| 15 def __iter__(self): |
| 16 """Yield '(op,arg)' pair for each operation in code object 'code'""" |
| 17 |
| 18 bytes = array.array('b', self.code.co_code) |
| 19 eof = len(self.code.co_code) |
| 20 |
| 21 ptr = 0 |
| 22 extended_arg = 0 |
| 23 |
| 24 while ptr < eof: |
| 25 |
| 26 op = bytes[ptr] |
| 27 |
| 28 if op >= dis.HAVE_ARGUMENT: |
| 29 |
| 30 arg = bytes[ptr + 1] + bytes[ptr + 2] * 256 + extended_arg |
| 31 ptr += 3 |
| 32 |
| 33 if op == dis.EXTENDED_ARG: |
| 34 long_type = six.integer_types[-1] |
| 35 extended_arg = arg * long_type(65536) |
| 36 continue |
| 37 |
| 38 else: |
| 39 arg = None |
| 40 ptr += 1 |
| 41 |
| 42 yield OpArg(op, arg) |
| 43 |
| 44 |
| 45 Bytecode = getattr(dis, 'Bytecode', Bytecode_compat) |
OLD | NEW |