1.1 --- a/micropython/inspect.py Fri Nov 02 23:59:34 2007 +0100
1.2 +++ b/micropython/inspect.py Sat Nov 03 02:59:43 2007 +0100
1.3 @@ -45,6 +45,9 @@
1.4 def __getitem__(self, name):
1.5 return self.namespace[name]
1.6
1.7 + def get(self, name, default=None):
1.8 + return self.namespace.get(name, default)
1.9 +
1.10 def __setitem__(self, name, value):
1.11 if name not in self.globals:
1.12 self.namespace[name] = value
1.13 @@ -318,18 +321,11 @@
1.14
1.15 # Program visitors.
1.16
1.17 -class Module(ASTVisitor, NamespaceDict):
1.18 -
1.19 - "An inspected module."
1.20 +class Module(NamespaceDict):
1.21
1.22 - def __init__(self, name, importer=None):
1.23 - ASTVisitor.__init__(self)
1.24 + def __init__(self, name):
1.25 NamespaceDict.__init__(self)
1.26 - self.visitor = self
1.27 -
1.28 self.name = name
1.29 - self.importer = importer
1.30 - self.loaded = 0
1.31
1.32 # Module attributes.
1.33
1.34 @@ -340,6 +336,50 @@
1.35
1.36 self.all_objects = []
1.37
1.38 + # Image generation details.
1.39 +
1.40 + self.location = None
1.41 +
1.42 + def full_name(self):
1.43 + return self.name
1.44 +
1.45 + def __repr__(self):
1.46 + return "Module(%r, location=%r)" % (self.name, self.location)
1.47 +
1.48 + # Attribute methods.
1.49 +
1.50 + def module_attribute_names(self):
1.51 +
1.52 + "Return the module attribute names provided by the module."
1.53 +
1.54 + if self.modattr_names is None:
1.55 + self.module_attributes()
1.56 + return self.modattr_names
1.57 +
1.58 + def module_attributes(self):
1.59 +
1.60 + "Return a dictionary mapping names to module attributes."
1.61 +
1.62 + if self.modattr is None:
1.63 + self.modattr = {}
1.64 + self.modattr_names = self.keys()
1.65 + for i, name in enumerate(self.modattr_names):
1.66 + self.modattr[name] = Attr(i, self, self[name])
1.67 +
1.68 + return self.modattr
1.69 +
1.70 +class InspectedModule(ASTVisitor, Module):
1.71 +
1.72 + "An inspected module."
1.73 +
1.74 + def __init__(self, name, importer=None):
1.75 + ASTVisitor.__init__(self)
1.76 + Module.__init__(self, name)
1.77 + self.visitor = self
1.78 +
1.79 + self.importer = importer
1.80 + self.loaded = 0
1.81 +
1.82 # Current expression state.
1.83
1.84 self.expr = None
1.85 @@ -350,10 +390,6 @@
1.86 self.namespaces = []
1.87 self.module = None
1.88
1.89 - # Image generation details.
1.90 -
1.91 - self.location = None
1.92 -
1.93 def parse(self, filename):
1.94
1.95 "Parse the file having the given 'filename'."
1.96 @@ -382,34 +418,6 @@
1.97 if isinstance(value, Module) and not value.loaded:
1.98 del self[name]
1.99
1.100 - def full_name(self):
1.101 - return self.name
1.102 -
1.103 - def __repr__(self):
1.104 - return "Module(%r, location=%r)" % (self.name, self.location)
1.105 -
1.106 - # Attribute methods.
1.107 -
1.108 - def module_attribute_names(self):
1.109 -
1.110 - "Return the module attribute names provided by the module."
1.111 -
1.112 - if self.modattr_names is None:
1.113 - self.module_attributes()
1.114 - return self.modattr_names
1.115 -
1.116 - def module_attributes(self):
1.117 -
1.118 - "Return a dictionary mapping names to module attributes."
1.119 -
1.120 - if self.modattr is None:
1.121 - self.modattr = {}
1.122 - self.modattr_names = self.keys()
1.123 - for i, name in enumerate(self.modattr_names):
1.124 - self.modattr[name] = Attr(i, self, self[name])
1.125 -
1.126 - return self.modattr
1.127 -
1.128 # Namespace methods.
1.129
1.130 def store(self, name, obj):
1.131 @@ -431,8 +439,10 @@
1.132 "Record instance attribute 'name' in the current class."
1.133
1.134 if self.in_init:
1.135 +
1.136 # Current namespace is the function.
1.137 # Previous namespace is the class.
1.138 +
1.139 self.namespaces[-2].add_instance_attribute(name)
1.140
1.141 def get_parent(self):
1.142 @@ -573,10 +583,13 @@
1.143 )
1.144
1.145 self.namespaces.append(function)
1.146 +
1.147 # Current namespace is the function.
1.148 # Previous namespace is the class.
1.149 +
1.150 if node.name == "__init__" and isinstance(self.namespaces[-2], Class):
1.151 self.in_init = 1
1.152 +
1.153 self.dispatch(node.code)
1.154 self.in_init = 0
1.155 self.namespaces.pop()
1.156 @@ -599,12 +612,14 @@
1.157 return expr.namespace.get(node.attrname)
1.158 elif isinstance(expr, UnresolvedName):
1.159 return UnresolvedName(node.attrname, expr.full_name())
1.160 - return builtins_namespace.get(node.attrname)
1.161 + return builtins.get(node.attrname)
1.162
1.163 def visitGlobal(self, node):
1.164 if self.namespaces:
1.165 for name in node.names:
1.166 self.namespaces[-1].make_global(name)
1.167 + if not self.has_key(name):
1.168 + self[name] = None
1.169
1.170 def visitIf(self, node):
1.171 for test, body in node.tests:
1.172 @@ -656,8 +671,8 @@
1.173 return Self()
1.174 elif self.has_key(name):
1.175 return self[name]
1.176 - elif builtins_namespace.has_key(name):
1.177 - return builtins_namespace[name]
1.178 + elif builtins.has_key(name):
1.179 + return builtins[name]
1.180 else:
1.181 return None
1.182
1.183 @@ -720,23 +735,31 @@
1.184
1.185 # Built-in types initialisation.
1.186
1.187 -builtins_namespace = {}
1.188 -for key in ['ArithmeticError', 'AssertionError', 'AttributeError',
1.189 - 'BaseException', 'DeprecationWarning', 'EOFError', 'Ellipsis',
1.190 - 'EnvironmentError', 'Exception', 'False', 'FloatingPointError',
1.191 - 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError',
1.192 - 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError',
1.193 - 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError',
1.194 - 'None', 'NotImplemented', 'NotImplementedError', 'OSError',
1.195 - 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError',
1.196 - 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration',
1.197 - 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit',
1.198 - 'TabError', 'True', 'TypeError', 'UnboundLocalError',
1.199 - 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError',
1.200 - 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning',
1.201 - 'ValueError', 'Warning', 'ZeroDivisionError',
1.202 - 'bool', 'buffer', 'complex', 'dict', 'file', 'float', 'int', 'list',
1.203 - 'long', 'object', 'slice', 'str', 'tuple', 'type', 'unicode']:
1.204 - builtins_namespace[key] = Class(key, "__builtins__")
1.205 +class Builtins(Module):
1.206 +
1.207 + "The special built-in types module."
1.208 +
1.209 + def __init__(self):
1.210 + Module.__init__(self, "__builtins__")
1.211 +
1.212 + for key in ['ArithmeticError', 'AssertionError', 'AttributeError',
1.213 + 'BaseException', 'DeprecationWarning', 'EOFError', 'Ellipsis',
1.214 + 'EnvironmentError', 'Exception', 'False', 'FloatingPointError',
1.215 + 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError',
1.216 + 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError',
1.217 + 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError',
1.218 + 'None', 'NotImplemented', 'NotImplementedError', 'OSError',
1.219 + 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError',
1.220 + 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration',
1.221 + 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit',
1.222 + 'TabError', 'True', 'TypeError', 'UnboundLocalError',
1.223 + 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError',
1.224 + 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning',
1.225 + 'ValueError', 'Warning', 'ZeroDivisionError',
1.226 + 'bool', 'buffer', 'complex', 'dict', 'file', 'float', 'int', 'list',
1.227 + 'long', 'object', 'slice', 'str', 'tuple', 'type', 'unicode']:
1.228 + self[key] = Class(key, self.full_name())
1.229 +
1.230 +builtins = Builtins()
1.231
1.232 # vim: tabstop=4 expandtab shiftwidth=4