From 5b916567fe2ddbf26c1a2cc7c5fa15bf0d15f534 Mon Sep 17 00:00:00 2001 From: rocky Date: Sun, 3 Dec 2017 06:30:33 -0500 Subject: [PATCH] NT try-middle -> except-handler to match AST --- .../simple_source/bug_pypy27/03_try_return.py | 4 +- test/simple_source/exception/01_try_except.py | 4 +- test/simple_source/exception/20_try_except.py | 4 +- test/simple_source/exception/25_try_except.py | 14 +++---- uncompyle6/parsers/parse2.py | 28 ++++++------- uncompyle6/parsers/parse25.py | 2 +- uncompyle6/parsers/parse26.py | 42 +++++++++---------- uncompyle6/parsers/parse27.py | 2 +- uncompyle6/parsers/parse3.py | 39 ++++++++--------- uncompyle6/parsers/parse32.py | 24 +++++------ uncompyle6/parsers/parse33.py | 2 +- uncompyle6/parsers/parse36.py | 10 ++--- 12 files changed, 88 insertions(+), 87 deletions(-) diff --git a/test/simple_source/bug_pypy27/03_try_return.py b/test/simple_source/bug_pypy27/03_try_return.py index aafdadc9..22fa23b7 100644 --- a/test/simple_source/bug_pypy27/03_try_return.py +++ b/test/simple_source/bug_pypy27/03_try_return.py @@ -1,7 +1,7 @@ # From PyPy 2.7 argparse.py # PyPY reduces branches as a result of the return statement -# So we need a new rules for try_except and try_middle which we -# suffix with _pypy, e.g. try_except_pypy, and try_middle_pypy +# So we need a new rules for try_except and except_handler which we +# suffix with _pypy, e.g. try_except_pypy, and except_handler_pypy def call(self, string): try: return open(string, self, self._bufsize) diff --git a/test/simple_source/exception/01_try_except.py b/test/simple_source/exception/01_try_except.py index a9825c53..e74df2c3 100644 --- a/test/simple_source/exception/01_try_except.py +++ b/test/simple_source/exception/01_try_except.py @@ -1,6 +1,6 @@ # Tests: # try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK -# try_middle COME_FROM +# except_handler COME_FROM # except_stmt ::= except try: @@ -10,7 +10,7 @@ except: # Tests: # try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK -# try_middle COME_FROM +# except_handler COME_FROM # except_stmt ::= except_cond1 except_suite # except_suite ::= ... diff --git a/test/simple_source/exception/20_try_except.py b/test/simple_source/exception/20_try_except.py index bb4863b3..d254584f 100644 --- a/test/simple_source/exception/20_try_except.py +++ b/test/simple_source/exception/20_try_except.py @@ -1,8 +1,8 @@ # Tests: # forstmt ::= SETUP_LOOP expr _for store for_block POP_BLOCK COME_FROM # for_block ::= l_stmts_opt JUMP_BACK -# try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK try_middle COME_FROM -# try_middle ::= jmp_abs COME_FROM except_stmts END_FINALLY +# try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK except_handler COME_FROM +# except_handler ::= jmp_abs COME_FROM except_stmts END_FINALLY # Had a bug with the end of the except matching the end of the # for loop. diff --git a/test/simple_source/exception/25_try_except.py b/test/simple_source/exception/25_try_except.py index b8e5c5c8..d03ea635 100644 --- a/test/simple_source/exception/25_try_except.py +++ b/test/simple_source/exception/25_try_except.py @@ -2,12 +2,12 @@ # # tryfinallystmt ::= SETUP_FINALLY suite_stmts POP_BLOCK LOAD_CONST COME_FROM suite_stmts_opt END_FINALLY # suite_stmts_opt ::= suite_stmts -# try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK try_middle COME_FROM -# try_middle ::= JUMP_FORWARD COME_FROM except_stmts END_FINALLY COME_FROM +# try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK except_handler COME_FROM +# except_handler ::= JUMP_FORWARD COME_FROM except_stmts END_FINALLY COME_FROM # except_stmt ::= except_cond1 except_suite # except_cond1 ::= DUP_TOP expr COMPARE_OP jmp_false POP_TOP POP_TOP POP_TOP -# try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK try_middle COME_FROM -# try_middle ::= JUMP_FORWARD COME_FROM except_stmts END_FINALLY COME_FROM +# try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK except_handler COME_FROM +# except_handler ::= JUMP_FORWARD COME_FROM except_stmts END_FINALLY COME_FROM # except_cond1 ::= DUP_TOP expr COMPARE_OP jmp_false POP_TOP POP_TOP POP_TOP try: try: @@ -21,15 +21,15 @@ finally: x = 4 # Tests Python3: -# try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK try_middle come_froms +# try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK except_handler come_froms # come_froms ::= COME_FROM COME_FROM # START ::= |- stmts # stmts ::= sstmt # sstmt ::= stmt # stmt ::= try_except -# try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK try_middle come_froms +# try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK except_handler come_froms # come_froms ::= COME_FROM -# try_middle ::= JUMP_FORWARD COME_FROM except_stmts END_FINALLY COME_FROM +# except_handler ::= JUMP_FORWARD COME_FROM except_stmts END_FINALLY COME_FROM # Python2 doesn't have the come_froms (which allows for 3 successive COME_FROMs) # it does place 2 COME_FROMs at the end of this code. diff --git a/uncompyle6/parsers/parse2.py b/uncompyle6/parsers/parse2.py index a15d783b..5946db2e 100644 --- a/uncompyle6/parsers/parse2.py +++ b/uncompyle6/parsers/parse2.py @@ -129,24 +129,24 @@ class Python2Parser(PythonParser): iflaststmtl ::= testexpr c_stmts_opt JUMP_BACK # this is nested inside a try_except - tryfinallystmt ::= SETUP_FINALLY suite_stmts_opt - POP_BLOCK LOAD_CONST - COME_FROM suite_stmts_opt END_FINALLY + tryfinallystmt ::= SETUP_FINALLY suite_stmts_opt + POP_BLOCK LOAD_CONST + COME_FROM suite_stmts_opt END_FINALLY # Move to 2.7? 2.6 may use come_froms - tryelsestmtc ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK - try_middle else_suitec COME_FROM + tryelsestmtc ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK + except_handler else_suitec COME_FROM - tryelsestmtl ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK - try_middle else_suitel COME_FROM + tryelsestmtl ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK + except_handler else_suitel COME_FROM try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK - try_middle COME_FROM + except_handler COME_FROM - try_middle ::= JUMP_FORWARD COME_FROM except_stmts - END_FINALLY COME_FROM - try_middle ::= jmp_abs COME_FROM except_stmts - END_FINALLY + except_handler ::= JUMP_FORWARD COME_FROM except_stmts + END_FINALLY COME_FROM + except_handler ::= jmp_abs COME_FROM except_stmts + END_FINALLY except_stmts ::= except_stmt+ @@ -415,8 +415,8 @@ class Python2Parser(PythonParser): # always be the case. self.add_unique_rules([ "stmt ::= try_except_pypy", - "try_except_pypy ::= SETUP_EXCEPT suite_stmts_opt try_middle_pypy", - "try_middle_pypy ::= COME_FROM except_stmts END_FINALLY COME_FROM" + "try_except_pypy ::= SETUP_EXCEPT suite_stmts_opt except_handler_pypy", + "except_handler_pypy ::= COME_FROM except_stmts END_FINALLY COME_FROM" ], customize) continue elif opname == 'SETUP_FINALLY': diff --git a/uncompyle6/parsers/parse25.py b/uncompyle6/parsers/parse25.py index 4c0de8d0..ba26b6a9 100644 --- a/uncompyle6/parsers/parse25.py +++ b/uncompyle6/parsers/parse25.py @@ -33,7 +33,7 @@ class Python25Parser(Python26Parser): store ::= STORE_NAME tryelsestmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK - try_middle else_suite come_froms + except_handler else_suite come_froms # Python 2.6 omits the LOAD_FAST DELETE_FAST below # withas is allowed as a "from future" in 2.5 diff --git a/uncompyle6/parsers/parse26.py b/uncompyle6/parsers/parse26.py index 107fde53..1dd6c1a9 100644 --- a/uncompyle6/parsers/parse26.py +++ b/uncompyle6/parsers/parse26.py @@ -15,43 +15,43 @@ class Python26Parser(Python2Parser): def p_try_except26(self, args): """ - except_stmt ::= except_cond3 except_suite - except_cond1 ::= DUP_TOP expr COMPARE_OP - JUMP_IF_FALSE POP_TOP POP_TOP POP_TOP POP_TOP - except_cond3 ::= DUP_TOP expr COMPARE_OP - JUMP_IF_FALSE POP_TOP POP_TOP store POP_TOP + except_stmt ::= except_cond3 except_suite + except_cond1 ::= DUP_TOP expr COMPARE_OP + JUMP_IF_FALSE POP_TOP POP_TOP POP_TOP POP_TOP + except_cond3 ::= DUP_TOP expr COMPARE_OP + JUMP_IF_FALSE POP_TOP POP_TOP store POP_TOP - try_middle ::= JUMP_FORWARD COME_FROM except_stmts - come_from_pop END_FINALLY come_froms + except_handler ::= JUMP_FORWARD COME_FROM except_stmts + come_from_pop END_FINALLY come_froms - try_middle ::= JUMP_FORWARD COME_FROM except_stmts END_FINALLY - come_froms + except_handler ::= JUMP_FORWARD COME_FROM except_stmts END_FINALLY + come_froms - try_middle ::= jmp_abs COME_FROM except_stmts - POP_TOP END_FINALLY + except_handler ::= jmp_abs COME_FROM except_stmts + POP_TOP END_FINALLY - try_middle ::= jmp_abs COME_FROM except_stmts - END_FINALLY JUMP_FORWARD + except_handler ::= jmp_abs COME_FROM except_stmts + END_FINALLY JUMP_FORWARD # Sometimes we don't put in COME_FROM to the next statement # like we do in 2.7. Perhaps we should? - try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK - try_middle + try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK + except_handler tryelsestmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK - try_middle else_suite COME_FROM + except_handler else_suite COME_FROM - _ifstmts_jump ::= c_stmts_opt JUMP_FORWARD COME_FROM POP_TOP + _ifstmts_jump ::= c_stmts_opt JUMP_FORWARD COME_FROM POP_TOP - except_suite ::= c_stmts_opt JUMP_FORWARD come_from_pop - except_suite ::= c_stmts_opt JUMP_FORWARD POP_TOP - except_suite ::= c_stmts_opt jmp_abs come_from_pop + except_suite ::= c_stmts_opt JUMP_FORWARD come_from_pop + except_suite ::= c_stmts_opt JUMP_FORWARD POP_TOP + except_suite ::= c_stmts_opt jmp_abs come_from_pop # This is what happens after a jump where # we start a new block. For reasons I don't fully # understand, there is also a value on the top of the stack come_from_pop ::= COME_FROM POP_TOP - come_froms_pop ::= come_froms POP_TOP + come_froms_pop ::= come_froms POP_TOP """ # In contrast to Python 2.7, Python 2.6 has a lot of diff --git a/uncompyle6/parsers/parse27.py b/uncompyle6/parsers/parse27.py index bd86f880..c801336d 100644 --- a/uncompyle6/parsers/parse27.py +++ b/uncompyle6/parsers/parse27.py @@ -48,7 +48,7 @@ class Python27Parser(Python2Parser): COME_FROM_FINALLY suite_stmts_opt END_FINALLY tryelsestmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK - try_middle else_suite COME_FROM + except_handler else_suite COME_FROM except_stmt ::= except_cond2 except_suite diff --git a/uncompyle6/parsers/parse3.py b/uncompyle6/parsers/parse3.py index e10e14fd..8df248cb 100644 --- a/uncompyle6/parsers/parse3.py +++ b/uncompyle6/parsers/parse3.py @@ -177,7 +177,7 @@ class Python3Parser(PythonParser): # COME_FROM targets from the wrong places try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK - try_middle opt_come_from_except + except_handler opt_come_from_except # this is nested inside a try_except tryfinallystmt ::= SETUP_FINALLY suite_stmts_opt @@ -185,27 +185,28 @@ class Python3Parser(PythonParser): COME_FROM_FINALLY suite_stmts_opt END_FINALLY tryelsestmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK - try_middle else_suite come_from_except_clauses + except_handler else_suite come_from_except_clauses tryelsestmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK - try_middle else_suite come_froms + except_handler else_suite come_froms - tryelsestmtc ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK - try_middle else_suitec come_from_except_clauses + tryelsestmtc ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK + except_handler else_suitec come_from_except_clauses - tryelsestmtl ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK - try_middle else_suitel come_from_except_clauses + tryelsestmtl ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK + except_handler else_suitel come_from_except_clauses - try_middle ::= jmp_abs COME_FROM except_stmts - END_FINALLY - try_middle ::= jmp_abs COME_FROM_EXCEPT except_stmts - END_FINALLY + except_handler ::= jmp_abs COME_FROM except_stmts + END_FINALLY + except_handler ::= jmp_abs COME_FROM_EXCEPT except_stmts + END_FINALLY # FIXME: remove this - try_middle ::= JUMP_FORWARD COME_FROM except_stmts - END_FINALLY COME_FROM - try_middle ::= JUMP_FORWARD COME_FROM except_stmts - END_FINALLY COME_FROM_EXCEPT + except_handler ::= JUMP_FORWARD COME_FROM except_stmts + END_FINALLY COME_FROM + + except_handler ::= JUMP_FORWARD COME_FROM except_stmts + END_FINALLY COME_FROM_EXCEPT except_stmts ::= except_stmts except_stmt except_stmts ::= except_stmt @@ -267,10 +268,10 @@ class Python3Parser(PythonParser): def p_misc3(self, args): """ - try_middle ::= JUMP_FORWARD COME_FROM_EXCEPT except_stmts - END_FINALLY COME_FROM - try_middle ::= JUMP_FORWARD COME_FROM_EXCEPT except_stmts - END_FINALLY COME_FROM_EXCEPT_CLAUSE + except_handler ::= JUMP_FORWARD COME_FROM_EXCEPT except_stmts + END_FINALLY COME_FROM + except_handler ::= JUMP_FORWARD COME_FROM_EXCEPT except_stmts + END_FINALLY COME_FROM_EXCEPT_CLAUSE for_block ::= l_stmts_opt come_from_loops JUMP_BACK for_block ::= l_stmts diff --git a/uncompyle6/parsers/parse32.py b/uncompyle6/parsers/parse32.py index a335cb8c..439be029 100644 --- a/uncompyle6/parsers/parse32.py +++ b/uncompyle6/parsers/parse32.py @@ -29,17 +29,17 @@ class Python32Parser(Python3Parser): # jump_excepts. But in 3.3 we need them added try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK - try_middle + except_handler jump_excepts come_from_except_clauses - try_middle ::= JUMP_FORWARD COME_FROM_EXCEPT except_stmts - END_FINALLY + except_handler ::= JUMP_FORWARD COME_FROM_EXCEPT except_stmts + END_FINALLY - tryelsestmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK - try_middle else_suite - jump_excepts come_from_except_clauses + tryelsestmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK + except_handler else_suite + jump_excepts come_from_except_clauses - jump_excepts ::= jump_except+ + jump_excepts ::= jump_except+ # Python 3.2+ has more loop optimization that removes # JUMP_FORWARD in some cases, and hence we also don't @@ -65,11 +65,11 @@ class Python32Parser(Python3Parser): def add_custom_rules(self, tokens, customize): self.remove_rules(""" - try_middle ::= JUMP_FORWARD COME_FROM except_stmts END_FINALLY COME_FROM - try_middle ::= JUMP_FORWARD COME_FROM except_stmts END_FINALLY COME_FROM_EXCEPT - try_middle ::= JUMP_FORWARD COME_FROM_EXCEPT except_stmts END_FINALLY COME_FROM_EXCEPT_CLAUSE - try_middle ::= jmp_abs COME_FROM except_stmts END_FINALLY - tryelsestmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK try_middle else_suite come_from_except_clauses + except_handler ::= JUMP_FORWARD COME_FROM except_stmts END_FINALLY COME_FROM + except_handler ::= JUMP_FORWARD COME_FROM except_stmts END_FINALLY COME_FROM_EXCEPT + except_handler ::= JUMP_FORWARD COME_FROM_EXCEPT except_stmts END_FINALLY COME_FROM_EXCEPT_CLAUSE + except_handler ::= jmp_abs COME_FROM except_stmts END_FINALLY + tryelsestmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK except_handler else_suite come_from_except_clauses whileTruestmt ::= SETUP_LOOP l_stmts_opt JUMP_BACK NOP COME_FROM_LOOP whileTruestmt ::= SETUP_LOOP l_stmts_opt JUMP_BACK POP_BLOCK NOP COME_FROM_LOOP """) diff --git a/uncompyle6/parsers/parse33.py b/uncompyle6/parsers/parse33.py index 85b82ab6..76e54120 100644 --- a/uncompyle6/parsers/parse33.py +++ b/uncompyle6/parsers/parse33.py @@ -22,7 +22,7 @@ class Python33Parser(Python32Parser): # jump_excepts. But in 3.3 we need them added try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK - try_middle + except_handler jump_excepts come_from_except_clauses """ diff --git a/uncompyle6/parsers/parse36.py b/uncompyle6/parsers/parse36.py index 86d0e214..479761f3 100644 --- a/uncompyle6/parsers/parse36.py +++ b/uncompyle6/parsers/parse36.py @@ -65,14 +65,14 @@ class Python36Parser(Python35Parser): # In 3.6+, A sequence of statements ending in a RETURN can cause # JUMP_FORWARD END_FINALLY to be omitted from try middle - except_return ::= POP_TOP POP_TOP POP_TOP return_stmts - try_middle ::= JUMP_FORWARD COME_FROM_EXCEPT except_return + except_return ::= POP_TOP POP_TOP POP_TOP return_stmts + except_handler ::= JUMP_FORWARD COME_FROM_EXCEPT except_return # Try middle following a return_stmts - try_middle36 ::= COME_FROM_EXCEPT except_stmts END_FINALLY + except_handler36 ::= COME_FROM_EXCEPT except_stmts END_FINALLY - stmt ::= try_except36 - try_except36 ::= SETUP_EXCEPT return_stmts try_middle36 opt_come_from_except + stmt ::= try_except36 + try_except36 ::= SETUP_EXCEPT return_stmts except_handler36 opt_come_from_except """ def add_custom_rules(self, tokens, customize):