Exemplo n.º 1
0
def RaisesOp(context, exceptionClass, indent, kws, arglist, node):
    with_item = Call(Name(context), [exceptionClass])
    with_item.prefix = " "
    args = []
    arglist = [a.clone() for a in arglist.children[4:]]
    if arglist:
        arglist[0].prefix = ""

    func = None

    # :fixme: this uses hardcoded parameter names, which may change
    if 'callableObj' in kws:
        func = kws['callableObj']
    elif 'callable_obj' in kws:
        func = kws['callable_obj']
    elif kws['args']:  # any arguments assigned to `*args`
        func = kws['args'][0]
    else:
        func = None

    if func is None:
        # Context manager
        return Node(syms.with_stmt, [with_item])

    if func.type == syms.lambdef:
        suite = func.children[-1].clone()
    else:
        suite = Call(func, arglist)

    suite.prefix = indent + (4 * " ")
    return Node(
        syms.with_stmt,
        [Name('with'), with_item,
         Name(':'), Newline(), suite])
Exemplo n.º 2
0
def RaisesOp(context, exceptionClass, indent, kws, arglist):
    with_item = Call(Name(context), [exceptionClass])
    with_item.prefix = " "
    args = []
    arglist = [a.clone() for a in arglist.children[4:]]
    if arglist:
        arglist[0].prefix=""

    func = None

    # :fixme: this uses hardcoded parameter names, which may change
    if 'callableObj' in kws:
        func = kws['callableObj']
    elif 'callable_obj' in kws:
        func = kws['callable_obj']
    elif kws['args']: # any arguments assigned to `*args`
        func = kws['args'][0]
    else:
        raise NotImplementedError('with %s is not implemented' % context)

    if func is unittest.case._sentinel:
        # with self.assertRaises(SomeException):
        return Node(syms.with_stmt,
                    [with_item])

    suite = Call(func, arglist)

    suite.prefix = indent + (4 * " ")
    return Node(syms.with_stmt,
                [Name('with'),
                 with_item,
                 Name(':'),
                 Newline(),
                 suite])
Exemplo n.º 3
0
    def transform(self, node, results):
        head = results["head"]
        method = results["method"][0]  # Extract node for method name
        tail = results["tail"]
        syms = self.syms
        method_name = method.value
        isiter = method_name.startswith(u"iter")
        isview = method_name.startswith(u"view")
        head = [n.clone() for n in head]
        tail = [n.clone() for n in tail]
        # no changes neccessary if the call is in a special context
        special = not tail and self.in_special_context(node, isiter)
        new = pytree.Node(syms.power, head)
        new.prefix = u""
        if isiter or isview:
            # replace the method with the six function
            # e.g. d.iteritems() -> from six import iteritems\n iteritems(d)
            new = Call(Name(method_name), [new])
            touch_import_top('six', method_name, node)
        elif special:
            # it is not neccessary to change this case
            return node
        elif method_name in ("items", "values"):
            # ensure to return a list in python 3
            new = Call(Name(u"list" + method_name), [new])
            touch_import_top('future.utils', 'list' + method_name, node)
        else:
            # method_name is "keys"; removed it and cast the dict to list
            new = Call(Name(u"list"), [new])

        if tail:
            new = pytree.Node(syms.power, [new] + tail)
        new.prefix = node.prefix
        return new
def RaisesOp(context, exceptionClass, indent, kws, arglist, node):
    exceptionClass.prefix = ""
    args = [exceptionClass]
    # Add match keyword arg to with statement if an expected regex was provided.
    # In py27 the keyword is `expected_regexp`, in py3 is `expected_regex`
    if 'expected_regex' in kws or 'expected_regexp' in kws:
        expected_regex = kws.get('expected_regex',
                                 kws.get('expected_regexp')).clone()
        expected_regex.prefix = ''
        args.append(String(', '))
        args.append(KeywordArg(Name('match'), expected_regex))
    with_item = Call(Name(context), args)
    with_item.prefix = " "
    args = []
    arglist = [a.clone() for a in arglist.children[4:]]
    if arglist:
        arglist[0].prefix = ""

    func = None

    # :fixme: this uses hardcoded parameter names, which may change
    if 'callableObj' in kws:
        func = kws['callableObj']
    elif 'callable_obj' in kws:
        func = kws['callable_obj']
    elif kws['args']:  # any arguments assigned to `*args`
        func = kws['args'][0]
    else:
        func = None

    if func is None:
        # Context manager
        return Node(syms.with_stmt, [with_item])

    if func.type == syms.lambdef:
        suite = func.children[-1].clone()
    else:
        # TODO: Newlines within arguments are not handled yet.
        # If argment prefix contains a newline, all whitespace around this
        # ought to be replaced by indent plus 4+1+len(func) spaces.
        suite = Call(func, arglist)

    suite.prefix = indent + (4 * " ")
    return Node(
        syms.with_stmt,
        [Name('with'), with_item,
         Name(':'), Newline(), suite])
Exemplo n.º 5
0
 def transform(self, node, results):
     print_stmt = results['string']
     print_stmt.prefix = ''
     
     raise_stmt = Call(Name("six.print_"), 
                       [String(print_stmt)])
     raise_stmt.prefix = node.prefix
     print(raise_stmt)
     return raise_stmt
Exemplo n.º 6
0
    def transform(self, node, results):
        syms = self.syms
        
        exc = results['exc'].clone() if 'exc' in results else None
        val = results['val'].clone() if 'val' in results else None
        tb = results['tb'].clone() if 'tb' in results else None

        exc.prefix = ' '

        #Change Error() to Error, if there are no arguments to pass in.
        try:
            if len(exc.children[1].children) == 2:
                exc = String(exc.children[0])
        except IndexError:
            pass
        
        if tb and val:
            exc.prefix = ''
            args = [exc]

            args.append(String(", "))
            args.append(val)
                
            args.append(String(", "))
            args.append(tb)
            
            raise_stmt = Call(Name("six.reraise"), args)
            raise_stmt.prefix = node.prefix
            return raise_stmt
        elif val:
            val.prefix = ''
            val = [val]
            raise_stmt = pytree.Node(syms.raise_stmt,
                                     [Name("raise"), Call(exc, val)])
        else:
            raise_stmt = pytree.Node(syms.raise_stmt, 
                                     [Name("raise"), String(exc)])
            
        raise_stmt.prefix = node.prefix
        return raise_stmt
Exemplo n.º 7
0
def RaisesOp(context, exceptionClass, indent, kws, arglist):
    with_item = Call(Name(context), [exceptionClass])
    with_item.prefix = " "
    args = []
    arglist = [a.clone() for a in arglist.children[4:]]
    if arglist:
        arglist[0].prefix=""
    # :fixme: this uses hardcoded parameter names, which may change
    if 'callableObj' in kws:
        suite = Call(kws['callableObj'], arglist)
    elif 'callable_obj' in kws:
        suite = Call(kws['callable_obj'], arglist)
    elif kws['args']: # any arguments assigned to `*args`
        suite = Call(kws['args'][0], arglist)
    else:
        raise NotImplementedError('with %s is not implemented' % context)
    suite.prefix = indent + (4 * " ")
    return Node(syms.with_stmt,
                [Name('with'),
                 with_item,
                 Name(':'),
                 Newline(),
                 suite])
Exemplo n.º 8
0
def RaisesOp(context, exceptionClass, indent, kws, arglist, node):
    with_item = Call(Name(context), [exceptionClass])
    with_item.prefix = " "
    args = []
    arglist = [a.clone() for a in arglist.children[4:]]
    if arglist:
        arglist[0].prefix=""

    func = None

    # :fixme: this uses hardcoded parameter names, which may change
    if 'callableObj' in kws:
        func = kws['callableObj']
    elif 'callable_obj' in kws:
        func = kws['callable_obj']
    elif kws['args']:  # any arguments assigned to `*args`
        func = kws['args'][0]
    else:
        func = None

    if func is None:
        # Context manager
        return Node(syms.with_stmt, [with_item])

    if func.type == syms.lambdef:
        suite = func.children[-1].clone()
    else:
        suite = Call(func, arglist)

    suite.prefix = indent + (4 * " ")
    return Node(syms.with_stmt,
                [Name('with'),
                 with_item,
                 Name(':'),
                 Newline(),
                 suite])
Exemplo n.º 9
0
    def transform(self, node, results):
        assert results

        bare_print = results.get("bare")

        if bare_print:
            # Special-case print all by itself.
            bare_print.replace(
                Call(Name(u"print"), [], prefix=bare_print.prefix))
            # The "from __future__ import print_function"" declaration is added
            # by the fix_print_with_import fixer, so we skip it here.
            # add_future(node, u'print_function')
            return
        assert node.children[0] == Name(u"print")
        args = node.children[1:]
        if len(args) == 1 and parend_expr.match(args[0]):
            # We don't want to keep sticking parens around an
            # already-parenthesised expression.
            return

        sep = end = file = None
        if args and args[-1] == Comma():
            args = args[:-1]
            end = " "
        if args and args[0] == pytree.Leaf(token.RIGHTSHIFT, u">>"):
            assert len(args) >= 2
            file = args[1].clone()
            args = args[3:]  # Strip a possible comma after the file expression
        # Now synthesize a print(args, sep=..., end=..., file=...) node.
        l_args = [arg.clone() for arg in args]
        if l_args:
            l_args[0].prefix = u""
        if sep is not None or end is not None or file is not None:
            if sep is not None:
                self.add_kwarg(l_args, u"sep", String(repr(sep)))
            if end is not None:
                self.add_kwarg(l_args, u"end", String(repr(end)))
            if file is not None:
                self.add_kwarg(l_args, u"file", file)
        n_stmt = Call(Name(u"print"), l_args)
        n_stmt.prefix = node.prefix

        # Note that there are corner cases where adding this future-import is
        # incorrect, for example when the file also has a 'print ()' statement
        # that was intended to print "()".
        # add_future(node, u'print_function')
        return n_stmt
Exemplo n.º 10
0
    def transform(self, node, results):
        assert results

        bare_print = results.get("bare")

        if bare_print:
            # Special-case print all by itself.
            bare_print.replace(Call(Name(u"print"), [],
                               prefix=bare_print.prefix))
            # The "from __future__ import print_function"" declaration is added
            # by the fix_print_with_import fixer, so we skip it here.
            # add_future(node, u'print_function')
            return
        assert node.children[0] == Name(u"print")
        args = node.children[1:]
        if len(args) == 1 and parend_expr.match(args[0]):
            # We don't want to keep sticking parens around an
            # already-parenthesised expression.
            return

        sep = end = file = None
        if args and args[-1] == Comma():
            args = args[:-1]
            end = " "
        if args and args[0] == pytree.Leaf(token.RIGHTSHIFT, u">>"):
            assert len(args) >= 2
            file = args[1].clone()
            args = args[3:] # Strip a possible comma after the file expression
        # Now synthesize a print(args, sep=..., end=..., file=...) node.
        l_args = [arg.clone() for arg in args]
        if l_args:
            l_args[0].prefix = u""
        if sep is not None or end is not None or file is not None:
            if sep is not None:
                self.add_kwarg(l_args, u"sep", String(repr(sep)))
            if end is not None:
                self.add_kwarg(l_args, u"end", String(repr(end)))
            if file is not None:
                self.add_kwarg(l_args, u"file", file)
        n_stmt = Call(Name(u"print"), l_args)
        n_stmt.prefix = node.prefix

        # Note that there are corner cases where adding this future-import is
        # incorrect, for example when the file also has a 'print ()' statement
        # that was intended to print "()".
        # add_future(node, u'print_function')
        return n_stmt
Exemplo n.º 11
0
    def transform(self, node, results):
        assert results

        bare_print = results.get('bare')

        if bare_print:
            # Special-case print all by itself
            bare_print.replace(Call(Name('print'), [],
                               prefix=bare_print.prefix))
            return
        assert node.children[0] == Name('print')
        args = node.children[1:]
        if len(args) == 1 and parend_expr.match(args[0]):
            # We don't want to keep sticking parens around an
            # already-parenthesised expression.
            return

        sep = end = file = None
        if args and args[-1] == Comma():
            args = args[:-1]
            end = ' '
        if args and args[0] == pytree.Leaf(token.RIGHTSHIFT, '>>'):
            assert len(args) >= 2
            file = args[1].clone()
            args = args[3:]  # Strip a possible comma after the file expression
        # Now synthesize a print(args, sep=..., end=..., file=...) node.
        l_args = [arg.clone() for arg in args]
        if l_args:
            l_args[0].prefix = ''
        if sep is not None or end is not None or file is not None:
            if sep is not None:
                self.add_kwarg(l_args, 'sep', String(repr(sep)))
            if end is not None:
                self.add_kwarg(l_args, 'end', String(repr(end)))
            if file is not None:
                self.add_kwarg(l_args, 'file', file)
        n_stmt = Call(Name('print'), l_args)
        n_stmt.prefix = node.prefix

        self.found_print = True
        return n_stmt
Exemplo n.º 12
0
    def transform(self, node, results):
        assert results

        bare_print = results.get("bare")

        if bare_print:
            # Special-case print all by itself
            bare_print.replace(
                Call(Name(u"print"), [], prefix=bare_print.prefix))
            return
        assert node.children[0] == Name(u"print")
        args = node.children[1:]
        if len(args) == 1 and parend_expr.match(args[0]):
            # We don't want to keep sticking parens around an
            # already-parenthesised expression.
            return

        sep = end = file = None
        if args and args[-1] == Comma():
            args = args[:-1]
            end = " "
        if args and args[0] == pytree.Leaf(token.RIGHTSHIFT, u">>"):
            assert len(args) >= 2
            file = args[1].clone()
            args = args[3:]  # Strip a possible comma after the file expression
        # Now synthesize a print(args, sep=..., end=..., file=...) node.
        l_args = [arg.clone() for arg in args]
        if l_args:
            l_args[0].prefix = u""
        if sep is not None or end is not None or file is not None:
            if sep is not None:
                self.add_kwarg(l_args, u"sep", String(repr(sep)))
            if end is not None:
                self.add_kwarg(l_args, u"end", String(repr(end)))
            if file is not None:
                self.add_kwarg(l_args, u"file", file)
        n_stmt = Call(Name(u"print"), l_args)
        n_stmt.prefix = node.prefix
        add_future(node, u'print_function')
        return n_stmt
Exemplo n.º 13
0
    def transform(self, node, results):
        assert results

        bare_print = results.get("bare")

        if bare_print:
            # Special-case print all by itself
            bare_print.replace(Call(Name(u"print"), [],
                               prefix=bare_print.prefix))
            return
        assert node.children[0] == Name(u"print")
        args = node.children[1:]
        if len(args) == 1 and parend_expr.match(args[0]):
            # We don't want to keep sticking parens around an
            # already-parenthesised expression.
            return

        sep = end = file = None
        if args and args[-1] == Comma():
            args = args[:-1]
            end = " "
        if args and args[0] == pytree.Leaf(token.RIGHTSHIFT, u">>"):
            assert len(args) >= 2
            file = args[1].clone()
            args = args[3:] # Strip a possible comma after the file expression
        # Now synthesize a print(args, sep=..., end=..., file=...) node.
        l_args = [arg.clone() for arg in args]
        if l_args:
            l_args[0].prefix = u""
        if sep is not None or end is not None or file is not None:
            if sep is not None:
                self.add_kwarg(l_args, u"sep", String(repr(sep)))
            if end is not None:
                self.add_kwarg(l_args, u"end", String(repr(end)))
            if file is not None:
                self.add_kwarg(l_args, u"file", file)
        n_stmt = Call(Name(u"print"), l_args)
        n_stmt.prefix = node.prefix
        touch_import(u'__future__', u'print_function', node)
        return n_stmt
Exemplo n.º 14
0
 def transform(self, node, results):
     assert results
     syms = self.syms
     anchor = results["anchor"]
     # There are 2 types of nodes in the AST - Node and Leaf.
     # Leaf nodes have a prefix and suffix that are meant for whitespaces and comments.
     # It usually suffices to use the prefix only as the prefix of a node is the suffix
     # of its previous node.
     prefix = node.prefix
     # before is the identifier that precedes the '.' before the 'None'.
     before = [n.clone() for n in results["before"]]
     if len(before) == 1:
         before = before[0]
     else:
         before = pytree.Node(syms.power, before)
     noneKeywd = String(repr("None"))
     l_args = [before, Comma(), noneKeywd]
     if l_args:
         l_args[0].prefix = u""
         l_args[2].prefix = u" "
     new = Call(Name(u"getattr"), l_args)
     new.prefix = prefix
     return new
Exemplo n.º 15
0
    def transform(self, node, results):
        syms = self.syms

        exc = results["exc"].clone()
        if exc.type == token.STRING:
            msg = "Python 3 does not support string exceptions"
            self.cannot_convert(node, msg)
            return

        # Python 2 supports
        #  raise ((((E1, E2), E3), E4), E5), V
        # as a synonym for
        #  raise E1, V
        # Since Python 3 will not support this, we recurse down any tuple
        # literals, always taking the first element.
        if is_tuple(exc):
            while is_tuple(exc):
                # exc.children[1:-1] is the unparenthesized tuple
                # exc.children[1].children[0] is the first element of the tuple
                exc = exc.children[1].children[0].clone()
            exc.prefix = u" "

        if "tb" in results:
            tb = results["tb"].clone()
        else:
            tb = None

        if "val" in results:
            val = results["val"].clone()
            if is_tuple(val):
                # Assume that exc is a subclass of Exception and call exc(*val).
                args = [c.clone() for c in val.children[1:-1]]
                exc = Call(exc, args)
            elif val.type in (token.NUMBER, token.STRING):
                # Handle numeric and string literals specially, e.g.
                # "raise Exception, 5" -> "raise Exception(5)".
                val.prefix = u""
                exc = Call(exc, [val])
            elif val.type == token.NAME and val.value == u"None":
                # Handle None specially, e.g.
                # "raise Exception, None" -> "raise Exception".
                pass
            else:
                # val is some other expression. If val evaluates to an instance
                # of exc, it should just be raised. If val evaluates to None,
                # a default instance of exc should be raised (as above). If val
                # evaluates to a tuple, exc(*val) should be called (as
                # above). Otherwise, exc(val) should be called. We can only
                # tell what to do at runtime, so defer to future.utils.raise_(),
                # which handles all of these cases.
                touch_import_top(u"future.utils", u"raise_", node)
                exc.prefix = u""
                args = [exc, Comma(), val]
                if tb is not None:
                    args += [Comma(), tb]
                return Call(Name(u"raise_"), args)

        if tb is not None:
            tb.prefix = ""
            exc_list = Attr(exc, Name('with_traceback')) + [ArgList([tb])]
        else:
            exc_list = [exc]

        return pytree.Node(syms.raise_stmt, [Name(u"raise")] + exc_list,
                           prefix=node.prefix)
Exemplo n.º 16
0
    def transform(self, node: Node, results):
        if "imp" in results:
            return self._handle_import(node, results)
        else:
            if "buildin" in results:
                method_name = results["buildin"].value
                return self._handle_buildin(node, method_name)

            if isinstance(results['method'], Leaf):
                method_name = results["method"].value
            else:
                method_name = results["method"][0].value

            if "obj" not in results:
                # Prefix: Path.
                return self._handle_no_args(node, method_name)
            else:
                obj = results["obj"]
                argname = obj.clone()
                if "submod" in results:
                    if method_name == "join" and len(argname.children) >= 1:
                        first_arg, remaining_args = self._split_arguments(
                            argname)

                        x = Call(Name("Path"), first_arg, prefix=node.prefix)

                        if len(remaining_args) > 0 and all(
                                a.type in [token.COMMA, token.STRING]
                                for a in remaining_args):
                            if str(remaining_args[0].value).startswith('*'):
                                x.append_child(
                                    Call(Name('joinpath', prefix=""),
                                         remaining_args,
                                         prefix="."))
                                return x
                            x.append_child(Slash)

                            for i, e in enumerate(remaining_args):
                                if isinstance(e.value, Node):
                                    val = e.value
                                elif isinstance(e.value, Leaf):
                                    val = e.value.value
                                else:
                                    continue

                                if e.type == token.STRING and val != ",":
                                    # if self.split_strings and "/" in e.value:
                                    #     # TODO: get more robust e.value without quotes
                                    #     parts = re.split('(/|\\\\)', e.value[1:-1])
                                    #     for part in parts:
                                    #         if part in ["/", "\\"]:
                                    #             x.append_child(Slash)
                                    #         else:
                                    #             x.append_child(String('"{}"'.format(part), prefix=" "))
                                    # else:
                                    if i < 1:
                                        p = " "
                                    else:
                                        p = ""
                                    x.append_child(String(e, prefix=p))
                                else:
                                    x.append_child(Slash)

                        return x
                    else:
                        first_arg, remaining_args = self._split_arguments(
                            argname)

                        x = Call(Name("Path"), first_arg, prefix=node.prefix)

                        new_names = {
                            "isabs": "is_absolute",
                            "isdir": "is_dir",
                            "isfile": "is_file",
                            "islink": "is_symlink",
                            "abspath": "resolve",
                            "realpath": "resolve",
                            "normpath": "resolve",
                            "same": "samefile",
                        }
                        new_attribs = {
                            "basename": "name",
                            "dirname": "parent",
                            "getsize": "stat().st_size",
                        }
                        if method_name in new_names:
                            x.append_child(
                                Call(Name(new_names[method_name], prefix=""),
                                     remaining_args,
                                     prefix="."))
                        elif method_name in new_attribs:
                            x.append_child(
                                String(new_attribs[method_name], prefix="."))
                        else:
                            x.append_child(
                                Call(Name(method_name, prefix=""),
                                     remaining_args,
                                     prefix="."))
                        return x
                else:
                    arglist = argname
                    first_arg, remaining_args = self._split_arguments(arglist)

                    x = Call(Name("Path"), first_arg, prefix=node.prefix)
                    if method_name == "remove":
                        method_name = "unlink"

                    if method_name == "listdir":
                        x.append_child(String('glob("*")', prefix="."))
                        x.prefix = ""
                        return Call(Name('list'), [x], prefix=node.prefix)
                    elif method_name == 'makedirs':
                        if len(remaining_args) > 0:
                            children = [
                                Leaf(12, ','),
                                Leaf(1, 'parents', prefix=' '),
                                Leaf(22, '='),
                                Leaf(3, 'True')
                            ]

                            remaining_args += [
                                Node(type=260, children=children)
                            ]
                        else:
                            remaining_args = [
                                Leaf(1, 'parents', prefix=''),
                                Leaf(22, '='),
                                Leaf(3, 'True')
                            ]

                        x.append_child(
                            Call(Name("mkdir"), remaining_args, prefix="."))
                    else:
                        x.append_child(
                            Call(Name(method_name), remaining_args,
                                 prefix="."))
                    return x