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