Example #1
0
    def can_use_c_for(self, node):
        """
        Check if a for loop can use classic C syntax.

        To use C syntax:
            - target should not be assign in the loop
            - xrange should be use as iterator
            - order have to be known at compile time
        """
        assert isinstance(node.target, ast.Name)
        pattern = ast.Call(func=ast.Attribute(value=ast.Name(id='__builtin__',
                                                             ctx=ast.Load(),
                                                             annotation=None),
                                              attr='xrange',
                                              ctx=ast.Load()),
                           args=AST_any(),
                           keywords=[])
        is_assigned = {node.target.id: False}
        [
            is_assigned.update(self.passmanager.gather(IsAssigned, stmt))
            for stmt in node.body
        ]

        if (node.iter not in ASTMatcher(pattern).search(node.iter)
                or is_assigned[node.target.id]):
            return False

        args = node.iter.args
        if len(args) < 3:
            return True
        if isinstance(args[2], ast.Num):
            return True
        return False
Example #2
0
    def can_use_c_for(self, node):
        """
        Check if a for loop can use classic C syntax.

        To use C syntax:
            - target should not be assign in the loop
            - range should be use as iterator
            - order have to be known at compile time
        """
        assert isinstance(node.target, ast.Name)
        pattern_range = ast.Call(func=ast.Attribute(value=ast.Name(
            'builtins', ast.Load(), None, None),
                                                    attr='range',
                                                    ctx=ast.Load()),
                                 args=AST_any(),
                                 keywords=[])
        is_assigned = set()
        for stmt in node.body:
            is_assigned.update({n.id for n in self.gather(IsAssigned, stmt)})

        nodes = ASTMatcher(pattern_range).search(node.iter)
        if node.iter not in nodes or node.target.id in is_assigned:
            return False

        args = node.iter.args
        if len(args) < 3:
            return True
        if isnum(args[2]):
            return True
        return False
Example #3
0
    def can_use_c_for(self, node):
        """
        Check if a for loop can use classic C syntax.

        To use C syntax:
            - target should not be assign in the loop
            - xrange should be use as iterator
            - order have to be known at compile time
        """
        assert isinstance(node.target, ast.Name)
        if sys.version_info.major == 3:
            range_name = 'range'
        else:
            range_name = 'xrange'
        pattern_range = ast.Call(func=ast.Attribute(value=ast.Name(
            id='__builtin__', ctx=ast.Load(), annotation=None),
                                                    attr=range_name,
                                                    ctx=ast.Load()),
                                 args=AST_any(),
                                 keywords=[])
        is_assigned = set()
        for stmt in node.body:
            is_assigned.update(self.gather(IsAssigned, stmt))

        nodes = ASTMatcher(pattern_range).search(node.iter)
        if node.iter not in nodes or node.target.id in is_assigned:
            return False

        args = node.iter.args
        if len(args) < 3:
            return True
        if isinstance(args[2], ast.Num):
            return True
        return False