From 6b0bb124ea3a36a07b25e55480d9c0aeb39bad12 Mon Sep 17 00:00:00 2001 From: rocky Date: Mon, 21 Dec 2015 15:33:10 -0500 Subject: [PATCH] Start Python3 execption handling --- test/bytecode_2.7/01_try_except.pyc | Bin 0 -> 295 bytes test/bytecode_2.7/README | 7 ++++++ test/bytecode_3.2/README | 8 ++++++ test/bytecode_3.4/00_assign.pyc | Bin 0 -> 215 bytes test/bytecode_3.4/01_try_except.pyc | Bin 0 -> 283 bytes test/bytecode_3.4/README | 7 ++++++ test/bytecode_3.4/assign.pyc | Bin 128 -> 0 bytes test/bytecode_3.4/assign_none.pyc | Bin 128 -> 0 bytes test/bytecode_3.4/assign_none_str.pyc | Bin 138 -> 0 bytes test/bytecompile-tests | 16 ++++++------ test/simple_source/README | 7 +++++- test/simple_source/exception/01_try_except.py | 23 ++++++++++++++++++ test/simple_source/misc/assign_none_str.py | 1 - .../assign.py => simple_stmts/00_assign.py} | 4 ++- uncompyle6/parsers/parse3.py | 9 ++++--- uncompyle6/semantics/pysource.py | 12 ++++++++- 16 files changed, 78 insertions(+), 16 deletions(-) create mode 100644 test/bytecode_2.7/01_try_except.pyc create mode 100644 test/bytecode_3.2/README create mode 100644 test/bytecode_3.4/00_assign.pyc create mode 100644 test/bytecode_3.4/01_try_except.pyc delete mode 100644 test/bytecode_3.4/assign.pyc delete mode 100644 test/bytecode_3.4/assign_none.pyc delete mode 100644 test/bytecode_3.4/assign_none_str.pyc delete mode 100644 test/simple_source/misc/assign_none_str.py rename test/simple_source/{misc/assign.py => simple_stmts/00_assign.py} (58%) diff --git a/test/bytecode_2.7/01_try_except.pyc b/test/bytecode_2.7/01_try_except.pyc new file mode 100644 index 0000000000000000000000000000000000000000..d68fa8dd91c360df6c6bdb70f8db44e76b6cdec5 GIT binary patch literal 295 zcmYj~%L>9U5JhL2_@Gk3mEae2rFJjs#)TU~jrJdg7@BIP@OkJTX}S literal 0 HcmV?d00001 diff --git a/test/bytecode_2.7/README b/test/bytecode_2.7/README index f4b40fd9..2c78566d 100644 --- a/test/bytecode_2.7/README +++ b/test/bytecode_2.7/README @@ -1 +1,8 @@ These are byte-compiled programs compiled by Python 2.7 + +The numbers in the filenames are to assist running the programs from +the simplest to more complex. For example, many tests have assignment +statements or function calls, so we want to test those constructs +first. + +Code and organization merged from uncompyle3, uncompyle{,2}, and pycdc diff --git a/test/bytecode_3.2/README b/test/bytecode_3.2/README new file mode 100644 index 00000000..fd8f4752 --- /dev/null +++ b/test/bytecode_3.2/README @@ -0,0 +1,8 @@ +These are byte-compiled programs compiled by Python 3.2 + +The numbers in the filenames are to assist running the programs from +the simplest to more complex. For example, many tests have assignment +statements or function calls, so we want to test those constructs +first. + +Code and organization merged from uncompyle3, uncompyle{,2}, and pycdc diff --git a/test/bytecode_3.4/00_assign.pyc b/test/bytecode_3.4/00_assign.pyc new file mode 100644 index 0000000000000000000000000000000000000000..a142dfbe54218f5d6a5b60e20af29b643000b24b GIT binary patch literal 215 zcmX|*!3u&v7=-^dlZd)>=pBM=Jq1BT(A`VWK?u=x(IwrL{nyY(>Xp29?*Tfsc5q-m zW)1`MSuE$PLwmabz}tEhvFU7$Yl{pZvLp}@$Q+2AAzwF>-VK%Hq1ZYAwvnVKX;0V- z^wh5Y_}(dMMtN(vmPzS98PZ&sqDiS%?9tJ>Gli0VBbln|uH<`aI2wwgs!JZDYBb|x eGnGLa1aX2Wa=EL!es-)>)|C9@4rd2=#3Mi7F*)7< literal 0 HcmV?d00001 diff --git a/test/bytecode_3.4/01_try_except.pyc b/test/bytecode_3.4/01_try_except.pyc new file mode 100644 index 0000000000000000000000000000000000000000..9cefc157348ca57197c824df6e78ac69eb77f99c GIT binary patch literal 283 zcmaFI!^>qKUlF#4k%8ec0}_x2vK@f9I2%Y*axtVZFhnshgy(TEFfuYm<}orvAPEXF zu%t3FWOFeT8N;QkG{M>!A=)J1DxyIuQkX!(U}M5lm>E)7K->r*D_E2DCD17hFPT6D zGm!9m$pR!bnQk#w+~W4kEyyn_aV;v!FIve^#151I6TdWyGjj`aQsayBON)|I^;0X7 zQwvHm^Yio#4C70RD&rvndIgoYIBatBQ%ZAE?bv{tizR>rGb0xxGb1Yph~{NvWnu%< G{7eAFRye8v literal 0 HcmV?d00001 diff --git a/test/bytecode_3.4/README b/test/bytecode_3.4/README index c3a1ca1c..1be059f8 100644 --- a/test/bytecode_3.4/README +++ b/test/bytecode_3.4/README @@ -1 +1,8 @@ These are byte-compiled programs compiled by Python 3.4 + +The numbers in the filenames are to assist running the programs from +the simplest to more complex. For example, many tests have assignment +statements or function calls, so we want to test those constructs +first. + +Code and organization merged from uncompyle3, uncompyle{,2}, and pycdc diff --git a/test/bytecode_3.4/assign.pyc b/test/bytecode_3.4/assign.pyc deleted file mode 100644 index 385fb1e94c689d3b5f2e29f01c512aeb1bf99f3d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 128 zcmaFI!^?Fuw>Zq0fq~&M5W@i@kmUfx#auulg@GXoNHQ`6Ycgf(FflOrX);E!mL(Qt z=C5QZ0x1O(zvT4vi!*Zza#D4R^Gl18Q}uH*i<9*ei;FYU^YjWTZ*kb<=BJeAq}s87 IOa@{G03ahAZ~y=R diff --git a/test/bytecode_3.4/assign_none.pyc b/test/bytecode_3.4/assign_none.pyc deleted file mode 100644 index ea9a15906f56006864375f1d7661dc1783380b5d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 128 zcmaFI!^`#GCnJoPfq~&M5W@i@kmUfx#auulg@GXoOa^N*`e`yov6dwkW#+GBC<3Vh z6Tg)8^ouid3vyC*i}Op1l2i3_GmDe;6N`&8)AQo<^7B&l3My}L*yQG?l;)(`f%Fvv GF#`Z#upPVr diff --git a/test/bytecode_3.4/assign_none_str.pyc b/test/bytecode_3.4/assign_none_str.pyc deleted file mode 100644 index 82973773fdaf17baa68158ecd3042205fbdef090..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 138 zcmaFI!^`#GCnJoXfq~&M5W@i@kmUfx#auulg@GXoNHQ`6Yckzp@ypLk_0wdGVl7K7 z%FJKMP{agO0w#W`>**I~<`(3n>K5mh7A2?Z=Vlfs>n9c$XQt=H=K=M_7nc<26;$5h Su*uC&Da}c>1KCpy#0&uC$06(h diff --git a/test/bytecompile-tests b/test/bytecompile-tests index 3d224d3b..851c473d 100755 --- a/test/bytecompile-tests +++ b/test/bytecompile-tests @@ -55,12 +55,12 @@ tests['2.3'] = tests['2.2'] tests['2.5'] = tests['2.3'] # tests['2.7'] = ['mine'] + tests['2.6'] tests['2.7'] = [ - # 'simple-source/branching/ifelse', - # 'simple-source/branching/if' - 'simple-source/misc/assert', - 'simple-source/misc/assign', - # 'simple-source/call_arguments/keyword', - # 'simple-source/call_arguments/positional' + # 'simple_source/branching/ifelse', + # 'simple_source/branching/if' + 'simple_source/misc/assert', + 'simple_source/misc/assign', + # 'simple_source/call_arguments/keyword', + # 'simple_source/call_arguments/positional' ] tests['2.6'] = tests['2.7'] @@ -71,9 +71,9 @@ def file_matches(files, root, basenames, patterns): if fnmatch(n, pat)]) PY = ('*.py', ) -files = ['simple-source'] +files = ['simple_source'] simple_source = [] -for root, dirs, basenames in os.walk('simple-source'): +for root, dirs, basenames in os.walk('simple_source'): for basename in basenames: if basename.endswith('.py'): simple_source.append(os.path.join(root, basename)[0:-3]) diff --git a/test/simple_source/README b/test/simple_source/README index 59dda111..c4d834fa 100644 --- a/test/simple_source/README +++ b/test/simple_source/README @@ -4,4 +4,9 @@ across all versions of Python. Their simnplicity is to try to make it easier to debug grammar and AST walking routines. -This code originally taken from https://github.com/DarkFenX/uncompyle3 +The numbers in the filenames are to assist running the programs from +the simplest to more complex. For example, many tests have assignment +statements or function calls, so we want to test those constructs +first. + +Code and organization merged from uncompyle3, uncompyle{,2}, and pycdc diff --git a/test/simple_source/exception/01_try_except.py b/test/simple_source/exception/01_try_except.py index 130fac63..8b520311 100644 --- a/test/simple_source/exception/01_try_except.py +++ b/test/simple_source/exception/01_try_except.py @@ -1,4 +1,27 @@ +# Tests: +# trystmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK +# try_middle COME_FROM +# except_stmt ::= except + try: x = 1 except: pass + +# Tests: +# trystmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK +# try_middle COME_FROM +# except_stmt ::= except_cond1 except_suite +# except_suite ::= ... + +try: + x = 1 +except ImportError: + pass + +try: + x = 2 +except ImportError: + x = 3 +finally: + x = 4 diff --git a/test/simple_source/misc/assign_none_str.py b/test/simple_source/misc/assign_none_str.py deleted file mode 100644 index d087be05..00000000 --- a/test/simple_source/misc/assign_none_str.py +++ /dev/null @@ -1 +0,0 @@ -vario = 'None' diff --git a/test/simple_source/misc/assign.py b/test/simple_source/simple_stmts/00_assign.py similarity index 58% rename from test/simple_source/misc/assign.py rename to test/simple_source/simple_stmts/00_assign.py index 13bd4414..a62aed95 100644 --- a/test/simple_source/misc/assign.py +++ b/test/simple_source/simple_stmts/00_assign.py @@ -1,4 +1,6 @@ # Tests: # assign ::= expr designator -vario = 556 +a = 'None' +b = None +c = 556 diff --git a/uncompyle6/parsers/parse3.py b/uncompyle6/parsers/parse3.py index c6a725c2..60a43d17 100644 --- a/uncompyle6/parsers/parse3.py +++ b/uncompyle6/parsers/parse3.py @@ -441,18 +441,19 @@ class Python3Parser(PythonParser): except_stmt ::= except_cond2 except_suite except_stmt ::= except - except_suite ::= c_stmts_opt JUMP_FORWARD - except_suite ::= c_stmts_opt jmp_abs + except_suite ::= c_stmts_opt POP_EXCEPT JUMP_FORWARD + except_suite ::= c_stmts_opt POP_EXCEPT jmp_abs except_suite ::= return_stmts except_cond1 ::= DUP_TOP expr COMPARE_OP jmp_false POP_TOP POP_TOP POP_TOP except_cond2 ::= DUP_TOP expr COMPARE_OP - jmp_false POP_TOP designator POP_TOP + jmp_false POP_TOP designator + except ::= POP_TOP POP_TOP POP_TOP POP_EXCEPT c_stmts_opt JUMP_FORWARD except ::= POP_TOP POP_TOP POP_TOP c_stmts_opt JUMP_FORWARD - except ::= POP_TOP POP_TOP POP_TOP c_stmts_opt jmp_abs + except ::= POP_TOP POP_TOP POP_TOP POP_EXCEPT c_stmts_opt jmp_abs except ::= POP_TOP POP_TOP POP_TOP return_stmts jmp_abs ::= JUMP_ABSOLUTE diff --git a/uncompyle6/semantics/pysource.py b/uncompyle6/semantics/pysource.py index c033c00b..9faa788d 100644 --- a/uncompyle6/semantics/pysource.py +++ b/uncompyle6/semantics/pysource.py @@ -314,7 +314,6 @@ TABLE_DIRECT = { 'tryelsestmtl': ( '%|try:\n%+%c%-%c%|else:\n%+%c%-', 1, 3, 4 ), 'tf_trystmt': ( '%c%-%c%+', 1, 3 ), 'tf_tryelsestmt': ( '%c%-%c%|else:\n%+%c', 1, 3, 4 ), - 'except': ( '%|except:\n%+%c%-', 3 ), 'except_cond1': ( '%|except %c:\n', 1 ), 'except_cond2': ( '%|except %c as %c:\n', 1, 5 ), 'except_suite': ( '%+%c%-%C', 0, (1, maxint, '') ), @@ -490,6 +489,14 @@ class Walker(GenericASTTraversal, object): self.currentclass = None self.pending_newlines = 0 + if version >= 3.0: + # Python 3 adds a POP_EXCEPT instruction + TABLE_DIRECT['except'] = ('%|except:\n%+%c%-', 4 ) + else: + TABLE_DIRECT['except'] = ('%|except:\n%+%c%-', 3 ) + pass + return + f = property(lambda s: s.params['f'], lambda s, x: s.params.__setitem__('f', x), lambda s: s.params.__delitem__('f'), @@ -1184,6 +1191,9 @@ class Walker(GenericASTTraversal, object): # self.print_("-----") # self.print(startnode) + # from trepan.api import debug + # debug(start_opts={'startup-profile': True}) + fmt = entry[0] arg = 1 i = 0