1 | n/a | "Usage: unparse.py <path to source file>" |
---|
2 | n/a | import sys |
---|
3 | n/a | import ast |
---|
4 | n/a | import tokenize |
---|
5 | n/a | import io |
---|
6 | n/a | import os |
---|
7 | n/a | |
---|
8 | n/a | # Large float and imaginary literals get turned into infinities in the AST. |
---|
9 | n/a | # We unparse those infinities to INFSTR. |
---|
10 | n/a | INFSTR = "1e" + repr(sys.float_info.max_10_exp + 1) |
---|
11 | n/a | |
---|
12 | n/a | def interleave(inter, f, seq): |
---|
13 | n/a | """Call f on each item in seq, calling inter() in between. |
---|
14 | n/a | """ |
---|
15 | n/a | seq = iter(seq) |
---|
16 | n/a | try: |
---|
17 | n/a | f(next(seq)) |
---|
18 | n/a | except StopIteration: |
---|
19 | n/a | pass |
---|
20 | n/a | else: |
---|
21 | n/a | for x in seq: |
---|
22 | n/a | inter() |
---|
23 | n/a | f(x) |
---|
24 | n/a | |
---|
25 | n/a | class Unparser: |
---|
26 | n/a | """Methods in this class recursively traverse an AST and |
---|
27 | n/a | output source code for the abstract syntax; original formatting |
---|
28 | n/a | is disregarded. """ |
---|
29 | n/a | |
---|
30 | n/a | def __init__(self, tree, file = sys.stdout): |
---|
31 | n/a | """Unparser(tree, file=sys.stdout) -> None. |
---|
32 | n/a | Print the source for tree to file.""" |
---|
33 | n/a | self.f = file |
---|
34 | n/a | self._indent = 0 |
---|
35 | n/a | self.dispatch(tree) |
---|
36 | n/a | print("", file=self.f) |
---|
37 | n/a | self.f.flush() |
---|
38 | n/a | |
---|
39 | n/a | def fill(self, text = ""): |
---|
40 | n/a | "Indent a piece of text, according to the current indentation level" |
---|
41 | n/a | self.f.write("\n"+" "*self._indent + text) |
---|
42 | n/a | |
---|
43 | n/a | def write(self, text): |
---|
44 | n/a | "Append a piece of text to the current line." |
---|
45 | n/a | self.f.write(text) |
---|
46 | n/a | |
---|
47 | n/a | def enter(self): |
---|
48 | n/a | "Print ':', and increase the indentation." |
---|
49 | n/a | self.write(":") |
---|
50 | n/a | self._indent += 1 |
---|
51 | n/a | |
---|
52 | n/a | def leave(self): |
---|
53 | n/a | "Decrease the indentation level." |
---|
54 | n/a | self._indent -= 1 |
---|
55 | n/a | |
---|
56 | n/a | def dispatch(self, tree): |
---|
57 | n/a | "Dispatcher function, dispatching tree type T to method _T." |
---|
58 | n/a | if isinstance(tree, list): |
---|
59 | n/a | for t in tree: |
---|
60 | n/a | self.dispatch(t) |
---|
61 | n/a | return |
---|
62 | n/a | meth = getattr(self, "_"+tree.__class__.__name__) |
---|
63 | n/a | meth(tree) |
---|
64 | n/a | |
---|
65 | n/a | |
---|
66 | n/a | ############### Unparsing methods ###################### |
---|
67 | n/a | # There should be one method per concrete grammar type # |
---|
68 | n/a | # Constructors should be grouped by sum type. Ideally, # |
---|
69 | n/a | # this would follow the order in the grammar, but # |
---|
70 | n/a | # currently doesn't. # |
---|
71 | n/a | ######################################################## |
---|
72 | n/a | |
---|
73 | n/a | def _Module(self, tree): |
---|
74 | n/a | for stmt in tree.body: |
---|
75 | n/a | self.dispatch(stmt) |
---|
76 | n/a | |
---|
77 | n/a | # stmt |
---|
78 | n/a | def _Expr(self, tree): |
---|
79 | n/a | self.fill() |
---|
80 | n/a | self.dispatch(tree.value) |
---|
81 | n/a | |
---|
82 | n/a | def _Import(self, t): |
---|
83 | n/a | self.fill("import ") |
---|
84 | n/a | interleave(lambda: self.write(", "), self.dispatch, t.names) |
---|
85 | n/a | |
---|
86 | n/a | def _ImportFrom(self, t): |
---|
87 | n/a | self.fill("from ") |
---|
88 | n/a | self.write("." * t.level) |
---|
89 | n/a | if t.module: |
---|
90 | n/a | self.write(t.module) |
---|
91 | n/a | self.write(" import ") |
---|
92 | n/a | interleave(lambda: self.write(", "), self.dispatch, t.names) |
---|
93 | n/a | |
---|
94 | n/a | def _Assign(self, t): |
---|
95 | n/a | self.fill() |
---|
96 | n/a | for target in t.targets: |
---|
97 | n/a | self.dispatch(target) |
---|
98 | n/a | self.write(" = ") |
---|
99 | n/a | self.dispatch(t.value) |
---|
100 | n/a | |
---|
101 | n/a | def _AugAssign(self, t): |
---|
102 | n/a | self.fill() |
---|
103 | n/a | self.dispatch(t.target) |
---|
104 | n/a | self.write(" "+self.binop[t.op.__class__.__name__]+"= ") |
---|
105 | n/a | self.dispatch(t.value) |
---|
106 | n/a | |
---|
107 | n/a | def _AnnAssign(self, t): |
---|
108 | n/a | self.fill() |
---|
109 | n/a | if not t.simple and isinstance(t.target, ast.Name): |
---|
110 | n/a | self.write('(') |
---|
111 | n/a | self.dispatch(t.target) |
---|
112 | n/a | if not t.simple and isinstance(t.target, ast.Name): |
---|
113 | n/a | self.write(')') |
---|
114 | n/a | self.write(": ") |
---|
115 | n/a | self.dispatch(t.annotation) |
---|
116 | n/a | if t.value: |
---|
117 | n/a | self.write(" = ") |
---|
118 | n/a | self.dispatch(t.value) |
---|
119 | n/a | |
---|
120 | n/a | def _Return(self, t): |
---|
121 | n/a | self.fill("return") |
---|
122 | n/a | if t.value: |
---|
123 | n/a | self.write(" ") |
---|
124 | n/a | self.dispatch(t.value) |
---|
125 | n/a | |
---|
126 | n/a | def _Pass(self, t): |
---|
127 | n/a | self.fill("pass") |
---|
128 | n/a | |
---|
129 | n/a | def _Break(self, t): |
---|
130 | n/a | self.fill("break") |
---|
131 | n/a | |
---|
132 | n/a | def _Continue(self, t): |
---|
133 | n/a | self.fill("continue") |
---|
134 | n/a | |
---|
135 | n/a | def _Delete(self, t): |
---|
136 | n/a | self.fill("del ") |
---|
137 | n/a | interleave(lambda: self.write(", "), self.dispatch, t.targets) |
---|
138 | n/a | |
---|
139 | n/a | def _Assert(self, t): |
---|
140 | n/a | self.fill("assert ") |
---|
141 | n/a | self.dispatch(t.test) |
---|
142 | n/a | if t.msg: |
---|
143 | n/a | self.write(", ") |
---|
144 | n/a | self.dispatch(t.msg) |
---|
145 | n/a | |
---|
146 | n/a | def _Global(self, t): |
---|
147 | n/a | self.fill("global ") |
---|
148 | n/a | interleave(lambda: self.write(", "), self.write, t.names) |
---|
149 | n/a | |
---|
150 | n/a | def _Nonlocal(self, t): |
---|
151 | n/a | self.fill("nonlocal ") |
---|
152 | n/a | interleave(lambda: self.write(", "), self.write, t.names) |
---|
153 | n/a | |
---|
154 | n/a | def _Await(self, t): |
---|
155 | n/a | self.write("(") |
---|
156 | n/a | self.write("await") |
---|
157 | n/a | if t.value: |
---|
158 | n/a | self.write(" ") |
---|
159 | n/a | self.dispatch(t.value) |
---|
160 | n/a | self.write(")") |
---|
161 | n/a | |
---|
162 | n/a | def _Yield(self, t): |
---|
163 | n/a | self.write("(") |
---|
164 | n/a | self.write("yield") |
---|
165 | n/a | if t.value: |
---|
166 | n/a | self.write(" ") |
---|
167 | n/a | self.dispatch(t.value) |
---|
168 | n/a | self.write(")") |
---|
169 | n/a | |
---|
170 | n/a | def _YieldFrom(self, t): |
---|
171 | n/a | self.write("(") |
---|
172 | n/a | self.write("yield from") |
---|
173 | n/a | if t.value: |
---|
174 | n/a | self.write(" ") |
---|
175 | n/a | self.dispatch(t.value) |
---|
176 | n/a | self.write(")") |
---|
177 | n/a | |
---|
178 | n/a | def _Raise(self, t): |
---|
179 | n/a | self.fill("raise") |
---|
180 | n/a | if not t.exc: |
---|
181 | n/a | assert not t.cause |
---|
182 | n/a | return |
---|
183 | n/a | self.write(" ") |
---|
184 | n/a | self.dispatch(t.exc) |
---|
185 | n/a | if t.cause: |
---|
186 | n/a | self.write(" from ") |
---|
187 | n/a | self.dispatch(t.cause) |
---|
188 | n/a | |
---|
189 | n/a | def _Try(self, t): |
---|
190 | n/a | self.fill("try") |
---|
191 | n/a | self.enter() |
---|
192 | n/a | self.dispatch(t.body) |
---|
193 | n/a | self.leave() |
---|
194 | n/a | for ex in t.handlers: |
---|
195 | n/a | self.dispatch(ex) |
---|
196 | n/a | if t.orelse: |
---|
197 | n/a | self.fill("else") |
---|
198 | n/a | self.enter() |
---|
199 | n/a | self.dispatch(t.orelse) |
---|
200 | n/a | self.leave() |
---|
201 | n/a | if t.finalbody: |
---|
202 | n/a | self.fill("finally") |
---|
203 | n/a | self.enter() |
---|
204 | n/a | self.dispatch(t.finalbody) |
---|
205 | n/a | self.leave() |
---|
206 | n/a | |
---|
207 | n/a | def _ExceptHandler(self, t): |
---|
208 | n/a | self.fill("except") |
---|
209 | n/a | if t.type: |
---|
210 | n/a | self.write(" ") |
---|
211 | n/a | self.dispatch(t.type) |
---|
212 | n/a | if t.name: |
---|
213 | n/a | self.write(" as ") |
---|
214 | n/a | self.write(t.name) |
---|
215 | n/a | self.enter() |
---|
216 | n/a | self.dispatch(t.body) |
---|
217 | n/a | self.leave() |
---|
218 | n/a | |
---|
219 | n/a | def _ClassDef(self, t): |
---|
220 | n/a | self.write("\n") |
---|
221 | n/a | for deco in t.decorator_list: |
---|
222 | n/a | self.fill("@") |
---|
223 | n/a | self.dispatch(deco) |
---|
224 | n/a | self.fill("class "+t.name) |
---|
225 | n/a | self.write("(") |
---|
226 | n/a | comma = False |
---|
227 | n/a | for e in t.bases: |
---|
228 | n/a | if comma: self.write(", ") |
---|
229 | n/a | else: comma = True |
---|
230 | n/a | self.dispatch(e) |
---|
231 | n/a | for e in t.keywords: |
---|
232 | n/a | if comma: self.write(", ") |
---|
233 | n/a | else: comma = True |
---|
234 | n/a | self.dispatch(e) |
---|
235 | n/a | self.write(")") |
---|
236 | n/a | |
---|
237 | n/a | self.enter() |
---|
238 | n/a | self.dispatch(t.body) |
---|
239 | n/a | self.leave() |
---|
240 | n/a | |
---|
241 | n/a | def _FunctionDef(self, t): |
---|
242 | n/a | self.__FunctionDef_helper(t, "def") |
---|
243 | n/a | |
---|
244 | n/a | def _AsyncFunctionDef(self, t): |
---|
245 | n/a | self.__FunctionDef_helper(t, "async def") |
---|
246 | n/a | |
---|
247 | n/a | def __FunctionDef_helper(self, t, fill_suffix): |
---|
248 | n/a | self.write("\n") |
---|
249 | n/a | for deco in t.decorator_list: |
---|
250 | n/a | self.fill("@") |
---|
251 | n/a | self.dispatch(deco) |
---|
252 | n/a | def_str = fill_suffix+" "+t.name + "(" |
---|
253 | n/a | self.fill(def_str) |
---|
254 | n/a | self.dispatch(t.args) |
---|
255 | n/a | self.write(")") |
---|
256 | n/a | if t.returns: |
---|
257 | n/a | self.write(" -> ") |
---|
258 | n/a | self.dispatch(t.returns) |
---|
259 | n/a | self.enter() |
---|
260 | n/a | self.dispatch(t.body) |
---|
261 | n/a | self.leave() |
---|
262 | n/a | |
---|
263 | n/a | def _For(self, t): |
---|
264 | n/a | self.__For_helper("for ", t) |
---|
265 | n/a | |
---|
266 | n/a | def _AsyncFor(self, t): |
---|
267 | n/a | self.__For_helper("async for ", t) |
---|
268 | n/a | |
---|
269 | n/a | def __For_helper(self, fill, t): |
---|
270 | n/a | self.fill(fill) |
---|
271 | n/a | self.dispatch(t.target) |
---|
272 | n/a | self.write(" in ") |
---|
273 | n/a | self.dispatch(t.iter) |
---|
274 | n/a | self.enter() |
---|
275 | n/a | self.dispatch(t.body) |
---|
276 | n/a | self.leave() |
---|
277 | n/a | if t.orelse: |
---|
278 | n/a | self.fill("else") |
---|
279 | n/a | self.enter() |
---|
280 | n/a | self.dispatch(t.orelse) |
---|
281 | n/a | self.leave() |
---|
282 | n/a | |
---|
283 | n/a | def _If(self, t): |
---|
284 | n/a | self.fill("if ") |
---|
285 | n/a | self.dispatch(t.test) |
---|
286 | n/a | self.enter() |
---|
287 | n/a | self.dispatch(t.body) |
---|
288 | n/a | self.leave() |
---|
289 | n/a | # collapse nested ifs into equivalent elifs. |
---|
290 | n/a | while (t.orelse and len(t.orelse) == 1 and |
---|
291 | n/a | isinstance(t.orelse[0], ast.If)): |
---|
292 | n/a | t = t.orelse[0] |
---|
293 | n/a | self.fill("elif ") |
---|
294 | n/a | self.dispatch(t.test) |
---|
295 | n/a | self.enter() |
---|
296 | n/a | self.dispatch(t.body) |
---|
297 | n/a | self.leave() |
---|
298 | n/a | # final else |
---|
299 | n/a | if t.orelse: |
---|
300 | n/a | self.fill("else") |
---|
301 | n/a | self.enter() |
---|
302 | n/a | self.dispatch(t.orelse) |
---|
303 | n/a | self.leave() |
---|
304 | n/a | |
---|
305 | n/a | def _While(self, t): |
---|
306 | n/a | self.fill("while ") |
---|
307 | n/a | self.dispatch(t.test) |
---|
308 | n/a | self.enter() |
---|
309 | n/a | self.dispatch(t.body) |
---|
310 | n/a | self.leave() |
---|
311 | n/a | if t.orelse: |
---|
312 | n/a | self.fill("else") |
---|
313 | n/a | self.enter() |
---|
314 | n/a | self.dispatch(t.orelse) |
---|
315 | n/a | self.leave() |
---|
316 | n/a | |
---|
317 | n/a | def _With(self, t): |
---|
318 | n/a | self.fill("with ") |
---|
319 | n/a | interleave(lambda: self.write(", "), self.dispatch, t.items) |
---|
320 | n/a | self.enter() |
---|
321 | n/a | self.dispatch(t.body) |
---|
322 | n/a | self.leave() |
---|
323 | n/a | |
---|
324 | n/a | def _AsyncWith(self, t): |
---|
325 | n/a | self.fill("async with ") |
---|
326 | n/a | interleave(lambda: self.write(", "), self.dispatch, t.items) |
---|
327 | n/a | self.enter() |
---|
328 | n/a | self.dispatch(t.body) |
---|
329 | n/a | self.leave() |
---|
330 | n/a | |
---|
331 | n/a | # expr |
---|
332 | n/a | def _Bytes(self, t): |
---|
333 | n/a | self.write(repr(t.s)) |
---|
334 | n/a | |
---|
335 | n/a | def _Str(self, tree): |
---|
336 | n/a | self.write(repr(tree.s)) |
---|
337 | n/a | |
---|
338 | n/a | def _JoinedStr(self, t): |
---|
339 | n/a | self.write("f") |
---|
340 | n/a | string = io.StringIO() |
---|
341 | n/a | self._fstring_JoinedStr(t, string.write) |
---|
342 | n/a | self.write(repr(string.getvalue())) |
---|
343 | n/a | |
---|
344 | n/a | def _FormattedValue(self, t): |
---|
345 | n/a | self.write("f") |
---|
346 | n/a | string = io.StringIO() |
---|
347 | n/a | self._fstring_FormattedValue(t, string.write) |
---|
348 | n/a | self.write(repr(string.getvalue())) |
---|
349 | n/a | |
---|
350 | n/a | def _fstring_JoinedStr(self, t, write): |
---|
351 | n/a | for value in t.values: |
---|
352 | n/a | meth = getattr(self, "_fstring_" + type(value).__name__) |
---|
353 | n/a | meth(value, write) |
---|
354 | n/a | |
---|
355 | n/a | def _fstring_Str(self, t, write): |
---|
356 | n/a | value = t.s.replace("{", "{{").replace("}", "}}") |
---|
357 | n/a | write(value) |
---|
358 | n/a | |
---|
359 | n/a | def _fstring_Constant(self, t, write): |
---|
360 | n/a | assert isinstance(t.value, str) |
---|
361 | n/a | value = t.value.replace("{", "{{").replace("}", "}}") |
---|
362 | n/a | write(value) |
---|
363 | n/a | |
---|
364 | n/a | def _fstring_FormattedValue(self, t, write): |
---|
365 | n/a | write("{") |
---|
366 | n/a | expr = io.StringIO() |
---|
367 | n/a | Unparser(t.value, expr) |
---|
368 | n/a | expr = expr.getvalue().rstrip("\n") |
---|
369 | n/a | if expr.startswith("{"): |
---|
370 | n/a | write(" ") # Separate pair of opening brackets as "{ {" |
---|
371 | n/a | write(expr) |
---|
372 | n/a | if t.conversion != -1: |
---|
373 | n/a | conversion = chr(t.conversion) |
---|
374 | n/a | assert conversion in "sra" |
---|
375 | n/a | write(f"!{conversion}") |
---|
376 | n/a | if t.format_spec: |
---|
377 | n/a | write(":") |
---|
378 | n/a | meth = getattr(self, "_fstring_" + type(t.format_spec).__name__) |
---|
379 | n/a | meth(t.format_spec, write) |
---|
380 | n/a | write("}") |
---|
381 | n/a | |
---|
382 | n/a | def _Name(self, t): |
---|
383 | n/a | self.write(t.id) |
---|
384 | n/a | |
---|
385 | n/a | def _write_constant(self, value): |
---|
386 | n/a | if isinstance(value, (float, complex)): |
---|
387 | n/a | self.write(repr(value).replace("inf", INFSTR)) |
---|
388 | n/a | else: |
---|
389 | n/a | self.write(repr(value)) |
---|
390 | n/a | |
---|
391 | n/a | def _Constant(self, t): |
---|
392 | n/a | value = t.value |
---|
393 | n/a | if isinstance(value, tuple): |
---|
394 | n/a | self.write("(") |
---|
395 | n/a | if len(value) == 1: |
---|
396 | n/a | self._write_constant(value[0]) |
---|
397 | n/a | self.write(",") |
---|
398 | n/a | else: |
---|
399 | n/a | interleave(lambda: self.write(", "), self._write_constant, value) |
---|
400 | n/a | self.write(")") |
---|
401 | n/a | else: |
---|
402 | n/a | self._write_constant(t.value) |
---|
403 | n/a | |
---|
404 | n/a | def _NameConstant(self, t): |
---|
405 | n/a | self.write(repr(t.value)) |
---|
406 | n/a | |
---|
407 | n/a | def _Num(self, t): |
---|
408 | n/a | # Substitute overflowing decimal literal for AST infinities. |
---|
409 | n/a | self.write(repr(t.n).replace("inf", INFSTR)) |
---|
410 | n/a | |
---|
411 | n/a | def _List(self, t): |
---|
412 | n/a | self.write("[") |
---|
413 | n/a | interleave(lambda: self.write(", "), self.dispatch, t.elts) |
---|
414 | n/a | self.write("]") |
---|
415 | n/a | |
---|
416 | n/a | def _ListComp(self, t): |
---|
417 | n/a | self.write("[") |
---|
418 | n/a | self.dispatch(t.elt) |
---|
419 | n/a | for gen in t.generators: |
---|
420 | n/a | self.dispatch(gen) |
---|
421 | n/a | self.write("]") |
---|
422 | n/a | |
---|
423 | n/a | def _GeneratorExp(self, t): |
---|
424 | n/a | self.write("(") |
---|
425 | n/a | self.dispatch(t.elt) |
---|
426 | n/a | for gen in t.generators: |
---|
427 | n/a | self.dispatch(gen) |
---|
428 | n/a | self.write(")") |
---|
429 | n/a | |
---|
430 | n/a | def _SetComp(self, t): |
---|
431 | n/a | self.write("{") |
---|
432 | n/a | self.dispatch(t.elt) |
---|
433 | n/a | for gen in t.generators: |
---|
434 | n/a | self.dispatch(gen) |
---|
435 | n/a | self.write("}") |
---|
436 | n/a | |
---|
437 | n/a | def _DictComp(self, t): |
---|
438 | n/a | self.write("{") |
---|
439 | n/a | self.dispatch(t.key) |
---|
440 | n/a | self.write(": ") |
---|
441 | n/a | self.dispatch(t.value) |
---|
442 | n/a | for gen in t.generators: |
---|
443 | n/a | self.dispatch(gen) |
---|
444 | n/a | self.write("}") |
---|
445 | n/a | |
---|
446 | n/a | def _comprehension(self, t): |
---|
447 | n/a | if t.is_async: |
---|
448 | n/a | self.write(" async for ") |
---|
449 | n/a | else: |
---|
450 | n/a | self.write(" for ") |
---|
451 | n/a | self.dispatch(t.target) |
---|
452 | n/a | self.write(" in ") |
---|
453 | n/a | self.dispatch(t.iter) |
---|
454 | n/a | for if_clause in t.ifs: |
---|
455 | n/a | self.write(" if ") |
---|
456 | n/a | self.dispatch(if_clause) |
---|
457 | n/a | |
---|
458 | n/a | def _IfExp(self, t): |
---|
459 | n/a | self.write("(") |
---|
460 | n/a | self.dispatch(t.body) |
---|
461 | n/a | self.write(" if ") |
---|
462 | n/a | self.dispatch(t.test) |
---|
463 | n/a | self.write(" else ") |
---|
464 | n/a | self.dispatch(t.orelse) |
---|
465 | n/a | self.write(")") |
---|
466 | n/a | |
---|
467 | n/a | def _Set(self, t): |
---|
468 | n/a | assert(t.elts) # should be at least one element |
---|
469 | n/a | self.write("{") |
---|
470 | n/a | interleave(lambda: self.write(", "), self.dispatch, t.elts) |
---|
471 | n/a | self.write("}") |
---|
472 | n/a | |
---|
473 | n/a | def _Dict(self, t): |
---|
474 | n/a | self.write("{") |
---|
475 | n/a | def write_key_value_pair(k, v): |
---|
476 | n/a | self.dispatch(k) |
---|
477 | n/a | self.write(": ") |
---|
478 | n/a | self.dispatch(v) |
---|
479 | n/a | |
---|
480 | n/a | def write_item(item): |
---|
481 | n/a | k, v = item |
---|
482 | n/a | if k is None: |
---|
483 | n/a | # for dictionary unpacking operator in dicts {**{'y': 2}} |
---|
484 | n/a | # see PEP 448 for details |
---|
485 | n/a | self.write("**") |
---|
486 | n/a | self.dispatch(v) |
---|
487 | n/a | else: |
---|
488 | n/a | write_key_value_pair(k, v) |
---|
489 | n/a | interleave(lambda: self.write(", "), write_item, zip(t.keys, t.values)) |
---|
490 | n/a | self.write("}") |
---|
491 | n/a | |
---|
492 | n/a | def _Tuple(self, t): |
---|
493 | n/a | self.write("(") |
---|
494 | n/a | if len(t.elts) == 1: |
---|
495 | n/a | elt = t.elts[0] |
---|
496 | n/a | self.dispatch(elt) |
---|
497 | n/a | self.write(",") |
---|
498 | n/a | else: |
---|
499 | n/a | interleave(lambda: self.write(", "), self.dispatch, t.elts) |
---|
500 | n/a | self.write(")") |
---|
501 | n/a | |
---|
502 | n/a | unop = {"Invert":"~", "Not": "not", "UAdd":"+", "USub":"-"} |
---|
503 | n/a | def _UnaryOp(self, t): |
---|
504 | n/a | self.write("(") |
---|
505 | n/a | self.write(self.unop[t.op.__class__.__name__]) |
---|
506 | n/a | self.write(" ") |
---|
507 | n/a | self.dispatch(t.operand) |
---|
508 | n/a | self.write(")") |
---|
509 | n/a | |
---|
510 | n/a | binop = { "Add":"+", "Sub":"-", "Mult":"*", "MatMult":"@", "Div":"/", "Mod":"%", |
---|
511 | n/a | "LShift":"<<", "RShift":">>", "BitOr":"|", "BitXor":"^", "BitAnd":"&", |
---|
512 | n/a | "FloorDiv":"//", "Pow": "**"} |
---|
513 | n/a | def _BinOp(self, t): |
---|
514 | n/a | self.write("(") |
---|
515 | n/a | self.dispatch(t.left) |
---|
516 | n/a | self.write(" " + self.binop[t.op.__class__.__name__] + " ") |
---|
517 | n/a | self.dispatch(t.right) |
---|
518 | n/a | self.write(")") |
---|
519 | n/a | |
---|
520 | n/a | cmpops = {"Eq":"==", "NotEq":"!=", "Lt":"<", "LtE":"<=", "Gt":">", "GtE":">=", |
---|
521 | n/a | "Is":"is", "IsNot":"is not", "In":"in", "NotIn":"not in"} |
---|
522 | n/a | def _Compare(self, t): |
---|
523 | n/a | self.write("(") |
---|
524 | n/a | self.dispatch(t.left) |
---|
525 | n/a | for o, e in zip(t.ops, t.comparators): |
---|
526 | n/a | self.write(" " + self.cmpops[o.__class__.__name__] + " ") |
---|
527 | n/a | self.dispatch(e) |
---|
528 | n/a | self.write(")") |
---|
529 | n/a | |
---|
530 | n/a | boolops = {ast.And: 'and', ast.Or: 'or'} |
---|
531 | n/a | def _BoolOp(self, t): |
---|
532 | n/a | self.write("(") |
---|
533 | n/a | s = " %s " % self.boolops[t.op.__class__] |
---|
534 | n/a | interleave(lambda: self.write(s), self.dispatch, t.values) |
---|
535 | n/a | self.write(")") |
---|
536 | n/a | |
---|
537 | n/a | def _Attribute(self,t): |
---|
538 | n/a | self.dispatch(t.value) |
---|
539 | n/a | # Special case: 3.__abs__() is a syntax error, so if t.value |
---|
540 | n/a | # is an integer literal then we need to either parenthesize |
---|
541 | n/a | # it or add an extra space to get 3 .__abs__(). |
---|
542 | n/a | if ((isinstance(t.value, ast.Num) and isinstance(t.value.n, int)) |
---|
543 | n/a | or (isinstance(t.value, ast.Constant) and isinstance(t.value.value, int))): |
---|
544 | n/a | self.write(" ") |
---|
545 | n/a | self.write(".") |
---|
546 | n/a | self.write(t.attr) |
---|
547 | n/a | |
---|
548 | n/a | def _Call(self, t): |
---|
549 | n/a | self.dispatch(t.func) |
---|
550 | n/a | self.write("(") |
---|
551 | n/a | comma = False |
---|
552 | n/a | for e in t.args: |
---|
553 | n/a | if comma: self.write(", ") |
---|
554 | n/a | else: comma = True |
---|
555 | n/a | self.dispatch(e) |
---|
556 | n/a | for e in t.keywords: |
---|
557 | n/a | if comma: self.write(", ") |
---|
558 | n/a | else: comma = True |
---|
559 | n/a | self.dispatch(e) |
---|
560 | n/a | self.write(")") |
---|
561 | n/a | |
---|
562 | n/a | def _Subscript(self, t): |
---|
563 | n/a | self.dispatch(t.value) |
---|
564 | n/a | self.write("[") |
---|
565 | n/a | self.dispatch(t.slice) |
---|
566 | n/a | self.write("]") |
---|
567 | n/a | |
---|
568 | n/a | def _Starred(self, t): |
---|
569 | n/a | self.write("*") |
---|
570 | n/a | self.dispatch(t.value) |
---|
571 | n/a | |
---|
572 | n/a | # slice |
---|
573 | n/a | def _Ellipsis(self, t): |
---|
574 | n/a | self.write("...") |
---|
575 | n/a | |
---|
576 | n/a | def _Index(self, t): |
---|
577 | n/a | self.dispatch(t.value) |
---|
578 | n/a | |
---|
579 | n/a | def _Slice(self, t): |
---|
580 | n/a | if t.lower: |
---|
581 | n/a | self.dispatch(t.lower) |
---|
582 | n/a | self.write(":") |
---|
583 | n/a | if t.upper: |
---|
584 | n/a | self.dispatch(t.upper) |
---|
585 | n/a | if t.step: |
---|
586 | n/a | self.write(":") |
---|
587 | n/a | self.dispatch(t.step) |
---|
588 | n/a | |
---|
589 | n/a | def _ExtSlice(self, t): |
---|
590 | n/a | interleave(lambda: self.write(', '), self.dispatch, t.dims) |
---|
591 | n/a | |
---|
592 | n/a | # argument |
---|
593 | n/a | def _arg(self, t): |
---|
594 | n/a | self.write(t.arg) |
---|
595 | n/a | if t.annotation: |
---|
596 | n/a | self.write(": ") |
---|
597 | n/a | self.dispatch(t.annotation) |
---|
598 | n/a | |
---|
599 | n/a | # others |
---|
600 | n/a | def _arguments(self, t): |
---|
601 | n/a | first = True |
---|
602 | n/a | # normal arguments |
---|
603 | n/a | defaults = [None] * (len(t.args) - len(t.defaults)) + t.defaults |
---|
604 | n/a | for a, d in zip(t.args, defaults): |
---|
605 | n/a | if first:first = False |
---|
606 | n/a | else: self.write(", ") |
---|
607 | n/a | self.dispatch(a) |
---|
608 | n/a | if d: |
---|
609 | n/a | self.write("=") |
---|
610 | n/a | self.dispatch(d) |
---|
611 | n/a | |
---|
612 | n/a | # varargs, or bare '*' if no varargs but keyword-only arguments present |
---|
613 | n/a | if t.vararg or t.kwonlyargs: |
---|
614 | n/a | if first:first = False |
---|
615 | n/a | else: self.write(", ") |
---|
616 | n/a | self.write("*") |
---|
617 | n/a | if t.vararg: |
---|
618 | n/a | self.write(t.vararg.arg) |
---|
619 | n/a | if t.vararg.annotation: |
---|
620 | n/a | self.write(": ") |
---|
621 | n/a | self.dispatch(t.vararg.annotation) |
---|
622 | n/a | |
---|
623 | n/a | # keyword-only arguments |
---|
624 | n/a | if t.kwonlyargs: |
---|
625 | n/a | for a, d in zip(t.kwonlyargs, t.kw_defaults): |
---|
626 | n/a | if first:first = False |
---|
627 | n/a | else: self.write(", ") |
---|
628 | n/a | self.dispatch(a), |
---|
629 | n/a | if d: |
---|
630 | n/a | self.write("=") |
---|
631 | n/a | self.dispatch(d) |
---|
632 | n/a | |
---|
633 | n/a | # kwargs |
---|
634 | n/a | if t.kwarg: |
---|
635 | n/a | if first:first = False |
---|
636 | n/a | else: self.write(", ") |
---|
637 | n/a | self.write("**"+t.kwarg.arg) |
---|
638 | n/a | if t.kwarg.annotation: |
---|
639 | n/a | self.write(": ") |
---|
640 | n/a | self.dispatch(t.kwarg.annotation) |
---|
641 | n/a | |
---|
642 | n/a | def _keyword(self, t): |
---|
643 | n/a | if t.arg is None: |
---|
644 | n/a | self.write("**") |
---|
645 | n/a | else: |
---|
646 | n/a | self.write(t.arg) |
---|
647 | n/a | self.write("=") |
---|
648 | n/a | self.dispatch(t.value) |
---|
649 | n/a | |
---|
650 | n/a | def _Lambda(self, t): |
---|
651 | n/a | self.write("(") |
---|
652 | n/a | self.write("lambda ") |
---|
653 | n/a | self.dispatch(t.args) |
---|
654 | n/a | self.write(": ") |
---|
655 | n/a | self.dispatch(t.body) |
---|
656 | n/a | self.write(")") |
---|
657 | n/a | |
---|
658 | n/a | def _alias(self, t): |
---|
659 | n/a | self.write(t.name) |
---|
660 | n/a | if t.asname: |
---|
661 | n/a | self.write(" as "+t.asname) |
---|
662 | n/a | |
---|
663 | n/a | def _withitem(self, t): |
---|
664 | n/a | self.dispatch(t.context_expr) |
---|
665 | n/a | if t.optional_vars: |
---|
666 | n/a | self.write(" as ") |
---|
667 | n/a | self.dispatch(t.optional_vars) |
---|
668 | n/a | |
---|
669 | n/a | def roundtrip(filename, output=sys.stdout): |
---|
670 | n/a | with open(filename, "rb") as pyfile: |
---|
671 | n/a | encoding = tokenize.detect_encoding(pyfile.readline)[0] |
---|
672 | n/a | with open(filename, "r", encoding=encoding) as pyfile: |
---|
673 | n/a | source = pyfile.read() |
---|
674 | n/a | tree = compile(source, filename, "exec", ast.PyCF_ONLY_AST) |
---|
675 | n/a | Unparser(tree, output) |
---|
676 | n/a | |
---|
677 | n/a | |
---|
678 | n/a | |
---|
679 | n/a | def testdir(a): |
---|
680 | n/a | try: |
---|
681 | n/a | names = [n for n in os.listdir(a) if n.endswith('.py')] |
---|
682 | n/a | except OSError: |
---|
683 | n/a | print("Directory not readable: %s" % a, file=sys.stderr) |
---|
684 | n/a | else: |
---|
685 | n/a | for n in names: |
---|
686 | n/a | fullname = os.path.join(a, n) |
---|
687 | n/a | if os.path.isfile(fullname): |
---|
688 | n/a | output = io.StringIO() |
---|
689 | n/a | print('Testing %s' % fullname) |
---|
690 | n/a | try: |
---|
691 | n/a | roundtrip(fullname, output) |
---|
692 | n/a | except Exception as e: |
---|
693 | n/a | print(' Failed to compile, exception is %s' % repr(e)) |
---|
694 | n/a | elif os.path.isdir(fullname): |
---|
695 | n/a | testdir(fullname) |
---|
696 | n/a | |
---|
697 | n/a | def main(args): |
---|
698 | n/a | if args[0] == '--testdir': |
---|
699 | n/a | for a in args[1:]: |
---|
700 | n/a | testdir(a) |
---|
701 | n/a | else: |
---|
702 | n/a | for a in args: |
---|
703 | n/a | roundtrip(a) |
---|
704 | n/a | |
---|
705 | n/a | if __name__=='__main__': |
---|
706 | n/a | main(sys.argv[1:]) |
---|