本文整理汇总了Python中Scanner.getscanner方法的典型用法代码示例。如果您正苦于以下问题:Python Scanner.getscanner方法的具体用法?Python Scanner.getscanner怎么用?Python Scanner.getscanner使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Scanner
的用法示例。
在下文中一共展示了Scanner.getscanner方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: decompyle
# 需要导入模块: import Scanner [as 别名]
# 或者: from Scanner import getscanner [as 别名]
def decompyle(version, co, out=None, showasm=0, showast=0):
"""
diassembles a given code block 'co'
"""
assert type(co) == types.CodeType
# store final output stream for case of error
__real_out = out or sys.stdout
scanner = Scanner.getscanner(version)
scanner.setShowAsm(showasm, out)
tokens, customize = scanner.disassemble(co)
# Build AST from disassembly.
walker = Walker.Walker(out, scanner, showast=showast)
try:
ast = walker.build_ast(tokens, customize)
except Walker.ParserError, e : # parser failed, dump disassembly
print >>__real_out, e
raise
示例2: cmp_code_objects
# 需要导入模块: import Scanner [as 别名]
# 或者: from Scanner import getscanner [as 别名]
def cmp_code_objects(version, code_obj1, code_obj2, name=''):
"""
Compare two code-objects.
This is the main part of this module.
"""
#print code_obj1, type(code_obj2)
assert type(code_obj1) == types.CodeType
assert type(code_obj2) == types.CodeType
#print dir(code_obj1)
if isinstance(code_obj1, object):
# new style classes (Python 2.2)
# assume _both_ code objects to be new stle classes
assert dir(code_obj1) == dir(code_obj2)
else:
# old style classes
assert dir(code_obj1) == code_obj1.__members__
assert dir(code_obj2) == code_obj2.__members__
assert code_obj1.__members__ == code_obj2.__members__
if name == '__main__':
name = code_obj1.co_name
else:
name = '%s.%s' % (name, code_obj1.co_name)
if name == '.?': name = '__main__'
if isinstance(code_obj1, object) and cmp(code_obj1, code_obj2):
# use the new style code-classes' __cmp__ method, which
# should be faster and more sophisticated
# if this compare fails, we use the old routine to
# find out, what exactly is nor equal
# if this compare succeds, simply return
#return
pass
if isinstance(code_obj1, object):
members = filter(lambda x: x.startswith('co_'), dir(code_obj1))
else:
members = dir(code_obj1);
members.sort(); #members.reverse()
tokens1 = None
for member in members:
if member in __IGNORE_CODE_MEMBERS__:
pass
elif member == 'co_code':
scanner = Scanner.getscanner(version)
scanner.setShowAsm( showasm=0 )
global JUMP_OPs
JUMP_OPs = scanner.JUMP_OPs
# use changed Token class
# we (re)set this here to save exception handling,
# which would get 'unubersichtlich'
scanner.setTokenClass(Token)
try:
# disassemble both code-objects
tokens1,customize = scanner.disassemble(code_obj1)
del customize # save memory
tokens2,customize = scanner.disassemble(code_obj2)
del customize # save memory
finally:
scanner.resetTokenClass() # restore Token class
# compare length
if len(tokens1) != len(tokens2):
raise CmpErrorCodeLen(name, tokens1, tokens2)
# compare contents
#print len(tokens1), type(tokens1), type(tokens2)
for i in xrange(len(tokens1)):
if tokens1[i] != tokens2[i]:
#print '-->', i, type(tokens1[i]), type(tokens2[i])
raise CmpErrorCode(name, i, tokens1[i],
tokens2[i])
del tokens1, tokens2 # save memory
elif member == 'co_consts':
# compare length
if len(code_obj1.co_consts) != len(code_obj2.co_consts):
raise CmpErrorConstsLen(name, code_obj1.co_consts ,code_obj2.co_consts)
# compare contents
for idx in xrange(len(code_obj1.co_consts)):
const1 = code_obj1.co_consts[idx]
const2 = code_obj2.co_consts[idx]
## print code_obj1.co_consts[idx], '\t',
## print code_obj2.co_consts[idx]
# same type?
if type(const1) != type(const2):
raise CmpErrorContType(name, idx)
if type(const1) == types.CodeType:
# code object -> recursive compare
cmp_code_objects(version, const1,
const2, name=name)
elif cmp(const1, const2) != 0:
# content differs
raise CmpErrorConsts(name, idx)
else:
# all other members must be equal
if getattr(code_obj1, member) != getattr(code_obj2, member):
raise CmpErrorMember(name, member,
getattr(code_obj1,member),
#.........这里部分代码省略.........
示例3: cmp_code_objects
# 需要导入模块: import Scanner [as 别名]
# 或者: from Scanner import getscanner [as 别名]
def cmp_code_objects(version, code_obj1, code_obj2, name=''):
"""
Compare two code-objects.
This is the main part of this module.
"""
#print code_obj1, type(code_obj2)
assert type(code_obj1) == types.CodeType
assert type(code_obj2) == types.CodeType
#print dir(code_obj1)
if isinstance(code_obj1, object):
# new style classes (Python 2.2)
# assume _both_ code objects to be new stle classes
assert dir(code_obj1) == dir(code_obj2)
else:
# old style classes
assert dir(code_obj1) == code_obj1.__members__
assert dir(code_obj2) == code_obj2.__members__
assert code_obj1.__members__ == code_obj2.__members__
if name == '__main__':
name = code_obj1.co_name
else:
name = '%s.%s' % (name, code_obj1.co_name)
if name == '.?': name = '__main__'
if isinstance(code_obj1, object) and cmp(code_obj1, code_obj2):
# use the new style code-classes' __cmp__ method, which
# should be faster and more sophisticated
# if this compare fails, we use the old routine to
# find out, what exactly is nor equal
# if this compare succeds, simply return
#return
pass
if isinstance(code_obj1, object):
members = filter(lambda x: x.startswith('co_'), dir(code_obj1))
else:
members = dir(code_obj1);
members.sort(); #members.reverse()
tokens1 = None
for member in members:
if member in __IGNORE_CODE_MEMBERS__:
pass
elif member == 'co_code':
scanner = Scanner.getscanner(version)
scanner.setShowAsm( showasm=0 )
global JUMP_OPs
JUMP_OPs = scanner.JUMP_OPs + ['JUMP_BACK']
# use changed Token class
# we (re)set this here to save exception handling,
# which would get 'unubersichtlich'
scanner.setTokenClass(Token)
try:
# disassemble both code-objects
tokens1,customize = scanner.disassemble(code_obj1)
del customize # save memory
tokens2,customize = scanner.disassemble(code_obj2)
del customize # save memory
finally:
scanner.resetTokenClass() # restore Token class
targets1 = dis.findlabels(code_obj1.co_code)
tokens1 = [t for t in tokens1 if t.type != 'COME_FROM']
tokens2 = [t for t in tokens2 if t.type != 'COME_FROM']
i1 = 0; i2 = 0
offset_map = {}; check_jumps = {}
while i1 < len(tokens1):
if i2 >= len(tokens2):
if len(tokens1) == len(tokens2) + 2 \
and tokens1[-1].type == 'RETURN_VALUE' \
and tokens1[-2].type == 'LOAD_CONST' \
and tokens1[-2].pattr == None \
and tokens1[-3].type == 'RETURN_VALUE':
break
else:
raise CmpErrorCodeLen(name, tokens1, tokens2)
offset_map[tokens1[i1].offset] = tokens2[i2].offset
for idx1, idx2, offset2 in check_jumps.get(tokens1[i1].offset, []):
if offset2 != tokens2[i2].offset:
raise CmpErrorCode(name, tokens1[idx1].offset, tokens1[idx1],
tokens2[idx2], tokens1, tokens2)
if tokens1[i1] != tokens2[i2]:
if tokens1[i1].type == 'LOAD_CONST' == tokens2[i2].type:
i = 1
while tokens1[i1+i].type == 'LOAD_CONST':
i += 1
if tokens1[i1+i].type.startswith(('BUILD_TUPLE', 'BUILD_LIST')) \
and i == int(tokens1[i1+i].type.split('_')[-1]):
t = tuple([ elem.pattr for elem in tokens1[i1:i1+i] ])
if t != tokens2[i2].pattr:
raise CmpErrorCode(name, tokens1[i1].offset, tokens1[i1],
tokens2[i2], tokens1, tokens2)
i1 += i + 1
#.........这里部分代码省略.........