1.1 --- a/micropython/inspect.py Sat Mar 22 22:31:54 2008 +0100
1.2 +++ b/micropython/inspect.py Mon Mar 24 23:53:18 2008 +0100
1.3 @@ -316,6 +316,7 @@
1.4
1.5 self.location = None
1.6 self.code_location = None
1.7 + self.instantiator = None
1.8
1.9 def __repr__(self):
1.10 if self.location is not None:
1.11 @@ -330,6 +331,15 @@
1.12 self.finalise_class_attributes()
1.13 self.finalise_instance_attributes()
1.14
1.15 + def get_instantiator(self):
1.16 +
1.17 + "Return a function which can be used to instantiate the class."
1.18 +
1.19 + if self.instantiator is None:
1.20 + init_method = self.all_class_attributes()["__init__"].value
1.21 + self.instantiator = init_method.function_from_method()
1.22 + return self.instantiator
1.23 +
1.24 # Class-specific methods.
1.25
1.26 def add_base(self, base):
1.27 @@ -665,6 +675,12 @@
1.28 for i, attr in enumerate(self.locals().values()):
1.29 attr.position = i + j
1.30
1.31 + def function_from_method(self):
1.32 +
1.33 + "Make a function from a method."
1.34 +
1.35 + return Function(self.name, self.parent, self.argnames[1:], self.has_star, self.has_dstar, self.global_namespace, self.node)
1.36 +
1.37 class UnresolvedName(NamespaceDict):
1.38
1.39 "A module, class or function which was mentioned but could not be imported."
1.40 @@ -767,6 +783,7 @@
1.41 self.visitor = self
1.42
1.43 self.importer = importer
1.44 + self.builtins = self.importer.modules.get("__builtins__")
1.45 self.loaded = 0
1.46
1.47 # Current expression state.
1.48 @@ -926,7 +943,7 @@
1.49 # NOTE: Potentially dubious measure to permit __init__ availability.
1.50 # If no bases exist, adopt the 'object' class.
1.51
1.52 - if not node.bases:
1.53 + if not node.bases and not (self.name == "__builtins__" and node.name == "object") :
1.54 expr = self.dispatch(compiler.ast.Name("object"))
1.55 cls.add_base(expr.value)
1.56
1.57 @@ -1054,7 +1071,10 @@
1.58 return value.namespace.get(node.attrname)
1.59 elif isinstance(value, UnresolvedName):
1.60 return UnresolvedName(node.attrname, value.full_name(), self)
1.61 - return builtins.get(node.attrname)
1.62 + if self.builtins is not None:
1.63 + return self.builtins.get(node.attrname)
1.64 + else:
1.65 + return UnresolvedName(node.attrname, value.full_name(), self)
1.66
1.67 def visitGlobal(self, node):
1.68 if self.namespaces:
1.69 @@ -1121,8 +1141,8 @@
1.70 return self.namespaces[-1][name]
1.71 elif self.has_key(name):
1.72 return self[name]
1.73 - elif builtins.has_key(name):
1.74 - return builtins[name]
1.75 + elif self.builtins is not None and self.builtins.has_key(name):
1.76 + return self.builtins[name]
1.77 else:
1.78 return None
1.79
1.80 @@ -1191,71 +1211,4 @@
1.81
1.82 pass
1.83
1.84 -# Built-in types initialisation.
1.85 -
1.86 -class Builtins(Module):
1.87 -
1.88 - "The special built-in types module."
1.89 -
1.90 - def __init__(self):
1.91 - Module.__init__(self, "__builtins__")
1.92 - self.loaded = 1
1.93 - self.module = None
1.94 -
1.95 - for key in ['ArithmeticError', 'AssertionError', 'AttributeError',
1.96 - 'BaseException', 'DeprecationWarning', 'EOFError', 'Ellipsis',
1.97 - 'EnvironmentError', 'Exception', 'False', 'FloatingPointError',
1.98 - 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError',
1.99 - 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError',
1.100 - 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError',
1.101 - 'None', 'NotImplemented', 'NotImplementedError', 'OSError',
1.102 - 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError',
1.103 - 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration',
1.104 - 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit',
1.105 - 'TabError', 'True', 'TypeError', 'UnboundLocalError',
1.106 - 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError',
1.107 - 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning',
1.108 - 'ValueError', 'Warning', 'ZeroDivisionError',
1.109 - 'basestring', 'bool', 'buffer', 'complex', 'dict', 'file', 'float',
1.110 - 'frozenset', 'int', # 'list',
1.111 - 'long', 'object', 'set', 'slice', 'str',
1.112 - 'tuple', 'type', 'unicode', 'xrange']:
1.113 -
1.114 - cls = Class(key, self, self)
1.115 - cls.set("__init__", Function("__init__", cls, [], 0, 0, cls))
1.116 - self.store(key, cls)
1.117 -
1.118 - # NOTE: Temporary measure - provide detailed built-ins.
1.119 -
1.120 - cls = Class("list", self, self)
1.121 - cls.set("__init__", Function("__init__", cls, [], 0, 0, cls))
1.122 - cls.set("__iter__", Function("__iter__", cls, [], 0, 0, cls))
1.123 - self.store("list", cls)
1.124 -
1.125 - cls = Class("listiterator", self, self)
1.126 - cls.set("__init__", Function("__init__", cls, [], 0, 0, cls))
1.127 - cls.set("next", Function("next", cls, [], 0, 0, cls))
1.128 - self.store("listiterator", cls)
1.129 -
1.130 - # NOTE: Incomplete: some functions have more than one parameter.
1.131 -
1.132 - for key in ['__import__', 'abs', 'all', 'any', 'callable', 'chr',
1.133 - 'classmethod', 'cmp', 'compile', 'delattr', 'dir', 'divmod',
1.134 - 'enumerate', 'eval', 'execfile', 'filter', 'getattr', 'globals',
1.135 - 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'isinstance',
1.136 - 'issubclass', 'iter', 'len', 'locals', 'map', 'max', 'min', 'oct',
1.137 - 'open', 'ord', 'pow', 'property', 'range', 'raw_input', 'reduce',
1.138 - 'reload', 'repr', 'reversed', 'round', 'setattr', 'sorted',
1.139 - 'staticmethod', 'sum', 'super', 'unichr', 'vars', 'zip']:
1.140 - self[key] = Function(key, self, ['arg'], 0, 0, self)
1.141 -
1.142 - def store(self, name, obj):
1.143 - self.set(name, obj)
1.144 - self.all_objects.add(obj)
1.145 -
1.146 - def vacuum(self):
1.147 - pass
1.148 -
1.149 -builtins = Builtins()
1.150 -
1.151 # vim: tabstop=4 expandtab shiftwidth=4