@@ -555,23 +555,44 @@ def visit_For(self, node):
555555 path = self .filenames [- 1 ]
556556 ))
557557
558- if isinstance (node .iter , ast .Call ) and get_call_names_as_string (node .iter .func ) in self .function_names :
559- last_node = self .visit (node .iter )
560- last_node .connect (for_node )
558+ self .process_loop_funcs (node .iter , for_node )
561559
562560 return self .loop_node_skeleton (for_node , node )
563561
562+ def process_loop_funcs (self , comp_n , loop_node ):
563+ """
564+ If the loop test node contains function calls, it connects the loop node to the nodes of
565+ those function calls.
566+
567+ :param comp_n: The test node of a loop that may contain functions.
568+ :param loop_node: The loop node itself to connect to the new function nodes if any
569+ :return: None
570+ """
571+ if isinstance (comp_n , ast .Call ) and get_call_names_as_string (comp_n .func ) in self .function_names :
572+ last_node = self .visit (comp_n )
573+ last_node .connect (loop_node )
574+
564575 def visit_While (self , node ):
565576 label_visitor = LabelVisitor ()
566- label_visitor .visit (node .test )
577+ test = node .test # the test condition of the while loop
578+ label_visitor .visit (test )
567579
568- test = self .append_node (Node (
580+ while_node = self .append_node (Node (
569581 'while ' + label_visitor .result + ':' ,
570582 node ,
571583 path = self .filenames [- 1 ]
572584 ))
573585
574- return self .loop_node_skeleton (test , node )
586+ if isinstance (test , ast .Compare ):
587+ # quirk. See https://greentreesnakes.readthedocs.io/en/latest/nodes.html#Compare
588+ self .process_loop_funcs (test .left , while_node )
589+
590+ for comp in test .comparators :
591+ self .process_loop_funcs (comp , while_node )
592+ else : # while foo():
593+ self .process_loop_funcs (test , while_node )
594+
595+ return self .loop_node_skeleton (while_node , node )
575596
576597 def add_blackbox_or_builtin_call (self , node , blackbox ): # noqa: C901
577598 """Processes a blackbox or builtin function when it is called.
0 commit comments