Ejemplo n.º 1
0
    def assign_multi_target(self, node, right_hand_side_variables):
        new_assignment_nodes = list()

        for target in node.targets:
            label = LabelVisitor()
            label.visit(target)
            left_hand_side = label.result
            label.result += " = "
            label.visit(node.value)
            new_assignment_nodes.append(
                self.append_node(
                    AssignmentNode(
                        label.result,
                        left_hand_side,
                        ast.Assign(target, node.value),
                        right_hand_side_variables,
                        line_number=node.lineno,
                        path=self.filenames[-1],
                    )
                )
            )

        connect_nodes(new_assignment_nodes)
        return ControlFlowNode(
            new_assignment_nodes[0], [new_assignment_nodes[-1]], []
        )  # return the last added node
Ejemplo n.º 2
0
    def stmt_star_handler(self, stmts, prev_node_to_avoid=None):
        """Handle stmt* expressions in an AST node.

        Links all statements together in a list of statements, accounting for statements with multiple last nodes.
        """
        break_nodes = list()
        cfg_statements = list()
        self.prev_nodes_to_avoid.append(prev_node_to_avoid)
        self.last_control_flow_nodes.append(None)

        first_node = None
        node_not_to_step_past = self.nodes[-1]
        for stmt in stmts:
            node = self.visit(stmt)
            if isinstance(node, IgnoredNode):
                continue
            if isinstance(node, ControlFlowNode) and not isinstance(
                    node.test, TryNode):
                self.last_control_flow_nodes.append(node.test)
            else:
                self.last_control_flow_nodes.append(None)

            if isinstance(node, ControlFlowNode):
                break_nodes.extend(node.break_statements)
            elif isinstance(node, BreakNode):
                break_nodes.append(node)
            cfg_statements.append(node)
            if not first_node:
                if isinstance(node, ControlFlowNode):
                    first_node = node.test
                else:
                    first_node = get_first_node(node, node_not_to_step_past)

        self.prev_nodes_to_avoid.pop()
        self.last_control_flow_nodes.pop()
        if cfg_statements:
            connect_nodes(cfg_statements)
            if first_node:
                first_statement = first_node
            else:
                first_statement = get_first_statement(cfg_statements[0])

            last_statements = get_last_statements(cfg_statements)
            return ConnectStatements(
                first_statement=first_statement,
                last_statements=last_statements,
                break_statements=break_nodes,
            )
        else:  # When body of module only contains ignored nodes
            return IgnoredNode()
Ejemplo n.º 3
0
    def assign_tuple_target(self, target_nodes, value_nodes, right_hand_side_variables):
        new_assignment_nodes = []
        remaining_variables = list(right_hand_side_variables)
        remaining_targets = list(target_nodes)
        remaining_values = list(value_nodes)  # May contain duplicates

        def visit(target, value):
            label = LabelVisitor()
            label.visit(target)
            rhs_visitor = RHSVisitor()
            rhs_visitor.visit(value)
            if isinstance(value, ast.Call):
                new_ast_node = ast.Assign(target, value)
                ast.copy_location(new_ast_node, target)
                new_assignment_nodes.append(
                    self.assignment_call_node(label.result, new_ast_node)
                )
            else:
                label.result += " = "
                label.visit(value)
                new_assignment_nodes.append(
                    self.append_node(
                        AssignmentNode(
                            label.result,
                            extract_left_hand_side(target),
                            ast.Assign(target, value),
                            rhs_visitor.result,
                            line_number=target.lineno,
                            path=self.filenames[-1],
                        )
                    )
                )
            remaining_targets.remove(target)
            remaining_values.remove(value)
            for var in rhs_visitor.result:
                remaining_variables.remove(var)

        # Pair targets and values until a Starred node is reached
        for target, value in zip(target_nodes, value_nodes):
            if isinstance(target, ast.Starred) or isinstance(value, ast.Starred):
                break
            visit(target, value)

        # If there was a Starred node, pair remaining targets and values from the end
        for target, value in zip(
            reversed(list(remaining_targets)), reversed(list(remaining_values))
        ):
            if isinstance(target, ast.Starred) or isinstance(value, ast.Starred):
                break
            visit(target, value)

        if remaining_targets:
            label = LabelVisitor()
            label.handle_comma_separated(remaining_targets)
            label.result += " = "
            label.handle_comma_separated(remaining_values)
            for target in remaining_targets:
                new_assignment_nodes.append(
                    self.append_node(
                        AssignmentNode(
                            label.result,
                            extract_left_hand_side(target),
                            ast.Assign(target, remaining_values[0]),
                            remaining_variables,
                            line_number=target.lineno,
                            path=self.filenames[-1],
                        )
                    )
                )

        connect_nodes(new_assignment_nodes)
        return ControlFlowNode(
            new_assignment_nodes[0], [new_assignment_nodes[-1]], []
        )  # return the last added node