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