Instruction fixup broken 3.x make_func...

for handling default values
This commit is contained in:
rocky
2018-03-02 08:03:51 -05:00
parent 8d503682b3
commit bb13988126
2 changed files with 29 additions and 15 deletions

View File

@@ -7,3 +7,9 @@ def foo3(bar, baz=1, qux=2):
return 3
def foo4(bar, baz, qux=1, quux=2):
return 4
# From 3.6 compileall.
# Bug was in omitting default which when used in an "if"
# are treated as False would be
def _walk_dir(dir, ddir=None, maxlevels=10, quiet=0):
return

View File

@@ -459,26 +459,28 @@ def make_function3(self, node, is_lambda, nested=1, codeNode=None):
# MAKE_CLOSURE adds an additional closure slot
# Thank you, Python, for a such a well-thought out system that has
# changed 4 or so times.
# Thank you, Python: such a well-thought out system that has
# changed and continues to change many times.
def build_param(ast, name, default):
"""build parameters:
- handle defaults
- handle format tuple parameters
"""
if default:
if self.version >= 3.6:
value = default
else:
value = self.traverse(default, indent='')
maybe_show_tree_param_default(self.showast, name, value)
result = '%s=%s' % (name, value)
if result[-2:] == '= ': # default was 'LOAD_CONST None'
result += 'None'
return result
if self.version >= 3.6:
value = default
else:
return name
value = self.traverse(default, indent='')
maybe_show_tree_param_default(self.showast, name, value)
result = '%s=%s' % (name, value)
# The below can probably be removed. This is probably
# a holdover from days when LOAD_CONST erroneously
# didn't handle LOAD_CONST None properly
if result[-2:] == '= ': # default was 'LOAD_CONST None'
result += 'None'
return result
# MAKE_FUNCTION_... or MAKE_CLOSURE_...
assert node[-1].kind.startswith('MAKE_')
@@ -552,8 +554,14 @@ def make_function3(self, node, is_lambda, nested=1, codeNode=None):
kw_pairs = args_node.attr[1] if self.version >= 3.0 else 0
# build parameters
params = [build_param(ast, name, d) for
name, d in zip_longest(paramnames, defparams, fillvalue=None)]
params = []
if defparams:
for i, defparam in enumerate(defparams):
params.append(build_param(ast, paramnames[i], defparam))
params += paramnames[i+1:]
else:
params = paramnames
if not 3.0 <= self.version <= 3.1 or self.version >= 3.6:
params.reverse() # back to correct order