@@ -590,7 +590,6 @@ def traverse(self, node, indent=None, isLambda=False):
590590 def write (self , * data ):
591591 if (len (data ) == 0 ) or (len (data ) == 1 and data [0 ] == '' ):
592592 return
593- # import pdb; pdb.set_trace()
594593 out = '' .join ((str (j ) for j in data ))
595594 n = 0
596595 for i in out :
@@ -812,10 +811,6 @@ def n_delete_subscr(self, node):
812811 if node [- 2 ][0 ][- 1 ] != 'BUILD_TUPLE_0' :
813812 node [- 2 ][0 ].type = 'build_tuple2'
814813 self .default (node )
815- # maybe_tuple = node[-2][-1]
816- # if maybe_tuple.type.startswith('BUILD_TUPLE'):
817- # maybe_tuple.type = 'build_tuple2'
818- # self.default(node)
819814
820815 n_store_subscr = n_binary_subscr = n_delete_subscr
821816
@@ -988,7 +983,7 @@ def n_mklambda(self, node):
988983 self .prune () # stop recursing
989984
990985 def n_list_compr (self , node ):
991- """List comprehensions the way they are done in Python2 .
986+ """List comprehensions the way they are done in Python 2 .
992987 """
993988 p = self .prec
994989 self .prec = 27
@@ -1057,7 +1052,10 @@ def n_genexpr(self, node):
10571052
10581053 def n_setcomp (self , node ):
10591054 self .write ('{' )
1060- self .comprehension_walk (node , iter_index = 4 )
1055+ if node [0 ] == 'LOAD_SETCOMP' :
1056+ self .listcomprehension_walk3 (node , 1 , 0 )
1057+ else :
1058+ self .comprehension_walk (node , iter_index = 4 )
10611059 self .write ('}' )
10621060 self .prune ()
10631061
@@ -1076,17 +1074,28 @@ def listcomprehension_walk3(self, node, iter_index, code_index=-5):
10761074
10771075 ast = self .build_ast (code ._tokens , code ._customize )
10781076 self .customize (code ._customize )
1079- ast = ast [0 ][0 ][0 ][0 ][0 ]
1080-
1081- n = ast [iter_index ]
1082- assert n == 'list_iter'
1077+ # skip over stmts sstmt smt
1078+ ast = ast [0 ][0 ][0 ]
1079+ designator = None
1080+ if ast == 'setcomp_func' :
1081+ for k in ast :
1082+ if k == 'comp_iter' :
1083+ n = k
1084+ elif k == 'designator' :
1085+ designator = k
1086+ pass
1087+ pass
1088+ pass
1089+ else :
1090+ ast = ast [0 ][0 ]
1091+ n = ast [iter_index ]
1092+ assert n == 'list_iter'
10831093
10841094 ## FIXME: I'm not totally sure this is right.
10851095
10861096 # find innermost node
1087- designator = None
10881097 list_if_node = None
1089- while n == 'list_iter' :
1098+ while n in ( 'list_iter' , 'comp_iter' ) :
10901099 n = n [0 ] # recurse one step
10911100 if n == 'list_for' :
10921101 if n [2 ] == 'designator' :
@@ -1099,8 +1108,8 @@ def listcomprehension_walk3(self, node, iter_index, code_index=-5):
10991108 n = n [2 ]
11001109 pass
11011110 pass
1102- assert n == 'lc_body' , ast
1103- assert designator , "Couldn't find designator in list comprehension"
1111+ assert n . type in ( 'lc_body' , 'comp_body' ) , ast
1112+ assert designator , "Couldn't find designator in list/set comprehension"
11041113
11051114 self .preorder (n [0 ])
11061115 self .write (' for ' )
@@ -1322,10 +1331,7 @@ def n_mapexpr(self, node):
13221331 l = list (kv_node )
13231332 i = 0
13241333 while i < len (l ):
1325- try :
1326- name = self .traverse (l [i + 1 ], indent = '' )
1327- except :
1328- from trepan .api import debug ; debug ()
1334+ name = self .traverse (l [i + 1 ], indent = '' )
13291335 value = self .traverse (l [i ], indent = self .indent + (len (name )+ 2 )* ' ' )
13301336 self .write (sep , name , ': ' , value )
13311337 sep = line_seperator
@@ -1504,7 +1510,6 @@ def engine(self, entry, startnode):
15041510 elif typ == 'C' :
15051511 low , high , sep = entry [arg ]
15061512 remaining = len (node [low :high ])
1507- # remaining = len(node[low:high])
15081513 for subnode in node [low :high ]:
15091514 self .preorder (subnode )
15101515 remaining -= 1
@@ -1821,7 +1826,7 @@ def build_class(self, code):
18211826 self .classes .pop (- 1 )
18221827
18231828 def gen_source (self , ast , name , customize , isLambda = False , returnNone = False ):
1824- """convert AST to source code"""
1829+ """convert AST to Python source code"""
18251830
18261831 rn = self .return_none
18271832 self .return_none = returnNone
0 commit comments