From 73df5f373752826bbfe14b9d9b941e28edb59610 Mon Sep 17 00:00:00 2001 From: rocky Date: Sat, 18 Jun 2016 23:43:50 -0400 Subject: [PATCH] 3.4 set comprehension if bug --- .../comprehension/06_setif_comprehension.py | 6 ++++++ uncompyle6/main.py | 1 + uncompyle6/parser.py | 11 +++++++++-- uncompyle6/semantics/pysource.py | 13 +++++++------ 4 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 test/simple_source/comprehension/06_setif_comprehension.py diff --git a/test/simple_source/comprehension/06_setif_comprehension.py b/test/simple_source/comprehension/06_setif_comprehension.py new file mode 100644 index 00000000..dcb825a1 --- /dev/null +++ b/test/simple_source/comprehension/06_setif_comprehension.py @@ -0,0 +1,6 @@ +# Bug in python 3.4 abc.py +# Set comprehension + +abstracts = {name + for name, value in namespace.items() + if getattr(value, "__isabstractmethod__", False)} diff --git a/uncompyle6/main.py b/uncompyle6/main.py index a03068f6..345c3c6d 100644 --- a/uncompyle6/main.py +++ b/uncompyle6/main.py @@ -34,6 +34,7 @@ def uncompyle(version, co, out=None, showasm=False, showast=False, except pysource.SourceWalkerError as e: # deparsing failed print("\n") + print(co.co_filename) if real_out != out: print("\n", file=real_out) print(e, file=real_out) diff --git a/uncompyle6/parser.py b/uncompyle6/parser.py index 6eaf2789..c80d0638 100644 --- a/uncompyle6/parser.py +++ b/uncompyle6/parser.py @@ -39,8 +39,15 @@ class PythonParser(GenericASTBuilder): for i in dir(self): setattr(self, i, None) - def error(self, token): - raise ParserError(token, token.offset) + def error(self, tokens, index): + start = index - 2 if index - 2 > 0 else 0 + finish = index +2 if index + 2 < len(tokens) else len(tokens) + err_token = tokens[index] + print("Token context:") + for i in range(start, finish): + indent = ' ' if i != index else '-> ' + print("%s%s" % (indent, tokens[i])) + raise ParserError(err_token, err_token.offset) def typestring(self, token): return token.type diff --git a/uncompyle6/semantics/pysource.py b/uncompyle6/semantics/pysource.py index 16ff4280..ec251f95 100644 --- a/uncompyle6/semantics/pysource.py +++ b/uncompyle6/semantics/pysource.py @@ -1095,20 +1095,21 @@ class SourceWalker(GenericASTTraversal, object): ## FIXME: I'm not totally sure this is right. # find innermost node - list_if_node = None + if_node = None while n in ('list_iter', 'comp_iter'): n = n[0] # recurse one step - if n == 'list_for': + if n == 'list_for': if n[2] == 'designator': designator = n[2] n = n[3] - elif n in ['list_if', 'list_if_not']: - list_if_node = n[0] + elif n in ['list_if', 'list_if_not', 'comp_if']: + if_node = n[0] if n[1] == 'designator': designator = n[1] n = n[2] pass pass + assert n.type in ('lc_body', 'comp_body'), ast assert designator, "Couldn't find designator in list/set comprehension" @@ -1117,9 +1118,9 @@ class SourceWalker(GenericASTTraversal, object): self.preorder(designator) self.write(' in ') self.preorder(node[-3]) - if list_if_node: + if if_node: self.write(' if ') - self.preorder(list_if_node) + self.preorder(if_node) self.prec = p def listcomprehension_walk2(self, node):