def test_opname(self):
self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
python类opmap()的实例源码
def test_boundaries(self):
self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
def test_stack_effect(self):
self.assertEqual(_opcode.stack_effect(dis.opmap['POP_TOP']), -1)
self.assertEqual(_opcode.stack_effect(dis.opmap['DUP_TOP_TWO']), 2)
self.assertEqual(_opcode.stack_effect(dis.opmap['BUILD_SLICE'], 0), -1)
self.assertEqual(_opcode.stack_effect(dis.opmap['BUILD_SLICE'], 1), -1)
self.assertEqual(_opcode.stack_effect(dis.opmap['BUILD_SLICE'], 3), -2)
self.assertRaises(ValueError, _opcode.stack_effect, 30000)
self.assertRaises(ValueError, _opcode.stack_effect, dis.opmap['BUILD_SLICE'])
self.assertRaises(ValueError, _opcode.stack_effect, dis.opmap['POP_TOP'], 0)
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 test_opname(self):
self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
def test_boundaries(self):
self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
def _opcode(name):
"""Return the opcode by name from the dis module."""
return dis.opmap[name]
def _opcode(name):
"""Return the opcode by name from the dis module."""
return dis.opmap[name]
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 test_opname(self):
self.assertEqual(dis.opname[dis.opmap["LOAD_FAST"]], "LOAD_FAST")
def test_boundaries(self):
self.assertEqual(dis.opmap["EXTENDED_ARG"], dis.EXTENDED_ARG)
self.assertEqual(dis.opmap["STORE_NAME"], dis.HAVE_ARGUMENT)
def test_stack_effect(self):
self.assertEqual(_opcode.stack_effect(dis.opmap['POP_TOP']), -1)
self.assertEqual(_opcode.stack_effect(dis.opmap['DUP_TOP_TWO']), 2)
self.assertEqual(_opcode.stack_effect(dis.opmap['BUILD_SLICE'], 0), -1)
self.assertEqual(_opcode.stack_effect(dis.opmap['BUILD_SLICE'], 1), -1)
self.assertEqual(_opcode.stack_effect(dis.opmap['BUILD_SLICE'], 3), -2)
self.assertRaises(ValueError, _opcode.stack_effect, 30000)
self.assertRaises(ValueError, _opcode.stack_effect, dis.opmap['BUILD_SLICE'])
self.assertRaises(ValueError, _opcode.stack_effect, dis.opmap['POP_TOP'], 0)
def init_opdata(l, from_mod, version=None, is_pypy=False):
"""Sets up a number of the structures found in Python's
opcode.py. Python opcode.py routines assign attributes to modules.
In order to do this in a modular way here, the local dictionary
for the module is passed.
"""
if version:
l['python_version'] = version
l['is_pypy'] = is_pypy
l['cmp_op'] = cmp_op
l['HAVE_ARGUMENT'] = HAVE_ARGUMENT
if version <= 3.5:
l['findlinestarts'] = findlinestarts
l['findlabels'] = findlabels
l['get_jump_targets'] = get_jump_targets
l['get_jump_target_maps'] = get_jump_target_maps
else:
l['findlinestarts'] = wordcode.findlinestarts
l['findlabels'] = wordcode.findlabels
l['get_jump_targets'] = wordcode.get_jump_targets
l['get_jump_target_maps'] = wordcode.get_jump_target_maps
l['opmap'] = deepcopy(from_mod.opmap)
l['opname'] = deepcopy(from_mod.opname)
for field in fields2copy:
l[field] = list(getattr(from_mod, field))
def def_op(l, op_name, opcode, pop=-2, push=-2, fallthrough=True):
l['opname'][opcode] = op_name
l['opmap'][op_name] = opcode
l['oppush'][opcode] = push
l['oppop'][opcode] = pop
if not fallthrough:
l['nofollow'].append(opcode)
def rm_op(l, name, op):
"""Remove an opcode. This is used when basing a new Python release off
of another one, and there is an opcode that is in the old release
that was removed in the new release.
We are pretty aggressive about removing traces of the op.
"""
# opname is an array, so we need to keep the position in there.
l['opname'][op] = '<%s>' % op
if op in l['hasconst']:
l['hasconst'].remove(op)
if op in l['hascompare']:
l['hascompare'].remove(op)
if op in l['hascondition']:
l['hascondition'].remove(op)
if op in l['hasfree']:
l['hasfree'].remove(op)
if op in l['hasjabs']:
l['hasjabs'].remove(op)
if op in l['hasname']:
l['hasname'].remove(op)
if op in l['hasjrel']:
l['hasjrel'].remove(op)
if op in l['haslocal']:
l['haslocal'].remove(op)
if op in l['hasname']:
l['hasname'].remove(op)
if op in l['hasnargs']:
l['hasnargs'].remove(op)
if op in l['hasvargs']:
l['hasvargs'].remove(op)
if op in l['nofollow']:
l['nofollow'].remove(op)
assert l['opmap'][name] == op
del l['opmap'][name]
def fix_opcode_names(opmap):
"""
Python stupidly named some OPCODES with a + which prevents using opcode name
directly as an attribute, e.g. SLICE+3. So we turn that into SLICE_3 so we
can then use opcode_23.SLICE_3. Later Python's fix this.
"""
return dict([(k.replace('+', '_'), v)
for (k, v) in opmap.items()])
def update_pj2(g, l):
g.update({'PJIF': l['opmap']['JUMP_IF_FALSE']})
g.update({'PJIT': l['opmap']['JUMP_IF_TRUE']})
update_sets(l)
def dump_opcodes(opmap):
"""Utility for dumping opcodes"""
op2name = {}
for k in opmap.keys():
op2name[opmap[k]] = k
for i in sorted(op2name.keys()):
print("%-3s %s" % (str(i), op2name[i]))
def _opcode(name):
"""Return the opcode by name from the dis module."""
return dis.opmap[name]
def _opcode(name):
"""Return the opcode by name from the dis module."""
return dis.opmap[name]