tok.format -> tok.__str__; simplify pypy code

This commit is contained in:
rocky
2016-07-27 09:26:39 -04:00
parent 5e801b5d74
commit 3a78332d59
9 changed files with 29 additions and 20 deletions

View File

@@ -61,7 +61,7 @@ def disco_loop(disasm, queue, real_out):
queue.append(t.pattr) queue.append(t.pattr)
elif iscode(t.attr): elif iscode(t.attr):
queue.append(t.attr) queue.append(t.attr)
print(t.format(), file=real_out) print(t, file=real_out)
pass pass
pass pass

View File

@@ -252,12 +252,12 @@ class Python2Parser(PythonParser):
for opname, v in list(customize.items()): for opname, v in list(customize.items()):
opname_base = opname[:opname.rfind('_')] opname_base = opname[:opname.rfind('_')]
if opname == 'PyPy': if opname == 'PyPy':
self.add_unique_rules([ self.addRule("""
'stmt ::= assign3_pypy', stmt ::= assign3_pypy
'stmt ::= assign2_pypy', stmt ::= assign2_pypy
'assign3_pypy ::= expr expr expr designator designator designator', assign3_pypy ::= expr expr expr designator designator designator
'assign2_pypy ::= expr expr designator designator' assign2_pypy ::= expr expr designator designator
], customize) """, nop_func)
continue continue
elif opname_base in ('BUILD_LIST', 'BUILD_TUPLE', 'BUILD_SET'): elif opname_base in ('BUILD_LIST', 'BUILD_TUPLE', 'BUILD_SET'):
thousands = (v//1024) thousands = (v//1024)

View File

@@ -17,7 +17,7 @@ that a later phase can turn into a sequence of ASCII text.
from __future__ import print_function from __future__ import print_function
from uncompyle6.parser import PythonParser, PythonParserSingle from uncompyle6.parser import PythonParser, PythonParserSingle, nop_func
from uncompyle6.parsers.astnode import AST from uncompyle6.parsers.astnode import AST
from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
from uncompyle6 import PYTHON3 from uncompyle6 import PYTHON3
@@ -440,7 +440,15 @@ class Python3Parser(PythonParser):
opname = token.type opname = token.type
opname_base = opname[:opname.rfind('_')] opname_base = opname[:opname.rfind('_')]
if opname in ('CALL_FUNCTION', 'CALL_FUNCTION_VAR', if opname == 'PyPy':
self.addRule("""
stmt ::= assign3_pypy
stmt ::= assign2_pypy
assign3_pypy ::= expr expr expr designator designator designator
assign2_pypy ::= expr expr designator designator
""", nop_func)
continue
elif opname in ('CALL_FUNCTION', 'CALL_FUNCTION_VAR',
'CALL_FUNCTION_VAR_KW', 'CALL_FUNCTION_KW'): 'CALL_FUNCTION_VAR_KW', 'CALL_FUNCTION_KW'):
self.custom_classfunc_rule(opname, token, customize) self.custom_classfunc_rule(opname, token, customize)
elif opname == 'LOAD_DICTCOMP': elif opname == 'LOAD_DICTCOMP':

View File

@@ -253,7 +253,7 @@ class Scanner2(scan.Scanner):
if show_asm in ('both', 'after'): if show_asm in ('both', 'after'):
for t in tokens: for t in tokens:
print(t.format()) print(t)
print() print()
return tokens, customize return tokens, customize
@@ -865,7 +865,7 @@ if __name__ == "__main__":
from uncompyle6 import PYTHON_VERSION from uncompyle6 import PYTHON_VERSION
tokens, customize = Scanner2(PYTHON_VERSION).disassemble(co) tokens, customize = Scanner2(PYTHON_VERSION).disassemble(co)
for t in tokens: for t in tokens:
print(t.format()) print(t)
else: else:
print("Need to be Python 3.2 or greater to demo; I am %s." % print("Need to be Python 3.2 or greater to demo; I am %s." %
PYTHON_VERSION) PYTHON_VERSION)

View File

@@ -108,7 +108,7 @@ if __name__ == "__main__":
co = inspect.currentframe().f_code co = inspect.currentframe().f_code
tokens, customize = Scanner27().disassemble(co) tokens, customize = Scanner27().disassemble(co)
for t in tokens: for t in tokens:
print(t.format()) print(t)
pass pass
else: else:
print("Need to be Python 2.7 to demo; I am %s." % print("Need to be Python 2.7 to demo; I am %s." %

View File

@@ -130,10 +130,13 @@ class Scanner3(scan.Scanner):
for instr in bytecode.get_instructions(co): for instr in bytecode.get_instructions(co):
print(instr._disassemble()) print(instr._disassemble())
customize = {}
# Container for tokens # Container for tokens
tokens = [] tokens = []
customize = {}
if self.is_pypy:
customize['PyPy'] = 1;
self.code = array('B', co.co_code) self.code = array('B', co.co_code)
self.build_lines_data(co) self.build_lines_data(co)
self.build_prev_op() self.build_prev_op()
@@ -307,7 +310,7 @@ class Scanner3(scan.Scanner):
if show_asm in ('both', 'after'): if show_asm in ('both', 'after'):
for t in tokens: for t in tokens:
print(t.format()) print(t)
print() print()
return tokens, customize return tokens, customize
@@ -801,7 +804,7 @@ if __name__ == "__main__":
from uncompyle6 import PYTHON_VERSION from uncompyle6 import PYTHON_VERSION
tokens, customize = Scanner3(PYTHON_VERSION).disassemble(co) tokens, customize = Scanner3(PYTHON_VERSION).disassemble(co)
for t in tokens: for t in tokens:
print(t.format()) print(t)
else: else:
print("Need to be Python 3.2 or greater to demo; I am %s." % print("Need to be Python 3.2 or greater to demo; I am %s." %
PYTHON_VERSION) PYTHON_VERSION)

View File

@@ -28,7 +28,7 @@ if __name__ == "__main__":
co = inspect.currentframe().f_code co = inspect.currentframe().f_code
tokens, customize = Scanner35().disassemble(co) tokens, customize = Scanner35().disassemble(co)
for t in tokens: for t in tokens:
print(t.format()) print(t)
pass pass
else: else:
print("Need to be Python 3.5 to demo; I am %s." % print("Need to be Python 3.5 to demo; I am %s." %

View File

@@ -52,7 +52,7 @@ class Token:
# return (prefix + # return (prefix +
# ('%9s %-18s %r' % (self.offset, self.type, pattr))) # ('%9s %-18s %r' % (self.offset, self.type, pattr)))
def format(self): def __str__(self):
prefix = '\n%4d ' % self.linestart if self.linestart else (' ' * 6) prefix = '\n%4d ' % self.linestart if self.linestart else (' ' * 6)
offset_opname = '%6s %-17s' % (self.offset, self.type) offset_opname = '%6s %-17s' % (self.offset, self.type)
if not self.has_arg: if not self.has_arg:
@@ -76,8 +76,6 @@ class Token:
pattr = '' pattr = ''
return "%s%s%s %r" % (prefix, offset_opname, argstr, pattr) return "%s%s%s %r" % (prefix, offset_opname, argstr, pattr)
__str__ = format
def __hash__(self): def __hash__(self):
return hash(self.type) return hash(self.type)

View File

@@ -14,7 +14,7 @@ def maybe_show_asm(showasm, tokens):
if showasm: if showasm:
stream = showasm if hasattr(showasm, 'write') else sys.stdout stream = showasm if hasattr(showasm, 'write') else sys.stdout
for t in tokens: for t in tokens:
stream.write(t.format()) stream.write(t)
stream.write('\n') stream.write('\n')