@@ -1096,18 +1096,30 @@ def visit_FunctionDef(self, node):
10961096 ## this is kept here as an option to be sure we are compatible with the
10971097 ## old-style code in runtime/pythonpythonjs.py and runtime/builtins.py
10981098 if not GLOBAL_VARIABLE_SCOPE :
1099- local_vars = set ()
1100- global_vars = set ()
1101- for n in node .body :
1102- if isinstance (n , Assign ) and isinstance (n .targets [0 ], Name ): ## assignment to local
1103- local_vars .add ( n .targets [0 ].id )
1104- elif isinstance (n , Global ):
1105- global_vars .update ( n .names )
1106- elif isinstance (n , With ) and isinstance ( n .context_expr , Name ) and n .context_expr .id == 'javascript' :
1107- for c in n .body :
1108- if isinstance (c , Assign ) and isinstance (c .targets [0 ], Name ): ## assignment to local
1109- local_vars .add ( c .targets [0 ].id )
1110-
1099+ def retrieve_vars (body ):
1100+ local_vars = set ()
1101+ global_vars = set ()
1102+ for n in body :
1103+ if isinstance (n , Assign ) and isinstance (n .targets [0 ], Name ): ## assignment to local
1104+ local_vars .add ( n .targets [0 ].id )
1105+ elif isinstance (n , Global ):
1106+ global_vars .update ( n .names )
1107+ elif isinstance (n , With ) and isinstance ( n .context_expr , Name ) and n .context_expr .id == 'javascript' :
1108+ for c in n .body :
1109+ if isinstance (c , Assign ) and isinstance (c .targets [0 ], Name ): ## assignment to local
1110+ local_vars .add ( c .targets [0 ].id )
1111+ elif hasattr (n , 'body' ) and not isinstance (n , FunctionDef ):
1112+ # do a recursive search inside new block except function def
1113+ l , g = retrieve_vars (n .body )
1114+ local_vars .update (l )
1115+ global_vars .update (g )
1116+ if hasattr (n , 'orelse' ):
1117+ l , g = retrieve_vars (n .orelse )
1118+ local_vars .update (l )
1119+ global_vars .update (g )
1120+ return local_vars , global_vars
1121+
1122+ local_vars , global_vars = retrieve_vars (node .body )
11111123 if local_vars - global_vars :
11121124 a = ',' .join ( local_vars - global_vars )
11131125 writer .write ('var(%s)' % a )
0 commit comments