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