From cb40caa73c0da681571abb279037d75103e91650 Mon Sep 17 00:00:00 2001 From: rocky Date: Wed, 5 Jun 2019 20:14:04 -0400 Subject: [PATCH] DRY x0ret's code a little bit. --- uncompyle6/semantics/customize.py | 5 ---- uncompyle6/semantics/customize26_27.py | 13 ++++++---- uncompyle6/semantics/customize3.py | 35 ++++++++++---------------- uncompyle6/semantics/customize35.py | 14 +++-------- uncompyle6/semantics/helper.py | 20 +++++++++++++++ 5 files changed, 45 insertions(+), 42 deletions(-) diff --git a/uncompyle6/semantics/customize.py b/uncompyle6/semantics/customize.py index d40d976e..5c18d325 100644 --- a/uncompyle6/semantics/customize.py +++ b/uncompyle6/semantics/customize.py @@ -49,11 +49,6 @@ def customize_for_version(self, is_pypy, version): 5, 6, 7, 0, 1, 2 ), }) if version >= 3.0: - TABLE_DIRECT.update({ - # Gotta love Python for its futzing around with syntax like this - 'raise_stmt2': ( '%|raise %c from %c\n', 0, 1), - }) - if version >= 3.2: TABLE_DIRECT.update({ 'del_deref_stmt': ( '%|del %c\n', 0), diff --git a/uncompyle6/semantics/customize26_27.py b/uncompyle6/semantics/customize26_27.py index a9ff1c61..8f7f907d 100644 --- a/uncompyle6/semantics/customize26_27.py +++ b/uncompyle6/semantics/customize26_27.py @@ -31,28 +31,31 @@ def customize_for_version26_27(self, version): if version > 2.6: TABLE_DIRECT.update({ 'except_cond2': ( '%|except %c as %c:\n', 1, 5 ), + # When a generator is a single parameter of a function, + # it doesn't need the surrounding parenethesis. + 'generator_no_parens': ('%c%P', 0, (1, -1, ', ', 100)), }) else: TABLE_DIRECT.update({ 'testtrue_then': ( 'not %p', (0, 22) ), - }) def n_call(node): mapping = self._get_mapping(node) - table = mapping[0] key = node for i in mapping[1:]: key = key[i] pass if key.kind == 'CALL_FUNCTION_1': + # A function with one argument. If this is a generator, + # no parenthesis is needed. args_node = node[-2] if args_node == 'expr': n = args_node[0] if n == 'generator_exp': - template = ('%c%P', 0, (1, -1, ', ', 100)) - self.template_engine(template, node) - self.prune() + node.kind = 'generator_no_parens' + pass + pass self.default(node) self.n_call = n_call diff --git a/uncompyle6/semantics/customize3.py b/uncompyle6/semantics/customize3.py index 065da6fe..8c95791b 100644 --- a/uncompyle6/semantics/customize3.py +++ b/uncompyle6/semantics/customize3.py @@ -19,6 +19,7 @@ from uncompyle6.semantics.consts import TABLE_DIRECT from xdis.code import iscode +from uncompyle6.semantics.helper import gen_function_parens_adjust from uncompyle6.semantics.make_function import make_function3_annotate from uncompyle6.semantics.customize35 import customize_for_version35 from uncompyle6.semantics.customize36 import customize_for_version36 @@ -33,8 +34,14 @@ def customize_for_version3(self, version): (2, 'expr') , (0, 'expr'), (4, 'expr') ), 'except_cond2' : ( '%|except %c as %c:\n', 1, 5 ), 'function_def_annotate': ( '\n\n%|def %c%c\n', -1, 0), + + # When a generator is a single parameter of a function, + # it doesn't need the surrounding parenethesis. + 'generator_no_parens': ('%c%P', 0, (1, -1, ', ', 100)), + 'importmultiple' : ( '%|import %c%c\n', 2, 3 ), 'import_cont' : ( ', %c', 2 ), + 'raise_stmt2' : ( '%|raise %c from %c\n', 0, 1), 'store_locals' : ( '%|# inspect.currentframe().f_locals = __locals__\n', ), 'withstmt' : ( '%|with %c:\n%+%c%-', 0, 3), 'withasstmt' : ( '%|with %c as (%c):\n%+%c%-', 0, 2, 3), @@ -195,7 +202,9 @@ def customize_for_version3(self, version): self.n_yield_from = n_yield_from if 3.2 <= version <= 3.4: + def n_call(node): + mapping = self._get_mapping(node) key = node for i in mapping[1:]: @@ -227,17 +236,10 @@ def customize_for_version3(self, version): -2, (-2-kwargs, -2, ', ')) self.template_engine(template, node) self.prune() - elif key.kind == 'CALL_FUNCTION_1': - args_node = node[-2] - if args_node == 'pos_arg': - assert args_node[0] == 'expr' - n = args_node[0][0] - if n == 'generator_exp': - template = ('%c%P', 0, (1, -1, ', ', 100)) - self.template_engine(template, node) - self.prune() - + else: + gen_function_parens_adjust(key, node) self.default(node) + self.n_call = n_call elif version < 3.2: def n_call(node): @@ -246,21 +248,10 @@ def customize_for_version3(self, version): for i in mapping[1:]: key = key[i] pass - if key.kind == 'CALL_FUNCTION_1': - args_node = node[-2] - if args_node == 'pos_arg': - assert args_node[0] == 'expr' - n = args_node[0][0] - if n == 'generator_exp': - template = ('%c%P', 0, (1, -1, ', ', 100)) - self.template_engine(template, node) - self.prune() - + gen_function_parens_adjust(key, node) self.default(node) self.n_call = n_call - - def n_mkfunc_annotate(node): if self.version >= 3.3 or node[-2] == 'kwargs': diff --git a/uncompyle6/semantics/customize35.py b/uncompyle6/semantics/customize35.py index 90fb3f3d..aab2bf25 100644 --- a/uncompyle6/semantics/customize35.py +++ b/uncompyle6/semantics/customize35.py @@ -19,7 +19,8 @@ from xdis.code import iscode from xdis.util import COMPILER_FLAG_BIT from uncompyle6.semantics.consts import ( INDENT_PER_LEVEL, TABLE_DIRECT) -from uncompyle6.semantics.helper import flatten_list +from uncompyle6.semantics.helper import ( + flatten_list, gen_function_parens_adjust) ####################### # Python 3.5+ Changes # @@ -112,15 +113,8 @@ def customize_for_version35(self, version): template = ('*%c)', nargs+1) self.template_engine(template, node) self.prune() - elif key.kind == 'CALL_FUNCTION_1': - args_node = node[-2] - if args_node == 'pos_arg': - assert args_node[0] == 'expr' - n = args_node[0][0] - if n == 'generator_exp': - template = ('%c%P', 0, (1, -1, ', ', 100)) - self.template_engine(template, node) - self.prune() + else: + gen_function_parens_adjust(key, node) self.default(node) self.n_call = n_call diff --git a/uncompyle6/semantics/helper.py b/uncompyle6/semantics/helper.py index 725e4f14..9bd16662 100644 --- a/uncompyle6/semantics/helper.py +++ b/uncompyle6/semantics/helper.py @@ -196,6 +196,26 @@ def flatten_list(node): pass return flat_elems +# Note: this is only used in Python > 3.0 +# Should move this somewhere more specific? +def gen_function_parens_adjust(mapping_key, node): + """If we can avoid the outer parenthesis + of a generator function, set the node key to + 'generator_no_parens' and the caller will do the default + action on that. Otherwise we do nothing. + """ + if mapping_key.kind != 'CALL_FUNCTION_1': + return + + args_node = node[-2] + if args_node == 'pos_arg': + assert args_node[0] == 'expr' + n = args_node[0][0] + if n == 'generator_exp': + node.kind = 'generator_no_parens' + pass + return + # if __name__ == '__main__': # if PYTHON3: