3.1 --- a/micropython/inspect.py Fri Nov 02 23:59:34 2007 +0100
3.2 +++ b/micropython/inspect.py Sat Nov 03 02:59:43 2007 +0100
3.3 @@ -45,6 +45,9 @@
3.4 def __getitem__(self, name):
3.5 return self.namespace[name]
3.6
3.7 + def get(self, name, default=None):
3.8 + return self.namespace.get(name, default)
3.9 +
3.10 def __setitem__(self, name, value):
3.11 if name not in self.globals:
3.12 self.namespace[name] = value
3.13 @@ -318,18 +321,11 @@
3.14
3.15 # Program visitors.
3.16
3.17 -class Module(ASTVisitor, NamespaceDict):
3.18 -
3.19 - "An inspected module."
3.20 +class Module(NamespaceDict):
3.21
3.22 - def __init__(self, name, importer=None):
3.23 - ASTVisitor.__init__(self)
3.24 + def __init__(self, name):
3.25 NamespaceDict.__init__(self)
3.26 - self.visitor = self
3.27 -
3.28 self.name = name
3.29 - self.importer = importer
3.30 - self.loaded = 0
3.31
3.32 # Module attributes.
3.33
3.34 @@ -340,6 +336,50 @@
3.35
3.36 self.all_objects = []
3.37
3.38 + # Image generation details.
3.39 +
3.40 + self.location = None
3.41 +
3.42 + def full_name(self):
3.43 + return self.name
3.44 +
3.45 + def __repr__(self):
3.46 + return "Module(%r, location=%r)" % (self.name, self.location)
3.47 +
3.48 + # Attribute methods.
3.49 +
3.50 + def module_attribute_names(self):
3.51 +
3.52 + "Return the module attribute names provided by the module."
3.53 +
3.54 + if self.modattr_names is None:
3.55 + self.module_attributes()
3.56 + return self.modattr_names
3.57 +
3.58 + def module_attributes(self):
3.59 +
3.60 + "Return a dictionary mapping names to module attributes."
3.61 +
3.62 + if self.modattr is None:
3.63 + self.modattr = {}
3.64 + self.modattr_names = self.keys()
3.65 + for i, name in enumerate(self.modattr_names):
3.66 + self.modattr[name] = Attr(i, self, self[name])
3.67 +
3.68 + return self.modattr
3.69 +
3.70 +class InspectedModule(ASTVisitor, Module):
3.71 +
3.72 + "An inspected module."
3.73 +
3.74 + def __init__(self, name, importer=None):
3.75 + ASTVisitor.__init__(self)
3.76 + Module.__init__(self, name)
3.77 + self.visitor = self
3.78 +
3.79 + self.importer = importer
3.80 + self.loaded = 0
3.81 +
3.82 # Current expression state.
3.83
3.84 self.expr = None
3.85 @@ -350,10 +390,6 @@
3.86 self.namespaces = []
3.87 self.module = None
3.88
3.89 - # Image generation details.
3.90 -
3.91 - self.location = None
3.92 -
3.93 def parse(self, filename):
3.94
3.95 "Parse the file having the given 'filename'."
3.96 @@ -382,34 +418,6 @@
3.97 if isinstance(value, Module) and not value.loaded:
3.98 del self[name]
3.99
3.100 - def full_name(self):
3.101 - return self.name
3.102 -
3.103 - def __repr__(self):
3.104 - return "Module(%r, location=%r)" % (self.name, self.location)
3.105 -
3.106 - # Attribute methods.
3.107 -
3.108 - def module_attribute_names(self):
3.109 -
3.110 - "Return the module attribute names provided by the module."
3.111 -
3.112 - if self.modattr_names is None:
3.113 - self.module_attributes()
3.114 - return self.modattr_names
3.115 -
3.116 - def module_attributes(self):
3.117 -
3.118 - "Return a dictionary mapping names to module attributes."
3.119 -
3.120 - if self.modattr is None:
3.121 - self.modattr = {}
3.122 - self.modattr_names = self.keys()
3.123 - for i, name in enumerate(self.modattr_names):
3.124 - self.modattr[name] = Attr(i, self, self[name])
3.125 -
3.126 - return self.modattr
3.127 -
3.128 # Namespace methods.
3.129
3.130 def store(self, name, obj):
3.131 @@ -431,8 +439,10 @@
3.132 "Record instance attribute 'name' in the current class."
3.133
3.134 if self.in_init:
3.135 +
3.136 # Current namespace is the function.
3.137 # Previous namespace is the class.
3.138 +
3.139 self.namespaces[-2].add_instance_attribute(name)
3.140
3.141 def get_parent(self):
3.142 @@ -573,10 +583,13 @@
3.143 )
3.144
3.145 self.namespaces.append(function)
3.146 +
3.147 # Current namespace is the function.
3.148 # Previous namespace is the class.
3.149 +
3.150 if node.name == "__init__" and isinstance(self.namespaces[-2], Class):
3.151 self.in_init = 1
3.152 +
3.153 self.dispatch(node.code)
3.154 self.in_init = 0
3.155 self.namespaces.pop()
3.156 @@ -599,12 +612,14 @@
3.157 return expr.namespace.get(node.attrname)
3.158 elif isinstance(expr, UnresolvedName):
3.159 return UnresolvedName(node.attrname, expr.full_name())
3.160 - return builtins_namespace.get(node.attrname)
3.161 + return builtins.get(node.attrname)
3.162
3.163 def visitGlobal(self, node):
3.164 if self.namespaces:
3.165 for name in node.names:
3.166 self.namespaces[-1].make_global(name)
3.167 + if not self.has_key(name):
3.168 + self[name] = None
3.169
3.170 def visitIf(self, node):
3.171 for test, body in node.tests:
3.172 @@ -656,8 +671,8 @@
3.173 return Self()
3.174 elif self.has_key(name):
3.175 return self[name]
3.176 - elif builtins_namespace.has_key(name):
3.177 - return builtins_namespace[name]
3.178 + elif builtins.has_key(name):
3.179 + return builtins[name]
3.180 else:
3.181 return None
3.182
3.183 @@ -720,23 +735,31 @@
3.184
3.185 # Built-in types initialisation.
3.186
3.187 -builtins_namespace = {}
3.188 -for key in ['ArithmeticError', 'AssertionError', 'AttributeError',
3.189 - 'BaseException', 'DeprecationWarning', 'EOFError', 'Ellipsis',
3.190 - 'EnvironmentError', 'Exception', 'False', 'FloatingPointError',
3.191 - 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError',
3.192 - 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError',
3.193 - 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError',
3.194 - 'None', 'NotImplemented', 'NotImplementedError', 'OSError',
3.195 - 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError',
3.196 - 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration',
3.197 - 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit',
3.198 - 'TabError', 'True', 'TypeError', 'UnboundLocalError',
3.199 - 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError',
3.200 - 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning',
3.201 - 'ValueError', 'Warning', 'ZeroDivisionError',
3.202 - 'bool', 'buffer', 'complex', 'dict', 'file', 'float', 'int', 'list',
3.203 - 'long', 'object', 'slice', 'str', 'tuple', 'type', 'unicode']:
3.204 - builtins_namespace[key] = Class(key, "__builtins__")
3.205 +class Builtins(Module):
3.206 +
3.207 + "The special built-in types module."
3.208 +
3.209 + def __init__(self):
3.210 + Module.__init__(self, "__builtins__")
3.211 +
3.212 + for key in ['ArithmeticError', 'AssertionError', 'AttributeError',
3.213 + 'BaseException', 'DeprecationWarning', 'EOFError', 'Ellipsis',
3.214 + 'EnvironmentError', 'Exception', 'False', 'FloatingPointError',
3.215 + 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError',
3.216 + 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError',
3.217 + 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError',
3.218 + 'None', 'NotImplemented', 'NotImplementedError', 'OSError',
3.219 + 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError',
3.220 + 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration',
3.221 + 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit',
3.222 + 'TabError', 'True', 'TypeError', 'UnboundLocalError',
3.223 + 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError',
3.224 + 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning',
3.225 + 'ValueError', 'Warning', 'ZeroDivisionError',
3.226 + 'bool', 'buffer', 'complex', 'dict', 'file', 'float', 'int', 'list',
3.227 + 'long', 'object', 'slice', 'str', 'tuple', 'type', 'unicode']:
3.228 + self[key] = Class(key, self.full_name())
3.229 +
3.230 +builtins = Builtins()
3.231
3.232 # vim: tabstop=4 expandtab shiftwidth=4