Python dis 模块,hasconst() 实例源码

我们从Python开源项目中,提取了以下16个代码示例,用于说明如何使用dis.hasconst()

项目:tailbiter    作者:darius    | 项目源码 | 文件源码
def parse_byte_and_args(self):
        code = self.f_code
        opcode = code.co_code[self.f_lasti]
        self.f_lasti = self.f_lasti + 1
        if opcode >= dis.HAVE_ARGUMENT:
            int_arg = (   code.co_code[self.f_lasti]
                       + (code.co_code[self.f_lasti+1] << 8))
            self.f_lasti = self.f_lasti + 2
            if opcode in dis.hasconst:
                arg = code.co_consts[int_arg]
            elif opcode in dis.hasfree:
                if int_arg < len(code.co_cellvars):
                    arg = code.co_cellvars[int_arg]
                else:
                    arg = code.co_freevars[int_arg - len(code.co_cellvars)]
            elif opcode in dis.hasname:
                arg = code.co_names[int_arg]
            elif opcode in dis.haslocal:
                arg = code.co_varnames[int_arg]
            elif opcode in dis.hasjrel:
                arg = self.f_lasti + int_arg
            else:
                arg = int_arg
            return dis.opname[opcode], (arg,)
        return dis.opname[opcode], ()
项目:third_person_im    作者:bstadie    | 项目源码 | 文件源码
def _get_base_class_names(frame):
    """Get baseclass names from the code object"""
    co, lasti = frame.f_code, frame.f_lasti
    code = co.co_code
    i = 0
    extended_arg = 0
    extends = []
    while i <= lasti:
        c = code[i]
        op = ord(c)
        i += 1
        if op >= dis.HAVE_ARGUMENT:
            oparg = ord(code[i]) + ord(code[i+1])*256 + extended_arg
            extended_arg = 0
            i += 2
            if op == dis.EXTENDED_ARG:
                extended_arg = oparg*int(65536)
            if op in dis.hasconst:
                if type(co.co_consts[oparg]) == str:
                    extends = []
            elif op in dis.hasname:
                if dis.opname[op] == 'LOAD_NAME':
                    extends.append(('name', co.co_names[oparg]))
                if dis.opname[op] == 'LOAD_ATTR':
                    extends.append(('attr', co.co_names[oparg]))
    items = []
    previous_item = []
    for t, s in extends:
        if t == 'name':
            if previous_item:
                items.append(previous_item)
            previous_item = [s]
        else:
            previous_item += [s]
    if previous_item:
        items.append(previous_item)
    return items
项目:rllabplusplus    作者:shaneshixiang    | 项目源码 | 文件源码
def _get_base_class_names(frame):
    """Get baseclass names from the code object"""
    co, lasti = frame.f_code, frame.f_lasti
    code = co.co_code
    i = 0
    extended_arg = 0
    extends = []
    while i <= lasti:
        c = code[i]
        op = ord(c)
        i += 1
        if op >= dis.HAVE_ARGUMENT:
            oparg = ord(code[i]) + ord(code[i+1])*256 + extended_arg
            extended_arg = 0
            i += 2
            if op == dis.EXTENDED_ARG:
                extended_arg = oparg*int(65536)
            if op in dis.hasconst:
                if type(co.co_consts[oparg]) == str:
                    extends = []
            elif op in dis.hasname:
                if dis.opname[op] == 'LOAD_NAME':
                    extends.append(('name', co.co_names[oparg]))
                if dis.opname[op] == 'LOAD_ATTR':
                    extends.append(('attr', co.co_names[oparg]))
    items = []
    previous_item = []
    for t, s in extends:
        if t == 'name':
            if previous_item:
                items.append(previous_item)
            previous_item = [s]
        else:
            previous_item += [s]
    if previous_item:
        items.append(previous_item)
    return items
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def test_opmap(self):
        self.assertEqual(dis.opmap["STOP_CODE"], 0)
        self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
        self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
项目:oil    作者:oilshell    | 项目源码 | 文件源码
def test_opmap(self):
        self.assertEqual(dis.opmap["STOP_CODE"], 0)
        self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
        self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
def test_opmap(self):
        self.assertEqual(dis.opmap["STOP_CODE"], 0)
        self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
        self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
项目:ml-utils    作者:LinxiFan    | 项目源码 | 文件源码
def _get_base_class_names(frame):
    """ Get baseclass names from the code object """
    co, lasti = frame.f_code, frame.f_lasti
    code = co.co_code

    extends = []
    for (op, oparg) in op_stream(code, lasti):
        if op in dis.hasconst:
            if type(co.co_consts[oparg]) == str:
                extends = []
        elif op in dis.hasname:
            if dis.opname[op] == 'LOAD_NAME':
                extends.append(('name', co.co_names[oparg]))
            if dis.opname[op] == 'LOAD_ATTR':
                extends.append(('attr', co.co_names[oparg]))
            if dis.opname[op] == 'LOAD_GLOBAL':
                extends.append(('name', co.co_names[oparg]))

    items = []
    previous_item = []
    for t, s in extends:
        if t == 'name':
            if previous_item:
                items.append(previous_item)
            previous_item = [s]
        else:
            previous_item += [s]
    if previous_item:
        items.append(previous_item)
    return items
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_opmap(self):
        self.assertEqual(dis.opmap["NOP"], 9)
        self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
        self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
项目:pefile.pypy    作者:cloudtracer    | 项目源码 | 文件源码
def test_opmap(self):
        self.assertEqual(dis.opmap["STOP_CODE"], 0)
        self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
        self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_opmap(self):
        self.assertEqual(dis.opmap["NOP"], 9)
        self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
        self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
项目:ndk-python    作者:gittor    | 项目源码 | 文件源码
def test_opmap(self):
        self.assertEqual(dis.opmap["STOP_CODE"], 0)
        self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
        self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
项目:gail-driver    作者:sisl    | 项目源码 | 文件源码
def _get_base_class_names(frame):
    """Get baseclass names from the code object"""
    co, lasti = frame.f_code, frame.f_lasti
    code = co.co_code
    i = 0
    extended_arg = 0
    extends = []
    while i <= lasti:
        c = code[i]
        op = ord(c)
        i += 1
        if op >= dis.HAVE_ARGUMENT:
            oparg = ord(code[i]) + ord(code[i + 1]) * 256 + extended_arg
            extended_arg = 0
            i += 2
            if op == dis.EXTENDED_ARG:
                extended_arg = oparg * int(65536)
            if op in dis.hasconst:
                if type(co.co_consts[oparg]) == str:
                    extends = []
            elif op in dis.hasname:
                if dis.opname[op] == 'LOAD_NAME':
                    extends.append(('name', co.co_names[oparg]))
                if dis.opname[op] == 'LOAD_ATTR':
                    extends.append(('attr', co.co_names[oparg]))
    items = []
    previous_item = []
    for t, s in extends:
        if t == 'name':
            if previous_item:
                items.append(previous_item)
            previous_item = [s]
        else:
            previous_item += [s]
    if previous_item:
        items.append(previous_item)
    return items
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def test_opmap(self):
        self.assertEqual(dis.opmap["NOP"], 9)
        self.assertIn(dis.opmap["LOAD_CONST"], dis.hasconst)
        self.assertIn(dis.opmap["STORE_NAME"], dis.hasname)
项目:rllab    作者:rll    | 项目源码 | 文件源码
def _get_base_class_names(frame):
    """Get baseclass names from the code object"""
    co, lasti = frame.f_code, frame.f_lasti
    code = co.co_code
    i = 0
    extended_arg = 0
    extends = []
    while i <= lasti:
        c = code[i]
        op = ord(c)
        i += 1
        if op >= dis.HAVE_ARGUMENT:
            oparg = ord(code[i]) + ord(code[i+1])*256 + extended_arg
            extended_arg = 0
            i += 2
            if op == dis.EXTENDED_ARG:
                extended_arg = oparg*int(65536)
            if op in dis.hasconst:
                if type(co.co_consts[oparg]) == str:
                    extends = []
            elif op in dis.hasname:
                if dis.opname[op] == 'LOAD_NAME':
                    extends.append(('name', co.co_names[oparg]))
                if dis.opname[op] == 'LOAD_ATTR':
                    extends.append(('attr', co.co_names[oparg]))
    items = []
    previous_item = []
    for t, s in extends:
        if t == 'name':
            if previous_item:
                items.append(previous_item)
            previous_item = [s]
        else:
            previous_item += [s]
    if previous_item:
        items.append(previous_item)
    return items
项目:maml_rl    作者:cbfinn    | 项目源码 | 文件源码
def _get_base_class_names(frame):
    """Get baseclass names from the code object"""
    co, lasti = frame.f_code, frame.f_lasti
    code = co.co_code
    i = 0
    extended_arg = 0
    extends = []
    while i <= lasti:
        c = code[i]
        op = ord(c)
        i += 1
        if op >= dis.HAVE_ARGUMENT:
            oparg = ord(code[i]) + ord(code[i+1])*256 + extended_arg
            extended_arg = 0
            i += 2
            if op == dis.EXTENDED_ARG:
                extended_arg = oparg*int(65536)
            if op in dis.hasconst:
                if type(co.co_consts[oparg]) == str:
                    extends = []
            elif op in dis.hasname:
                if dis.opname[op] == 'LOAD_NAME':
                    extends.append(('name', co.co_names[oparg]))
                if dis.opname[op] == 'LOAD_ATTR':
                    extends.append(('attr', co.co_names[oparg]))
    items = []
    previous_item = []
    for t, s in extends:
        if t == 'name':
            if previous_item:
                items.append(previous_item)
            previous_item = [s]
        else:
            previous_item += [s]
    if previous_item:
        items.append(previous_item)
    return items
项目:oil    作者:oilshell    | 项目源码 | 文件源码
def parse_byte_and_args(self):
        """ Parse 1 - 3 bytes of bytecode into
        an instruction and optionally arguments."""
        f = self.frame
        opoffset = f.f_lasti
        byteCode = ord(f.f_code.co_code[opoffset])
        f.f_lasti += 1
        byteName = dis.opname[byteCode]
        arg = None
        arguments = []
        if byteCode >= dis.HAVE_ARGUMENT:
            arg = f.f_code.co_code[f.f_lasti:f.f_lasti+2]
            f.f_lasti += 2
            intArg = ord(arg[0]) + (ord(arg[1]) << 8)
            if byteCode in dis.hasconst:
                arg = f.f_code.co_consts[intArg]
            elif byteCode in dis.hasfree:
                if intArg < len(f.f_code.co_cellvars):
                    arg = f.f_code.co_cellvars[intArg]
                else:
                    var_idx = intArg - len(f.f_code.co_cellvars)
                    arg = f.f_code.co_freevars[var_idx]
            elif byteCode in dis.hasname:
                arg = f.f_code.co_names[intArg]
            elif byteCode in dis.hasjrel:
                arg = f.f_lasti + intArg
            elif byteCode in dis.hasjabs:
                arg = intArg
            elif byteCode in dis.haslocal:
                arg = f.f_code.co_varnames[intArg]
            else:
                arg = intArg
            arguments = [arg]

        return byteName, arguments, opoffset