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