diff options
-rw-r--r-- | setuptools/depends.py | 5 | ||||
-rw-r--r-- | setuptools/py33compat.py | 46 |
2 files changed, 49 insertions, 2 deletions
diff --git a/setuptools/depends.py b/setuptools/depends.py index d417fa32..45e7052d 100644 --- a/setuptools/depends.py +++ b/setuptools/depends.py @@ -1,10 +1,11 @@ import sys import imp import marshal -import dis from distutils.version import StrictVersion from imp import PKG_DIRECTORY, PY_COMPILED, PY_SOURCE, PY_FROZEN +from .py33compat import Bytecode + __all__ = [ 'Require', 'find_module', 'get_module_constant', 'extract_constant' @@ -155,7 +156,7 @@ def extract_constant(code, symbol, default=-1): const = default - for byte_code in dis.Bytecode(code): + for byte_code in Bytecode(code): op = byte_code.opcode arg = byte_code.arg 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) |