annotate Tools/parser/unparse.py @ 83447:678564fc0962 3.3

#17789: test_random now works with unittest test discovery. Patch by Zachary Ware.
author Ezio Melotti <ezio.melotti@gmail.com>
date Fri, 19 Apr 2013 05:45:27 +0300
parents 5b71f5891c54
children 03f92a9f0875
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
1 "Usage: unparse.py <path to source file>"
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
2 import sys
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
3 import ast
62621
a4e1c56be532 unparse.py: respect coding cookie in input files
Mark Dickinson <dickinsm@gmail.com>
parents: 62614
diff changeset
4 import tokenize
42304
7d16b42fcdbd Run 2to3 over the Demo/ directory to shut up parse errors from 2to3 about lingering print statements.
Collin Winter <collinw@gmail.com>
parents: 41554
diff changeset
5 import io
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
6 import os
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
7
62651
043ef04f64d2 Unparse infinite imaginary literals correctly. Add some more numeric tests.
Mark Dickinson <dickinsm@gmail.com>
parents: 62650
diff changeset
8 # Large float and imaginary literals get turned into infinities in the AST.
043ef04f64d2 Unparse infinite imaginary literals correctly. Add some more numeric tests.
Mark Dickinson <dickinsm@gmail.com>
parents: 62650
diff changeset
9 # We unparse those infinities to INFSTR.
043ef04f64d2 Unparse infinite imaginary literals correctly. Add some more numeric tests.
Mark Dickinson <dickinsm@gmail.com>
parents: 62650
diff changeset
10 INFSTR = "1e" + repr(sys.float_info.max_10_exp + 1)
043ef04f64d2 Unparse infinite imaginary literals correctly. Add some more numeric tests.
Mark Dickinson <dickinsm@gmail.com>
parents: 62650
diff changeset
11
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
12 def interleave(inter, f, seq):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
13 """Call f on each item in seq, calling inter() in between.
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
14 """
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
15 seq = iter(seq)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
16 try:
42304
7d16b42fcdbd Run 2to3 over the Demo/ directory to shut up parse errors from 2to3 about lingering print statements.
Collin Winter <collinw@gmail.com>
parents: 41554
diff changeset
17 f(next(seq))
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
18 except StopIteration:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
19 pass
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
20 else:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
21 for x in seq:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
22 inter()
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
23 f(x)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
24
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
25 class Unparser:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
26 """Methods in this class recursively traverse an AST and
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
27 output source code for the abstract syntax; original formatting
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
28 is disregarded. """
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
29
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
30 def __init__(self, tree, file = sys.stdout):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
31 """Unparser(tree, file=sys.stdout) -> None.
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
32 Print the source for tree to file."""
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
33 self.f = file
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
34 self._indent = 0
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
35 self.dispatch(tree)
42304
7d16b42fcdbd Run 2to3 over the Demo/ directory to shut up parse errors from 2to3 about lingering print statements.
Collin Winter <collinw@gmail.com>
parents: 41554
diff changeset
36 print("", file=self.f)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
37 self.f.flush()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
38
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
39 def fill(self, text = ""):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
40 "Indent a piece of text, according to the current indentation level"
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
41 self.f.write("\n"+" "*self._indent + text)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
42
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
43 def write(self, text):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
44 "Append a piece of text to the current line."
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
45 self.f.write(text)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
46
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
47 def enter(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
48 "Print ':', and increase the indentation."
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
49 self.write(":")
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
50 self._indent += 1
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
51
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
52 def leave(self):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
53 "Decrease the indentation level."
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
54 self._indent -= 1
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
55
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
56 def dispatch(self, tree):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
57 "Dispatcher function, dispatching tree type T to method _T."
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
58 if isinstance(tree, list):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
59 for t in tree:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
60 self.dispatch(t)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
61 return
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
62 meth = getattr(self, "_"+tree.__class__.__name__)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
63 meth(tree)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
64
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
65
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
66 ############### Unparsing methods ######################
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
67 # There should be one method per concrete grammar type #
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
68 # Constructors should be grouped by sum type. Ideally, #
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
69 # this would follow the order in the grammar, but #
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
70 # currently doesn't. #
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
71 ########################################################
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
72
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
73 def _Module(self, tree):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
74 for stmt in tree.body:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
75 self.dispatch(stmt)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
76
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
77 # stmt
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
78 def _Expr(self, tree):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
79 self.fill()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
80 self.dispatch(tree.value)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
81
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
82 def _Import(self, t):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
83 self.fill("import ")
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
84 interleave(lambda: self.write(", "), self.dispatch, t.names)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
85
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
86 def _ImportFrom(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
87 self.fill("from ")
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
88 self.write("." * t.level)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
89 if t.module:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
90 self.write(t.module)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
91 self.write(" import ")
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
92 interleave(lambda: self.write(", "), self.dispatch, t.names)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
93
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
94 def _Assign(self, t):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
95 self.fill()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
96 for target in t.targets:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
97 self.dispatch(target)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
98 self.write(" = ")
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
99 self.dispatch(t.value)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
100
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
101 def _AugAssign(self, t):
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
102 self.fill()
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
103 self.dispatch(t.target)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
104 self.write(" "+self.binop[t.op.__class__.__name__]+"= ")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
105 self.dispatch(t.value)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
106
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
107 def _Return(self, t):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
108 self.fill("return")
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
109 if t.value:
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
110 self.write(" ")
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
111 self.dispatch(t.value)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
112
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
113 def _Pass(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
114 self.fill("pass")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
115
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
116 def _Break(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
117 self.fill("break")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
118
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
119 def _Continue(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
120 self.fill("continue")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
121
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
122 def _Delete(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
123 self.fill("del ")
62609
68b670f3614e Merged revisions 82338,82340-82341 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 48771
diff changeset
124 interleave(lambda: self.write(", "), self.dispatch, t.targets)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
125
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
126 def _Assert(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
127 self.fill("assert ")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
128 self.dispatch(t.test)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
129 if t.msg:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
130 self.write(", ")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
131 self.dispatch(t.msg)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
132
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
133 def _Global(self, t):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
134 self.fill("global ")
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
135 interleave(lambda: self.write(", "), self.write, t.names)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
136
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
137 def _Nonlocal(self, t):
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
138 self.fill("nonlocal ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
139 interleave(lambda: self.write(", "), self.write, t.names)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
140
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
141 def _Yield(self, t):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
142 self.write("(")
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
143 self.write("yield")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
144 if t.value:
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
145 self.write(" ")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
146 self.dispatch(t.value)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
147 self.write(")")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
148
76803
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
149 def _YieldFrom(self, t):
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
150 self.write("(")
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
151 self.write("yield from")
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
152 if t.value:
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
153 self.write(" ")
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
154 self.dispatch(t.value)
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
155 self.write(")")
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
156
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
157 def _Raise(self, t):
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
158 self.fill("raise")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
159 if not t.exc:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
160 assert not t.cause
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
161 return
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
162 self.write(" ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
163 self.dispatch(t.exc)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
164 if t.cause:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
165 self.write(" from ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
166 self.dispatch(t.cause)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
167
76803
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
168 def _Try(self, t):
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
169 self.fill("try")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
170 self.enter()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
171 self.dispatch(t.body)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
172 self.leave()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
173 for ex in t.handlers:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
174 self.dispatch(ex)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
175 if t.orelse:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
176 self.fill("else")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
177 self.enter()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
178 self.dispatch(t.orelse)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
179 self.leave()
76803
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
180 if t.finalbody:
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
181 self.fill("finally")
62644
e6bb990ac9f6 Output try-except-finally statements where appropriate.
Mark Dickinson <dickinsm@gmail.com>
parents: 62643
diff changeset
182 self.enter()
76803
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
183 self.dispatch(t.finalbody)
62644
e6bb990ac9f6 Output try-except-finally statements where appropriate.
Mark Dickinson <dickinsm@gmail.com>
parents: 62643
diff changeset
184 self.leave()
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
185
48771
ea3d13cd7c1e Merged revisions 65780,65782,65785,65809,65812,65834,65846,65859,65861 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 42304
diff changeset
186 def _ExceptHandler(self, t):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
187 self.fill("except")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
188 if t.type:
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
189 self.write(" ")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
190 self.dispatch(t.type)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
191 if t.name:
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
192 self.write(" as ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
193 self.write(t.name)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
194 self.enter()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
195 self.dispatch(t.body)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
196 self.leave()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
197
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
198 def _ClassDef(self, t):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
199 self.write("\n")
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
200 for deco in t.decorator_list:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
201 self.fill("@")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
202 self.dispatch(deco)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
203 self.fill("class "+t.name)
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
204 self.write("(")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
205 comma = False
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
206 for e in t.bases:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
207 if comma: self.write(", ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
208 else: comma = True
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
209 self.dispatch(e)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
210 for e in t.keywords:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
211 if comma: self.write(", ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
212 else: comma = True
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
213 self.dispatch(e)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
214 if t.starargs:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
215 if comma: self.write(", ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
216 else: comma = True
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
217 self.write("*")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
218 self.dispatch(t.starargs)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
219 if t.kwargs:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
220 if comma: self.write(", ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
221 else: comma = True
62637
158166ef6f06 Fix typo in unparsing of a class definition.
Mark Dickinson <dickinsm@gmail.com>
parents: 62623
diff changeset
222 self.write("**")
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
223 self.dispatch(t.kwargs)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
224 self.write(")")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
225
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
226 self.enter()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
227 self.dispatch(t.body)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
228 self.leave()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
229
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
230 def _FunctionDef(self, t):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
231 self.write("\n")
48771
ea3d13cd7c1e Merged revisions 65780,65782,65785,65809,65812,65834,65846,65859,65861 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 42304
diff changeset
232 for deco in t.decorator_list:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
233 self.fill("@")
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39782
diff changeset
234 self.dispatch(deco)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
235 self.fill("def "+t.name + "(")
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
236 self.dispatch(t.args)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
237 self.write(")")
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
238 if t.returns:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
239 self.write(" -> ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
240 self.dispatch(t.returns)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
241 self.enter()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
242 self.dispatch(t.body)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
243 self.leave()
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
244
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
245 def _For(self, t):
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
246 self.fill("for ")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
247 self.dispatch(t.target)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
248 self.write(" in ")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
249 self.dispatch(t.iter)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
250 self.enter()
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
251 self.dispatch(t.body)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
252 self.leave()
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
253 if t.orelse:
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
254 self.fill("else")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
255 self.enter()
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
256 self.dispatch(t.orelse)
62609
68b670f3614e Merged revisions 82338,82340-82341 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 48771
diff changeset
257 self.leave()
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
258
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
259 def _If(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
260 self.fill("if ")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
261 self.dispatch(t.test)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
262 self.enter()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
263 self.dispatch(t.body)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
264 self.leave()
62643
25fbf023102d Collapse else: if: ... into elif:
Mark Dickinson <dickinsm@gmail.com>
parents: 62637
diff changeset
265 # collapse nested ifs into equivalent elifs.
25fbf023102d Collapse else: if: ... into elif:
Mark Dickinson <dickinsm@gmail.com>
parents: 62637
diff changeset
266 while (t.orelse and len(t.orelse) == 1 and
25fbf023102d Collapse else: if: ... into elif:
Mark Dickinson <dickinsm@gmail.com>
parents: 62637
diff changeset
267 isinstance(t.orelse[0], ast.If)):
25fbf023102d Collapse else: if: ... into elif:
Mark Dickinson <dickinsm@gmail.com>
parents: 62637
diff changeset
268 t = t.orelse[0]
25fbf023102d Collapse else: if: ... into elif:
Mark Dickinson <dickinsm@gmail.com>
parents: 62637
diff changeset
269 self.fill("elif ")
25fbf023102d Collapse else: if: ... into elif:
Mark Dickinson <dickinsm@gmail.com>
parents: 62637
diff changeset
270 self.dispatch(t.test)
25fbf023102d Collapse else: if: ... into elif:
Mark Dickinson <dickinsm@gmail.com>
parents: 62637
diff changeset
271 self.enter()
25fbf023102d Collapse else: if: ... into elif:
Mark Dickinson <dickinsm@gmail.com>
parents: 62637
diff changeset
272 self.dispatch(t.body)
25fbf023102d Collapse else: if: ... into elif:
Mark Dickinson <dickinsm@gmail.com>
parents: 62637
diff changeset
273 self.leave()
25fbf023102d Collapse else: if: ... into elif:
Mark Dickinson <dickinsm@gmail.com>
parents: 62637
diff changeset
274 # final else
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
275 if t.orelse:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
276 self.fill("else")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
277 self.enter()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
278 self.dispatch(t.orelse)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
279 self.leave()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
280
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
281 def _While(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
282 self.fill("while ")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
283 self.dispatch(t.test)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
284 self.enter()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
285 self.dispatch(t.body)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
286 self.leave()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
287 if t.orelse:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
288 self.fill("else")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
289 self.enter()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
290 self.dispatch(t.orelse)
62609
68b670f3614e Merged revisions 82338,82340-82341 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 48771
diff changeset
291 self.leave()
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
292
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
293 def _With(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
294 self.fill("with ")
76803
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
295 interleave(lambda: self.write(", "), self.dispatch, t.items)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
296 self.enter()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
297 self.dispatch(t.body)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
298 self.leave()
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
299
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
300 # expr
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
301 def _Bytes(self, t):
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
302 self.write(repr(t.s))
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
303
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
304 def _Str(self, tree):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
305 self.write(repr(tree.s))
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
306
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
307 def _Name(self, t):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
308 self.write(t.id)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
309
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
310 def _Num(self, t):
62651
043ef04f64d2 Unparse infinite imaginary literals correctly. Add some more numeric tests.
Mark Dickinson <dickinsm@gmail.com>
parents: 62650
diff changeset
311 # Substitute overflowing decimal literal for AST infinities.
043ef04f64d2 Unparse infinite imaginary literals correctly. Add some more numeric tests.
Mark Dickinson <dickinsm@gmail.com>
parents: 62650
diff changeset
312 self.write(repr(t.n).replace("inf", INFSTR))
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
313
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
314 def _List(self, t):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
315 self.write("[")
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
316 interleave(lambda: self.write(", "), self.dispatch, t.elts)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
317 self.write("]")
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
318
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
319 def _ListComp(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
320 self.write("[")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
321 self.dispatch(t.elt)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
322 for gen in t.generators:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
323 self.dispatch(gen)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
324 self.write("]")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
325
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
326 def _GeneratorExp(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
327 self.write("(")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
328 self.dispatch(t.elt)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
329 for gen in t.generators:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
330 self.dispatch(gen)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
331 self.write(")")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
332
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
333 def _SetComp(self, t):
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
334 self.write("{")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
335 self.dispatch(t.elt)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
336 for gen in t.generators:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
337 self.dispatch(gen)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
338 self.write("}")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
339
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
340 def _DictComp(self, t):
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
341 self.write("{")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
342 self.dispatch(t.key)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
343 self.write(": ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
344 self.dispatch(t.value)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
345 for gen in t.generators:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
346 self.dispatch(gen)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
347 self.write("}")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
348
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
349 def _comprehension(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
350 self.write(" for ")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
351 self.dispatch(t.target)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
352 self.write(" in ")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
353 self.dispatch(t.iter)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
354 for if_clause in t.ifs:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
355 self.write(" if ")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
356 self.dispatch(if_clause)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
357
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
358 def _IfExp(self, t):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
359 self.write("(")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
360 self.dispatch(t.body)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
361 self.write(" if ")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
362 self.dispatch(t.test)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
363 self.write(" else ")
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
364 self.dispatch(t.orelse)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
365 self.write(")")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
366
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
367 def _Set(self, t):
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
368 assert(t.elts) # should be at least one element
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
369 self.write("{")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
370 interleave(lambda: self.write(", "), self.dispatch, t.elts)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
371 self.write("}")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
372
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
373 def _Dict(self, t):
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
374 self.write("{")
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
375 def write_pair(pair):
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
376 (k, v) = pair
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
377 self.dispatch(k)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
378 self.write(": ")
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
379 self.dispatch(v)
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
380 interleave(lambda: self.write(", "), write_pair, zip(t.keys, t.values))
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
381 self.write("}")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
382
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
383 def _Tuple(self, t):
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
384 self.write("(")
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
385 if len(t.elts) == 1:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
386 (elt,) = t.elts
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
387 self.dispatch(elt)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
388 self.write(",")
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
389 else:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
390 interleave(lambda: self.write(", "), self.dispatch, t.elts)
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
391 self.write(")")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
392
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
393 unop = {"Invert":"~", "Not": "not", "UAdd":"+", "USub":"-"}
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
394 def _UnaryOp(self, t):
62609
68b670f3614e Merged revisions 82338,82340-82341 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 48771
diff changeset
395 self.write("(")
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
396 self.write(self.unop[t.op.__class__.__name__])
62609
68b670f3614e Merged revisions 82338,82340-82341 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 48771
diff changeset
397 self.write(" ")
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
398 self.dispatch(t.operand)
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
399 self.write(")")
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
400
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
401 binop = { "Add":"+", "Sub":"-", "Mult":"*", "Div":"/", "Mod":"%",
62609
68b670f3614e Merged revisions 82338,82340-82341 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 48771
diff changeset
402 "LShift":"<<", "RShift":">>", "BitOr":"|", "BitXor":"^", "BitAnd":"&",
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
403 "FloorDiv":"//", "Pow": "**"}
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
404 def _BinOp(self, t):
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
405 self.write("(")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
406 self.dispatch(t.left)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
407 self.write(" " + self.binop[t.op.__class__.__name__] + " ")
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
408 self.dispatch(t.right)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
409 self.write(")")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
410
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
411 cmpops = {"Eq":"==", "NotEq":"!=", "Lt":"<", "LtE":"<=", "Gt":">", "GtE":">=",
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
412 "Is":"is", "IsNot":"is not", "In":"in", "NotIn":"not in"}
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
413 def _Compare(self, t):
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
414 self.write("(")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
415 self.dispatch(t.left)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
416 for o, e in zip(t.ops, t.comparators):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
417 self.write(" " + self.cmpops[o.__class__.__name__] + " ")
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
418 self.dispatch(e)
62612
69f6d2130281 Merged revisions 82345 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62609
diff changeset
419 self.write(")")
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
420
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
421 boolops = {ast.And: 'and', ast.Or: 'or'}
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
422 def _BoolOp(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
423 self.write("(")
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
424 s = " %s " % self.boolops[t.op.__class__]
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
425 interleave(lambda: self.write(s), self.dispatch, t.values)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
426 self.write(")")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
427
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
428 def _Attribute(self,t):
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
429 self.dispatch(t.value)
62645
4fce25abfdd8 Better solution for attribute access on integer literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 62644
diff changeset
430 # Special case: 3.__abs__() is a syntax error, so if t.value
4fce25abfdd8 Better solution for attribute access on integer literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 62644
diff changeset
431 # is an integer literal then we need to either parenthesize
4fce25abfdd8 Better solution for attribute access on integer literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 62644
diff changeset
432 # it or add an extra space to get 3 .__abs__().
4fce25abfdd8 Better solution for attribute access on integer literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 62644
diff changeset
433 if isinstance(t.value, ast.Num) and isinstance(t.value.n, int):
4fce25abfdd8 Better solution for attribute access on integer literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 62644
diff changeset
434 self.write(" ")
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
435 self.write(".")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
436 self.write(t.attr)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
437
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
438 def _Call(self, t):
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
439 self.dispatch(t.func)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
440 self.write("(")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
441 comma = False
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
442 for e in t.args:
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
443 if comma: self.write(", ")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
444 else: comma = True
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
445 self.dispatch(e)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
446 for e in t.keywords:
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
447 if comma: self.write(", ")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
448 else: comma = True
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
449 self.dispatch(e)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
450 if t.starargs:
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
451 if comma: self.write(", ")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
452 else: comma = True
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
453 self.write("*")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
454 self.dispatch(t.starargs)
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
455 if t.kwargs:
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
456 if comma: self.write(", ")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
457 else: comma = True
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
458 self.write("**")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
459 self.dispatch(t.kwargs)
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
460 self.write(")")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
461
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
462 def _Subscript(self, t):
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
463 self.dispatch(t.value)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
464 self.write("[")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
465 self.dispatch(t.slice)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
466 self.write("]")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
467
76802
c80576303892 Issue #14965: Fix missing support for starred assignments in Tools/parser/unparse.py.
Mark Dickinson <mdickinson@enthought.com>
parents: 67221
diff changeset
468 def _Starred(self, t):
c80576303892 Issue #14965: Fix missing support for starred assignments in Tools/parser/unparse.py.
Mark Dickinson <mdickinson@enthought.com>
parents: 67221
diff changeset
469 self.write("*")
c80576303892 Issue #14965: Fix missing support for starred assignments in Tools/parser/unparse.py.
Mark Dickinson <mdickinson@enthought.com>
parents: 67221
diff changeset
470 self.dispatch(t.value)
c80576303892 Issue #14965: Fix missing support for starred assignments in Tools/parser/unparse.py.
Mark Dickinson <mdickinson@enthought.com>
parents: 67221
diff changeset
471
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
472 # slice
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
473 def _Ellipsis(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
474 self.write("...")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
475
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
476 def _Index(self, t):
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
477 self.dispatch(t.value)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
478
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
479 def _Slice(self, t):
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
480 if t.lower:
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
481 self.dispatch(t.lower)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
482 self.write(":")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
483 if t.upper:
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
484 self.dispatch(t.upper)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
485 if t.step:
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
486 self.write(":")
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
487 self.dispatch(t.step)
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
488
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
489 def _ExtSlice(self, t):
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
490 interleave(lambda: self.write(', '), self.dispatch, t.dims)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
491
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
492 # argument
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
493 def _arg(self, t):
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
494 self.write(t.arg)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
495 if t.annotation:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
496 self.write(": ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
497 self.dispatch(t.annotation)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
498
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
499 # others
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
500 def _arguments(self, t):
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
501 first = True
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
502 # normal arguments
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
503 defaults = [None] * (len(t.args) - len(t.defaults)) + t.defaults
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
504 for a, d in zip(t.args, defaults):
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
505 if first:first = False
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
506 else: self.write(", ")
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
507 self.dispatch(a)
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
508 if d:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
509 self.write("=")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
510 self.dispatch(d)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
511
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
512 # varargs, or bare '*' if no varargs but keyword-only arguments present
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
513 if t.vararg or t.kwonlyargs:
36736
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
514 if first:first = False
bbce8ec2ff81 unparse.py can now unparse itself.
Martin v. Löwis <martin@v.loewis.de>
parents: 36723
diff changeset
515 else: self.write(", ")
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
516 self.write("*")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
517 if t.vararg:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
518 self.write(t.vararg)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
519 if t.varargannotation:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
520 self.write(": ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
521 self.dispatch(t.varargannotation)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
522
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
523 # keyword-only arguments
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
524 if t.kwonlyargs:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
525 for a, d in zip(t.kwonlyargs, t.kw_defaults):
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
526 if first:first = False
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
527 else: self.write(", ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
528 self.dispatch(a),
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
529 if d:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
530 self.write("=")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
531 self.dispatch(d)
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
532
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
533 # kwargs
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
534 if t.kwarg:
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
535 if first:first = False
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
536 else: self.write(", ")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
537 self.write("**"+t.kwarg)
62613
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
538 if t.kwargannotation:
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
539 self.write(": ")
b817e716516e Update Demo/parser/unparse.py to current Python 3.x syntax. Additions:
Mark Dickinson <dickinsm@gmail.com>
parents: 62612
diff changeset
540 self.dispatch(t.kwargannotation)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
541
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
542 def _keyword(self, t):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
543 self.write(t.arg)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
544 self.write("=")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
545 self.dispatch(t.value)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
546
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
547 def _Lambda(self, t):
62623
fe44828ea7ed More unparse.py fixes:
Mark Dickinson <dickinsm@gmail.com>
parents: 62622
diff changeset
548 self.write("(")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
549 self.write("lambda ")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
550 self.dispatch(t.args)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
551 self.write(": ")
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
552 self.dispatch(t.body)
62623
fe44828ea7ed More unparse.py fixes:
Mark Dickinson <dickinsm@gmail.com>
parents: 62622
diff changeset
553 self.write(")")
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
554
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
555 def _alias(self, t):
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
556 self.write(t.name)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
557 if t.asname:
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
558 self.write(" as "+t.asname)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40587
diff changeset
559
76803
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
560 def _withitem(self, t):
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
561 self.dispatch(t.context_expr)
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
562 if t.optional_vars:
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
563 self.write(" as ")
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
564 self.dispatch(t.optional_vars)
89e928048903 Issue #14965: Bring Tools/parser/unparse.py up to date with the Python 3.3. Grammar.
Mark Dickinson <mdickinson@enthought.com>
parents: 76802
diff changeset
565
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
566 def roundtrip(filename, output=sys.stdout):
62621
a4e1c56be532 unparse.py: respect coding cookie in input files
Mark Dickinson <dickinsm@gmail.com>
parents: 62614
diff changeset
567 with open(filename, "rb") as pyfile:
a4e1c56be532 unparse.py: respect coding cookie in input files
Mark Dickinson <dickinsm@gmail.com>
parents: 62614
diff changeset
568 encoding = tokenize.detect_encoding(pyfile.readline)[0]
a4e1c56be532 unparse.py: respect coding cookie in input files
Mark Dickinson <dickinsm@gmail.com>
parents: 62614
diff changeset
569 with open(filename, "r", encoding=encoding) as pyfile:
a4e1c56be532 unparse.py: respect coding cookie in input files
Mark Dickinson <dickinsm@gmail.com>
parents: 62614
diff changeset
570 source = pyfile.read()
62614
95f8b25e53ce unparse.py: Typo fix.
Mark Dickinson <dickinsm@gmail.com>
parents: 62613
diff changeset
571 tree = compile(source, filename, "exec", ast.PyCF_ONLY_AST)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
572 Unparser(tree, output)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
573
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
574
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
575
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
576 def testdir(a):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
577 try:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
578 names = [n for n in os.listdir(a) if n.endswith('.py')]
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
579 except OSError:
42304
7d16b42fcdbd Run 2to3 over the Demo/ directory to shut up parse errors from 2to3 about lingering print statements.
Collin Winter <collinw@gmail.com>
parents: 41554
diff changeset
580 print("Directory not readable: %s" % a, file=sys.stderr)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
581 else:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
582 for n in names:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
583 fullname = os.path.join(a, n)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
584 if os.path.isfile(fullname):
42304
7d16b42fcdbd Run 2to3 over the Demo/ directory to shut up parse errors from 2to3 about lingering print statements.
Collin Winter <collinw@gmail.com>
parents: 41554
diff changeset
585 output = io.StringIO()
7d16b42fcdbd Run 2to3 over the Demo/ directory to shut up parse errors from 2to3 about lingering print statements.
Collin Winter <collinw@gmail.com>
parents: 41554
diff changeset
586 print('Testing %s' % fullname)
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
587 try:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
588 roundtrip(fullname, output)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40450
diff changeset
589 except Exception as e:
42304
7d16b42fcdbd Run 2to3 over the Demo/ directory to shut up parse errors from 2to3 about lingering print statements.
Collin Winter <collinw@gmail.com>
parents: 41554
diff changeset
590 print(' Failed to compile, exception is %s' % repr(e))
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
591 elif os.path.isdir(fullname):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
592 testdir(fullname)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
593
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
594 def main(args):
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
595 if args[0] == '--testdir':
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
596 for a in args[1:]:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
597 testdir(a)
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
598 else:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
599 for a in args:
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
600 roundtrip(a)
36723
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
601
e580c8b0dcea Set EOL style to native.
Tim Peters <tim.peters@gmail.com>
parents: 36721
diff changeset
602 if __name__=='__main__':
37879
f731859e9b3b Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents: 36736
diff changeset
603 main(sys.argv[1:])