1.1 --- a/micropython/inspect.py Sat Jun 05 15:11:26 2010 +0200
1.2 +++ b/micropython/inspect.py Sun Jun 06 03:03:09 2010 +0200
1.3 @@ -261,6 +261,10 @@
1.4
1.5 # Namespace methods.
1.6
1.7 + def in_class(self, namespaces=None):
1.8 + namespaces = namespaces or self.namespaces
1.9 + return len(namespaces) > 1 and isinstance(namespaces[-2], Class)
1.10 +
1.11 def store(self, name, obj):
1.12
1.13 "Record attribute or local 'name', storing 'obj'."
1.14 @@ -454,19 +458,28 @@
1.15
1.16 self.functions.append((node, self.namespaces + [function]))
1.17
1.18 + # Store the function.
1.19 +
1.20 if name is not None:
1.21 self.store(name, function)
1.22 else:
1.23 self.store_lambda(function)
1.24
1.25 - # Where defaults exist, an instance needs creating. Thus, it makes
1.26 - # no sense to return a reference to the function here, since the
1.27 - # recipient will not be referencing the function itself.
1.28 + # Test the defaults and assess whether an dynamic object will result.
1.29 +
1.30 + if function.make_dynamic():
1.31 +
1.32 + # Forbid dynamic methods, since the context of methods is the
1.33 + # instance itself.
1.34
1.35 - if node.defaults:
1.36 - return Instance() # indicates no known target
1.37 + if self.in_class():
1.38 + raise InspectError(self.full_name(), node, "Methods cannot define dynamic defaults.")
1.39
1.40 - return function
1.41 + result = Instance() # indicates no known target
1.42 + else:
1.43 + result = function
1.44 +
1.45 + return result
1.46
1.47 def _visitFunctionBody(self, node, namespaces):
1.48
1.49 @@ -475,7 +488,7 @@
1.50 # Current namespace is the function.
1.51 # Previous namespace is the class.
1.52
1.53 - if len(namespaces) > 1 and isinstance(namespaces[-2], Class):
1.54 + if self.in_class(namespaces):
1.55 if namespaces[-1].name == "__init__":
1.56 self.in_init = 1
1.57 self.in_method = 1