1.1 --- a/translator.py Wed Nov 30 16:39:41 2016 +0100
1.2 +++ b/translator.py Wed Nov 30 18:41:04 2016 +0100
1.3 @@ -877,7 +877,7 @@
1.4
1.5 # Obtain details of the defaults.
1.6
1.7 - defaults = self.process_function_defaults(n, name, "&%s" % objpath)
1.8 + defaults = self.process_function_defaults(n, name, objpath)
1.9 if defaults:
1.10 for default in defaults:
1.11 self.writeline("%s;" % default)
1.12 @@ -897,13 +897,14 @@
1.13 context and "&%s" % encode_path(context) or "0",
1.14 encode_path(objpath))))
1.15
1.16 - def process_function_defaults(self, n, name, instance_name):
1.17 + def process_function_defaults(self, n, name, objpath, instance_name=None):
1.18
1.19 """
1.20 Process the given function or lambda node 'n', initialising defaults
1.21 that are dynamically set. The given 'name' indicates the name of the
1.22 - function. The given 'instance_name' indicates the name of any separate
1.23 - instance of the function created to hold the defaults.
1.24 + function. The given 'objpath' indicates the origin of the function.
1.25 + The given 'instance_name' indicates the name of any separate instance
1.26 + of the function created to hold the defaults.
1.27
1.28 Return a list of operations setting defaults on a function instance.
1.29 """
1.30 @@ -915,10 +916,20 @@
1.31
1.32 # Determine whether any unidentified defaults are involved.
1.33
1.34 - need_defaults = [argname for argname, default in function_defaults if default.has_kind("<var>")]
1.35 - if not need_defaults:
1.36 + for argname, default in function_defaults:
1.37 + if not default.static():
1.38 + break
1.39 + else:
1.40 return None
1.41
1.42 + # Handle bound methods.
1.43 +
1.44 + if not instance_name:
1.45 + if self.is_method(objpath):
1.46 + instance_name = "&%s" % encode_bound_reference(objpath)
1.47 + else:
1.48 + instance_name = "&%s" % encode_path(objpath)
1.49 +
1.50 # Where defaults are involved but cannot be identified, obtain a new
1.51 # instance of the lambda and populate the defaults.
1.52
1.53 @@ -942,7 +953,7 @@
1.54 continue
1.55
1.56 if name_ref:
1.57 - defaults.append("__SETDEFAULT(%s, %s, %s)" % (encode_path(instance_name), i, name_ref))
1.58 + defaults.append("__SETDEFAULT(%s, %s, %s)" % (instance_name, i, name_ref))
1.59
1.60 return defaults
1.61
1.62 @@ -998,7 +1009,7 @@
1.63
1.64 if expr.has_kind("<class>"):
1.65 target = encode_instantiator_pointer(objpath)
1.66 - target_structure = encode_initialiser_pointer(objpath)
1.67 + target_structure = "&%s" % encode_bound_reference("%s.__init__" % objpath)
1.68
1.69 # Only plain functions and bound methods employ function pointers.
1.70
1.71 @@ -1011,7 +1022,9 @@
1.72
1.73 if not self.is_method(objpath) or accessor_kinds and len(accessor_kinds) == 1 and first(accessor_kinds) == "<instance>":
1.74 target = encode_function_pointer(objpath)
1.75 - target_structure = encode_path(objpath)
1.76 + target_structure = self.is_method(objpath) and \
1.77 + encode_bound_reference(objpath) or \
1.78 + "&%s" % encode_path(objpath)
1.79
1.80 # Other targets are retrieved at run-time.
1.81
1.82 @@ -1079,7 +1092,7 @@
1.83 for i, (argname, default) in enumerate(function_defaults):
1.84 argnum = parameters.index(argname)
1.85 if not args[argnum+1]:
1.86 - args[argnum+1] = "__GETDEFAULT(&%s, %d)" % (target_structure, i)
1.87 + args[argnum+1] = "__GETDEFAULT(%s, %d)" % (target_structure, i)
1.88
1.89 # Test for missing arguments.
1.90
1.91 @@ -1163,7 +1176,7 @@
1.92 name = self.get_lambda_name()
1.93 function_name = self.get_object_path(name)
1.94
1.95 - defaults = self.process_function_defaults(n, name, "__tmp_value")
1.96 + defaults = self.process_function_defaults(n, name, function_name, "__tmp_value")
1.97
1.98 # Without defaults, produce an attribute referring to the function.
1.99