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