paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | Inspect source files, obtaining details of classes and attributes. |
paul@0 | 5 | |
paul@0 | 6 | Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 7 | |
paul@0 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@0 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@0 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@0 | 11 | version. |
paul@0 | 12 | |
paul@0 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@0 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@0 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@0 | 16 | details. |
paul@0 | 17 | |
paul@0 | 18 | You should have received a copy of the GNU General Public License along with |
paul@0 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@0 | 20 | """ |
paul@0 | 21 | |
paul@0 | 22 | import compiler.ast |
paul@0 | 23 | from compiler.visitor import ASTVisitor |
paul@0 | 24 | try: |
paul@0 | 25 | set |
paul@0 | 26 | except NameError: |
paul@0 | 27 | from sets import Set as set |
paul@0 | 28 | |
paul@0 | 29 | class InspectError(Exception): |
paul@0 | 30 | |
paul@0 | 31 | "An inspection error." |
paul@0 | 32 | |
paul@0 | 33 | pass |
paul@0 | 34 | |
paul@12 | 35 | # Mix-ins and abstract classes. |
paul@12 | 36 | |
paul@3 | 37 | class NamespaceDict: |
paul@3 | 38 | |
paul@3 | 39 | "A mix-in providing dictionary methods." |
paul@3 | 40 | |
paul@12 | 41 | def __init__(self): |
paul@12 | 42 | self.namespace = {} |
paul@12 | 43 | self.globals = set() |
paul@12 | 44 | |
paul@3 | 45 | def __getitem__(self, name): |
paul@3 | 46 | return self.namespace[name] |
paul@3 | 47 | |
paul@15 | 48 | def get(self, name, default=None): |
paul@15 | 49 | return self.namespace.get(name, default) |
paul@15 | 50 | |
paul@12 | 51 | def __setitem__(self, name, value): |
paul@14 | 52 | if name not in self.globals: |
paul@14 | 53 | self.namespace[name] = value |
paul@12 | 54 | |
paul@12 | 55 | def __delitem__(self, name): |
paul@12 | 56 | del self.namespace[name] |
paul@12 | 57 | |
paul@13 | 58 | def has_key(self, name): |
paul@13 | 59 | return self.namespace.has_key(name) |
paul@13 | 60 | |
paul@3 | 61 | def keys(self): |
paul@3 | 62 | return self.namespace.keys() |
paul@3 | 63 | |
paul@7 | 64 | def values(self): |
paul@7 | 65 | return self.namespace.values() |
paul@7 | 66 | |
paul@7 | 67 | def items(self): |
paul@7 | 68 | return self.namespace.items() |
paul@7 | 69 | |
paul@12 | 70 | def make_global(self, name): |
paul@12 | 71 | if not self.namespace.has_key(name): |
paul@12 | 72 | self.globals.add(name) |
paul@12 | 73 | else: |
paul@12 | 74 | raise InspectError, "Name %r is global and local in %r" % (name, self) |
paul@12 | 75 | |
paul@4 | 76 | class Naming: |
paul@4 | 77 | |
paul@4 | 78 | "A mix-in providing naming conveniences." |
paul@4 | 79 | |
paul@4 | 80 | def full_name(self): |
paul@4 | 81 | if self.name is not None: |
paul@7 | 82 | return self.parent_name + "." + self.name |
paul@4 | 83 | else: |
paul@7 | 84 | return self.parent_name |
paul@4 | 85 | |
paul@12 | 86 | # Program data structures. |
paul@12 | 87 | |
paul@10 | 88 | class Attr: |
paul@10 | 89 | |
paul@10 | 90 | "An attribute entry." |
paul@10 | 91 | |
paul@10 | 92 | def __init__(self, position, parent, value=None): |
paul@10 | 93 | self.position = position |
paul@10 | 94 | self.parent = parent |
paul@10 | 95 | self.value = value |
paul@10 | 96 | |
paul@10 | 97 | def __repr__(self): |
paul@10 | 98 | return "Attr(%d, %r, %r)" % (self.position, self.parent, self.value) |
paul@10 | 99 | |
paul@4 | 100 | class Class(NamespaceDict, Naming): |
paul@0 | 101 | |
paul@0 | 102 | "An inspected class." |
paul@0 | 103 | |
paul@7 | 104 | def __init__(self, name, parent_name): |
paul@12 | 105 | NamespaceDict.__init__(self) |
paul@0 | 106 | self.name = name |
paul@7 | 107 | self.parent_name = parent_name |
paul@12 | 108 | |
paul@0 | 109 | self.bases = [] |
paul@9 | 110 | self.instattr = set() # instance attributes |
paul@12 | 111 | |
paul@12 | 112 | self.all_instattr = None # cache for instance_attributes |
paul@13 | 113 | self.all_instattr_names = None # from all_instattr |
paul@9 | 114 | self.classattr = None # cache for class_attributes |
paul@10 | 115 | self.classattr_names = None # from classattr |
paul@12 | 116 | self.all_classattr = None # cache for all_class_attributes |
paul@12 | 117 | self.all_classattr_names = None # from all_classattr |
paul@9 | 118 | self.allattr = None # cache for all_attributes |
paul@10 | 119 | self.allattr_names = None # from allattr |
paul@9 | 120 | |
paul@9 | 121 | # Image generation details. |
paul@9 | 122 | |
paul@9 | 123 | self.location = None |
paul@0 | 124 | |
paul@12 | 125 | def __repr__(self): |
paul@12 | 126 | return "Class(%r, %r, location=%r)" % (self.name, self.parent_name, self.location) |
paul@12 | 127 | |
paul@0 | 128 | def add_base(self, base): |
paul@0 | 129 | self.bases.append(base) |
paul@0 | 130 | |
paul@12 | 131 | def add_instance_attribute(self, name): |
paul@12 | 132 | self.instattr.add(name) |
paul@9 | 133 | |
paul@9 | 134 | def class_attribute_names(self): |
paul@9 | 135 | |
paul@12 | 136 | "Return the attribute names provided by this class only." |
paul@9 | 137 | |
paul@10 | 138 | if self.classattr_names is None: |
paul@10 | 139 | self.class_attributes() |
paul@9 | 140 | return self.classattr_names |
paul@4 | 141 | |
paul@4 | 142 | def class_attributes(self): |
paul@4 | 143 | |
paul@12 | 144 | "Return class attributes provided by this class only." |
paul@4 | 145 | |
paul@9 | 146 | if self.classattr is None: |
paul@9 | 147 | self.classattr = {} |
paul@13 | 148 | self.classattr_names = self.keys() |
paul@10 | 149 | |
paul@12 | 150 | for i, name in enumerate(self.classattr_names): |
paul@12 | 151 | self.classattr[name] = Attr(i, self, self[name]) |
paul@12 | 152 | |
paul@12 | 153 | return self.classattr |
paul@12 | 154 | |
paul@12 | 155 | def all_class_attribute_names(self): |
paul@12 | 156 | |
paul@12 | 157 | "Return the attribute names provided by classes in this hierarchy." |
paul@12 | 158 | |
paul@12 | 159 | if self.all_classattr_names is None: |
paul@12 | 160 | self.all_class_attributes() |
paul@12 | 161 | return self.all_classattr_names |
paul@12 | 162 | |
paul@12 | 163 | def all_class_attributes(self): |
paul@12 | 164 | |
paul@12 | 165 | "Return all class attributes, indicating the class which provides them." |
paul@12 | 166 | |
paul@12 | 167 | if self.all_classattr is None: |
paul@12 | 168 | self.all_classattr = {} |
paul@12 | 169 | |
paul@9 | 170 | reversed_bases = self.bases[:] |
paul@9 | 171 | reversed_bases.reverse() |
paul@9 | 172 | for cls in reversed_bases: |
paul@12 | 173 | self.all_classattr.update(cls.all_class_attributes()) |
paul@10 | 174 | |
paul@10 | 175 | # Record attributes provided by this class, along with their |
paul@10 | 176 | # positions. |
paul@10 | 177 | |
paul@12 | 178 | self.all_classattr.update(self.class_attributes()) |
paul@9 | 179 | |
paul@12 | 180 | return self.all_classattr |
paul@4 | 181 | |
paul@7 | 182 | def instance_attribute_names(self): |
paul@7 | 183 | |
paul@7 | 184 | "Return the instance attribute names provided by the class." |
paul@7 | 185 | |
paul@12 | 186 | if self.all_instattr_names is None: |
paul@12 | 187 | self.instance_attributes() |
paul@12 | 188 | return self.all_instattr_names |
paul@7 | 189 | |
paul@4 | 190 | def instance_attributes(self): |
paul@4 | 191 | |
paul@9 | 192 | "Return instance-only attributes for instances of this class." |
paul@9 | 193 | |
paul@12 | 194 | if self.all_instattr is None: |
paul@12 | 195 | self.all_instattr = {} |
paul@12 | 196 | instattr = set() |
paul@12 | 197 | |
paul@12 | 198 | reversed_bases = self.bases[:] |
paul@12 | 199 | reversed_bases.reverse() |
paul@12 | 200 | for cls in reversed_bases: |
paul@12 | 201 | instattr.update(cls.instance_attributes().keys()) |
paul@12 | 202 | |
paul@12 | 203 | # Record instance attributes provided by this class and its bases, |
paul@12 | 204 | # along with their positions. |
paul@12 | 205 | |
paul@12 | 206 | instattr.update(self.instattr) |
paul@12 | 207 | |
paul@12 | 208 | for i, name in enumerate(instattr): |
paul@12 | 209 | self.all_instattr[name] = Attr(i, None) |
paul@12 | 210 | |
paul@12 | 211 | self.all_instattr_names = self.all_instattr.keys() |
paul@12 | 212 | |
paul@12 | 213 | return self.all_instattr |
paul@9 | 214 | |
paul@9 | 215 | def all_attribute_names(self): |
paul@9 | 216 | |
paul@9 | 217 | """ |
paul@9 | 218 | Return the names of all attributes provided by instances of this class. |
paul@9 | 219 | """ |
paul@9 | 220 | |
paul@9 | 221 | self.allattr_names = self.allattr_names or self.all_attributes().keys() |
paul@9 | 222 | return self.allattr_names |
paul@9 | 223 | |
paul@9 | 224 | def all_attributes(self): |
paul@9 | 225 | |
paul@4 | 226 | """ |
paul@4 | 227 | Return all attributes for an instance, indicating either the class which |
paul@4 | 228 | provides them or that the instance itself provides them. |
paul@4 | 229 | """ |
paul@4 | 230 | |
paul@9 | 231 | if self.allattr is None: |
paul@9 | 232 | self.allattr = {} |
paul@12 | 233 | self.allattr.update(self.all_class_attributes()) |
paul@9 | 234 | for i, name in enumerate(self.instance_attribute_names()): |
paul@9 | 235 | if self.allattr.has_key(name): |
paul@9 | 236 | print "Instance attribute %r in %r overrides class attribute." % (name, self) |
paul@10 | 237 | self.allattr[name] = Attr(i, None) |
paul@9 | 238 | return self.allattr |
paul@4 | 239 | |
paul@12 | 240 | class Function(NamespaceDict, Naming): |
paul@0 | 241 | |
paul@0 | 242 | "An inspected function." |
paul@0 | 243 | |
paul@7 | 244 | def __init__(self, name, parent_name, argnames, has_star, has_dstar): |
paul@12 | 245 | NamespaceDict.__init__(self) |
paul@3 | 246 | self.name = name |
paul@7 | 247 | self.parent_name = parent_name |
paul@3 | 248 | self.argnames = argnames |
paul@3 | 249 | self.has_star = has_star |
paul@3 | 250 | self.has_dstar = has_dstar |
paul@3 | 251 | |
paul@12 | 252 | self.localnames = None # cache for locals |
paul@12 | 253 | self.alllocalnames = None # cache for all_locals |
paul@12 | 254 | |
paul@9 | 255 | # Image generation details. |
paul@9 | 256 | |
paul@9 | 257 | self.location = None |
paul@9 | 258 | |
paul@4 | 259 | def __repr__(self): |
paul@9 | 260 | return "Function(%r, %r, %r, %r, %r, location=%r)" % ( |
paul@9 | 261 | self.name, self.parent_name, self.argnames, self.has_star, self.has_dstar, self.location |
paul@4 | 262 | ) |
paul@4 | 263 | |
paul@12 | 264 | def make_global(self, name): |
paul@13 | 265 | if name not in self.argnames and not self.has_key(name): |
paul@12 | 266 | self.globals.add(name) |
paul@12 | 267 | else: |
paul@12 | 268 | raise InspectError, "Name %r is global and local in %r" % (name, self) |
paul@12 | 269 | |
paul@7 | 270 | def parameters(self): |
paul@7 | 271 | |
paul@7 | 272 | """ |
paul@7 | 273 | Return a dictionary mapping parameter names to their position in the |
paul@7 | 274 | parameter list. |
paul@7 | 275 | """ |
paul@7 | 276 | |
paul@7 | 277 | parameters = {} |
paul@7 | 278 | for i, name in enumerate(self.argnames): |
paul@7 | 279 | parameters[name] = i |
paul@7 | 280 | return parameters |
paul@7 | 281 | |
paul@12 | 282 | def locals(self): |
paul@12 | 283 | |
paul@12 | 284 | "Return a dictionary mapping names to local details." |
paul@12 | 285 | |
paul@12 | 286 | if self.localnames is None: |
paul@12 | 287 | self.localnames = {} |
paul@12 | 288 | start = len(self.argnames) |
paul@13 | 289 | for i, name in enumerate(self.keys()): |
paul@12 | 290 | self.localnames[name] = Attr(start + i, None) |
paul@12 | 291 | return self.localnames |
paul@12 | 292 | |
paul@12 | 293 | def all_locals(self): |
paul@12 | 294 | |
paul@12 | 295 | "Return a dictionary mapping names to local and parameter details." |
paul@12 | 296 | |
paul@12 | 297 | if self.alllocalnames is None: |
paul@12 | 298 | self.alllocalnames = {} |
paul@12 | 299 | self.alllocalnames.update(self.locals()) |
paul@12 | 300 | for i, name in enumerate(self.argnames): |
paul@12 | 301 | self.alllocalnames[name] = Attr(i, None) |
paul@12 | 302 | return self.alllocalnames |
paul@12 | 303 | |
paul@4 | 304 | class UnresolvedName(NamespaceDict, Naming): |
paul@3 | 305 | |
paul@3 | 306 | "A module, class or function which was mentioned but could not be imported." |
paul@3 | 307 | |
paul@7 | 308 | def __init__(self, name, parent_name): |
paul@12 | 309 | NamespaceDict.__init__(self) |
paul@0 | 310 | self.name = name |
paul@7 | 311 | self.parent_name = parent_name |
paul@0 | 312 | |
paul@12 | 313 | def all_class_attributes(self): |
paul@7 | 314 | return {} |
paul@7 | 315 | |
paul@13 | 316 | def instance_attributes(self): |
paul@13 | 317 | return {} |
paul@13 | 318 | |
paul@4 | 319 | def __repr__(self): |
paul@7 | 320 | return "UnresolvedName(%r, %r)" % (self.name, self.parent_name) |
paul@4 | 321 | |
paul@12 | 322 | # Program visitors. |
paul@12 | 323 | |
paul@15 | 324 | class Module(NamespaceDict): |
paul@0 | 325 | |
paul@15 | 326 | def __init__(self, name): |
paul@12 | 327 | NamespaceDict.__init__(self) |
paul@1 | 328 | self.name = name |
paul@0 | 329 | |
paul@10 | 330 | # Module attributes. |
paul@10 | 331 | |
paul@10 | 332 | self.modattr = None # cache for module_attributes |
paul@10 | 333 | self.modattr_names = None # from modattr |
paul@10 | 334 | |
paul@7 | 335 | # Complete lists of classes and functions. |
paul@7 | 336 | |
paul@7 | 337 | self.all_objects = [] |
paul@0 | 338 | |
paul@15 | 339 | # Image generation details. |
paul@15 | 340 | |
paul@15 | 341 | self.location = None |
paul@15 | 342 | |
paul@15 | 343 | def full_name(self): |
paul@15 | 344 | return self.name |
paul@15 | 345 | |
paul@15 | 346 | def __repr__(self): |
paul@15 | 347 | return "Module(%r, location=%r)" % (self.name, self.location) |
paul@15 | 348 | |
paul@15 | 349 | # Attribute methods. |
paul@15 | 350 | |
paul@15 | 351 | def module_attribute_names(self): |
paul@15 | 352 | |
paul@15 | 353 | "Return the module attribute names provided by the module." |
paul@15 | 354 | |
paul@15 | 355 | if self.modattr_names is None: |
paul@15 | 356 | self.module_attributes() |
paul@15 | 357 | return self.modattr_names |
paul@15 | 358 | |
paul@15 | 359 | def module_attributes(self): |
paul@15 | 360 | |
paul@15 | 361 | "Return a dictionary mapping names to module attributes." |
paul@15 | 362 | |
paul@15 | 363 | if self.modattr is None: |
paul@15 | 364 | self.modattr = {} |
paul@15 | 365 | self.modattr_names = self.keys() |
paul@15 | 366 | for i, name in enumerate(self.modattr_names): |
paul@15 | 367 | self.modattr[name] = Attr(i, self, self[name]) |
paul@15 | 368 | |
paul@15 | 369 | return self.modattr |
paul@15 | 370 | |
paul@15 | 371 | class InspectedModule(ASTVisitor, Module): |
paul@15 | 372 | |
paul@15 | 373 | "An inspected module." |
paul@15 | 374 | |
paul@15 | 375 | def __init__(self, name, importer=None): |
paul@15 | 376 | ASTVisitor.__init__(self) |
paul@15 | 377 | Module.__init__(self, name) |
paul@15 | 378 | self.visitor = self |
paul@15 | 379 | |
paul@15 | 380 | self.importer = importer |
paul@15 | 381 | self.loaded = 0 |
paul@15 | 382 | |
paul@1 | 383 | # Current expression state. |
paul@1 | 384 | |
paul@1 | 385 | self.expr = None |
paul@1 | 386 | |
paul@1 | 387 | # Namespace state. |
paul@1 | 388 | |
paul@0 | 389 | self.in_init = 0 |
paul@12 | 390 | self.namespaces = [] |
paul@0 | 391 | self.module = None |
paul@0 | 392 | |
paul@0 | 393 | def parse(self, filename): |
paul@9 | 394 | |
paul@9 | 395 | "Parse the file having the given 'filename'." |
paul@9 | 396 | |
paul@0 | 397 | module = compiler.parseFile(filename) |
paul@0 | 398 | self.process(module) |
paul@0 | 399 | |
paul@0 | 400 | def process(self, module): |
paul@9 | 401 | |
paul@9 | 402 | "Process the given 'module'." |
paul@9 | 403 | |
paul@0 | 404 | self.module = module |
paul@1 | 405 | processed = self.dispatch(module) |
paul@13 | 406 | if self.has_key("__all__"): |
paul@12 | 407 | all = self["__all__"] |
paul@1 | 408 | if isinstance(all, compiler.ast.List): |
paul@1 | 409 | for n in all.nodes: |
paul@12 | 410 | self[n.value] = self.importer.add_module(self.name + "." + n.value) |
paul@1 | 411 | return processed |
paul@0 | 412 | |
paul@3 | 413 | def vacuum(self): |
paul@9 | 414 | |
paul@9 | 415 | "Vacuum the module namespace, removing unloaded module references." |
paul@9 | 416 | |
paul@13 | 417 | for name, value in self.items(): |
paul@3 | 418 | if isinstance(value, Module) and not value.loaded: |
paul@12 | 419 | del self[name] |
paul@0 | 420 | |
paul@3 | 421 | # Namespace methods. |
paul@0 | 422 | |
paul@0 | 423 | def store(self, name, obj): |
paul@12 | 424 | |
paul@12 | 425 | "Record attribute or local 'name', storing 'obj'." |
paul@12 | 426 | |
paul@12 | 427 | if not self.namespaces: |
paul@12 | 428 | self[name] = obj |
paul@12 | 429 | else: |
paul@12 | 430 | self.namespaces[-1][name] = obj |
paul@12 | 431 | |
paul@12 | 432 | # Record all non-local objects. |
paul@12 | 433 | |
paul@12 | 434 | if not (self.namespaces and isinstance(self.namespaces[-1], Function)): |
paul@7 | 435 | self.all_objects.append(obj) |
paul@0 | 436 | |
paul@0 | 437 | def store_attr(self, name): |
paul@12 | 438 | |
paul@12 | 439 | "Record instance attribute 'name' in the current class." |
paul@12 | 440 | |
paul@0 | 441 | if self.in_init: |
paul@15 | 442 | |
paul@12 | 443 | # Current namespace is the function. |
paul@12 | 444 | # Previous namespace is the class. |
paul@15 | 445 | |
paul@12 | 446 | self.namespaces[-2].add_instance_attribute(name) |
paul@0 | 447 | |
paul@7 | 448 | def get_parent(self): |
paul@12 | 449 | return (self.namespaces[-1:] or [self])[0] |
paul@7 | 450 | |
paul@3 | 451 | # Visitor methods. |
paul@3 | 452 | |
paul@3 | 453 | def default(self, node, *args): |
paul@3 | 454 | raise InspectError, node.__class__ |
paul@3 | 455 | |
paul@3 | 456 | def dispatch(self, node, *args): |
paul@3 | 457 | return ASTVisitor.dispatch(self, node, *args) |
paul@3 | 458 | |
paul@0 | 459 | def NOP(self, node): |
paul@0 | 460 | return node |
paul@0 | 461 | |
paul@1 | 462 | visitAdd = NOP |
paul@1 | 463 | |
paul@0 | 464 | visitAnd = NOP |
paul@0 | 465 | |
paul@4 | 466 | visitAssert = NOP |
paul@4 | 467 | |
paul@0 | 468 | def visitAssign(self, node): |
paul@0 | 469 | self.expr = self.dispatch(node.expr) |
paul@0 | 470 | for n in node.nodes: |
paul@0 | 471 | self.dispatch(n) |
paul@0 | 472 | return None |
paul@0 | 473 | |
paul@0 | 474 | def visitAssAttr(self, node): |
paul@0 | 475 | expr = self.dispatch(node.expr) |
paul@0 | 476 | if expr is not None and isinstance(expr, Self): |
paul@0 | 477 | self.store_attr(node.attrname) |
paul@0 | 478 | return None |
paul@0 | 479 | |
paul@0 | 480 | def visitAssList(self, node): |
paul@0 | 481 | for n in node.nodes: |
paul@0 | 482 | self.dispatch(n) |
paul@0 | 483 | return None |
paul@0 | 484 | |
paul@0 | 485 | def visitAssName(self, node): |
paul@0 | 486 | self.store(node.name, self.expr) |
paul@0 | 487 | return None |
paul@0 | 488 | |
paul@0 | 489 | visitAssTuple = visitAssList |
paul@0 | 490 | |
paul@2 | 491 | visitAugAssign = NOP |
paul@2 | 492 | |
paul@4 | 493 | visitBackquote = NOP |
paul@4 | 494 | |
paul@2 | 495 | visitBitand = NOP |
paul@2 | 496 | |
paul@2 | 497 | visitBitor = NOP |
paul@2 | 498 | |
paul@4 | 499 | visitBitxor = NOP |
paul@4 | 500 | |
paul@4 | 501 | visitBreak = NOP |
paul@4 | 502 | |
paul@0 | 503 | visitCallFunc = NOP |
paul@0 | 504 | |
paul@0 | 505 | def visitClass(self, node): |
paul@12 | 506 | if self.namespaces: |
paul@4 | 507 | print "Class %r in %r is not global: ignored." % (node.name, self) |
paul@0 | 508 | else: |
paul@7 | 509 | cls = Class(node.name, self.get_parent().full_name()) |
paul@0 | 510 | for base in node.bases: |
paul@0 | 511 | base_ref = self.dispatch(base) |
paul@0 | 512 | if base_ref is None: |
paul@4 | 513 | raise InspectError, "Base class %r for class %r in %r is not found: it may be hidden in some way." % (base, self, cls) |
paul@0 | 514 | cls.add_base(base_ref) |
paul@0 | 515 | |
paul@0 | 516 | # Make an entry for the class. |
paul@0 | 517 | |
paul@0 | 518 | self.store(node.name, cls) |
paul@0 | 519 | |
paul@12 | 520 | self.namespaces.append(cls) |
paul@0 | 521 | self.dispatch(node.code) |
paul@12 | 522 | self.namespaces.pop() |
paul@0 | 523 | |
paul@0 | 524 | return node |
paul@0 | 525 | |
paul@4 | 526 | visitCompare = NOP |
paul@4 | 527 | |
paul@0 | 528 | visitConst = NOP |
paul@0 | 529 | |
paul@4 | 530 | visitContinue = NOP |
paul@4 | 531 | |
paul@4 | 532 | visitDecorators = NOP |
paul@4 | 533 | |
paul@0 | 534 | visitDict = NOP |
paul@0 | 535 | |
paul@0 | 536 | visitDiscard = NOP |
paul@0 | 537 | |
paul@4 | 538 | visitDiv = NOP |
paul@4 | 539 | |
paul@4 | 540 | visitEllipsis = NOP |
paul@4 | 541 | |
paul@4 | 542 | visitExec = NOP |
paul@4 | 543 | |
paul@4 | 544 | visitExpression = NOP |
paul@4 | 545 | |
paul@4 | 546 | visitFloorDiv = NOP |
paul@4 | 547 | |
paul@0 | 548 | visitFor = NOP |
paul@0 | 549 | |
paul@0 | 550 | def visitFrom(self, node): |
paul@0 | 551 | if self.importer is None: |
paul@0 | 552 | raise InspectError, "Please use the micropython.Importer class for code which uses the 'from' statement." |
paul@0 | 553 | |
paul@0 | 554 | module = self.importer.load(node.modname, 1) |
paul@0 | 555 | |
paul@0 | 556 | if module is None: |
paul@0 | 557 | print "Warning:", node.modname, "not imported." |
paul@0 | 558 | |
paul@0 | 559 | for name, alias in node.names: |
paul@0 | 560 | if name != "*": |
paul@4 | 561 | if module is not None and module.namespace.has_key(name): |
paul@12 | 562 | self[alias or name] = attr = module[name] |
paul@2 | 563 | if isinstance(attr, Module) and not attr.loaded: |
paul@2 | 564 | self.importer.load(attr.name) |
paul@3 | 565 | else: |
paul@12 | 566 | self[alias or name] = UnresolvedName(name, node.modname) |
paul@3 | 567 | else: |
paul@3 | 568 | if module is not None: |
paul@3 | 569 | for n in module.namespace.keys(): |
paul@12 | 570 | self[n] = attr = module[n] |
paul@3 | 571 | if isinstance(attr, Module) and not attr.loaded: |
paul@3 | 572 | self.importer.load(attr.name) |
paul@0 | 573 | |
paul@0 | 574 | return None |
paul@0 | 575 | |
paul@0 | 576 | def visitFunction(self, node): |
paul@3 | 577 | function = Function( |
paul@3 | 578 | node.name, |
paul@7 | 579 | self.get_parent().full_name(), |
paul@3 | 580 | node.argnames, |
paul@3 | 581 | has_star=(node.flags & 4 != 0), |
paul@3 | 582 | has_dstar=(node.flags & 8 != 0) |
paul@3 | 583 | ) |
paul@0 | 584 | |
paul@12 | 585 | self.namespaces.append(function) |
paul@15 | 586 | |
paul@12 | 587 | # Current namespace is the function. |
paul@12 | 588 | # Previous namespace is the class. |
paul@15 | 589 | |
paul@12 | 590 | if node.name == "__init__" and isinstance(self.namespaces[-2], Class): |
paul@0 | 591 | self.in_init = 1 |
paul@15 | 592 | |
paul@12 | 593 | self.dispatch(node.code) |
paul@12 | 594 | self.in_init = 0 |
paul@12 | 595 | self.namespaces.pop() |
paul@3 | 596 | |
paul@3 | 597 | self.store(node.name, function) |
paul@0 | 598 | return None |
paul@0 | 599 | |
paul@4 | 600 | visitGenExpr = NOP |
paul@4 | 601 | |
paul@4 | 602 | visitGenExprFor = NOP |
paul@4 | 603 | |
paul@4 | 604 | visitGenExprIf = NOP |
paul@4 | 605 | |
paul@4 | 606 | visitGenExprInner = NOP |
paul@4 | 607 | |
paul@0 | 608 | def visitGetattr(self, node): |
paul@0 | 609 | expr = self.dispatch(node.expr) |
paul@4 | 610 | if expr is not None: |
paul@4 | 611 | if isinstance(expr, Module): |
paul@4 | 612 | return expr.namespace.get(node.attrname) |
paul@4 | 613 | elif isinstance(expr, UnresolvedName): |
paul@4 | 614 | return UnresolvedName(node.attrname, expr.full_name()) |
paul@15 | 615 | return builtins.get(node.attrname) |
paul@4 | 616 | |
paul@12 | 617 | def visitGlobal(self, node): |
paul@12 | 618 | if self.namespaces: |
paul@12 | 619 | for name in node.names: |
paul@12 | 620 | self.namespaces[-1].make_global(name) |
paul@15 | 621 | if not self.has_key(name): |
paul@15 | 622 | self[name] = None |
paul@0 | 623 | |
paul@0 | 624 | def visitIf(self, node): |
paul@0 | 625 | for test, body in node.tests: |
paul@0 | 626 | self.dispatch(body) |
paul@0 | 627 | if node.else_ is not None: |
paul@0 | 628 | self.dispatch(node.else_) |
paul@0 | 629 | return None |
paul@0 | 630 | |
paul@12 | 631 | visitIfExp = NOP |
paul@12 | 632 | |
paul@0 | 633 | def visitImport(self, node): |
paul@0 | 634 | if self.importer is None: |
paul@0 | 635 | raise InspectError, "Please use the micropython.Importer class for code which uses the 'import' statement." |
paul@0 | 636 | |
paul@0 | 637 | for name, alias in node.names: |
paul@2 | 638 | if alias is not None: |
paul@12 | 639 | self[alias] = self.importer.load(name, 1) or UnresolvedName(None, name) |
paul@2 | 640 | else: |
paul@12 | 641 | self[name.split(".")[0]] = self.importer.load(name) or UnresolvedName(None, name.split(".")[0]) |
paul@0 | 642 | |
paul@0 | 643 | return None |
paul@0 | 644 | |
paul@4 | 645 | visitInvert = NOP |
paul@4 | 646 | |
paul@4 | 647 | visitKeyword = NOP |
paul@4 | 648 | |
paul@4 | 649 | visitLambda = NOP |
paul@4 | 650 | |
paul@1 | 651 | visitLeftShift = NOP |
paul@1 | 652 | |
paul@0 | 653 | visitList = NOP |
paul@0 | 654 | |
paul@4 | 655 | visitListComp = NOP |
paul@4 | 656 | |
paul@4 | 657 | visitListCompFor = NOP |
paul@4 | 658 | |
paul@4 | 659 | visitListCompIf = NOP |
paul@4 | 660 | |
paul@4 | 661 | visitMod = NOP |
paul@4 | 662 | |
paul@0 | 663 | def visitModule(self, node): |
paul@0 | 664 | return self.dispatch(node.node) |
paul@0 | 665 | |
paul@2 | 666 | visitMul = NOP |
paul@2 | 667 | |
paul@0 | 668 | def visitName(self, node): |
paul@0 | 669 | name = node.name |
paul@0 | 670 | if name == "self": |
paul@0 | 671 | return Self() |
paul@13 | 672 | elif self.has_key(name): |
paul@12 | 673 | return self[name] |
paul@15 | 674 | elif builtins.has_key(name): |
paul@15 | 675 | return builtins[name] |
paul@0 | 676 | else: |
paul@0 | 677 | return None |
paul@0 | 678 | |
paul@0 | 679 | visitNot = NOP |
paul@0 | 680 | |
paul@0 | 681 | visitOr = NOP |
paul@0 | 682 | |
paul@0 | 683 | visitPass = NOP |
paul@0 | 684 | |
paul@4 | 685 | visitPower = NOP |
paul@4 | 686 | |
paul@4 | 687 | visitPrint = NOP |
paul@4 | 688 | |
paul@4 | 689 | visitPrintnl = NOP |
paul@4 | 690 | |
paul@0 | 691 | visitRaise = NOP |
paul@0 | 692 | |
paul@4 | 693 | visitReturn = NOP |
paul@4 | 694 | |
paul@1 | 695 | visitRightShift = NOP |
paul@1 | 696 | |
paul@4 | 697 | visitSlice = NOP |
paul@4 | 698 | |
paul@0 | 699 | def visitStmt(self, node): |
paul@0 | 700 | for n in node.nodes: |
paul@0 | 701 | self.dispatch(n) |
paul@0 | 702 | return None |
paul@0 | 703 | |
paul@4 | 704 | visitSub = NOP |
paul@4 | 705 | |
paul@0 | 706 | visitSubscript = NOP |
paul@0 | 707 | |
paul@0 | 708 | def visitTryExcept(self, node): |
paul@0 | 709 | self.dispatch(node.body) |
paul@0 | 710 | for name, var, n in node.handlers: |
paul@0 | 711 | self.dispatch(n) |
paul@0 | 712 | if node.else_ is not None: |
paul@0 | 713 | self.dispatch(node.else_) |
paul@0 | 714 | return None |
paul@0 | 715 | |
paul@4 | 716 | visitTryFinally = NOP |
paul@4 | 717 | |
paul@1 | 718 | visitTuple = NOP |
paul@1 | 719 | |
paul@4 | 720 | visitUnaryAdd = NOP |
paul@4 | 721 | |
paul@4 | 722 | visitUnarySub = NOP |
paul@4 | 723 | |
paul@4 | 724 | visitWhile = NOP |
paul@4 | 725 | |
paul@4 | 726 | visitWith = NOP |
paul@4 | 727 | |
paul@4 | 728 | visitYield = NOP |
paul@4 | 729 | |
paul@0 | 730 | class Self: |
paul@0 | 731 | |
paul@0 | 732 | "A reference to an object within a method." |
paul@0 | 733 | |
paul@0 | 734 | pass |
paul@0 | 735 | |
paul@4 | 736 | # Built-in types initialisation. |
paul@4 | 737 | |
paul@15 | 738 | class Builtins(Module): |
paul@15 | 739 | |
paul@15 | 740 | "The special built-in types module." |
paul@15 | 741 | |
paul@15 | 742 | def __init__(self): |
paul@15 | 743 | Module.__init__(self, "__builtins__") |
paul@15 | 744 | |
paul@15 | 745 | for key in ['ArithmeticError', 'AssertionError', 'AttributeError', |
paul@15 | 746 | 'BaseException', 'DeprecationWarning', 'EOFError', 'Ellipsis', |
paul@15 | 747 | 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', |
paul@15 | 748 | 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', |
paul@15 | 749 | 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', |
paul@15 | 750 | 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', |
paul@15 | 751 | 'None', 'NotImplemented', 'NotImplementedError', 'OSError', |
paul@15 | 752 | 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', |
paul@15 | 753 | 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', |
paul@15 | 754 | 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', |
paul@15 | 755 | 'TabError', 'True', 'TypeError', 'UnboundLocalError', |
paul@15 | 756 | 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', |
paul@15 | 757 | 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', |
paul@15 | 758 | 'ValueError', 'Warning', 'ZeroDivisionError', |
paul@15 | 759 | 'bool', 'buffer', 'complex', 'dict', 'file', 'float', 'int', 'list', |
paul@15 | 760 | 'long', 'object', 'slice', 'str', 'tuple', 'type', 'unicode']: |
paul@15 | 761 | self[key] = Class(key, self.full_name()) |
paul@15 | 762 | |
paul@15 | 763 | builtins = Builtins() |
paul@0 | 764 | |
paul@0 | 765 | # vim: tabstop=4 expandtab shiftwidth=4 |