From ecbbc7dfeac969b9776e3053d745470d18d175ed Mon Sep 17 00:00:00 2001 From: rocky Date: Tue, 26 Jul 2016 15:56:49 -0400 Subject: [PATCH] Custom PyPy rules for tryfinallysmt, assign{2,3} --- test/bytecode_pypy2.7/00_assign_pypy.pyc | Bin 0 -> 211 bytes test/bytecode_pypy2.7/03_try_return.pyc | Bin 318 -> 551 bytes .../bug_pypy27/00_assign_pypy.py | 4 ++++ .../simple_source/bug_pypy27/03_try_return.py | 7 ++++++ uncompyle6/main.py | 1 - uncompyle6/parsers/parse2.py | 22 ++++++++++++++++-- uncompyle6/scanners/scanner2.py | 6 ++++- uncompyle6/semantics/pysource.py | 5 ++++ 8 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 test/bytecode_pypy2.7/00_assign_pypy.pyc create mode 100644 test/simple_source/bug_pypy27/00_assign_pypy.py diff --git a/test/bytecode_pypy2.7/00_assign_pypy.pyc b/test/bytecode_pypy2.7/00_assign_pypy.pyc new file mode 100644 index 0000000000000000000000000000000000000000..7b2106789ba0e8b072abe339251298bddae06a59 GIT binary patch literal 211 zcmd=3%*$1KY0%`yBL#>U8KM{%q8LGp6eflgW`-yxhA1G(!jQtk5Ujxp zRF(yNsTYgFD*(= y)lVu-k1wb!s5COyH!z4#EH2JW&jSnS6;zgR0F~S1=BJeAq}qY3ECyN5!w3MT@+7VR literal 0 HcmV?d00001 diff --git a/test/bytecode_pypy2.7/03_try_return.pyc b/test/bytecode_pypy2.7/03_try_return.pyc index b663b2efac8c62963377cc0612d6175647037ee7..e587aeea1bae9ebd4db9873cc0f862b8a5059dc5 100644 GIT binary patch delta 305 zcmXX>K}rKb5Uie!2?_2Y1pI+8Ie7I8-U4n8ikBts&IZF8mYG>Z33%}X;=w2Q2*G>u z27loPtlsQE*Hl+mPxt(oufgTyFP&U}f8G@6kMwsqBPoG5;EqH(A}9@(HhI7JeJu

tHFPgG_fljx1Jno%iNb ZszC=q5}BEW)vskLWa-CL=0nZLqkq8PI%EI< delta 71 zcmZ3^vX4oZ>oYG`WZLxbWCkc;gwn;lKt>7!Lkj~#6p+El5Ujy8(d-U~p9V-p5ZmM* JjG8PUwE(~F3zYx> diff --git a/test/simple_source/bug_pypy27/00_assign_pypy.py b/test/simple_source/bug_pypy27/00_assign_pypy.py new file mode 100644 index 00000000..08c22891 --- /dev/null +++ b/test/simple_source/bug_pypy27/00_assign_pypy.py @@ -0,0 +1,4 @@ +# From PyPy 2.6.1 datetime +# PyPy has simpler handling of assign3 and assign2 +i, n = 0, 1 +a, b, c = 1, 2, 3 diff --git a/test/simple_source/bug_pypy27/03_try_return.py b/test/simple_source/bug_pypy27/03_try_return.py index da118312..f8057ba5 100644 --- a/test/simple_source/bug_pypy27/03_try_return.py +++ b/test/simple_source/bug_pypy27/03_try_return.py @@ -7,3 +7,10 @@ def call(self, string): return open(string, self, self._bufsize) except IOError: pass + +# From PyPy 2.6.1 function.py +def _call_funcptr(self, funcptr, *newargs): + try: + return self._build_result(self._restype_, result) + finally: + funcptr.free_temp_buffers() diff --git a/uncompyle6/main.py b/uncompyle6/main.py index 0cffb34e..383d2061 100644 --- a/uncompyle6/main.py +++ b/uncompyle6/main.py @@ -16,7 +16,6 @@ def uncompyle( """ disassembles and deparses a given code block 'co' """ - assert iscode(co) # store final output stream for case of error diff --git a/uncompyle6/parsers/parse2.py b/uncompyle6/parsers/parse2.py index d9ca5ce1..1c77ed76 100644 --- a/uncompyle6/parsers/parse2.py +++ b/uncompyle6/parsers/parse2.py @@ -251,7 +251,15 @@ class Python2Parser(PythonParser): ''' for opname, v in list(customize.items()): opname_base = opname[:opname.rfind('_')] - if opname_base in ('BUILD_LIST', 'BUILD_TUPLE', 'BUILD_SET'): + if opname == 'PyPy': + self.add_unique_rules([ + 'stmt ::= assign3_pypy', + 'stmt ::= assign2_pypy', + 'assign3_pypy ::= expr expr expr designator designator designator', + 'assign2_pypy ::= expr expr designator designator' + ], customize) + continue + elif opname_base in ('BUILD_LIST', 'BUILD_TUPLE', 'BUILD_SET'): thousands = (v//1024) thirty32s = ((v//32)%32) if thirty32s > 0: @@ -308,6 +316,16 @@ class Python2Parser(PythonParser): "try_middle_pypy ::= COME_FROM except_stmts END_FINALLY COME_FROM" ], customize) continue + elif opname == 'SETUP_FINALLY': + # FIXME: have a way here to detect PyPy. Right now we + # only have SETUP_EXCEPT customization for PyPy, but that might not + # always be the case. + self.add_unique_rules([ + "stmt ::= tryfinallystmt_pypy", + "tryfinallystmt_pypy ::= SETUP_FINALLY suite_stmts_opt COME_FROM " + "suite_stmts_opt END_FINALLY" + ], customize) + continue elif opname_base in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'): rule = 'unpack ::= ' + opname + ' designator'*v elif opname_base == 'UNPACK_LIST': @@ -321,7 +339,7 @@ class Python2Parser(PythonParser): ('pos_arg '*v, opname), nop_func) rule = 'mkfunc ::= %s LOAD_CONST %s' % ('expr '*v, opname) elif opname_base == 'MAKE_CLOSURE': - # FIXME: use add_uniqe_rules to tidy this up. + # FIXME: use add_unique_rules to tidy this up. self.addRule('mklambda ::= %s load_closure LOAD_LAMBDA %s' % ('expr '*v, opname), nop_func) self.addRule('genexpr ::= %s load_closure LOAD_GENEXPR %s expr GET_ITER CALL_FUNCTION_1' % diff --git a/uncompyle6/scanners/scanner2.py b/uncompyle6/scanners/scanner2.py index d1c58c35..4a1d734f 100755 --- a/uncompyle6/scanners/scanner2.py +++ b/uncompyle6/scanners/scanner2.py @@ -67,6 +67,9 @@ class Scanner2(scan.Scanner): tokens = [] customize = {} + if self.is_pypy: + customize['PyPy'] = 1; + Token = self.Token # shortcut n = self.setup_code(co) @@ -208,7 +211,8 @@ class Scanner2(scan.Scanner): customize[opname] = oparg elif self.is_pypy and opname in ('LOOKUP_METHOD', 'JUMP_IF_NOT_DEBUG', - 'SETUP_EXCEPT'): + 'SETUP_EXCEPT', + 'SETUP_FINALLY'): # The value in the dict is in special cases in semantic actions, such # as CALL_FUNCTION. The value is not used in these cases, so we put # in arbitrary value 0. diff --git a/uncompyle6/semantics/pysource.py b/uncompyle6/semantics/pysource.py index 85c23d08..81967692 100644 --- a/uncompyle6/semantics/pysource.py +++ b/uncompyle6/semantics/pysource.py @@ -383,10 +383,15 @@ TABLE_DIRECT = { ######################## # PyPy Additions + # FIXME: we could remove the corresponding + # rules without _pypy if we have pypy ####################### 'assert_pypy': ( '%|assert %c\n' , 1 ), 'assert2_pypy': ( '%|assert %c, %c\n' , 1, 4 ), 'trystmt_pypy': ( '%|try:\n%+%c%-%c\n\n', 1, 2 ), + 'tryfinallystmt_pypy': ( '%|try:\n%+%c%-%|finally:\n%+%c%-\n\n', 1, 3 ), + 'assign3_pypy': ( '%|%c, %c, %c = %c, %c, %c\n', 5, 4, 3, 0, 1, 2 ), + 'assign2_pypy': ( '%|%c, %c = %c, %c\n', 3, 2, 0, 1), }