NT trystmt -> try_except to match AST

This commit is contained in:
rocky 2017-12-02 22:20:24 -05:00
parent de594ce7f2
commit eafb32b9a0
15 changed files with 39 additions and 39 deletions

View File

@ -1,5 +1,5 @@
# From 3.2 distutils/core
# Ensure we handle funky trystmt
# Ensure we handle funky try_except
def setup (ok, dist, **attrs):
if ok:
try:

View File

@ -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 trystmt and try_middle which we
# suffix with _pypy, e.g. trystmt_pypy, and try_middle_pypy
# 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
def call(self, string):
try:
return open(string, self, self._bufsize)

View File

@ -1,5 +1,5 @@
# Tests:
# trystmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
# try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
# try_middle COME_FROM
# except_stmt ::= except
@ -9,7 +9,7 @@ except:
pass
# Tests:
# trystmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
# try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
# try_middle COME_FROM
# except_stmt ::= except_cond1 except_suite
# except_suite ::= ...

View File

@ -1,7 +1,7 @@
# Tests:
# forstmt ::= SETUP_LOOP expr _for store for_block POP_BLOCK COME_FROM
# for_block ::= l_stmts_opt JUMP_BACK
# trystmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK try_middle COME_FROM
# try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK try_middle COME_FROM
# try_middle ::= jmp_abs COME_FROM except_stmts END_FINALLY
# Had a bug with the end of the except matching the end of the

View File

@ -2,11 +2,11 @@
#
# tryfinallystmt ::= SETUP_FINALLY suite_stmts POP_BLOCK LOAD_CONST COME_FROM suite_stmts_opt END_FINALLY
# suite_stmts_opt ::= suite_stmts
# trystmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK try_middle COME_FROM
# 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
# except_stmt ::= except_cond1 except_suite
# except_cond1 ::= DUP_TOP expr COMPARE_OP jmp_false POP_TOP POP_TOP POP_TOP
# trystmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK try_middle COME_FROM
# 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
# except_cond1 ::= DUP_TOP expr COMPARE_OP jmp_false POP_TOP POP_TOP POP_TOP
try:
@ -21,13 +21,13 @@ finally:
x = 4
# Tests Python3:
# trystmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK try_middle come_froms
# try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK try_middle come_froms
# come_froms ::= COME_FROM COME_FROM
# START ::= |- stmts
# stmts ::= sstmt
# sstmt ::= stmt
# stmt ::= trystmt
# trystmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK try_middle come_froms
# stmt ::= try_except
# try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK try_middle come_froms
# come_froms ::= COME_FROM
# try_middle ::= JUMP_FORWARD COME_FROM except_stmts END_FINALLY COME_FROM
# Python2 doesn't have the come_froms (which allows for 3 successive COME_FROMs)

View File

@ -254,7 +254,7 @@ class PythonParser(GenericASTBuilder):
stmt ::= while1elsestmt
stmt ::= forstmt
stmt ::= forelsestmt
stmt ::= trystmt
stmt ::= try_except
stmt ::= tryelsestmt
stmt ::= tryfinallystmt
stmt ::= withstmt

View File

@ -128,7 +128,7 @@ class Python2Parser(PythonParser):
iflaststmt ::= testexpr c_stmts_opt JUMP_ABSOLUTE
iflaststmtl ::= testexpr c_stmts_opt JUMP_BACK
# this is nested inside a trystmt
# this is nested inside a try_except
tryfinallystmt ::= SETUP_FINALLY suite_stmts_opt
POP_BLOCK LOAD_CONST
COME_FROM suite_stmts_opt END_FINALLY
@ -140,7 +140,7 @@ class Python2Parser(PythonParser):
tryelsestmtl ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
try_middle else_suitel COME_FROM
trystmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
try_middle COME_FROM
try_middle ::= JUMP_FORWARD COME_FROM except_stmts
@ -414,8 +414,8 @@ class Python2Parser(PythonParser):
# only have SETUP_EXCEPT customization for PyPy, but that might not
# always be the case.
self.add_unique_rules([
"stmt ::= trystmt_pypy",
"trystmt_pypy ::= SETUP_EXCEPT suite_stmts_opt try_middle_pypy",
"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"
], customize)
continue

View File

@ -35,8 +35,8 @@ class Python26Parser(Python2Parser):
# Sometimes we don't put in COME_FROM to the next statement
# like we do in 2.7. Perhaps we should?
trystmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
try_middle
try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
try_middle
tryelsestmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
try_middle else_suite COME_FROM

View File

@ -176,10 +176,10 @@ class Python3Parser(PythonParser):
# one COME_FROM for Python 2.7 seems to associate the
# COME_FROM targets from the wrong places
trystmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
try_middle opt_come_from_except
# this is nested inside a trystmt
# this is nested inside a try_except
tryfinallystmt ::= SETUP_FINALLY suite_stmts_opt
POP_BLOCK LOAD_CONST
COME_FROM_FINALLY suite_stmts_opt END_FINALLY

View File

@ -28,9 +28,9 @@ class Python32Parser(Python3Parser):
# Python 3.5+ has jump optimization to remove the redundant
# jump_excepts. But in 3.3 we need them added
trystmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
try_middle
jump_excepts come_from_except_clauses
try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
try_middle
jump_excepts come_from_except_clauses
try_middle ::= JUMP_FORWARD COME_FROM_EXCEPT except_stmts
END_FINALLY

View File

@ -21,7 +21,7 @@ class Python33Parser(Python32Parser):
# Python 3.5+ has jump optimization to remove the redundant
# jump_excepts. But in 3.3 we need them added
trystmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
try_except ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK
try_middle
jump_excepts come_from_except_clauses
"""

View File

@ -71,8 +71,8 @@ class Python36Parser(Python35Parser):
# Try middle following a return_stmts
try_middle36 ::= COME_FROM_EXCEPT except_stmts END_FINALLY
stmt ::= trystmt36
trystmt36 ::= SETUP_EXCEPT return_stmts try_middle36 opt_come_from_except
stmt ::= try_except36
try_except36 ::= SETUP_EXCEPT return_stmts try_middle36 opt_come_from_except
"""
def add_custom_rules(self, tokens, customize):

View File

@ -257,11 +257,11 @@ TABLE_DIRECT = {
'%|for %c in %c:\n%+%c%-%|else:\n%+%c%-', 3, 1, 4, -2 ),
'forelselaststmtl': (
'%|for %c in %c:\n%+%c%-%|else:\n%+%c%-\n\n', 3, 1, 4, -2 ),
'trystmt': ( '%|try:\n%+%c%-%c\n\n', 1, 3 ),
'try_except': ( '%|try:\n%+%c%-%c\n\n', 1, 3 ),
'tryelsestmt': ( '%|try:\n%+%c%-%c%|else:\n%+%c%-\n\n', 1, 3, 4 ),
'tryelsestmtc': ( '%|try:\n%+%c%-%c%|else:\n%+%c%-', 1, 3, 4 ),
'tryelsestmtl': ( '%|try:\n%+%c%-%c%|else:\n%+%c%-', 1, 3, 4 ),
'tf_trystmt': ( '%c%-%c%+', 1, 3 ),
'tf_try_except': ( '%c%-%c%+', 1, 3 ),
'tf_tryelsestmt': ( '%c%-%c%|else:\n%+%c', 1, 3, 4 ),
'tryfinallystmt': ( '%|try:\n%+%c%-%|finally:\n%+%c%-\n\n', 1, 5 ),
'except': ( '%|except:\n%+%c%-', 3 ),

View File

@ -199,12 +199,12 @@ class FragmentsWalker(pysource.SourceWalker, object):
self.set_pos_info(node, start, start+len("pass"))
self.default(node)
def n_trystmt(self, node):
def n_try_except(self, node):
start = len(self.f.getvalue()) + len(self.indent)
self.set_pos_info(node[0], start, start+len("try:"))
self.default(node)
n_tryelsestmt = n_tryelsestmtc = n_tryelsestmtl = n_tryfinallystmt = n_trystmt
n_tryelsestmt = n_tryelsestmtc = n_tryelsestmtl = n_tryfinallystmt = n_try_except
def n_return_stmt(self, node):
start = len(self.f.getvalue()) + len(self.indent)

View File

@ -242,10 +242,10 @@ class SourceWalker(GenericASTTraversal, object):
TABLE_DIRECT.update({
'assert_pypy': ( '%|assert %c\n' , 1 ),
'assert2_pypy': ( '%|assert %c, %c\n' , 1, 4 ),
'trystmt_pypy': ( '%|try:\n%+%c%-%c\n\n', 1, 2 ),
'try_except_pypy': ( '%|try:\n%+%c%-%c\n\n', 1, 2 ),
'tryfinallystmt_pypy': ( '%|try:\n%+%c%-%|finally:\n%+%c%-\n\n', 1, 3 ),
'assign3_pypy': ( '%|%c, %c, %c = %c, %c, %c\n', 5, 4, 3, 0, 1, 2 ),
'assign2_pypy': ( '%|%c, %c = %c, %c\n', 3, 2, 0, 1),
'assign3_pypy': ( '%|%c, %c, %c = %c, %c, %c\n', 5, 4, 3, 0, 1, 2 ),
'assign2_pypy': ( '%|%c, %c = %c, %c\n', 3, 2, 0, 1),
})
else:
########################
@ -254,9 +254,9 @@ class SourceWalker(GenericASTTraversal, object):
TABLE_DIRECT.update({
'assert': ( '%|assert %c\n' , 0 ),
'assert2': ( '%|assert %c, %c\n' , 0, 3 ),
'trystmt': ( '%|try:\n%+%c%-%c\n\n', 1, 3 ),
'assign2': ( '%|%c, %c = %c, %c\n', 3, 4, 0, 1 ),
'assign3': ( '%|%c, %c, %c = %c, %c, %c\n', 5, 6, 7, 0, 1, 2 ),
'try_except': ( '%|try:\n%+%c%-%c\n\n', 1, 3 ),
'assign2': ( '%|%c, %c = %c, %c\n', 3, 4, 0, 1 ),
'assign3': ( '%|%c, %c, %c = %c, %c, %c\n', 5, 6, 7, 0, 1, 2 ),
})
if version < 3.0:
TABLE_R.update({
@ -435,7 +435,7 @@ class SourceWalker(GenericASTTraversal, object):
})
if version >= 3.6:
TABLE_DIRECT.update({
'trystmt36': ( '%|try:\n%+%c%-%c\n\n', 1, 2 ),
'try_except36': ( '%|try:\n%+%c%-%c\n\n', 1, 2 ),
})
def n_async_call(node):
@ -857,8 +857,8 @@ class SourceWalker(GenericASTTraversal, object):
# 'tryfinallystmt': ( '%|try:\n%+%c%-%|finally:\n%+%c%-', 1, 5 ),
def n_tryfinallystmt(self, node):
if len(node[1][0]) == 1 and node[1][0][0] == 'stmt':
if node[1][0][0][0] == 'trystmt':
node[1][0][0][0].kind = 'tf_trystmt'
if node[1][0][0][0] == 'try_except':
node[1][0][0][0].kind = 'tf_try_except'
if node[1][0][0][0] == 'tryelsestmt':
node[1][0][0][0].kind = 'tf_tryelsestmt'
self.default(node)