Mercurial > repos > shellac > guppy_basecaller
comparison env/lib/python3.7/site-packages/libfuturize/fixes/fix_print.py @ 0:26e78fe6e8c4 draft
"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author | shellac |
---|---|
date | Sat, 02 May 2020 07:14:21 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:26e78fe6e8c4 |
---|---|
1 # Copyright 2006 Google, Inc. All Rights Reserved. | |
2 # Licensed to PSF under a Contributor Agreement. | |
3 | |
4 """Fixer for print. | |
5 | |
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 | |
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 """ | |
35 | |
36 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 # The "from __future__ import print_function"" declaration is added | |
46 # by the fix_print_with_import fixer, so we skip it here. | |
47 # add_future(node, u'print_function') | |
48 return | |
49 assert node.children[0] == Name(u"print") | |
50 args = node.children[1:] | |
51 if len(args) == 1 and parend_expr.match(args[0]): | |
52 # We don't want to keep sticking parens around an | |
53 # already-parenthesised expression. | |
54 return | |
55 | |
56 sep = end = file = None | |
57 if args and args[-1] == Comma(): | |
58 args = args[:-1] | |
59 end = " " | |
60 if args and args[0] == pytree.Leaf(token.RIGHTSHIFT, u">>"): | |
61 assert len(args) >= 2 | |
62 file = args[1].clone() | |
63 args = args[3:] # Strip a possible comma after the file expression | |
64 # Now synthesize a print(args, sep=..., end=..., file=...) node. | |
65 l_args = [arg.clone() for arg in args] | |
66 if l_args: | |
67 l_args[0].prefix = u"" | |
68 if sep is not None or end is not None or file is not None: | |
69 if sep is not None: | |
70 self.add_kwarg(l_args, u"sep", String(repr(sep))) | |
71 if end is not None: | |
72 self.add_kwarg(l_args, u"end", String(repr(end))) | |
73 if file is not None: | |
74 self.add_kwarg(l_args, u"file", file) | |
75 n_stmt = Call(Name(u"print"), l_args) | |
76 n_stmt.prefix = node.prefix | |
77 | |
78 # Note that there are corner cases where adding this future-import is | |
79 # incorrect, for example when the file also has a 'print ()' statement | |
80 # that was intended to print "()". | |
81 # add_future(node, u'print_function') | |
82 return n_stmt | |
83 | |
84 def add_kwarg(self, l_nodes, s_kwd, n_expr): | |
85 # XXX All this prefix-setting may lose comments (though rarely) | |
86 n_expr.prefix = u"" | |
87 n_argument = pytree.Node(self.syms.argument, | |
88 (Name(s_kwd), | |
89 pytree.Leaf(token.EQUAL, u"="), | |
90 n_expr)) | |
91 if l_nodes: | |
92 l_nodes.append(Comma()) | |
93 n_argument.prefix = u" " | |
94 l_nodes.append(n_argument) |