From 15efaffe8d290f4047849e292c40bb00c19940bd Mon Sep 17 00:00:00 2001 From: rocky Date: Sat, 16 Oct 2021 11:41:22 -0400 Subject: [PATCH] More Python version tuple comparison conversion --- uncompyle6/parsers/parse2.py | 6 +++--- uncompyle6/parsers/parse24.py | 2 +- uncompyle6/parsers/parse25.py | 2 +- uncompyle6/parsers/parse26.py | 6 +++--- uncompyle6/parsers/parse3.py | 6 +++--- uncompyle6/parsers/parse35.py | 2 +- uncompyle6/parsers/parse36.py | 2 +- uncompyle6/parsers/parse37.py | 2 +- uncompyle6/parsers/parse37base.py | 2 +- uncompyle6/parsers/reducecheck/except_handler.py | 2 +- uncompyle6/parsers/reducecheck/except_handler_else.py | 4 ++-- uncompyle6/parsers/reducecheck/ifelsestmt.py | 2 +- uncompyle6/parsers/reducecheck/or_check.py | 2 +- uncompyle6/parsers/reducecheck/testtrue.py | 4 ++-- uncompyle6/parsers/reducecheck/while1elsestmt.py | 2 +- 15 files changed, 23 insertions(+), 23 deletions(-) diff --git a/uncompyle6/parsers/parse2.py b/uncompyle6/parsers/parse2.py index 23e3573d..617c60b8 100644 --- a/uncompyle6/parsers/parse2.py +++ b/uncompyle6/parsers/parse2.py @@ -1,4 +1,4 @@ -# Copyright (c) 2015-2020 Rocky Bernstein +# Copyright (c) 2015-2021 Rocky Bernstein # Copyright (c) 2000-2002 by hartmut Goebel # # Copyright (c) 1999 John Aycock @@ -350,7 +350,7 @@ class Python2Parser(PythonParser): ], customize, ) - if self.version >= 2.7: + if self.version >= (2, 7): self.add_unique_rule( "dict_comp_func ::= BUILD_MAP_n LOAD_FAST FOR_ITER store " "comp_iter JUMP_BACK RETURN_VALUE RETURN_LAST", @@ -577,7 +577,7 @@ class Python2Parser(PythonParser): customize, ) - if self.version >= 2.7: + if self.version >= (2, 7): if i > 0: prev_tok = tokens[i - 1] if prev_tok == "LOAD_DICTCOMP": diff --git a/uncompyle6/parsers/parse24.py b/uncompyle6/parsers/parse24.py index dccf9db9..11bf4dcc 100644 --- a/uncompyle6/parsers/parse24.py +++ b/uncompyle6/parsers/parse24.py @@ -94,7 +94,7 @@ class Python24Parser(Python25Parser): """) super(Python24Parser, self).customize_grammar_rules(tokens, customize) self.remove_rules_24() - if self.version == 2.4: + if self.version[:2] == (2, 4): self.check_reduce['nop_stmt'] = 'tokens' def reduce_is_invalid(self, rule, ast, tokens, first, last): diff --git a/uncompyle6/parsers/parse25.py b/uncompyle6/parsers/parse25.py index 3c30a2e4..9efde865 100644 --- a/uncompyle6/parsers/parse25.py +++ b/uncompyle6/parsers/parse25.py @@ -89,7 +89,7 @@ class Python25Parser(Python26Parser): return_stmt_lambda LAMBDA_MARKER """) 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["aug_assign1"] = "AST" self.check_reduce["ifelsestmt"] = "AST" diff --git a/uncompyle6/parsers/parse26.py b/uncompyle6/parsers/parse26.py index ef123c01..778a2f9d 100644 --- a/uncompyle6/parsers/parse26.py +++ b/uncompyle6/parsers/parse26.py @@ -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. """ @@ -390,7 +390,7 @@ class Python26Parser(Python2Parser): # 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* # 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 test_index = last @@ -425,7 +425,7 @@ class Python26Parser(Python2Parser): # since the operand can be a relative offset rather than # an absolute 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 return tokens[last-1].off2int() > setup_inst.argval elif rule == ("ifstmt", ("testexpr", "_ifstmts_jump")): diff --git a/uncompyle6/parsers/parse3.py b/uncompyle6/parsers/parse3.py index f3841846..ca400273 100644 --- a/uncompyle6/parsers/parse3.py +++ b/uncompyle6/parsers/parse3.py @@ -1361,7 +1361,7 @@ class Python3Parser(PythonParser): "LOAD_CODE LOAD_STR ", opname, ) - elif self.version > (3, 5): + elif self.version >= (3, 6): # positional args before keyword args rule = "mkfunc ::= %s%s %s%s" % ( "pos_arg " * args_pos, @@ -1369,7 +1369,7 @@ class Python3Parser(PythonParser): "LOAD_CODE LOAD_STR ", opname, ) - elif self.version > (3, 3): + elif self.version >= (3, 4): # positional args before keyword args rule = "mkfunc ::= %s%s %s%s" % ( "pos_arg " * args_pos, @@ -1595,7 +1595,7 @@ class Python3Parser(PythonParser): elif lhs == "kwarg": arg = tokens[first].attr 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) elif rule == ("ifstmt", ("testexpr", "_ifstmts_jump")): # FIXME: go over what's up with 3.0. Evetually I'd like to remove RETURN_END_IF diff --git a/uncompyle6/parsers/parse35.py b/uncompyle6/parsers/parse35.py index eef26fe2..0cdc6264 100644 --- a/uncompyle6/parsers/parse35.py +++ b/uncompyle6/parsers/parse35.py @@ -157,7 +157,7 @@ class Python35Parser(Python34Parser): # FIXME: I suspect this is wrong for 3.6 and 3.5, but # I haven't verified what the 3.7ish fix is elif opname == 'BUILD_MAP_UNPACK_WITH_CALL': - if self.version < 3.7: + if self.version < (3, 7): self.addRule("expr ::= unmapexpr", nop_func) nargs = token.attr % 256 map_unpack_n = "map_unpack_%s" % nargs diff --git a/uncompyle6/parsers/parse36.py b/uncompyle6/parsers/parse36.py index b0a99f3e..005246f6 100644 --- a/uncompyle6/parsers/parse36.py +++ b/uncompyle6/parsers/parse36.py @@ -396,7 +396,7 @@ class Python36Parser(Python35Parser): starred ::= expr call_ex ::= expr starred CALL_FUNCTION_EX """, nop_func) - if self.version >= 3.6: + if self.version >= (3, 6): if 'BUILD_MAP_UNPACK_WITH_CALL' in self.seen_ops: self.addRule(""" expr ::= call_ex_kw diff --git a/uncompyle6/parsers/parse37.py b/uncompyle6/parsers/parse37.py index d1ea51cf..7b166c5f 100644 --- a/uncompyle6/parsers/parse37.py +++ b/uncompyle6/parsers/parse37.py @@ -1292,7 +1292,7 @@ class Python37Parser(Python37BaseParser): withasstmt ::= expr SETUP_WITH store suite_stmts_opt COME_FROM_WITH WITH_CLEANUP_START WITH_CLEANUP_FINISH END_FINALLY """ - if self.version < 3.8: + if self.version < (3, 8): rules_str += """ with ::= expr SETUP_WITH POP_TOP suite_stmts_opt POP_BLOCK LOAD_CONST diff --git a/uncompyle6/parsers/parse37base.py b/uncompyle6/parsers/parse37base.py index 23a23c9f..6d9f8498 100644 --- a/uncompyle6/parsers/parse37base.py +++ b/uncompyle6/parsers/parse37base.py @@ -1034,7 +1034,7 @@ class Python37BaseParser(PythonParser): POP_BLOCK LOAD_CONST COME_FROM_WITH with_suffix """ - if self.version < 3.8: + if self.version < (3, 8): rules_str += """ with ::= expr SETUP_WITH POP_TOP suite_stmts_opt POP_BLOCK LOAD_CONST diff --git a/uncompyle6/parsers/reducecheck/except_handler.py b/uncompyle6/parsers/reducecheck/except_handler.py index 88cb8895..8d2341d9 100644 --- a/uncompyle6/parsers/reducecheck/except_handler.py +++ b/uncompyle6/parsers/reducecheck/except_handler.py @@ -10,7 +10,7 @@ def except_handler(self, lhs, n, rule, ast, tokens, first, last): # FIXME: Figure out why this doesn't work on # bytecode-1.4/anydbm.pyc - if self.version == 1.4: + if self.version[:2] == (1, 4): return False # Make sure come froms all come from within "except_handler". diff --git a/uncompyle6/parsers/reducecheck/except_handler_else.py b/uncompyle6/parsers/reducecheck/except_handler_else.py index a5b012c2..e8d39359 100644 --- a/uncompyle6/parsers/reducecheck/except_handler_else.py +++ b/uncompyle6/parsers/reducecheck/except_handler_else.py @@ -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): # 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 if tokens[first] in ("JUMP_FORWARD", "JUMP_ABSOLUTE"): diff --git a/uncompyle6/parsers/reducecheck/ifelsestmt.py b/uncompyle6/parsers/reducecheck/ifelsestmt.py index 29c52673..d0f390da 100644 --- a/uncompyle6/parsers/reducecheck/ifelsestmt.py +++ b/uncompyle6/parsers/reducecheck/ifelsestmt.py @@ -191,7 +191,7 @@ def ifelsestmt(self, lhs, n, rule, ast, tokens, first, last): if last == n: last -= 1 jmp = if_condition[1] - if self.version > (2, 6): + if self.version >= (2, 7): jmp_target = jmp[0].attr else: jmp_target = int(jmp[0].pattr) diff --git a/uncompyle6/parsers/reducecheck/or_check.py b/uncompyle6/parsers/reducecheck/or_check.py index 91e84730..84278155 100644 --- a/uncompyle6/parsers/reducecheck/or_check.py +++ b/uncompyle6/parsers/reducecheck/or_check.py @@ -45,7 +45,7 @@ def or_check(self, lhs, n, rule, ast, tokens, first, last): return True # 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: # For a backwards loop, well compare to the instruction *after* # then POP_JUMP... diff --git a/uncompyle6/parsers/reducecheck/testtrue.py b/uncompyle6/parsers/reducecheck/testtrue.py index 45450de0..33d98314 100644 --- a/uncompyle6/parsers/reducecheck/testtrue.py +++ b/uncompyle6/parsers/reducecheck/testtrue.py @@ -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): # FIXME: make this work for all versions - if self.version != 3.7: + if self.version[:2] != (3, 7): return False if rule == ("testtrue", ("expr", "jmp_true")): pjit = tokens[min(last - 1, n - 2)] diff --git a/uncompyle6/parsers/reducecheck/while1elsestmt.py b/uncompyle6/parsers/reducecheck/while1elsestmt.py index 31272eec..c3f07e64 100644 --- a/uncompyle6/parsers/reducecheck/while1elsestmt.py +++ b/uncompyle6/parsers/reducecheck/while1elsestmt.py @@ -20,4 +20,4 @@ def while1elsestmt(self, lhs, n, rule, ast, tokens, first, last): # not while1else. Also do for whileTrue? last += 1 # 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()