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