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