Exemplo n.º 1
0
 def visit_slice(self, node, parent):
     """visit a Slice node by returning a fresh instance of it"""
     newnode = nodes.Slice(parent=parent)
     newnode.postinit(_visit_or_none(node, 'lower', self, newnode),
                      _visit_or_none(node, 'upper', self, newnode),
                      _visit_or_none(node, 'step', self, newnode))
     return newnode
Exemplo n.º 2
0
def infer_slice(node, context=None):
    """Understand `slice` calls."""
    args = node.args
    if not 0 < len(args) <= 3:
        raise UseInferenceDefault

    infer_func = partial(helpers.safe_infer, context=context)
    args = [infer_func(arg) for arg in args]
    for arg in args:
        if not arg or arg is util.Uninferable:
            raise UseInferenceDefault
        if not isinstance(arg, nodes.Const):
            raise UseInferenceDefault
        if not isinstance(arg.value, (type(None), int)):
            raise UseInferenceDefault

    if len(args) < 3:
        # Make sure we have 3 arguments.
        args.extend([None] * (3 - len(args)))

    slice_node = nodes.Slice(lineno=node.lineno,
                             col_offset=node.col_offset,
                             parent=node.parent)
    slice_node.postinit(*args)
    return slice_node
Exemplo n.º 3
0
def slice_node(draw):
    lower = draw(hs.one_of(const_node(hs.integers()), hs.none()))
    upper = draw(hs.one_of(const_node(hs.integers()), hs.none()))
    step = draw(hs.one_of(const_node(hs.integers()), hs.none()))
    node = nodes.Slice()
    node.postinit(lower, upper, step)
    return node
Exemplo n.º 4
0
def test_inference_invalid_slice(node):
    sub_node = nodes.Subscript()
    slice = nodes.Slice()
    slice.postinit(nodes.Const(0), nodes.Const("a"))
    sub_node.postinit(node, slice)
    module, _ = cs._parse_text(sub_node)
    for subscript_node in module.nodes_of_class(nodes.Subscript):
        assert isinstance(subscript_node.inf_type, TypeFail)
Exemplo n.º 5
0
 def visit_slice(self, node, parent):
     """visit a Slice node by returning a fresh instance of it"""
     newnode = new.Slice()
     newnode.parent = parent
     if node.lower is not None:
         newnode.lower = self.visit(node.lower, newnode)
     if node.upper is not None:
         newnode.upper = self.visit(node.upper, newnode)
     if node.step is not None:
         newnode.step = self.visit(node.step, newnode)
     return newnode
Exemplo n.º 6
0
 def visit_slice(self, node, parent):
     """visit a Slice node by returning a fresh instance of it"""
     newnode = new.Slice()
     _lineno_parent(node, newnode, parent)
     if node.lower is not None:
         newnode.lower = self.visit(node.lower, newnode)
     if node.upper is not None:
         newnode.upper = self.visit(node.upper, newnode)
     if node.step is not None:
         newnode.step = self.visit(node.step, newnode)
     newnode.set_line_info(newnode.last_child())
     return newnode