blob: 99727978f8a0e15add7dd6e0792fd0a509f43ae7 [file] [log] [blame]
Tim Peters400cbc32006-02-28 18:44:41 +00001"Usage: unparse.py <path to source file>"
2import sys
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +00003import ast
Mark Dickinson82c8d932010-06-29 07:48:23 +00004import tokenize
Collin Winter6f2df4d2007-07-17 20:59:35 +00005import io
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006import os
Tim Peters400cbc32006-02-28 18:44:41 +00007
Mark Dickinsoncba8c102010-06-30 11:45:53 +00008# Large float and imaginary literals get turned into infinities in the AST.
9# We unparse those infinities to INFSTR.
10INFSTR = "1e" + repr(sys.float_info.max_10_exp + 1)
11
Guido van Rossumd8faa362007-04-27 19:54:29 +000012def interleave(inter, f, seq):
13 """Call f on each item in seq, calling inter() in between.
14 """
15 seq = iter(seq)
16 try:
Collin Winter6f2df4d2007-07-17 20:59:35 +000017 f(next(seq))
Guido van Rossumd8faa362007-04-27 19:54:29 +000018 except StopIteration:
19 pass
20 else:
21 for x in seq:
22 inter()
23 f(x)
24
Tim Peters400cbc32006-02-28 18:44:41 +000025class Unparser:
26 """Methods in this class recursively traverse an AST and
27 output source code for the abstract syntax; original formatting
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +000028 is disregarded. """
Tim Peters400cbc32006-02-28 18:44:41 +000029
30 def __init__(self, tree, file = sys.stdout):
31 """Unparser(tree, file=sys.stdout) -> None.
32 Print the source for tree to file."""
33 self.f = file
34 self._indent = 0
35 self.dispatch(tree)
Collin Winter6f2df4d2007-07-17 20:59:35 +000036 print("", file=self.f)
Tim Peters400cbc32006-02-28 18:44:41 +000037 self.f.flush()
38
39 def fill(self, text = ""):
40 "Indent a piece of text, according to the current indentation level"
41 self.f.write("\n"+" "*self._indent + text)
42
43 def write(self, text):
44 "Append a piece of text to the current line."
45 self.f.write(text)
46
47 def enter(self):
48 "Print ':', and increase the indentation."
49 self.write(":")
50 self._indent += 1
51
52 def leave(self):
53 "Decrease the indentation level."
54 self._indent -= 1
55
56 def dispatch(self, tree):
57 "Dispatcher function, dispatching tree type T to method _T."
58 if isinstance(tree, list):
59 for t in tree:
60 self.dispatch(t)
61 return
62 meth = getattr(self, "_"+tree.__class__.__name__)
63 meth(tree)
64
65
66 ############### Unparsing methods ######################
67 # There should be one method per concrete grammar type #
68 # Constructors should be grouped by sum type. Ideally, #
69 # this would follow the order in the grammar, but #
70 # currently doesn't. #
71 ########################################################
72
73 def _Module(self, tree):
74 for stmt in tree.body:
75 self.dispatch(stmt)
76
77 # stmt
78 def _Expr(self, tree):
79 self.fill()
80 self.dispatch(tree.value)
81
82 def _Import(self, t):
83 self.fill("import ")
Guido van Rossumd8faa362007-04-27 19:54:29 +000084 interleave(lambda: self.write(", "), self.dispatch, t.names)
Tim Peters400cbc32006-02-28 18:44:41 +000085
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000086 def _ImportFrom(self, t):
87 self.fill("from ")
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +000088 self.write("." * t.level)
89 if t.module:
90 self.write(t.module)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000091 self.write(" import ")
Guido van Rossumd8faa362007-04-27 19:54:29 +000092 interleave(lambda: self.write(", "), self.dispatch, t.names)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000093
Tim Peters400cbc32006-02-28 18:44:41 +000094 def _Assign(self, t):
95 self.fill()
96 for target in t.targets:
97 self.dispatch(target)
98 self.write(" = ")
99 self.dispatch(t.value)
100
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000101 def _AugAssign(self, t):
102 self.fill()
103 self.dispatch(t.target)
104 self.write(" "+self.binop[t.op.__class__.__name__]+"= ")
105 self.dispatch(t.value)
106
107 def _Return(self, t):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000108 self.fill("return")
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000109 if t.value:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000110 self.write(" ")
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000111 self.dispatch(t.value)
112
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000113 def _Pass(self, t):
114 self.fill("pass")
115
116 def _Break(self, t):
117 self.fill("break")
118
119 def _Continue(self, t):
120 self.fill("continue")
121
122 def _Delete(self, t):
123 self.fill("del ")
Mark Dickinsonae100052010-06-28 19:44:20 +0000124 interleave(lambda: self.write(", "), self.dispatch, t.targets)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000125
126 def _Assert(self, t):
127 self.fill("assert ")
128 self.dispatch(t.test)
129 if t.msg:
130 self.write(", ")
131 self.dispatch(t.msg)
132
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000133 def _Global(self, t):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000134 self.fill("global ")
135 interleave(lambda: self.write(", "), self.write, t.names)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000136
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000137 def _Nonlocal(self, t):
138 self.fill("nonlocal ")
139 interleave(lambda: self.write(", "), self.write, t.names)
140
Yury Selivanov75445082015-05-11 22:57:16 -0400141 def _Await(self, t):
142 self.write("(")
143 self.write("await")
144 if t.value:
145 self.write(" ")
146 self.dispatch(t.value)
147 self.write(")")
148
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000149 def _Yield(self, t):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000150 self.write("(")
151 self.write("yield")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000152 if t.value:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000153 self.write(" ")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000154 self.dispatch(t.value)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000155 self.write(")")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000156
Mark Dickinsonfe8440a2012-05-06 17:35:19 +0100157 def _YieldFrom(self, t):
158 self.write("(")
159 self.write("yield from")
160 if t.value:
161 self.write(" ")
162 self.dispatch(t.value)
163 self.write(")")
164
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000165 def _Raise(self, t):
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000166 self.fill("raise")
167 if not t.exc:
168 assert not t.cause
169 return
170 self.write(" ")
171 self.dispatch(t.exc)
172 if t.cause:
173 self.write(" from ")
174 self.dispatch(t.cause)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000175
Mark Dickinsonfe8440a2012-05-06 17:35:19 +0100176 def _Try(self, t):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000177 self.fill("try")
178 self.enter()
179 self.dispatch(t.body)
180 self.leave()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000181 for ex in t.handlers:
182 self.dispatch(ex)
183 if t.orelse:
184 self.fill("else")
185 self.enter()
186 self.dispatch(t.orelse)
187 self.leave()
Mark Dickinsonfe8440a2012-05-06 17:35:19 +0100188 if t.finalbody:
189 self.fill("finally")
Mark Dickinson81ad8cc2010-06-30 08:46:53 +0000190 self.enter()
Mark Dickinsonfe8440a2012-05-06 17:35:19 +0100191 self.dispatch(t.finalbody)
Mark Dickinson81ad8cc2010-06-30 08:46:53 +0000192 self.leave()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000193
Benjamin Petersonc4fe6f32008-08-19 18:57:56 +0000194 def _ExceptHandler(self, t):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000195 self.fill("except")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000196 if t.type:
Guido van Rossumd8faa362007-04-27 19:54:29 +0000197 self.write(" ")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000198 self.dispatch(t.type)
199 if t.name:
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000200 self.write(" as ")
201 self.write(t.name)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000202 self.enter()
203 self.dispatch(t.body)
204 self.leave()
205
Tim Peters400cbc32006-02-28 18:44:41 +0000206 def _ClassDef(self, t):
207 self.write("\n")
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000208 for deco in t.decorator_list:
209 self.fill("@")
210 self.dispatch(deco)
Tim Peters400cbc32006-02-28 18:44:41 +0000211 self.fill("class "+t.name)
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000212 self.write("(")
213 comma = False
214 for e in t.bases:
215 if comma: self.write(", ")
216 else: comma = True
217 self.dispatch(e)
218 for e in t.keywords:
219 if comma: self.write(", ")
220 else: comma = True
221 self.dispatch(e)
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000222 self.write(")")
223
Tim Peters400cbc32006-02-28 18:44:41 +0000224 self.enter()
225 self.dispatch(t.body)
226 self.leave()
227
228 def _FunctionDef(self, t):
Yury Selivanov75445082015-05-11 22:57:16 -0400229 self.__FunctionDef_helper(t, "def")
230
231 def _AsyncFunctionDef(self, t):
232 self.__FunctionDef_helper(t, "async def")
233
234 def __FunctionDef_helper(self, t, fill_suffix):
Tim Peters400cbc32006-02-28 18:44:41 +0000235 self.write("\n")
Benjamin Petersonc4fe6f32008-08-19 18:57:56 +0000236 for deco in t.decorator_list:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000237 self.fill("@")
238 self.dispatch(deco)
Yury Selivanov75445082015-05-11 22:57:16 -0400239 def_str = fill_suffix+" "+t.name + "("
240 self.fill(def_str)
Tim Peters400cbc32006-02-28 18:44:41 +0000241 self.dispatch(t.args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000242 self.write(")")
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000243 if t.returns:
244 self.write(" -> ")
245 self.dispatch(t.returns)
Tim Peters400cbc32006-02-28 18:44:41 +0000246 self.enter()
247 self.dispatch(t.body)
248 self.leave()
249
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000250 def _For(self, t):
Yury Selivanov75445082015-05-11 22:57:16 -0400251 self.__For_helper("for ", t)
252
253 def _AsyncFor(self, t):
254 self.__For_helper("async for ", t)
255
256 def __For_helper(self, fill, t):
257 self.fill(fill)
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000258 self.dispatch(t.target)
259 self.write(" in ")
260 self.dispatch(t.iter)
261 self.enter()
262 self.dispatch(t.body)
263 self.leave()
264 if t.orelse:
265 self.fill("else")
266 self.enter()
267 self.dispatch(t.orelse)
Mark Dickinsonae100052010-06-28 19:44:20 +0000268 self.leave()
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000269
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000270 def _If(self, t):
271 self.fill("if ")
272 self.dispatch(t.test)
273 self.enter()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000274 self.dispatch(t.body)
275 self.leave()
Mark Dickinson8d6d7602010-06-30 08:32:11 +0000276 # collapse nested ifs into equivalent elifs.
277 while (t.orelse and len(t.orelse) == 1 and
278 isinstance(t.orelse[0], ast.If)):
279 t = t.orelse[0]
280 self.fill("elif ")
281 self.dispatch(t.test)
282 self.enter()
283 self.dispatch(t.body)
284 self.leave()
285 # final else
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000286 if t.orelse:
287 self.fill("else")
288 self.enter()
289 self.dispatch(t.orelse)
290 self.leave()
291
292 def _While(self, t):
293 self.fill("while ")
294 self.dispatch(t.test)
295 self.enter()
296 self.dispatch(t.body)
297 self.leave()
298 if t.orelse:
299 self.fill("else")
300 self.enter()
301 self.dispatch(t.orelse)
Mark Dickinsonae100052010-06-28 19:44:20 +0000302 self.leave()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000303
304 def _With(self, t):
305 self.fill("with ")
Mark Dickinsonfe8440a2012-05-06 17:35:19 +0100306 interleave(lambda: self.write(", "), self.dispatch, t.items)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000307 self.enter()
308 self.dispatch(t.body)
309 self.leave()
310
Yury Selivanov75445082015-05-11 22:57:16 -0400311 def _AsyncWith(self, t):
312 self.fill("async with ")
313 interleave(lambda: self.write(", "), self.dispatch, t.items)
314 self.enter()
315 self.dispatch(t.body)
316 self.leave()
317
Tim Peters400cbc32006-02-28 18:44:41 +0000318 # expr
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000319 def _Bytes(self, t):
320 self.write(repr(t.s))
321
Tim Peters400cbc32006-02-28 18:44:41 +0000322 def _Str(self, tree):
323 self.write(repr(tree.s))
324
Eric V. Smith608adf92015-09-20 15:09:15 -0400325 def _JoinedStr(self, t):
326 self.write("f")
327 string = io.StringIO()
328 self._fstring_JoinedStr(t, string.write)
329 self.write(repr(string.getvalue()))
330
331 def _FormattedValue(self, t):
332 self.write("f")
333 string = io.StringIO()
334 self._fstring_FormattedValue(t, string.write)
335 self.write(repr(string.getvalue()))
336
337 def _fstring_JoinedStr(self, t, write):
338 for value in t.values:
339 meth = getattr(self, "_fstring_" + type(value).__name__)
340 meth(value, write)
341
342 def _fstring_Str(self, t, write):
343 value = t.s.replace("{", "{{").replace("}", "}}")
344 write(value)
345
346 def _fstring_FormattedValue(self, t, write):
347 write("{")
348 expr = io.StringIO()
349 Unparser(t.value, expr)
350 expr = expr.getvalue().rstrip("\n")
351 if expr.startswith("{"):
352 write(" ") # Separate pair of opening brackets as "{ {"
353 write(expr)
354 if t.conversion != -1:
355 conversion = chr(t.conversion)
356 assert conversion in "sra"
357 write(f"!{conversion}")
358 if t.format_spec:
359 write(":")
360 meth = getattr(self, "_fstring_" + type(t.format_spec).__name__)
361 meth(t.format_spec, write)
362 write("}")
363
Tim Peters400cbc32006-02-28 18:44:41 +0000364 def _Name(self, t):
365 self.write(t.id)
366
Benjamin Peterson442f2092012-12-06 17:41:04 -0500367 def _NameConstant(self, t):
368 self.write(repr(t.value))
369
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000370 def _Num(self, t):
Mark Dickinsoncba8c102010-06-30 11:45:53 +0000371 # Substitute overflowing decimal literal for AST infinities.
372 self.write(repr(t.n).replace("inf", INFSTR))
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000373
Tim Peters400cbc32006-02-28 18:44:41 +0000374 def _List(self, t):
375 self.write("[")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000376 interleave(lambda: self.write(", "), self.dispatch, t.elts)
Tim Peters400cbc32006-02-28 18:44:41 +0000377 self.write("]")
378
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000379 def _ListComp(self, t):
380 self.write("[")
381 self.dispatch(t.elt)
382 for gen in t.generators:
383 self.dispatch(gen)
384 self.write("]")
385
386 def _GeneratorExp(self, t):
387 self.write("(")
388 self.dispatch(t.elt)
389 for gen in t.generators:
390 self.dispatch(gen)
391 self.write(")")
392
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000393 def _SetComp(self, t):
394 self.write("{")
395 self.dispatch(t.elt)
396 for gen in t.generators:
397 self.dispatch(gen)
398 self.write("}")
399
400 def _DictComp(self, t):
401 self.write("{")
402 self.dispatch(t.key)
403 self.write(": ")
404 self.dispatch(t.value)
405 for gen in t.generators:
406 self.dispatch(gen)
407 self.write("}")
408
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000409 def _comprehension(self, t):
410 self.write(" for ")
411 self.dispatch(t.target)
412 self.write(" in ")
413 self.dispatch(t.iter)
414 for if_clause in t.ifs:
415 self.write(" if ")
416 self.dispatch(if_clause)
417
418 def _IfExp(self, t):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000419 self.write("(")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000420 self.dispatch(t.body)
421 self.write(" if ")
422 self.dispatch(t.test)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000423 self.write(" else ")
424 self.dispatch(t.orelse)
425 self.write(")")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000426
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000427 def _Set(self, t):
428 assert(t.elts) # should be at least one element
429 self.write("{")
430 interleave(lambda: self.write(", "), self.dispatch, t.elts)
431 self.write("}")
432
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000433 def _Dict(self, t):
434 self.write("{")
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000435 def write_pair(pair):
436 (k, v) = pair
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000437 self.dispatch(k)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000438 self.write(": ")
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000439 self.dispatch(v)
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000440 interleave(lambda: self.write(", "), write_pair, zip(t.keys, t.values))
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000441 self.write("}")
442
443 def _Tuple(self, t):
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000444 self.write("(")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000445 if len(t.elts) == 1:
446 (elt,) = t.elts
447 self.dispatch(elt)
448 self.write(",")
449 else:
450 interleave(lambda: self.write(", "), self.dispatch, t.elts)
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000451 self.write(")")
452
Tim Peters400cbc32006-02-28 18:44:41 +0000453 unop = {"Invert":"~", "Not": "not", "UAdd":"+", "USub":"-"}
454 def _UnaryOp(self, t):
Tim Peters400cbc32006-02-28 18:44:41 +0000455 self.write("(")
Mark Dickinsonae100052010-06-28 19:44:20 +0000456 self.write(self.unop[t.op.__class__.__name__])
457 self.write(" ")
Tim Peters400cbc32006-02-28 18:44:41 +0000458 self.dispatch(t.operand)
459 self.write(")")
460
Benjamin Peterson63c46b22014-04-10 00:17:48 -0400461 binop = { "Add":"+", "Sub":"-", "Mult":"*", "MatMult":"@", "Div":"/", "Mod":"%",
Mark Dickinsonae100052010-06-28 19:44:20 +0000462 "LShift":"<<", "RShift":">>", "BitOr":"|", "BitXor":"^", "BitAnd":"&",
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000463 "FloorDiv":"//", "Pow": "**"}
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000464 def _BinOp(self, t):
465 self.write("(")
466 self.dispatch(t.left)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000467 self.write(" " + self.binop[t.op.__class__.__name__] + " ")
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000468 self.dispatch(t.right)
469 self.write(")")
470
471 cmpops = {"Eq":"==", "NotEq":"!=", "Lt":"<", "LtE":"<=", "Gt":">", "GtE":">=",
472 "Is":"is", "IsNot":"is not", "In":"in", "NotIn":"not in"}
473 def _Compare(self, t):
474 self.write("(")
475 self.dispatch(t.left)
476 for o, e in zip(t.ops, t.comparators):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000477 self.write(" " + self.cmpops[o.__class__.__name__] + " ")
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000478 self.dispatch(e)
Mark Dickinsonf5451e52010-06-28 20:09:18 +0000479 self.write(")")
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000480
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000481 boolops = {ast.And: 'and', ast.Or: 'or'}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000482 def _BoolOp(self, t):
483 self.write("(")
Guido van Rossumd8faa362007-04-27 19:54:29 +0000484 s = " %s " % self.boolops[t.op.__class__]
485 interleave(lambda: self.write(s), self.dispatch, t.values)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000486 self.write(")")
487
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000488 def _Attribute(self,t):
489 self.dispatch(t.value)
Mark Dickinsonb67e15c2010-06-30 09:05:47 +0000490 # Special case: 3.__abs__() is a syntax error, so if t.value
491 # is an integer literal then we need to either parenthesize
492 # it or add an extra space to get 3 .__abs__().
493 if isinstance(t.value, ast.Num) and isinstance(t.value.n, int):
494 self.write(" ")
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000495 self.write(".")
496 self.write(t.attr)
497
498 def _Call(self, t):
499 self.dispatch(t.func)
500 self.write("(")
501 comma = False
502 for e in t.args:
503 if comma: self.write(", ")
504 else: comma = True
505 self.dispatch(e)
506 for e in t.keywords:
507 if comma: self.write(", ")
508 else: comma = True
509 self.dispatch(e)
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000510 self.write(")")
511
512 def _Subscript(self, t):
513 self.dispatch(t.value)
514 self.write("[")
515 self.dispatch(t.slice)
516 self.write("]")
517
Mark Dickinson1b2e9442012-05-06 17:27:39 +0100518 def _Starred(self, t):
519 self.write("*")
520 self.dispatch(t.value)
521
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000522 # slice
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000523 def _Ellipsis(self, t):
524 self.write("...")
525
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000526 def _Index(self, t):
527 self.dispatch(t.value)
528
529 def _Slice(self, t):
530 if t.lower:
531 self.dispatch(t.lower)
532 self.write(":")
533 if t.upper:
534 self.dispatch(t.upper)
535 if t.step:
536 self.write(":")
537 self.dispatch(t.step)
538
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000539 def _ExtSlice(self, t):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000540 interleave(lambda: self.write(', '), self.dispatch, t.dims)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000541
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000542 # argument
543 def _arg(self, t):
544 self.write(t.arg)
545 if t.annotation:
546 self.write(": ")
547 self.dispatch(t.annotation)
548
Tim Peters400cbc32006-02-28 18:44:41 +0000549 # others
550 def _arguments(self, t):
551 first = True
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000552 # normal arguments
553 defaults = [None] * (len(t.args) - len(t.defaults)) + t.defaults
554 for a, d in zip(t.args, defaults):
Tim Peters400cbc32006-02-28 18:44:41 +0000555 if first:first = False
556 else: self.write(", ")
557 self.dispatch(a)
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000558 if d:
559 self.write("=")
560 self.dispatch(d)
561
562 # varargs, or bare '*' if no varargs but keyword-only arguments present
563 if t.vararg or t.kwonlyargs:
Martin v. Löwis87a8b4f2006-02-28 21:41:30 +0000564 if first:first = False
565 else: self.write(", ")
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000566 self.write("*")
567 if t.vararg:
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700568 self.write(t.vararg.arg)
569 if t.vararg.annotation:
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000570 self.write(": ")
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700571 self.dispatch(t.vararg.annotation)
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000572
573 # keyword-only arguments
574 if t.kwonlyargs:
575 for a, d in zip(t.kwonlyargs, t.kw_defaults):
576 if first:first = False
577 else: self.write(", ")
578 self.dispatch(a),
579 if d:
580 self.write("=")
581 self.dispatch(d)
582
583 # kwargs
Tim Peters400cbc32006-02-28 18:44:41 +0000584 if t.kwarg:
585 if first:first = False
586 else: self.write(", ")
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700587 self.write("**"+t.kwarg.arg)
588 if t.kwarg.annotation:
Mark Dickinsonfa2e4e92010-06-28 21:14:17 +0000589 self.write(": ")
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700590 self.dispatch(t.kwarg.annotation)
Tim Peters400cbc32006-02-28 18:44:41 +0000591
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000592 def _keyword(self, t):
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400593 if t.arg is None:
594 self.write("**")
595 else:
596 self.write(t.arg)
597 self.write("=")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000598 self.dispatch(t.value)
599
600 def _Lambda(self, t):
Mark Dickinson8042e282010-06-29 10:01:48 +0000601 self.write("(")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000602 self.write("lambda ")
603 self.dispatch(t.args)
604 self.write(": ")
605 self.dispatch(t.body)
Mark Dickinson8042e282010-06-29 10:01:48 +0000606 self.write(")")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000607
Guido van Rossumd8faa362007-04-27 19:54:29 +0000608 def _alias(self, t):
609 self.write(t.name)
610 if t.asname:
611 self.write(" as "+t.asname)
612
Mark Dickinsonfe8440a2012-05-06 17:35:19 +0100613 def _withitem(self, t):
614 self.dispatch(t.context_expr)
615 if t.optional_vars:
616 self.write(" as ")
617 self.dispatch(t.optional_vars)
618
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000619def roundtrip(filename, output=sys.stdout):
Mark Dickinson82c8d932010-06-29 07:48:23 +0000620 with open(filename, "rb") as pyfile:
621 encoding = tokenize.detect_encoding(pyfile.readline)[0]
622 with open(filename, "r", encoding=encoding) as pyfile:
623 source = pyfile.read()
Mark Dickinson3d1bfbf2010-06-28 21:39:51 +0000624 tree = compile(source, filename, "exec", ast.PyCF_ONLY_AST)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000625 Unparser(tree, output)
626
627
628
629def testdir(a):
630 try:
631 names = [n for n in os.listdir(a) if n.endswith('.py')]
632 except OSError:
Collin Winter6f2df4d2007-07-17 20:59:35 +0000633 print("Directory not readable: %s" % a, file=sys.stderr)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000634 else:
635 for n in names:
636 fullname = os.path.join(a, n)
637 if os.path.isfile(fullname):
Collin Winter6f2df4d2007-07-17 20:59:35 +0000638 output = io.StringIO()
639 print('Testing %s' % fullname)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000640 try:
641 roundtrip(fullname, output)
Guido van Rossumb940e112007-01-10 16:19:56 +0000642 except Exception as e:
Collin Winter6f2df4d2007-07-17 20:59:35 +0000643 print(' Failed to compile, exception is %s' % repr(e))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000644 elif os.path.isdir(fullname):
645 testdir(fullname)
646
647def main(args):
648 if args[0] == '--testdir':
649 for a in args[1:]:
650 testdir(a)
651 else:
652 for a in args:
653 roundtrip(a)
Tim Peters400cbc32006-02-28 18:44:41 +0000654
655if __name__=='__main__':
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000656 main(sys.argv[1:])