More Python version tuple comparison conversion

This commit is contained in:
rocky
2021-10-16 11:41:22 -04:00
parent e8e006bb8c
commit 15efaffe8d
15 changed files with 23 additions and 23 deletions

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2015-2020 Rocky Bernstein # Copyright (c) 2015-2021 Rocky Bernstein
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com> # Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
# #
# Copyright (c) 1999 John Aycock # Copyright (c) 1999 John Aycock
@@ -350,7 +350,7 @@ class Python2Parser(PythonParser):
], ],
customize, customize,
) )
if self.version >= 2.7: if self.version >= (2, 7):
self.add_unique_rule( self.add_unique_rule(
"dict_comp_func ::= BUILD_MAP_n LOAD_FAST FOR_ITER store " "dict_comp_func ::= BUILD_MAP_n LOAD_FAST FOR_ITER store "
"comp_iter JUMP_BACK RETURN_VALUE RETURN_LAST", "comp_iter JUMP_BACK RETURN_VALUE RETURN_LAST",
@@ -577,7 +577,7 @@ class Python2Parser(PythonParser):
customize, customize,
) )
if self.version >= 2.7: if self.version >= (2, 7):
if i > 0: if i > 0:
prev_tok = tokens[i - 1] prev_tok = tokens[i - 1]
if prev_tok == "LOAD_DICTCOMP": if prev_tok == "LOAD_DICTCOMP":

View File

@@ -94,7 +94,7 @@ class Python24Parser(Python25Parser):
""") """)
super(Python24Parser, self).customize_grammar_rules(tokens, customize) super(Python24Parser, self).customize_grammar_rules(tokens, customize)
self.remove_rules_24() self.remove_rules_24()
if self.version == 2.4: if self.version[:2] == (2, 4):
self.check_reduce['nop_stmt'] = 'tokens' self.check_reduce['nop_stmt'] = 'tokens'
def reduce_is_invalid(self, rule, ast, tokens, first, last): def reduce_is_invalid(self, rule, ast, tokens, first, last):

View File

@@ -89,7 +89,7 @@ class Python25Parser(Python26Parser):
return_stmt_lambda LAMBDA_MARKER return_stmt_lambda LAMBDA_MARKER
""") """)
super(Python25Parser, self).customize_grammar_rules(tokens, customize) super(Python25Parser, self).customize_grammar_rules(tokens, customize)
if self.version == 2.5: if self.version[:2] == (2, 5):
self.check_reduce["try_except"] = "tokens" self.check_reduce["try_except"] = "tokens"
self.check_reduce["aug_assign1"] = "AST" self.check_reduce["aug_assign1"] = "AST"
self.check_reduce["ifelsestmt"] = "AST" self.check_reduce["ifelsestmt"] = "AST"

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2017-2020 Rocky Bernstein # Copyright (c) 2017-2021 Rocky Bernstein
""" """
spark grammar differences over Python2 for Python 2.6. spark grammar differences over Python2 for Python 2.6.
""" """
@@ -390,7 +390,7 @@ class Python26Parser(Python2Parser):
# For now, we won't let the 2nd 'expr' be a "if_exp_not" # For now, we won't let the 2nd 'expr' be a "if_exp_not"
# However in < 2.6 where we don't have if/else expression it *can* # However in < 2.6 where we don't have if/else expression it *can*
# be. # be.
if self.version >= 2.6 and ast[2][0] == "if_exp_not": if self.version >= (2, 6) and ast[2][0] == "if_exp_not":
return True return True
test_index = last test_index = last
@@ -425,7 +425,7 @@ class Python26Parser(Python2Parser):
# since the operand can be a relative offset rather than # since the operand can be a relative offset rather than
# an absolute offset. # an absolute offset.
setup_inst = self.insts[self.offset2inst_index[tokens[first].offset]] setup_inst = self.insts[self.offset2inst_index[tokens[first].offset]]
if self.version <= 2.2 and tokens[last] == "COME_FROM": if self.version <= (2, 2) and tokens[last] == "COME_FROM":
last += 1 last += 1
return tokens[last-1].off2int() > setup_inst.argval return tokens[last-1].off2int() > setup_inst.argval
elif rule == ("ifstmt", ("testexpr", "_ifstmts_jump")): elif rule == ("ifstmt", ("testexpr", "_ifstmts_jump")):

View File

@@ -1361,7 +1361,7 @@ class Python3Parser(PythonParser):
"LOAD_CODE LOAD_STR ", "LOAD_CODE LOAD_STR ",
opname, opname,
) )
elif self.version > (3, 5): elif self.version >= (3, 6):
# positional args before keyword args # positional args before keyword args
rule = "mkfunc ::= %s%s %s%s" % ( rule = "mkfunc ::= %s%s %s%s" % (
"pos_arg " * args_pos, "pos_arg " * args_pos,
@@ -1369,7 +1369,7 @@ class Python3Parser(PythonParser):
"LOAD_CODE LOAD_STR ", "LOAD_CODE LOAD_STR ",
opname, opname,
) )
elif self.version > (3, 3): elif self.version >= (3, 4):
# positional args before keyword args # positional args before keyword args
rule = "mkfunc ::= %s%s %s%s" % ( rule = "mkfunc ::= %s%s %s%s" % (
"pos_arg " * args_pos, "pos_arg " * args_pos,
@@ -1595,7 +1595,7 @@ class Python3Parser(PythonParser):
elif lhs == "kwarg": elif lhs == "kwarg":
arg = tokens[first].attr arg = tokens[first].attr
return not (isinstance(arg, str) or isinstance(arg, unicode)) return not (isinstance(arg, str) or isinstance(arg, unicode))
elif lhs in ("iflaststmt", "iflaststmtl") and self.version == (3, 6): elif lhs in ("iflaststmt", "iflaststmtl") and self.version[:2] == (3, 6):
return ifstmt(self, lhs, n, rule, ast, tokens, first, last) return ifstmt(self, lhs, n, rule, ast, tokens, first, last)
elif rule == ("ifstmt", ("testexpr", "_ifstmts_jump")): elif rule == ("ifstmt", ("testexpr", "_ifstmts_jump")):
# FIXME: go over what's up with 3.0. Evetually I'd like to remove RETURN_END_IF # FIXME: go over what's up with 3.0. Evetually I'd like to remove RETURN_END_IF

View File

@@ -157,7 +157,7 @@ class Python35Parser(Python34Parser):
# FIXME: I suspect this is wrong for 3.6 and 3.5, but # FIXME: I suspect this is wrong for 3.6 and 3.5, but
# I haven't verified what the 3.7ish fix is # I haven't verified what the 3.7ish fix is
elif opname == 'BUILD_MAP_UNPACK_WITH_CALL': elif opname == 'BUILD_MAP_UNPACK_WITH_CALL':
if self.version < 3.7: if self.version < (3, 7):
self.addRule("expr ::= unmapexpr", nop_func) self.addRule("expr ::= unmapexpr", nop_func)
nargs = token.attr % 256 nargs = token.attr % 256
map_unpack_n = "map_unpack_%s" % nargs map_unpack_n = "map_unpack_%s" % nargs

View File

@@ -396,7 +396,7 @@ class Python36Parser(Python35Parser):
starred ::= expr starred ::= expr
call_ex ::= expr starred CALL_FUNCTION_EX call_ex ::= expr starred CALL_FUNCTION_EX
""", nop_func) """, nop_func)
if self.version >= 3.6: if self.version >= (3, 6):
if 'BUILD_MAP_UNPACK_WITH_CALL' in self.seen_ops: if 'BUILD_MAP_UNPACK_WITH_CALL' in self.seen_ops:
self.addRule(""" self.addRule("""
expr ::= call_ex_kw expr ::= call_ex_kw

View File

@@ -1292,7 +1292,7 @@ class Python37Parser(Python37BaseParser):
withasstmt ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH withasstmt ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH
WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY
""" """
if self.version < 3.8: if self.version < (3, 8):
rules_str += """ rules_str += """
with ::= expr SETUP_WITH POP_TOP suite_stmts_opt POP_BLOCK with ::= expr SETUP_WITH POP_TOP suite_stmts_opt POP_BLOCK
LOAD_CONST LOAD_CONST

View File

@@ -1034,7 +1034,7 @@ class Python37BaseParser(PythonParser):
POP_BLOCK LOAD_CONST COME_FROM_WITH POP_BLOCK LOAD_CONST COME_FROM_WITH
with_suffix with_suffix
""" """
if self.version < 3.8: if self.version < (3, 8):
rules_str += """ rules_str += """
with ::= expr SETUP_WITH POP_TOP suite_stmts_opt POP_BLOCK with ::= expr SETUP_WITH POP_TOP suite_stmts_opt POP_BLOCK
LOAD_CONST LOAD_CONST

View File

@@ -10,7 +10,7 @@ def except_handler(self, lhs, n, rule, ast, tokens, first, last):
# FIXME: Figure out why this doesn't work on # FIXME: Figure out why this doesn't work on
# bytecode-1.4/anydbm.pyc # bytecode-1.4/anydbm.pyc
if self.version == 1.4: if self.version[:2] == (1, 4):
return False return False
# Make sure come froms all come from within "except_handler". # Make sure come froms all come from within "except_handler".

View File

@@ -1,8 +1,8 @@
# Copyright (c) 2020 Rocky Bernstein # Copyright (c) 2020-2021 Rocky Bernstein
def except_handler_else(self, lhs, n, rule, ast, tokens, first, last): def except_handler_else(self, lhs, n, rule, ast, tokens, first, last):
# FIXME: expand this to other versions # FIXME: expand this to other versions
if self.version not in (2.7, 3.5): if self.version[:2] not in ((2, 7), (3, 5)):
return False return False
if tokens[first] in ("JUMP_FORWARD", "JUMP_ABSOLUTE"): if tokens[first] in ("JUMP_FORWARD", "JUMP_ABSOLUTE"):

View File

@@ -191,7 +191,7 @@ def ifelsestmt(self, lhs, n, rule, ast, tokens, first, last):
if last == n: if last == n:
last -= 1 last -= 1
jmp = if_condition[1] jmp = if_condition[1]
if self.version > (2, 6): if self.version >= (2, 7):
jmp_target = jmp[0].attr jmp_target = jmp[0].attr
else: else:
jmp_target = int(jmp[0].pattr) jmp_target = int(jmp[0].pattr)

View File

@@ -45,7 +45,7 @@ def or_check(self, lhs, n, rule, ast, tokens, first, last):
return True return True
# If the jmp is backwards # If the jmp is backwards
if last_token == "POP_JUMP_IF_FALSE" and not self.version in (2.7, 3.5, 3.6): if last_token == "POP_JUMP_IF_FALSE" and not self.version[:2] in ((2, 7), (3, 5), (3, 6)):
if last_token.attr < last_token_offset: if last_token.attr < last_token_offset:
# For a backwards loop, well compare to the instruction *after* # For a backwards loop, well compare to the instruction *after*
# then POP_JUMP... # then POP_JUMP...

View File

@@ -1,9 +1,9 @@
# Copyright (c) 2020 Rocky Bernstein # Copyright (c) 2020-2021 Rocky Bernstein
def testtrue(self, lhs, n, rule, ast, tokens, first, last): def testtrue(self, lhs, n, rule, ast, tokens, first, last):
# FIXME: make this work for all versions # FIXME: make this work for all versions
if self.version != 3.7: if self.version[:2] != (3, 7):
return False return False
if rule == ("testtrue", ("expr", "jmp_true")): if rule == ("testtrue", ("expr", "jmp_true")):
pjit = tokens[min(last - 1, n - 2)] pjit = tokens[min(last - 1, n - 2)]

View File

@@ -20,4 +20,4 @@ def while1elsestmt(self, lhs, n, rule, ast, tokens, first, last):
# not while1else. Also do for whileTrue? # not while1else. Also do for whileTrue?
last += 1 last += 1
# 3.8+ Doesn't have SETUP_LOOP # 3.8+ Doesn't have SETUP_LOOP
return self.version < 3.8 and tokens[first].attr > tokens[last].off2int() return self.version < (3, 8) and tokens[first].attr > tokens[last].off2int()