From c953701623d04ecdd7253bf798e89ac005751455 Mon Sep 17 00:00:00 2001 From: rocky Date: Mon, 4 Dec 2017 14:14:45 -0500 Subject: [PATCH] Slightly better 3.x list comprehension handling --- test/bytecode_2.7/03_weird26.pyc | Bin 488 -> 479 bytes test/bytecode_3.2/03_weird26.pyc | Bin 209 -> 633 bytes test/bytecode_3.3/03_weird26.pyc | Bin 213 -> 672 bytes test/bytecode_3.3/05_set_comprehension.pyc | Bin 617 -> 1330 bytes test/bytecode_3.4/03_weird26.pyc | Bin 195 -> 536 bytes test/bytecode_3.4/05_set_comprehension.pyc | Bin 1216 -> 898 bytes test/bytecode_3.5/03_weird26.pyc | Bin 195 -> 536 bytes test/bytecode_3.5/05_set_comprehension.pyc | Bin 275 -> 898 bytes test/simple_source/bug26/03_weird26.py | 2 +- uncompyle6/semantics/consts.py | 2 +- uncompyle6/semantics/pysource.py | 23 +++++++++++++++++---- 11 files changed, 21 insertions(+), 6 deletions(-) diff --git a/test/bytecode_2.7/03_weird26.pyc b/test/bytecode_2.7/03_weird26.pyc index 39e093f2adf6d774762ab998cbaccae336f5b0a9..db4f2a172d90dc18a62da394821ddca1caac5a89 100644 GIT binary patch delta 60 zcmaFCe4kmG`7v3M)ejBSSM217ivsLs2CI PkSwU#SQpR8B+LW=`k@V` diff --git a/test/bytecode_3.2/03_weird26.pyc b/test/bytecode_3.2/03_weird26.pyc index a47cc33a15b5392403e4e205f0bee36d6f417e6a..14f0a2baf5e0a8fd9a084e74de267572f4305947 100644 GIT binary patch literal 633 zcmb_Z!AiqG6r9~ONgMPg9=xarp?YXbg`PqY^xT7m9uz4xyM%?LNfQ?s)4x& zq+#sxJsqUvIBjeZ1@1`*Q8rL@X#HNxpgP+xO_djB~DW zs^@xHR>P2OvF`web1*)G3yTO0dl)$JRt>{*V9S zM>ubb+UmuFOLiD`_U(H!d0pQK-_Q21-V_jFX_d*3xr-qo=BMxsXc7-|g;Q>&72*=& z@-Ici*vFV+m8^#R62$Bp+5?3+z}$0kKsm&DeT`ir&fu0ul>R5&#UUX9X28uZo)l(m z3T)=SW9pN=c$oTpNNSCP5?P%a?##+rqh+PX!{S&pyGJNYnvL|JnAkkh&0CwaJI&V7 z;9i?NZg(2lqYZhhJ2J&xG)c3bFE#RBfFFvHiz_~43JJD2Ac%>bWfP?@6OLV|DI@b} zL(0Bw^#998z-POO9_xoJ?^R_ivEjq62u4CO6#$nwg3PC delta 150 zcmZ3$dX=$$9uF^)|EkjV-nm_P&&Gh{LY2|o>>3?sv2cP1}x M9w5IMBrVJY05M4tX8-^I diff --git a/test/bytecode_3.3/05_set_comprehension.pyc b/test/bytecode_3.3/05_set_comprehension.pyc index 23ddb8e4e8b399370baba96e39a094024a8427e6..57c91d60c91aa9bc29e9ef9cdd6d963aab088afb 100644 GIT binary patch literal 1330 zcmb_cO-md>5UrlsT|ZbMutbpK`q`&j zK~q6b4qf&*i6dkkGD^7NZiy;4@3C@^=Q>U&iIO@mi%{)^c{(Z7SYj!_MPYK3EvA z>Cfyc76zbzlbM1|ws{V4aEk*EVj9B3#Mc^47(x~nwcRl!o^kPs$5LkMTFT)l$wNQU z2g7&TERr>BN>v)DLS}xdv}YirP7SbJ;+fr}t$OoKEQ}wuE0oJT)S#KfmN17_fm#Qs zQs2gHo{a5n+^XIYM8u=wOX<*)OP7PjyE340hLoUPN+WVpoxt2s-}cBGY2mKGU;Wg; zaV5&J*F#xc*}~5*l(`8iD^peYSIS@s_6&-kNq|WRe9iK3X7!1{XkmchVU*@knJD`y M0Im&p)9rWP0K-8O^Z)<= delta 70 zcmdnQ^^!$-9uF^9sY+V-Y6b>|WFUqEW+2M}h>J}oDw{C}Yp`vsY-VKk(_ooAkws(j Nd6q~vCZMc1GXT!d45k18 diff --git a/test/bytecode_3.4/03_weird26.pyc b/test/bytecode_3.4/03_weird26.pyc index 68ef0ebefcd938213fae614d7c261dcdcab56b6d..dbfda1f8b38ca1319125aeba896607df96d419b4 100644 GIT binary patch literal 536 zcmZWl!AiqG6rA0(X%qF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4d32414b8aa966b22cf..f9628f42646dfb9585d623105279b9042d92a751 100644 GIT binary patch literal 536 zcmZWl!AiqG6rA0(X%p$So3A_TDh{RIhoJwc` zb0CGAeLb<%&* zL!;WAM)tf4yQ86fj3;T<^Qq6WV*pd3Ul9`g3Chkfm5m5E<^Fa`Zj*XSB<*P$iVQJ0SPbz*$zNlYz8D!7#Z?-7*fHE6efmdCI-e7W`-g?1|V5r z2$Ns|N#rpzq_6;`Ss8*g*L-<^8=2`F7{`~VW)`Iwnduc&-r}&y%}*)KNwwnv>M91ADa-@_ D1WzVP diff --git a/test/bytecode_3.5/05_set_comprehension.pyc b/test/bytecode_3.5/05_set_comprehension.pyc index 9c4aa532f5b1e58e9223973a946a272cc9d81380..ef114502c2847e2bf065e2b69ffaa88b10b4cbfb 100644 GIT binary patch literal 898 zcmZWnO;6iE5S?AeNt_a?QdBBbs`iAmmw<#gHA0o(j8x@ULe#TVV4wv!l@N- zrlaUchH&W*6O3MBqE#fFO z`~78gf`wg~scfXQ%%e;h!F2~?!&V6zmscI-cU4u`{|2NylJL=~59OGzb3SS@dj1ZW+3myDIZYG_9|r{8ZUM<>lf`Eh8t8v_HwV<3hDW+2M}h>N9xL<$fwGPE!-qyiaC49$!TE+<1N;r#Ju#>$)U{3EI=&`0PZXhZ2$lO diff --git a/test/simple_source/bug26/03_weird26.py b/test/simple_source/bug26/03_weird26.py index 3447f701..9ff54cfc 100644 --- a/test/simple_source/bug26/03_weird26.py +++ b/test/simple_source/bug26/03_weird26.py @@ -9,6 +9,6 @@ list(x for x in range(10) if x % 2 if x % 3) # expresion which evaluates True unconditionally, # but leave dead code or junk around that we have to match on. # Tests "conditional_true" rule -(5 if 1 else max(5, 2)) +5 if 1 else 2 0 or max(5, 3) if 0 else 3 diff --git a/uncompyle6/semantics/consts.py b/uncompyle6/semantics/consts.py index 1fe26b1b..002335ce 100644 --- a/uncompyle6/semantics/consts.py +++ b/uncompyle6/semantics/consts.py @@ -167,7 +167,7 @@ TABLE_DIRECT = { 'list_for': ( ' for %c in %c%c', 2, 0, 3 ), 'list_if': ( ' if %c%c', 0, 2 ), 'list_if_not': ( ' if not %p%c', (0, 22), 2 ), - 'lc_body': ( '', ), # ignore when recusing + 'lc_body': ( '', ), # ignore when recursing 'comp_iter': ( '%c', 0 ), 'comp_if': ( ' if %c%c', 0, 2 ), diff --git a/uncompyle6/semantics/pysource.py b/uncompyle6/semantics/pysource.py index 2ee2dcda..224b1591 100644 --- a/uncompyle6/semantics/pysource.py +++ b/uncompyle6/semantics/pysource.py @@ -1083,7 +1083,7 @@ class SourceWalker(GenericASTTraversal, object): assert n == 'list_iter' # Find the list comprehension body. It is the inner-most - # node. + # node that is not list_.. . while n == 'list_iter': n = n[0] # iterate one nesting deeper if n == 'list_for': n = n[3] @@ -1210,7 +1210,8 @@ class SourceWalker(GenericASTTraversal, object): n = ast[iter_index] assert n == 'comp_iter', n - # find innermost node + # Find the comprehension body. It is the inner-most + # node that is not list_.. . while n == 'comp_iter': # list_iter n = n[0] # recurse one step if n == 'comp_for': @@ -1293,7 +1294,8 @@ class SourceWalker(GenericASTTraversal, object): # FIXME: I'm not totally sure this is right. - # find innermost node + # Find the list comprehension body. It is the inner-most + # node that is not list_.. . if_node = None comp_for = None comp_store = None @@ -1303,7 +1305,7 @@ class SourceWalker(GenericASTTraversal, object): have_not = False while n in ('list_iter', 'comp_iter'): - n = n[0] # recurse one step + n = n[0] # iterate one nesting deeper if n in ('list_for', 'comp_for'): if n[2] == 'store': store = n[2] @@ -1329,8 +1331,20 @@ class SourceWalker(GenericASTTraversal, object): else: self.preorder(store) + # FIXME this is all merely approximate + # from trepan.api import debug; debug() self.write(' in ') self.preorder(node[-3]) + + if ast == 'list_comp': + list_iter = ast[1] + assert list_iter == 'list_iter' + if list_iter == 'list_for': + self.preorder(list_iter[3]) + self.prec = p + return + pass + if comp_store: self.preorder(comp_for) elif if_node: @@ -1338,6 +1352,7 @@ class SourceWalker(GenericASTTraversal, object): if have_not: self.write('not ') self.preorder(if_node) + pass self.prec = p def listcomprehension_walk2(self, node):