|
63 | 63 |
|
64 | 64 | import inspect, sys, re |
65 | 65 |
|
66 | | - |
67 | 66 | from uncompyle6 import PYTHON3 |
68 | 67 | from uncompyle6.parser import get_python_parser |
69 | 68 | from uncompyle6.parsers.astnode import AST |
|
124 | 123 | INDENT_PER_LEVEL = ' ' # additional intent per pretty-print level |
125 | 124 |
|
126 | 125 | TABLE_R = { |
127 | | - 'POP_TOP': ( '%|%c\n', 0 ), |
128 | 126 | 'STORE_ATTR': ( '%c.%[1]{pattr}', 0), |
129 | 127 | # 'STORE_SUBSCR': ( '%c[%c]', 0, 1 ), |
| 128 | + 'DELETE_ATTR': ( '%|del %c.%[-1]{pattr}\n', 0 ), |
| 129 | +# 'EXEC_STMT': ( '%|exec %c in %[1]C\n', 0, (0,maxint,', ') ), |
| 130 | +} |
| 131 | + |
| 132 | +if not PYTHON3: |
| 133 | + TABLE_R.update({ |
130 | 134 | 'STORE_SLICE+0': ( '%c[:]', 0 ), |
131 | 135 | 'STORE_SLICE+1': ( '%c[%p:]', 0, (1, 100) ), |
132 | 136 | 'STORE_SLICE+2': ( '%c[:%p]', 0, (1, 100) ), |
|
135 | 139 | 'DELETE_SLICE+1': ( '%|del %c[%c:]\n', 0, 1 ), |
136 | 140 | 'DELETE_SLICE+2': ( '%|del %c[:%c]\n', 0, 1 ), |
137 | 141 | 'DELETE_SLICE+3': ( '%|del %c[%c:%c]\n', 0, 1, 2 ), |
138 | | - 'DELETE_ATTR': ( '%|del %c.%[-1]{pattr}\n', 0 ), |
139 | | -# 'EXEC_STMT': ( '%|exec %c in %[1]C\n', 0, (0,maxint,', ') ), |
140 | | -} |
| 142 | + }) |
| 143 | + |
141 | 144 | TABLE_R0 = { |
142 | 145 | # 'BUILD_LIST': ( '[%C]', (0,-1,', ') ), |
143 | 146 | # 'BUILD_TUPLE': ( '(%C)', (0,-1,', ') ), |
|
317 | 320 | 'except_cond1': ( '%|except %c:\n', 1 ), |
318 | 321 | 'except_cond2': ( '%|except %c as %c:\n', 1, 5 ), |
319 | 322 | 'except_suite': ( '%+%c%-%C', 0, (1, maxint, '') ), |
| 323 | + 'except_suite_finalize': ( '%+%c%-%C', 1, (3, maxint, '') ), |
320 | 324 | 'tryfinallystmt': ( '%|try:\n%+%c%-%|finally:\n%+%c%-\n\n', 1, 5 ), |
321 | 325 | 'withstmt': ( '%|with %c:\n%+%c%-', 0, 3), |
322 | 326 | 'withasstmt': ( '%|with %c as %c:\n%+%c%-', 0, 2, 3), |
@@ -438,7 +442,6 @@ def __str__(self): |
438 | 442 | lines.extend( ['', str(self.error)] ) |
439 | 443 | return '\n'.join(lines) |
440 | 444 |
|
441 | | - |
442 | 445 | def find_globals(node, globs): |
443 | 446 | """Find globals in this statement.""" |
444 | 447 | for n in node: |
@@ -467,6 +470,13 @@ def find_none(node): |
467 | 470 | return True |
468 | 471 | return False |
469 | 472 |
|
| 473 | +class WalkerError(Exception): |
| 474 | + def __init__(self, errmsg): |
| 475 | + self.errmsg = errmsg |
| 476 | + |
| 477 | + def __str__(self): |
| 478 | + return self.errmsg |
| 479 | + |
470 | 480 | class Walker(GenericASTTraversal, object): |
471 | 481 | stacked_params = ('f', 'indent', 'isLambda', '_globals') |
472 | 482 |
|
@@ -1233,8 +1243,6 @@ def engine(self, entry, startnode): |
1233 | 1243 | # self.print_("-----") |
1234 | 1244 | # self.print(startnode) |
1235 | 1245 |
|
1236 | | - # from trepan.api import debug |
1237 | | - # debug(start_opts={'startup-profile': True}) |
1238 | 1246 |
|
1239 | 1247 | fmt = entry[0] |
1240 | 1248 | arg = 1 |
@@ -1262,7 +1270,18 @@ def engine(self, entry, startnode): |
1262 | 1270 | elif typ == ',': |
1263 | 1271 | pass |
1264 | 1272 | elif typ == 'c': |
1265 | | - self.preorder(node[entry[arg]]) |
| 1273 | + # FIXME: In Python3 sometimes like from |
| 1274 | + # importfrom |
| 1275 | + # importlist2 |
| 1276 | + # import_as |
| 1277 | + # designator |
| 1278 | + # STORE_NAME 'load_entry_point' |
| 1279 | + # POP_TOP '' (2, (0, 1)) |
| 1280 | + # we get that weird POP_TOP tuple, e.g (2, (0,1)). |
| 1281 | + # Why? and |
| 1282 | + # Is there some sort of invalid bounds access going on? |
| 1283 | + if isinstance(entry[arg], int): |
| 1284 | + self.preorder(node[entry[arg]]) |
1266 | 1285 | arg += 1 |
1267 | 1286 | elif typ == 'p': |
1268 | 1287 | p = self.prec |
@@ -1637,6 +1656,8 @@ def deparse_code(version, co, out=sys.stdout, showasm=False, showast=False, |
1637 | 1656 | for g in deparsed.mod_globs: |
1638 | 1657 | deparsed.write('# global %s ## Warning: Unused global' % g) |
1639 | 1658 |
|
| 1659 | + if deparsed.ERROR: |
| 1660 | + raise WalkerError("Deparsing stopped due to parse error") |
1640 | 1661 | return deparsed |
1641 | 1662 |
|
1642 | 1663 | if __name__ == '__main__': |
|
0 commit comments