我们从Python开源项目中,提取了以下16个代码示例,用于说明如何使用dis.hasconst()。
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], ()
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
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)
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
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)
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
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