Handle Python 2.4 "if 1...."

This commit is contained in:
rocky
2017-04-12 04:50:22 -04:00
parent bb24df596d
commit 45bd8e4058

View File

@@ -14,6 +14,15 @@ class Python24Parser(Python25Parser):
def p_misc24(self, args):
'''
# Python 2.4 only adds something like the below for if 1:
# However we will just treat it as a noop (which of course messes up
# simple verify of bytecode.
# See also below in reduce_is_invalid where we check that the JUMP_FORWARD
# target matches the COME_FROM target
stmt ::= nop_stmt
nop_stmt ::= JUMP_FORWARD POP_TOP COME_FROM
# 2.5+ has two LOAD_CONSTs, one for the number '.'s in a relative import
# keep positions similar to simplify semantic actions
@@ -37,6 +46,25 @@ class Python24Parser(Python25Parser):
gen_comp_body ::= expr YIELD_VALUE
'''
def add_custom_rules(self, tokens, customize):
super(Python24Parser, self).add_custom_rules(tokens, customize)
if self.version == 2.4:
self.check_reduce['nop_stmt'] = 'tokens'
def reduce_is_invalid(self, rule, ast, tokens, first, last):
invalid = super(Python24Parser,
self).reduce_is_invalid(rule, ast,
tokens, first, last)
if invalid:
return invalid
# FiXME: this code never gets called...
lhs = rule[0]
if lhs == 'nop_stmt':
return not int(tokens[first].pattr) == tokens[last].offset
return False
class Python24ParserSingle(Python24Parser, PythonParserSingle):
pass