本文整理匯總了Python中dis.opname方法的典型用法代碼示例。如果您正苦於以下問題:Python dis.opname方法的具體用法?Python dis.opname怎麽用?Python dis.opname使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類dis
的用法示例。
在下文中一共展示了dis.opname方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: build_op
# 需要導入模塊: import dis [as 別名]
# 或者: from dis import opname [as 別名]
def build_op(self, op, arg=None):
"""Return a byte representation of a Python instruction."""
if isinstance(op, str):
op = dis.opmap[op]
if arg is None and op >= dis.HAVE_ARGUMENT:
raise ValueError("Operation requires an argument: %s" % dis.opname[op])
if arg is not None and op < dis.HAVE_ARGUMENT:
raise ValueError("Operation takes no argument: %s" % dis.opname[op])
if arg is None:
arg = 0
# Python 3.6 changed the argument to an 8-bit integer, so this
# could be a practical consideration
if arg >= self.OPARG_SIZE:
return self.build_op(
"EXTENDED_ARG", arg // self.OPARG_SIZE
) + self.build_op(op, arg % self.OPARG_SIZE)
if not self.OPARG_VARI:
return bytearray((op, arg))
elif op >= dis.HAVE_ARGUMENT:
return bytearray((op, arg % 256, arg // 256))
else:
return bytearray((op,))
示例2: dump
# 需要導入模塊: import dis [as 別名]
# 或者: from dis import opname [as 別名]
def dump(self, io=None):
if io:
save = sys.stdout
sys.stdout = io
pc = 0
for t in self.insts:
opname = t[0]
if opname == "SET_LINENO":
print
if len(t) == 1:
print "\t", "%3d" % pc, opname
pc = pc + 1
else:
print "\t", "%3d" % pc, opname, t[1]
pc = pc + 3
if io:
sys.stdout = save
示例3: __init__
# 需要導入模塊: import dis [as 別名]
# 或者: from dis import opname [as 別名]
def __init__(self):
code = (lambda: x if x else y).__code__.co_code
opcode, oparg = struct.unpack_from('BB', code, 2)
# Starting with Python 3.6, the bytecode format has changed, using
# 16-bit words (8-bit opcode + 8-bit argument) for each instruction,
# as opposed to previously 24 bit (8-bit opcode + 16-bit argument)
# for instructions that expect an argument and otherwise 8 bit.
# https://bugs.python.org/issue26647
if dis.opname[opcode] == 'POP_JUMP_IF_FALSE':
self.argument = struct.Struct('B')
self.have_argument = 0
# As of Python 3.6, jump targets are still addressed by their
# byte unit. This is matter to change, so that jump targets,
# in the future might refer to code units (address in bytes / 2).
# https://bugs.python.org/issue26647
self.jump_unit = 8 // oparg
else:
self.argument = struct.Struct('<H')
self.have_argument = dis.HAVE_ARGUMENT
self.jump_unit = 1
示例4: get_instructions
# 需要導入模塊: import dis [as 別名]
# 或者: from dis import opname [as 別名]
def get_instructions(co):
code = co.co_code
n = len(code)
i = 0
extended_arg = 0
while i < n:
offset = i
c = code[i]
op = ord(c)
argval = None
i = i + 1
if op >= HAVE_ARGUMENT:
oparg = ord(code[i]) + ord(code[i + 1]) * 256 + extended_arg
extended_arg = 0
i = i + 2
if op == EXTENDED_ARG:
extended_arg = oparg * 65536
if op in hasconst:
argval = co.co_consts[oparg]
yield Instruction(offset, argval, opname[op])
示例5: _get_instructions
# 需要導入模塊: import dis [as 別名]
# 或者: from dis import opname [as 別名]
def _get_instructions(co):
code = co.co_code
linestarts = dict(findlinestarts(co))
n = len(code)
i = 0
extended_arg = 0
while i < n:
offset = i
c = code[i]
op = ord(c)
lineno = linestarts.get(i)
argval = None
i = i + 1
if op >= HAVE_ARGUMENT:
oparg = ord(code[i]) + ord(code[i + 1]) * 256 + extended_arg
extended_arg = 0
i = i + 2
if op == EXTENDED_ARG:
extended_arg = oparg * 65536
if op in hasconst:
argval = co.co_consts[oparg]
yield Instruction(offset, argval, opname[op], lineno)
示例6: get_original_instructions
# 需要導入模塊: import dis [as 別名]
# 或者: from dis import opname [as 別名]
def get_original_instructions(self):
result = self.clean_instructions(self.code)
# pypy sometimes (when is not clear)
# inserts JUMP_IF_NOT_DEBUG instructions in bytecode
# If they're not present in our compiled instructions,
# ignore them in the original bytecode
if not any(
inst.opname == "JUMP_IF_NOT_DEBUG"
for inst in self.compile_instructions()
):
result = [
inst for inst in result
if inst.opname != "JUMP_IF_NOT_DEBUG"
]
return result
示例7: from_opcode
# 需要導入模塊: import dis [as 別名]
# 或者: from dis import opname [as 別名]
def from_opcode(cls, opcode, arg=_no_arg):
"""
Create an instruction from an opcode and raw argument.
Parameters
----------
opcode : int
Opcode for the instruction to create.
arg : int, optional
The argument for the instruction.
Returns
-------
intsr : Instruction
An instance of the instruction named by ``opcode``.
"""
return type(cls)(opname[opcode], (cls,), {}, opcode=opcode)(arg)
示例8: makeByteCode
# 需要導入模塊: import dis [as 別名]
# 或者: from dis import opname [as 別名]
def makeByteCode(self):
assert self.stage == CONV
self.lnotab = lnotab = LineAddrTable()
for t in self.insts:
opname = t[0]
if len(t) == 1:
lnotab.addCode(self.opnum[opname])
else:
oparg = t[1]
if opname == "SET_LINENO":
lnotab.nextLine(oparg)
continue
hi, lo = twobyte(oparg)
try:
lnotab.addCode(self.opnum[opname], lo, hi)
except ValueError:
print opname, oparg
print self.opnum[opname], lo, hi
raise
self.stage = DONE
示例9: test_opname
# 需要導入模塊: import dis [as 別名]
# 或者: from dis import opname [as 別名]
def test_opname(self):
self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
示例10: flattenGraph
# 需要導入模塊: import dis [as 別名]
# 或者: from dis import opname [as 別名]
def flattenGraph(self):
"""Arrange the blocks in order and resolve jumps"""
assert self.stage == RAW
self.insts = insts = []
pc = 0
begin = {}
end = {}
for b in self.getBlocksInOrder():
begin[b] = pc
for inst in b.getInstructions():
insts.append(inst)
if len(inst) == 1:
pc = pc + 1
elif inst[0] != "SET_LINENO":
# arg takes 2 bytes
pc = pc + 3
end[b] = pc
pc = 0
for i in range(len(insts)):
inst = insts[i]
if len(inst) == 1:
pc = pc + 1
elif inst[0] != "SET_LINENO":
pc = pc + 3
opname = inst[0]
if opname in self.hasjrel:
oparg = inst[1]
offset = begin[oparg] - pc
insts[i] = opname, offset
elif opname in self.hasjabs:
insts[i] = opname, begin[inst[1]]
self.stage = FLAT
示例11: convertArgs
# 需要導入模塊: import dis [as 別名]
# 或者: from dis import opname [as 別名]
def convertArgs(self):
"""Convert arguments from symbolic to concrete form"""
assert self.stage == FLAT
self.consts.insert(0, self.docstring)
self.sort_cellvars()
for i in range(len(self.insts)):
t = self.insts[i]
if len(t) == 2:
opname, oparg = t
conv = self._converters.get(opname, None)
if conv:
self.insts[i] = opname, conv(self, oparg)
self.stage = CONV
示例12: isJump
# 需要導入模塊: import dis [as 別名]
# 或者: from dis import opname [as 別名]
def isJump(opname):
if opname[:4] == 'JUMP':
return 1
示例13: findDepth
# 需要導入模塊: import dis [as 別名]
# 或者: from dis import opname [as 別名]
def findDepth(self, insts, debug=0):
depth = 0
maxDepth = 0
for i in insts:
opname = i[0]
if debug:
print i,
delta = self.effect.get(opname, None)
if delta is not None:
depth = depth + delta
else:
# now check patterns
for pat, pat_delta in self.patterns:
if opname[:len(pat)] == pat:
delta = pat_delta
depth = depth + delta
break
# if we still haven't found a match
if delta is None:
meth = getattr(self, opname, None)
if meth is not None:
depth = depth + meth(i[1])
if depth > maxDepth:
maxDepth = depth
if debug:
print depth, maxDepth
return maxDepth
示例14: _get_base_class_names
# 需要導入模塊: import dis [as 別名]
# 或者: from dis import opname [as 別名]
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 = [] # type: List[Tuple[str, str]]
add_last_step = False
for (op, oparg) in op_stream(code, lasti):
if op in dis.hasname:
if not add_last_step:
extends = []
if dis.opname[op] == "LOAD_NAME":
extends.append(("name", co.co_names[oparg]))
add_last_step = True
elif dis.opname[op] == "LOAD_ATTR":
extends.append(("attr", co.co_names[oparg]))
add_last_step = True
elif dis.opname[op] == "LOAD_GLOBAL":
extends.append(("name", co.co_names[oparg]))
add_last_step = True
else:
add_last_step = False
items = []
previous_item = [] # type: List[str]
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