From 07ec8fa1fb40c8dadda842d9bb46088e91474315 Mon Sep 17 00:00:00 2001 From: rocky Date: Sun, 10 Nov 2019 18:44:43 -0500 Subject: [PATCH] More Python 3.0 custom "if" statment handling. --- test/bytecode_3.0/03_ifelse.pyc | Bin 428 -> 692 bytes test/simple_source/bug30/03_ifelse.py | 11 +++++++++++ uncompyle6/parsers/parse30.py | 21 +++++++++++++++++++-- uncompyle6/semantics/customize3.py | 6 ++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/test/bytecode_3.0/03_ifelse.pyc b/test/bytecode_3.0/03_ifelse.pyc index bfe7240fb97d22045118f622ed6597864473c3a6..daa754a5a0a17d8186caf5281aa2fa55d21b8a5a 100644 GIT binary patch delta 304 zcmZ`zF>1p=5S%^9a%^Ns;l?Qg29-%20s$B4Ody@Xm_w4`5-b^=jx-7`V!yzBUXUh` zNBCKpb(JOyGtBMI&h1z9)4RV*N5j+SX_@0s8R`eZPe=u@2No29!eB30P(*mn;_usQ z**T#pk4aiRLiLRBQ&Jb9zJhT1Jx0g3K_}>991JEO!iiy2MCbgRye7V}#=Cv$VzRX)!SC{Pj3lgL+iHv2Q VX>!n8H`Z0*pT(n<$3&fj^cM&MG&cYM delta 82 zcmdnOx`x@%nunKbX77pEWCkR_2xL0|aWOBDNMT@TVPJ>?G8h?xHJCQqDlzi;X@HcJ RvH*$P#LUU*Ozt2i0{|H13>W|a diff --git a/test/simple_source/bug30/03_ifelse.py b/test/simple_source/bug30/03_ifelse.py index 97aea0d1..1af08601 100644 --- a/test/simple_source/bug30/03_ifelse.py +++ b/test/simple_source/bug30/03_ifelse.py @@ -7,3 +7,14 @@ def main(args, f): func(f, sys.stdout.buffer) else: func(sys.stdin.buffer, sys.stdout.buffer) + +# From Python 3.0 _markupbase.py. +# +# The Problem was in the way "if"s are generated in 3.0 which is sort +# of like a more optimized Python 2.6, with reduced extraneous jumps, +# but still 2.6-ish and not 2.7- or 3.1-ish. +def parse_marked_section(fn, i, rawdata, report=1): + if report: + j = 1 + fn(rawdata[i: j]) + return 10 diff --git a/uncompyle6/parsers/parse30.py b/uncompyle6/parsers/parse30.py index 16f25e0f..c9288e5d 100644 --- a/uncompyle6/parsers/parse30.py +++ b/uncompyle6/parsers/parse30.py @@ -91,6 +91,9 @@ class Python30Parser(Python31Parser): except_suite ::= c_stmts POP_EXCEPT jump_except POP_TOP except_suite_finalize ::= SETUP_FINALLY c_stmts_opt except_var_finalize END_FINALLY _jump COME_FROM POP_TOP + + _ifstmts_jump ::= c_stmts_opt JUMP_FORWARD COME_FROM POP_TOP + jump_except ::= JUMP_FORWARD COME_FROM POP_TOP jump_except ::= JUMP_ABSOLUTE COME_FROM POP_TOP or ::= expr jmp_false expr jmp_true expr @@ -101,8 +104,22 @@ class Python30Parser(Python31Parser): # JUMP_IF_FALSE # The below rules in fact are the same or similar. - jmp_true ::= JUMP_IF_TRUE POP_TOP - jmp_false ::= JUMP_IF_FALSE _come_froms POP_TOP + jmp_true ::= JUMP_IF_TRUE POP_TOP + jmp_false ::= JUMP_IF_FALSE _come_froms POP_TOP + jmp_false_then ::= JUMP_IF_FALSE POP_TOP + + # We don't have hacky THEN detection, so we do it + # in the grammar below which is also somewhat hacky. + + stmt ::= ifstmt30 + ifstmt30 ::= testexpr_then _ifstmts_jump30 + + testexpr_then ::= testfalse_then + testfalse_then ::= expr jmp_false_then + call_stmt ::= expr COME_FROM + _ifstmts_jump30 ::= c_stmts POP_TOP + + ################################################################################ for_block ::= l_stmts_opt _come_froms POP_TOP JUMP_BACK diff --git a/uncompyle6/semantics/customize3.py b/uncompyle6/semantics/customize3.py index 9801538c..407b28e9 100644 --- a/uncompyle6/semantics/customize3.py +++ b/uncompyle6/semantics/customize3.py @@ -181,6 +181,12 @@ def customize_for_version3(self, version): # the iteration variable. These rules we can ignore # since we pick up the iteration variable some other way and # we definitely don't include in the source _[dd]. + TABLE_DIRECT.update({ + 'ifstmt30': ( '%|if %c:\n%+%c%-', + (0, "testexpr_then"), + (1, "_ifstmts_jump30") ), + }) + def n_comp_iter(node): if node[0] == "expr": n = node[0][0]