Handle Python 2.6 and below "except <cond>, <var>"

This commit is contained in:
rocky
2016-09-01 02:17:49 -04:00
parent c9f364df9f
commit 1d567d5d9a
5 changed files with 39 additions and 2 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,18 @@
# From 2.6.9 ConfigParser.py
# Bug was being able to handle:
# except KeyError, e
# vs 2.6+.
# except KeyError as e
#
# In terms of table syntax:
# 2.7+:
# 'except_cond2': ( '%|except %c as %c:\n', 1, 5 )
# vs 2.6 and before
# 'except_cond3': ( '%|except %c, %c:\n', 1, 6 )
#
# Python 2.6 allows both, but we use the older form since
# that matches the grammar for how this gets parsed
try:
value = "foo"
except KeyError, e:
raise RuntimeError('foo')

View File

@@ -16,9 +16,10 @@ 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_cond2 ::= DUP_TOP expr COMPARE_OP
except_cond3 ::= DUP_TOP expr COMPARE_OP
JUMP_IF_FALSE POP_TOP POP_TOP designator POP_TOP
# Might be a bug from when COME_FROM wasn't properly handled

View File

@@ -319,7 +319,6 @@ TABLE_DIRECT = {
'tryfinallystmt': ( '%|try:\n%+%c%-%|finally:\n%+%c%-\n\n', 1, 5 ),
'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, '') ),
'except_suite_finalize': ( '%+%c%-%C', 1, (3, maxint, '') ),
'withstmt': ( '%|with %c:\n%+%c%-', 0, 3),
@@ -580,6 +579,25 @@ class SourceWalker(GenericASTTraversal, object):
'import_cont' : ( ', %c', 2 ),
})
########################################
# Python 2.6+
# except <condition> as <var>
# vs. older:
# except <condition> , <var>
#
# For 2.6 we use the older syntax which
# matches how we parse this in bytecode
########################################
if version > 2.6:
TABLE_DIRECT.update({
'except_cond2': ( '%|except %c as %c:\n', 1, 5 ),
})
else:
TABLE_DIRECT.update({
'except_cond3': ( '%|except %c, %c:\n', 1, 6 ),
})
##########################
# Python 3.2 and 3.3 only
##########################