Exemplo n.º 1
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.º 2
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.º 3
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.º 4
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.º 5
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.º 6
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.º 7
0
 def transform_isinstance(self, node, results):
     x = results["x"].clone() # The thing inside of type()
     T = results["T"].clone() # The type being compared against
     x.set_prefix("")
     T.set_prefix(" ")
     test = Call(Name("isinstance"), [x, Comma(), T])
     if "n" in results:
         test.set_prefix(" ")
         test = Node(syms.not_test, [Name("not"), test])
     test.set_prefix(node.get_prefix())
     return test
Exemplo n.º 8
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])