From d47415a677c3cfbc451d421f2d8c3e41ea7dc44c Mon Sep 17 00:00:00 2001 From: rocky Date: Wed, 23 Dec 2015 15:42:52 -0500 Subject: [PATCH] Start Python3 class(superclass) handling --- test/bytecode_2.7/00_pass.pyc | Bin 0 -> 126 bytes test/bytecode_2.7/01_class.pyc | Bin 296 -> 296 bytes test/bytecode_3.4/00_import.pyc | Bin 0 -> 176 bytes test/bytecode_3.4/00_pass.pyc | Bin 0 -> 122 bytes test/bytecode_3.4/01_class.pyc | Bin 260 -> 274 bytes test/simple_source/simple_stmts/00_import.py | 4 ++ test/simple_source/simple_stmts/00_pass.py | 3 ++ uncompyle6/parsers/parse3.py | 7 ++- uncompyle6/semantics/pysource.py | 43 +++++++++++++++---- 9 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 test/bytecode_2.7/00_pass.pyc create mode 100644 test/bytecode_3.4/00_import.pyc create mode 100644 test/bytecode_3.4/00_pass.pyc create mode 100644 test/simple_source/simple_stmts/00_import.py create mode 100644 test/simple_source/simple_stmts/00_pass.py diff --git a/test/bytecode_2.7/00_pass.pyc b/test/bytecode_2.7/00_pass.pyc new file mode 100644 index 0000000000000000000000000000000000000000..d19d555f6a7deb13471d4991585ce5788e61e0e4 GIT binary patch literal 126 zcmZSn%*(Z%wK^=B0SXv_v;zPO2TqykaIG!N3FnF`5{e literal 0 HcmV?d00001 diff --git a/test/bytecode_2.7/01_class.pyc b/test/bytecode_2.7/01_class.pyc index 543da485fd698edb305f958420b190cca3ca6101..f7babcd0d45ad102a259fe72c91c10084f79c929 100644 GIT binary patch delta 16 XcmZ3%w1SD9`7WYl3P-&Z(tA)l*}(G(JQFD#bJ}1pHiBWYR3%JTMROXhYyNsTYg sFD*(=)rav)a!ZQ!4GiK75{rxV3My}L*yQG?l;)(`fwUJh0SN{s0J8WSm;e9( literal 0 HcmV?d00001 diff --git a/test/bytecode_3.4/01_class.pyc b/test/bytecode_3.4/01_class.pyc index a86f0663e393ecb369822e9e4875fced1aa2be66..71a91979238c6660c4c9bc86088c6357d2e48437 100644 GIT binary patch delta 116 zcmZo+n#5%Nj)#}4_*hj~3?l=>V+JI^0%SV?aj_JTa8F@iNMU4X0h6f=49(08QH&EU vOc|Xf=7r1gfs`=;2_D8GpfL=7noPGiT`Q7P3raHc^CsR<<^-z%$}j)`lY$hP delta 102 zcmbQl)WT%(j)#})Y;|Q=3?l=>V+JI^3}ib1aj`g%a8F@iNMU4X0h7&43{eadtxXvn gCl-babA!|{0SO+)BA^)zewvIEpDA;Kg@IfK0C?FCr~m)} diff --git a/test/simple_source/simple_stmts/00_import.py b/test/simple_source/simple_stmts/00_import.py new file mode 100644 index 00000000..5c611426 --- /dev/null +++ b/test/simple_source/simple_stmts/00_import.py @@ -0,0 +1,4 @@ +# Tests: + +import sys +from os import path diff --git a/test/simple_source/simple_stmts/00_pass.py b/test/simple_source/simple_stmts/00_pass.py new file mode 100644 index 00000000..3deb4538 --- /dev/null +++ b/test/simple_source/simple_stmts/00_pass.py @@ -0,0 +1,3 @@ +# Tests: +# assign ::= expr designator +pass diff --git a/uncompyle6/parsers/parse3.py b/uncompyle6/parsers/parse3.py index 0dca702e..3093a1b6 100644 --- a/uncompyle6/parsers/parse3.py +++ b/uncompyle6/parsers/parse3.py @@ -370,9 +370,12 @@ class Python3Parser(PythonParser): kwarg ::= LOAD_CONST expr + classdef ::= buildclass designator + # Python3 introduced LOAD_BUILD_CLASS - classdef ::= LOAD_BUILD_CLASS mkfunc LOAD_CONST - CALL_FUNCTION_2 designator + # FIXME: the below should be created by custom rules + buildclass ::= LOAD_BUILD_CLASS mkfunc LOAD_CONST LOAD_NAME CALL_FUNCTION_3 + buildclass ::= LOAD_BUILD_CLASS mkfunc LOAD_CONST CALL_FUNCTION_2 stmt ::= classdefdeco classdefdeco ::= classdefdeco1 designator diff --git a/uncompyle6/semantics/pysource.py b/uncompyle6/semantics/pysource.py index edf8475b..41f52727 100644 --- a/uncompyle6/semantics/pysource.py +++ b/uncompyle6/semantics/pysource.py @@ -1053,26 +1053,29 @@ class Walker(GenericASTTraversal, object): # class definition ('class X(A,B,C):') cclass = self.currentclass + if self.version > 3.0: buildclass = node[1] - self.currentclass = str(buildclass[1].pattr) + build_list = node[0] + subclass = build_list[1][0].attr else: buildclass = node[0] - self.currentclass = str(buildclass[0].pattr) + build_list = buildclass[1][0] + subclass = buildclass[-3][0].attr self.write('\n\n') + self.currentclass = str(buildclass[0].pattr) self.write(self.indent, 'class ', self.currentclass) - self.print_super_classes(buildclass) + if self.version > 3.0: + self.print_super_classes3(build_list) + else: + self.print_super_classes(build_list) self.print_(':') # class body self.indentMore() - - if self.version > 3.0: - self.build_class(buildclass[0].attr) - else: - self.build_class(buildclass[-3][0].attr) + self.build_class(subclass) self.indentLess() self.currentclass = cclass @@ -1086,7 +1089,6 @@ class Walker(GenericASTTraversal, object): n_classdefdeco2 = n_classdef def print_super_classes(self, node): - node = node[1][0] if not (node == 'build_list'): return @@ -1100,6 +1102,29 @@ class Walker(GenericASTTraversal, object): self.write(')') + def print_super_classes3(self, node): + + # FIXME: put blow logic into grammar + # as a custom rule + i = 0 + for i, n in enumerate(node[:-1]): + if n.type == 'LOAD_NAME': + break + pass + + if i == 0: + return + self.write('(') + line_separator = ', ' + sep = '' + while i <= len(node) - 2: + value = self.traverse(node[i]) + i += 1 + self.write(sep, value) + sep = line_separator + + self.write(')') + def n_mapexpr(self, node): """ prettyprint a mapexpr