1 | n/a | # Copyright 2006 Google, Inc. All Rights Reserved. |
---|
2 | n/a | # Licensed to PSF under a Contributor Agreement. |
---|
3 | n/a | |
---|
4 | n/a | """Fixer for print. |
---|
5 | n/a | |
---|
6 | n/a | Change: |
---|
7 | n/a | 'print' into 'print()' |
---|
8 | n/a | 'print ...' into 'print(...)' |
---|
9 | n/a | 'print ... ,' into 'print(..., end=" ")' |
---|
10 | n/a | 'print >>x, ...' into 'print(..., file=x)' |
---|
11 | n/a | |
---|
12 | n/a | No changes are applied if print_function is imported from __future__ |
---|
13 | n/a | |
---|
14 | n/a | """ |
---|
15 | n/a | |
---|
16 | n/a | # Local imports |
---|
17 | n/a | from .. import patcomp |
---|
18 | n/a | from .. import pytree |
---|
19 | n/a | from ..pgen2 import token |
---|
20 | n/a | from .. import fixer_base |
---|
21 | n/a | from ..fixer_util import Name, Call, Comma, String |
---|
22 | n/a | |
---|
23 | n/a | |
---|
24 | n/a | parend_expr = patcomp.compile_pattern( |
---|
25 | n/a | """atom< '(' [atom|STRING|NAME] ')' >""" |
---|
26 | n/a | ) |
---|
27 | n/a | |
---|
28 | n/a | |
---|
29 | n/a | class FixPrint(fixer_base.BaseFix): |
---|
30 | n/a | |
---|
31 | n/a | BM_compatible = True |
---|
32 | n/a | |
---|
33 | n/a | PATTERN = """ |
---|
34 | n/a | simple_stmt< any* bare='print' any* > | print_stmt |
---|
35 | n/a | """ |
---|
36 | n/a | |
---|
37 | n/a | def transform(self, node, results): |
---|
38 | n/a | assert results |
---|
39 | n/a | |
---|
40 | n/a | bare_print = results.get("bare") |
---|
41 | n/a | |
---|
42 | n/a | if bare_print: |
---|
43 | n/a | # Special-case print all by itself |
---|
44 | n/a | bare_print.replace(Call(Name("print"), [], |
---|
45 | n/a | prefix=bare_print.prefix)) |
---|
46 | n/a | return |
---|
47 | n/a | assert node.children[0] == Name("print") |
---|
48 | n/a | args = node.children[1:] |
---|
49 | n/a | if len(args) == 1 and parend_expr.match(args[0]): |
---|
50 | n/a | # We don't want to keep sticking parens around an |
---|
51 | n/a | # already-parenthesised expression. |
---|
52 | n/a | return |
---|
53 | n/a | |
---|
54 | n/a | sep = end = file = None |
---|
55 | n/a | if args and args[-1] == Comma(): |
---|
56 | n/a | args = args[:-1] |
---|
57 | n/a | end = " " |
---|
58 | n/a | if args and args[0] == pytree.Leaf(token.RIGHTSHIFT, ">>"): |
---|
59 | n/a | assert len(args) >= 2 |
---|
60 | n/a | file = args[1].clone() |
---|
61 | n/a | args = args[3:] # Strip a possible comma after the file expression |
---|
62 | n/a | # Now synthesize a print(args, sep=..., end=..., file=...) node. |
---|
63 | n/a | l_args = [arg.clone() for arg in args] |
---|
64 | n/a | if l_args: |
---|
65 | n/a | l_args[0].prefix = "" |
---|
66 | n/a | if sep is not None or end is not None or file is not None: |
---|
67 | n/a | if sep is not None: |
---|
68 | n/a | self.add_kwarg(l_args, "sep", String(repr(sep))) |
---|
69 | n/a | if end is not None: |
---|
70 | n/a | self.add_kwarg(l_args, "end", String(repr(end))) |
---|
71 | n/a | if file is not None: |
---|
72 | n/a | self.add_kwarg(l_args, "file", file) |
---|
73 | n/a | n_stmt = Call(Name("print"), l_args) |
---|
74 | n/a | n_stmt.prefix = node.prefix |
---|
75 | n/a | return n_stmt |
---|
76 | n/a | |
---|
77 | n/a | def add_kwarg(self, l_nodes, s_kwd, n_expr): |
---|
78 | n/a | # XXX All this prefix-setting may lose comments (though rarely) |
---|
79 | n/a | n_expr.prefix = "" |
---|
80 | n/a | n_argument = pytree.Node(self.syms.argument, |
---|
81 | n/a | (Name(s_kwd), |
---|
82 | n/a | pytree.Leaf(token.EQUAL, "="), |
---|
83 | n/a | n_expr)) |
---|
84 | n/a | if l_nodes: |
---|
85 | n/a | l_nodes.append(Comma()) |
---|
86 | n/a | n_argument.prefix = " " |
---|
87 | n/a | l_nodes.append(n_argument) |
---|