diff options
author | Jason R. Coombs <jaraco@jaraco.com> | 2016-12-28 20:52:09 -0500 |
---|---|---|
committer | Jason R. Coombs <jaraco@jaraco.com> | 2016-12-28 20:52:09 -0500 |
commit | 3000d975dbb11430be5c79498a461d33a5522a40 (patch) | |
tree | 149739220bea14bd39f395c08795939053aba85f /setuptools/py33compat.py | |
parent | a01cf3c234877ee6d2ff4cc0b1098cc05b53d6d1 (diff) | |
download | python-setuptools-git-3000d975dbb11430be5c79498a461d33a5522a40.tar.gz |
Re-introduce _iter_code functionality as a Bytecode backport. Fixes failing tests. Ref #866.
Diffstat (limited to 'setuptools/py33compat.py')
-rw-r--r-- | setuptools/py33compat.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/setuptools/py33compat.py b/setuptools/py33compat.py new file mode 100644 index 00000000..2588d680 --- /dev/null +++ b/setuptools/py33compat.py @@ -0,0 +1,46 @@ +import dis +import code +import array +import collections + +from setuptools.extern import six + + +OpArg = collections.namedtuple('OpArg', 'opcode arg') + + +class Bytecode_compat(object): + def __init__(self, code): + self.code = code + + def __iter__(self): + """Yield '(op,arg)' pair for each operation in code object 'code'""" + + bytes = array.array('b', self.code.co_code) + eof = len(self.code.co_code) + + ptr = 0 + extended_arg = 0 + + while ptr < eof: + + op = bytes[ptr] + + if op >= dis.HAVE_ARGUMENT: + + arg = bytes[ptr + 1] + bytes[ptr + 2] * 256 + extended_arg + ptr += 3 + + if op == dis.EXTENDED_ARG: + long_type = six.integer_types[-1] + extended_arg = arg * long_type(65536) + continue + + else: + arg = None + ptr += 1 + + yield OpArg(op, arg) + + +Bytecode = getattr(dis, 'Bytecode', Bytecode_compat) |