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