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@39 | 48 | from micropython.common import * |
paul@0 | 49 | import compiler.ast |
paul@0 | 50 | from compiler.visitor import ASTVisitor |
paul@0 | 51 | try: |
paul@0 | 52 | set |
paul@0 | 53 | except NameError: |
paul@0 | 54 | from sets import Set as set |
paul@0 | 55 | |
paul@39 | 56 | class InspectError(ProcessingError): pass |
paul@0 | 57 | |
paul@31 | 58 | class AtLeast: |
paul@31 | 59 | |
paul@31 | 60 | "A special representation for numbers of a given value or greater." |
paul@31 | 61 | |
paul@31 | 62 | def __init__(self, count): |
paul@31 | 63 | self.count = count |
paul@31 | 64 | |
paul@31 | 65 | def __eq__(self, other): |
paul@31 | 66 | return 0 |
paul@31 | 67 | |
paul@31 | 68 | __lt__ = __le__ = __eq__ |
paul@31 | 69 | |
paul@31 | 70 | def __ne__(self, other): |
paul@31 | 71 | return 1 |
paul@31 | 72 | |
paul@31 | 73 | def __gt__(self, other): |
paul@31 | 74 | if isinstance(other, AtLeast): |
paul@31 | 75 | return 0 |
paul@31 | 76 | else: |
paul@31 | 77 | return self.count > other |
paul@31 | 78 | |
paul@31 | 79 | def __ge__(self, other): |
paul@31 | 80 | if isinstance(other, AtLeast): |
paul@31 | 81 | return 0 |
paul@31 | 82 | else: |
paul@31 | 83 | return self.count >= other |
paul@31 | 84 | |
paul@31 | 85 | def __iadd__(self, other): |
paul@31 | 86 | if isinstance(other, AtLeast): |
paul@31 | 87 | self.count += other.count |
paul@31 | 88 | else: |
paul@31 | 89 | self.count += other |
paul@31 | 90 | return self |
paul@31 | 91 | |
paul@31 | 92 | def __radd__(self, other): |
paul@31 | 93 | if isinstance(other, AtLeast): |
paul@31 | 94 | return AtLeast(self.count + other.count) |
paul@31 | 95 | else: |
paul@31 | 96 | return AtLeast(self.count + other) |
paul@31 | 97 | |
paul@31 | 98 | def __repr__(self): |
paul@31 | 99 | return "AtLeast(%r)" % self.count |
paul@31 | 100 | |
paul@12 | 101 | # Mix-ins and abstract classes. |
paul@12 | 102 | |
paul@3 | 103 | class NamespaceDict: |
paul@3 | 104 | |
paul@3 | 105 | "A mix-in providing dictionary methods." |
paul@3 | 106 | |
paul@24 | 107 | def __init__(self, global_namespace=None): |
paul@12 | 108 | self.namespace = {} |
paul@12 | 109 | self.globals = set() |
paul@24 | 110 | self.global_namespace = global_namespace |
paul@23 | 111 | |
paul@3 | 112 | def __getitem__(self, name): |
paul@3 | 113 | return self.namespace[name] |
paul@3 | 114 | |
paul@15 | 115 | def get(self, name, default=None): |
paul@15 | 116 | return self.namespace.get(name, default) |
paul@15 | 117 | |
paul@12 | 118 | def __setitem__(self, name, value): |
paul@37 | 119 | self.set(name, value) |
paul@31 | 120 | |
paul@34 | 121 | def set(self, name, value, single_assignment=1): |
paul@47 | 122 | |
paul@47 | 123 | """ |
paul@47 | 124 | A more powerful set operation, making 'name' refer to 'value' whilst |
paul@47 | 125 | indicating whether a 'single_assignment' (true by default) occurs in |
paul@47 | 126 | this operation (or whether the operation covers potentially many |
paul@47 | 127 | assignments in the lifetime of a program). |
paul@47 | 128 | """ |
paul@47 | 129 | |
paul@37 | 130 | if name in self.globals: |
paul@37 | 131 | self.global_namespace.set(name, value, 0) |
paul@37 | 132 | else: |
paul@37 | 133 | attr = self._set(name, value) |
paul@12 | 134 | |
paul@37 | 135 | # NOTE: Insist on assignments with known values. |
paul@23 | 136 | |
paul@37 | 137 | if value is not None: |
paul@37 | 138 | attr.update(value, single_assignment) |
paul@32 | 139 | |
paul@32 | 140 | def set_module(self, name, value): |
paul@47 | 141 | |
paul@47 | 142 | """ |
paul@47 | 143 | A specialised set operation, making 'name' refer to 'value' in the |
paul@47 | 144 | context of making a module reference available in association with |
paul@47 | 145 | 'name' as part of the import of that module or a submodule of that |
paul@47 | 146 | module. |
paul@47 | 147 | """ |
paul@47 | 148 | |
paul@36 | 149 | attr = self._set(name, value) |
paul@36 | 150 | if attr.assignments is None: |
paul@36 | 151 | attr.assignments = 1 |
paul@36 | 152 | attr.assignment_values.add(value) |
paul@36 | 153 | |
paul@36 | 154 | def _set(self, name, value): |
paul@47 | 155 | |
paul@47 | 156 | "The underlying set operation associating 'name' with 'value'." |
paul@47 | 157 | |
paul@36 | 158 | if not self.namespace.has_key(name): |
paul@49 | 159 | self.namespace[name] = Attr(None, self, name, value) |
paul@36 | 160 | return self.namespace[name] |
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@43 | 181 | raise InspectError(self.full_name(), self.node, "Name %r is both global and local in %r" % (name, self.full_name())) |
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@49 | 189 | def attributes_as_list(self): |
paul@49 | 190 | self.finalise_attributes() |
paul@49 | 191 | l = [None] * len(self.keys()) |
paul@49 | 192 | for attr in self.values(): |
paul@37 | 193 | l[attr.position] = attr |
paul@37 | 194 | return l |
paul@37 | 195 | |
paul@49 | 196 | def finalise_attributes(self): |
paul@49 | 197 | |
paul@49 | 198 | "Make sure all attributes are fully defined." |
paul@49 | 199 | |
paul@49 | 200 | # The default action is to assign attribute positions sequentially. |
paul@49 | 201 | |
paul@49 | 202 | for i, attr in enumerate(self.values()): |
paul@49 | 203 | attr.position = i |
paul@49 | 204 | |
paul@4 | 205 | class Naming: |
paul@4 | 206 | |
paul@4 | 207 | "A mix-in providing naming conveniences." |
paul@4 | 208 | |
paul@4 | 209 | def full_name(self): |
paul@4 | 210 | if self.name is not None: |
paul@7 | 211 | return self.parent_name + "." + self.name |
paul@4 | 212 | else: |
paul@7 | 213 | return self.parent_name |
paul@4 | 214 | |
paul@12 | 215 | # Program data structures. |
paul@12 | 216 | |
paul@10 | 217 | class Attr: |
paul@10 | 218 | |
paul@10 | 219 | "An attribute entry." |
paul@10 | 220 | |
paul@36 | 221 | def __init__(self, position, parent, name, value=None, assignments=None): |
paul@10 | 222 | self.position = position |
paul@10 | 223 | self.parent = parent |
paul@36 | 224 | self.name = name |
paul@10 | 225 | self.value = value |
paul@10 | 226 | |
paul@36 | 227 | # Number of assignments per name. |
paul@36 | 228 | |
paul@36 | 229 | self.assignments = assignments |
paul@36 | 230 | self.assignment_values = set() |
paul@36 | 231 | |
paul@36 | 232 | def update(self, value, single_assignment): |
paul@47 | 233 | |
paul@47 | 234 | """ |
paul@47 | 235 | Update the attribute, adding the 'value' provided to the known values |
paul@47 | 236 | associated with the attribute, changing the number of assignments |
paul@47 | 237 | according to the 'single_assignment' status of the operation, where |
paul@47 | 238 | a true value indicates that only one assignment is associated with the |
paul@47 | 239 | update, and a false value indicates that potentially many assignments |
paul@47 | 240 | may be involved. |
paul@47 | 241 | """ |
paul@47 | 242 | |
paul@36 | 243 | if self.assignments is None: |
paul@36 | 244 | if single_assignment: |
paul@36 | 245 | self.assignments = 1 |
paul@36 | 246 | else: |
paul@36 | 247 | self.assignments = AtLeast(1) |
paul@36 | 248 | else: |
paul@36 | 249 | if single_assignment: |
paul@36 | 250 | self.assignments += 1 |
paul@36 | 251 | else: |
paul@36 | 252 | self.assignments += AtLeast(1) |
paul@36 | 253 | self.assignment_values.add(value) |
paul@36 | 254 | |
paul@10 | 255 | def __repr__(self): |
paul@36 | 256 | return "Attr(%r, %r, %r, %r, %r)" % (self.position, self.parent, self.name, self.value, self.assignments) |
paul@10 | 257 | |
paul@21 | 258 | class Const: |
paul@21 | 259 | |
paul@21 | 260 | "A constant object." |
paul@21 | 261 | |
paul@21 | 262 | def __init__(self, value): |
paul@21 | 263 | self.value = value |
paul@21 | 264 | |
paul@21 | 265 | # Image generation details. |
paul@21 | 266 | |
paul@21 | 267 | self.location = None |
paul@21 | 268 | |
paul@21 | 269 | def __repr__(self): |
paul@37 | 270 | if self.location is not None: |
paul@37 | 271 | return "Const(%r, location=%r)" % (self.value, self.location) |
paul@37 | 272 | else: |
paul@37 | 273 | return "Const(%r)" % self.value |
paul@21 | 274 | |
paul@21 | 275 | def __eq__(self, other): |
paul@21 | 276 | return self.value == other.value |
paul@21 | 277 | |
paul@21 | 278 | def __hash__(self): |
paul@21 | 279 | return hash(self.value) |
paul@21 | 280 | |
paul@4 | 281 | class Class(NamespaceDict, Naming): |
paul@0 | 282 | |
paul@0 | 283 | "An inspected class." |
paul@0 | 284 | |
paul@24 | 285 | def __init__(self, name, parent_name, global_namespace=None, node=None): |
paul@24 | 286 | NamespaceDict.__init__(self, global_namespace) |
paul@0 | 287 | self.name = name |
paul@7 | 288 | self.parent_name = parent_name |
paul@17 | 289 | self.node = node |
paul@12 | 290 | |
paul@47 | 291 | # Superclasses and attributes. |
paul@47 | 292 | |
paul@0 | 293 | self.bases = [] |
paul@46 | 294 | self.instattr = set() # instance attributes |
paul@49 | 295 | self.relocated = set() # attributes which do not have the same |
paul@49 | 296 | # position as those of the same name in |
paul@49 | 297 | # some superclasses |
paul@12 | 298 | |
paul@47 | 299 | # Caches. |
paul@47 | 300 | |
paul@46 | 301 | self.all_instattr = None # cache for instance_attributes |
paul@46 | 302 | self.all_instattr_names = None # from all_instattr |
paul@46 | 303 | self.all_classattr = None # cache for all_class_attributes |
paul@12 | 304 | self.all_classattr_names = None # from all_classattr |
paul@46 | 305 | self.allattr = None # cache for all_attributes |
paul@46 | 306 | self.allattr_names = None # from allattr |
paul@9 | 307 | |
paul@9 | 308 | # Image generation details. |
paul@9 | 309 | |
paul@9 | 310 | self.location = None |
paul@19 | 311 | self.code_location = None |
paul@0 | 312 | |
paul@12 | 313 | def __repr__(self): |
paul@37 | 314 | if self.location is not None: |
paul@37 | 315 | return "Class(%r, %r, location=%r)" % (self.name, self.parent_name, self.location) |
paul@37 | 316 | else: |
paul@37 | 317 | return "Class(%r, %r)" % (self.name, self.parent_name) |
paul@12 | 318 | |
paul@49 | 319 | def finalise_attributes(self): |
paul@49 | 320 | |
paul@49 | 321 | "Make sure that all attributes are fully defined." |
paul@49 | 322 | |
paul@49 | 323 | self.finalise_class_attributes() |
paul@49 | 324 | self.finalise_instance_attributes() |
paul@49 | 325 | |
paul@49 | 326 | # Class-specific methods. |
paul@49 | 327 | |
paul@0 | 328 | def add_base(self, base): |
paul@0 | 329 | self.bases.append(base) |
paul@0 | 330 | |
paul@12 | 331 | def add_instance_attribute(self, name): |
paul@12 | 332 | self.instattr.add(name) |
paul@9 | 333 | |
paul@36 | 334 | "Return the attribute names provided by this class only." |
paul@9 | 335 | |
paul@36 | 336 | class_attribute_names = NamespaceDict.keys |
paul@4 | 337 | |
paul@4 | 338 | def class_attributes(self): |
paul@4 | 339 | |
paul@12 | 340 | "Return class attributes provided by this class only." |
paul@4 | 341 | |
paul@49 | 342 | self.finalise_class_attributes() |
paul@36 | 343 | return self |
paul@12 | 344 | |
paul@12 | 345 | def all_class_attribute_names(self): |
paul@12 | 346 | |
paul@12 | 347 | "Return the attribute names provided by classes in this hierarchy." |
paul@12 | 348 | |
paul@12 | 349 | if self.all_classattr_names is None: |
paul@12 | 350 | self.all_class_attributes() |
paul@12 | 351 | return self.all_classattr_names |
paul@12 | 352 | |
paul@12 | 353 | def all_class_attributes(self): |
paul@12 | 354 | |
paul@12 | 355 | "Return all class attributes, indicating the class which provides them." |
paul@12 | 356 | |
paul@49 | 357 | self.finalise_class_attributes() |
paul@49 | 358 | return self.all_classattr |
paul@49 | 359 | |
paul@49 | 360 | def finalise_class_attributes(self): |
paul@49 | 361 | |
paul@49 | 362 | "Make sure that the class attributes are fully defined." |
paul@49 | 363 | |
paul@12 | 364 | if self.all_classattr is None: |
paul@12 | 365 | self.all_classattr = {} |
paul@49 | 366 | clsattr = {} |
paul@49 | 367 | |
paul@49 | 368 | # Record provisional position information for attributes of this |
paul@49 | 369 | # class. |
paul@49 | 370 | |
paul@49 | 371 | for name in self.class_attributes().keys(): |
paul@49 | 372 | clsattr[name] = set() # position not yet defined |
paul@12 | 373 | |
paul@9 | 374 | reversed_bases = self.bases[:] |
paul@9 | 375 | reversed_bases.reverse() |
paul@49 | 376 | |
paul@49 | 377 | # For the bases in reverse order, acquire class attribute details. |
paul@49 | 378 | |
paul@9 | 379 | for cls in reversed_bases: |
paul@49 | 380 | for name, attr in cls.all_class_attributes().items(): |
paul@49 | 381 | self.all_classattr[name] = attr |
paul@10 | 382 | |
paul@49 | 383 | # Record previous attribute information. |
paul@49 | 384 | |
paul@49 | 385 | if clsattr.has_key(name): |
paul@49 | 386 | clsattr[name].add(attr.position) |
paul@49 | 387 | |
paul@49 | 388 | # Record class attributes provided by this class and its bases, |
paul@49 | 389 | # along with their positions. |
paul@10 | 390 | |
paul@12 | 391 | self.all_classattr.update(self.class_attributes()) |
paul@9 | 392 | |
paul@49 | 393 | if clsattr: |
paul@49 | 394 | for i, name in enumerate(self._get_position_list(clsattr)): |
paul@49 | 395 | self.all_classattr[name].position = i |
paul@49 | 396 | |
paul@12 | 397 | return self.all_classattr |
paul@4 | 398 | |
paul@7 | 399 | def instance_attribute_names(self): |
paul@7 | 400 | |
paul@7 | 401 | "Return the instance attribute names provided by the class." |
paul@7 | 402 | |
paul@12 | 403 | if self.all_instattr_names is None: |
paul@12 | 404 | self.instance_attributes() |
paul@12 | 405 | return self.all_instattr_names |
paul@7 | 406 | |
paul@4 | 407 | def instance_attributes(self): |
paul@4 | 408 | |
paul@9 | 409 | "Return instance-only attributes for instances of this class." |
paul@9 | 410 | |
paul@49 | 411 | self.finalise_instance_attributes() |
paul@49 | 412 | return self.all_instattr |
paul@49 | 413 | |
paul@49 | 414 | def finalise_instance_attributes(self): |
paul@49 | 415 | |
paul@49 | 416 | "Make sure that the instance attributes are fully defined." |
paul@49 | 417 | |
paul@12 | 418 | if self.all_instattr is None: |
paul@12 | 419 | self.all_instattr = {} |
paul@46 | 420 | instattr = {} |
paul@12 | 421 | |
paul@49 | 422 | # Record provisional position information for attributes of this |
paul@49 | 423 | # instance. |
paul@49 | 424 | |
paul@49 | 425 | for name in self.instattr: |
paul@49 | 426 | instattr[name] = set() # position not yet defined |
paul@49 | 427 | |
paul@12 | 428 | reversed_bases = self.bases[:] |
paul@12 | 429 | reversed_bases.reverse() |
paul@46 | 430 | |
paul@46 | 431 | # For the bases in reverse order, acquire instance attribute |
paul@46 | 432 | # details. |
paul@46 | 433 | |
paul@12 | 434 | for cls in reversed_bases: |
paul@46 | 435 | for name, attr in cls.instance_attributes().items(): |
paul@49 | 436 | |
paul@49 | 437 | # Record previous attribute information. |
paul@12 | 438 | |
paul@49 | 439 | if instattr.has_key(name): |
paul@49 | 440 | instattr[name].add(attr.position) |
paul@46 | 441 | |
paul@48 | 442 | # Cache the attributes by converting the positioned attributes into |
paul@48 | 443 | # a dictionary. |
paul@48 | 444 | |
paul@46 | 445 | if not instattr: |
paul@46 | 446 | self.all_instattr = {} |
paul@46 | 447 | else: |
paul@49 | 448 | self.all_instattr = self._get_attributes(instattr) |
paul@12 | 449 | |
paul@12 | 450 | self.all_instattr_names = self.all_instattr.keys() |
paul@12 | 451 | |
paul@12 | 452 | return self.all_instattr |
paul@9 | 453 | |
paul@49 | 454 | def _get_position_list(self, positions): |
paul@48 | 455 | |
paul@48 | 456 | """ |
paul@49 | 457 | Return a list of attribute names for the given 'positions' mapping from |
paul@49 | 458 | names to positions, indicating the positions of the attributes in the |
paul@49 | 459 | final instance structure. |
paul@48 | 460 | """ |
paul@48 | 461 | |
paul@49 | 462 | position_items = positions.items() |
paul@49 | 463 | namearray = [None] * len(position_items) |
paul@48 | 464 | |
paul@48 | 465 | # Get the positions in ascending order of list size, with lists |
paul@48 | 466 | # of the same size ordered according to their smallest position |
paul@48 | 467 | # value. |
paul@48 | 468 | |
paul@49 | 469 | position_items.sort(self._cmp_positions) |
paul@48 | 470 | |
paul@48 | 471 | # Get the names in position order. |
paul@48 | 472 | |
paul@48 | 473 | held = [] |
paul@48 | 474 | |
paul@49 | 475 | for name, pos in position_items: |
paul@48 | 476 | pos = list(pos) |
paul@51 | 477 | pos.sort() |
paul@51 | 478 | if pos and pos[0] < len(namearray) and namearray[pos[0]] is None: |
paul@49 | 479 | namearray[pos[0]] = name |
paul@48 | 480 | else: |
paul@49 | 481 | if pos: |
paul@49 | 482 | self.relocated.add(name) |
paul@48 | 483 | held.append((name, pos)) |
paul@48 | 484 | |
paul@49 | 485 | for i, attr in enumerate(namearray): |
paul@48 | 486 | if attr is None: |
paul@48 | 487 | name, pos = held.pop() |
paul@49 | 488 | namearray[i] = name |
paul@49 | 489 | |
paul@49 | 490 | #print self.name, positions |
paul@49 | 491 | #print "->", namearray |
paul@49 | 492 | return namearray |
paul@49 | 493 | |
paul@49 | 494 | def _get_attributes(self, positions): |
paul@48 | 495 | |
paul@49 | 496 | """ |
paul@49 | 497 | For the given 'positions' mapping from names to positions, return a |
paul@49 | 498 | dictionary mapping names to Attr instances incorporating information |
paul@49 | 499 | about their positions in the final instance structure. |
paul@49 | 500 | """ |
paul@49 | 501 | |
paul@49 | 502 | d = {} |
paul@49 | 503 | for i, name in enumerate(self._get_position_list(positions)): |
paul@49 | 504 | d[name] = Attr(i, None, None, name) |
paul@49 | 505 | return d |
paul@48 | 506 | |
paul@46 | 507 | def _cmp_positions(self, a, b): |
paul@48 | 508 | |
paul@48 | 509 | "Compare name plus position list operands 'a' and 'b'." |
paul@48 | 510 | |
paul@46 | 511 | name_a, list_a = a |
paul@46 | 512 | name_b, list_b = b |
paul@46 | 513 | if len(list_a) < len(list_b): |
paul@46 | 514 | return -1 |
paul@46 | 515 | elif len(list_a) > len(list_b): |
paul@46 | 516 | return 1 |
paul@49 | 517 | elif not list_a: |
paul@49 | 518 | return 0 |
paul@46 | 519 | else: |
paul@46 | 520 | return cmp(min(list_a), min(list_b)) |
paul@46 | 521 | |
paul@9 | 522 | def all_attribute_names(self): |
paul@9 | 523 | |
paul@9 | 524 | """ |
paul@9 | 525 | Return the names of all attributes provided by instances of this class. |
paul@9 | 526 | """ |
paul@9 | 527 | |
paul@9 | 528 | self.allattr_names = self.allattr_names or self.all_attributes().keys() |
paul@9 | 529 | return self.allattr_names |
paul@9 | 530 | |
paul@9 | 531 | def all_attributes(self): |
paul@9 | 532 | |
paul@4 | 533 | """ |
paul@4 | 534 | Return all attributes for an instance, indicating either the class which |
paul@4 | 535 | provides them or that the instance itself provides them. |
paul@4 | 536 | """ |
paul@4 | 537 | |
paul@9 | 538 | if self.allattr is None: |
paul@9 | 539 | self.allattr = {} |
paul@12 | 540 | self.allattr.update(self.all_class_attributes()) |
paul@47 | 541 | for name, attr in self.instance_attributes().items(): |
paul@9 | 542 | if self.allattr.has_key(name): |
paul@9 | 543 | print "Instance attribute %r in %r overrides class attribute." % (name, self) |
paul@47 | 544 | self.allattr[name] = attr |
paul@9 | 545 | return self.allattr |
paul@4 | 546 | |
paul@12 | 547 | class Function(NamespaceDict, Naming): |
paul@0 | 548 | |
paul@0 | 549 | "An inspected function." |
paul@0 | 550 | |
paul@24 | 551 | def __init__(self, name, parent_name, argnames, has_star, has_dstar, global_namespace=None, node=None): |
paul@24 | 552 | NamespaceDict.__init__(self, global_namespace) |
paul@3 | 553 | self.name = name |
paul@7 | 554 | self.parent_name = parent_name |
paul@3 | 555 | self.argnames = argnames |
paul@3 | 556 | self.has_star = has_star |
paul@3 | 557 | self.has_dstar = has_dstar |
paul@17 | 558 | self.node = node |
paul@3 | 559 | |
paul@36 | 560 | # Caches. |
paul@36 | 561 | |
paul@12 | 562 | self.localnames = None # cache for locals |
paul@12 | 563 | |
paul@17 | 564 | # Add parameters to the namespace. |
paul@17 | 565 | |
paul@25 | 566 | self._add_parameters(argnames) |
paul@17 | 567 | |
paul@9 | 568 | # Image generation details. |
paul@9 | 569 | |
paul@9 | 570 | self.location = None |
paul@19 | 571 | self.code_location = None |
paul@9 | 572 | |
paul@25 | 573 | def _add_parameters(self, argnames): |
paul@25 | 574 | for name in argnames: |
paul@25 | 575 | if isinstance(name, tuple): |
paul@25 | 576 | self._add_parameters(name) |
paul@25 | 577 | else: |
paul@25 | 578 | self[name] = None |
paul@25 | 579 | |
paul@4 | 580 | def __repr__(self): |
paul@37 | 581 | if self.location is not None: |
paul@37 | 582 | return "Function(%r, %r, %r, %r, %r, location=%r)" % ( |
paul@37 | 583 | self.name, self.parent_name, self.argnames, self.has_star, self.has_dstar, self.location |
paul@37 | 584 | ) |
paul@37 | 585 | else: |
paul@37 | 586 | return "Function(%r, %r, %r, %r, %r)" % ( |
paul@37 | 587 | self.name, self.parent_name, self.argnames, self.has_star, self.has_dstar |
paul@37 | 588 | ) |
paul@4 | 589 | |
paul@12 | 590 | def make_global(self, name): |
paul@13 | 591 | if name not in self.argnames and not self.has_key(name): |
paul@12 | 592 | self.globals.add(name) |
paul@12 | 593 | else: |
paul@40 | 594 | raise InspectError(self.full_name(), self.node, "Name %r is global and local in %r" % (name, self.full_name())) |
paul@12 | 595 | |
paul@7 | 596 | def parameters(self): |
paul@7 | 597 | |
paul@7 | 598 | """ |
paul@7 | 599 | Return a dictionary mapping parameter names to their position in the |
paul@7 | 600 | parameter list. |
paul@7 | 601 | """ |
paul@7 | 602 | |
paul@7 | 603 | parameters = {} |
paul@7 | 604 | for i, name in enumerate(self.argnames): |
paul@7 | 605 | parameters[name] = i |
paul@7 | 606 | return parameters |
paul@7 | 607 | |
paul@36 | 608 | def all_locals(self): |
paul@36 | 609 | |
paul@36 | 610 | "Return a dictionary mapping names to local and parameter details." |
paul@36 | 611 | |
paul@36 | 612 | return self |
paul@36 | 613 | |
paul@12 | 614 | def locals(self): |
paul@12 | 615 | |
paul@12 | 616 | "Return a dictionary mapping names to local details." |
paul@12 | 617 | |
paul@12 | 618 | if self.localnames is None: |
paul@12 | 619 | self.localnames = {} |
paul@36 | 620 | self.localnames.update(self.all_locals()) |
paul@36 | 621 | for name in self.argnames: |
paul@36 | 622 | del self.localnames[name] |
paul@12 | 623 | return self.localnames |
paul@12 | 624 | |
paul@4 | 625 | class UnresolvedName(NamespaceDict, Naming): |
paul@3 | 626 | |
paul@3 | 627 | "A module, class or function which was mentioned but could not be imported." |
paul@3 | 628 | |
paul@24 | 629 | def __init__(self, name, parent_name, global_namespace=None): |
paul@24 | 630 | NamespaceDict.__init__(self, global_namespace) |
paul@0 | 631 | self.name = name |
paul@7 | 632 | self.parent_name = parent_name |
paul@0 | 633 | |
paul@12 | 634 | def all_class_attributes(self): |
paul@7 | 635 | return {} |
paul@7 | 636 | |
paul@13 | 637 | def instance_attributes(self): |
paul@13 | 638 | return {} |
paul@13 | 639 | |
paul@4 | 640 | def __repr__(self): |
paul@7 | 641 | return "UnresolvedName(%r, %r)" % (self.name, self.parent_name) |
paul@4 | 642 | |
paul@15 | 643 | class Module(NamespaceDict): |
paul@0 | 644 | |
paul@20 | 645 | "An inspected module's core details." |
paul@20 | 646 | |
paul@15 | 647 | def __init__(self, name): |
paul@24 | 648 | NamespaceDict.__init__(self, self) |
paul@1 | 649 | self.name = name |
paul@0 | 650 | |
paul@7 | 651 | # Complete lists of classes and functions. |
paul@7 | 652 | |
paul@33 | 653 | self.all_objects = set() |
paul@0 | 654 | |
paul@22 | 655 | # Constant records. |
paul@22 | 656 | |
paul@22 | 657 | self.constant_values = {} |
paul@22 | 658 | self.constant_list = None # cache for constants |
paul@22 | 659 | |
paul@15 | 660 | # Image generation details. |
paul@15 | 661 | |
paul@15 | 662 | self.location = None |
paul@19 | 663 | self.code_location = None |
paul@15 | 664 | |
paul@20 | 665 | # Original location details. |
paul@20 | 666 | |
paul@20 | 667 | self.node = None |
paul@20 | 668 | |
paul@15 | 669 | def full_name(self): |
paul@15 | 670 | return self.name |
paul@15 | 671 | |
paul@15 | 672 | def __repr__(self): |
paul@37 | 673 | if self.location is not None: |
paul@37 | 674 | return "Module(%r, location=%r)" % (self.name, self.location) |
paul@37 | 675 | else: |
paul@37 | 676 | return "Module(%r)" % self.name |
paul@15 | 677 | |
paul@15 | 678 | # Attribute methods. |
paul@15 | 679 | |
paul@36 | 680 | "Return the module attribute names provided by the module." |
paul@15 | 681 | |
paul@36 | 682 | module_attribute_names = NamespaceDict.keys |
paul@15 | 683 | |
paul@15 | 684 | def module_attributes(self): |
paul@15 | 685 | |
paul@15 | 686 | "Return a dictionary mapping names to module attributes." |
paul@15 | 687 | |
paul@36 | 688 | return self |
paul@15 | 689 | |
paul@22 | 690 | def constants(self): |
paul@22 | 691 | |
paul@22 | 692 | "Return a list of constants." |
paul@22 | 693 | |
paul@22 | 694 | if self.constant_list is None: |
paul@22 | 695 | self.constant_list = list(self.constant_values.values()) |
paul@22 | 696 | |
paul@22 | 697 | return self.constant_list |
paul@22 | 698 | |
paul@21 | 699 | # Program visitors. |
paul@21 | 700 | |
paul@15 | 701 | class InspectedModule(ASTVisitor, Module): |
paul@15 | 702 | |
paul@20 | 703 | """ |
paul@20 | 704 | An inspected module, providing core details via the Module superclass, but |
paul@20 | 705 | capable of being used as an AST visitor. |
paul@20 | 706 | """ |
paul@15 | 707 | |
paul@15 | 708 | def __init__(self, name, importer=None): |
paul@15 | 709 | ASTVisitor.__init__(self) |
paul@15 | 710 | Module.__init__(self, name) |
paul@15 | 711 | self.visitor = self |
paul@15 | 712 | |
paul@15 | 713 | self.importer = importer |
paul@15 | 714 | self.loaded = 0 |
paul@15 | 715 | |
paul@1 | 716 | # Current expression state. |
paul@1 | 717 | |
paul@1 | 718 | self.expr = None |
paul@1 | 719 | |
paul@1 | 720 | # Namespace state. |
paul@1 | 721 | |
paul@34 | 722 | self.in_init = 0 # Find instance attributes in __init__ methods. |
paul@34 | 723 | self.in_loop = 0 # Note loop "membership", affecting assignments. |
paul@12 | 724 | self.namespaces = [] |
paul@0 | 725 | self.module = None |
paul@0 | 726 | |
paul@0 | 727 | def parse(self, filename): |
paul@9 | 728 | |
paul@9 | 729 | "Parse the file having the given 'filename'." |
paul@9 | 730 | |
paul@0 | 731 | module = compiler.parseFile(filename) |
paul@0 | 732 | self.process(module) |
paul@0 | 733 | |
paul@0 | 734 | def process(self, module): |
paul@9 | 735 | |
paul@9 | 736 | "Process the given 'module'." |
paul@9 | 737 | |
paul@20 | 738 | self.node = self.module = module |
paul@1 | 739 | processed = self.dispatch(module) |
paul@13 | 740 | if self.has_key("__all__"): |
paul@12 | 741 | all = self["__all__"] |
paul@1 | 742 | if isinstance(all, compiler.ast.List): |
paul@1 | 743 | for n in all.nodes: |
paul@12 | 744 | self[n.value] = self.importer.add_module(self.name + "." + n.value) |
paul@1 | 745 | return processed |
paul@0 | 746 | |
paul@3 | 747 | def vacuum(self): |
paul@9 | 748 | |
paul@9 | 749 | "Vacuum the module namespace, removing unloaded module references." |
paul@9 | 750 | |
paul@13 | 751 | for name, value in self.items(): |
paul@3 | 752 | if isinstance(value, Module) and not value.loaded: |
paul@12 | 753 | del self[name] |
paul@0 | 754 | |
paul@16 | 755 | # Complain about globals not initialised at the module level. |
paul@16 | 756 | |
paul@16 | 757 | if isinstance(value, Global): |
paul@16 | 758 | print "Warning: global %r in module %r not initialised at the module level." % (name, self.name) |
paul@16 | 759 | |
paul@3 | 760 | # Namespace methods. |
paul@0 | 761 | |
paul@0 | 762 | def store(self, name, obj): |
paul@12 | 763 | |
paul@12 | 764 | "Record attribute or local 'name', storing 'obj'." |
paul@12 | 765 | |
paul@12 | 766 | if not self.namespaces: |
paul@34 | 767 | self.set(name, obj, not self.in_loop) |
paul@12 | 768 | else: |
paul@34 | 769 | self.namespaces[-1].set(name, obj, not self.in_loop) |
paul@12 | 770 | |
paul@12 | 771 | # Record all non-local objects. |
paul@12 | 772 | |
paul@12 | 773 | if not (self.namespaces and isinstance(self.namespaces[-1], Function)): |
paul@33 | 774 | self.all_objects.add(obj) |
paul@0 | 775 | |
paul@34 | 776 | def store_instance_attr(self, name): |
paul@12 | 777 | |
paul@12 | 778 | "Record instance attribute 'name' in the current class." |
paul@12 | 779 | |
paul@0 | 780 | if self.in_init: |
paul@15 | 781 | |
paul@12 | 782 | # Current namespace is the function. |
paul@12 | 783 | # Previous namespace is the class. |
paul@15 | 784 | |
paul@12 | 785 | self.namespaces[-2].add_instance_attribute(name) |
paul@0 | 786 | |
paul@7 | 787 | def get_parent(self): |
paul@12 | 788 | return (self.namespaces[-1:] or [self])[0] |
paul@7 | 789 | |
paul@3 | 790 | # Visitor methods. |
paul@3 | 791 | |
paul@3 | 792 | def default(self, node, *args): |
paul@39 | 793 | raise InspectError(self.full_name(), node, "Node class %r is not supported." % node.__class__) |
paul@3 | 794 | |
paul@3 | 795 | def dispatch(self, node, *args): |
paul@3 | 796 | return ASTVisitor.dispatch(self, node, *args) |
paul@3 | 797 | |
paul@0 | 798 | def NOP(self, node): |
paul@21 | 799 | for n in node.getChildNodes(): |
paul@21 | 800 | self.dispatch(n) |
paul@19 | 801 | return None |
paul@0 | 802 | |
paul@1 | 803 | visitAdd = NOP |
paul@1 | 804 | |
paul@0 | 805 | visitAnd = NOP |
paul@0 | 806 | |
paul@4 | 807 | visitAssert = NOP |
paul@4 | 808 | |
paul@0 | 809 | def visitAssign(self, node): |
paul@0 | 810 | self.expr = self.dispatch(node.expr) |
paul@0 | 811 | for n in node.nodes: |
paul@0 | 812 | self.dispatch(n) |
paul@0 | 813 | return None |
paul@0 | 814 | |
paul@0 | 815 | def visitAssAttr(self, node): |
paul@0 | 816 | expr = self.dispatch(node.expr) |
paul@36 | 817 | if isinstance(expr, Attr) and expr.name == "self": |
paul@34 | 818 | self.store_instance_attr(node.attrname) |
paul@0 | 819 | return None |
paul@0 | 820 | |
paul@0 | 821 | def visitAssList(self, node): |
paul@0 | 822 | for n in node.nodes: |
paul@0 | 823 | self.dispatch(n) |
paul@0 | 824 | return None |
paul@0 | 825 | |
paul@0 | 826 | def visitAssName(self, node): |
paul@36 | 827 | if isinstance(self.expr, Attr): |
paul@36 | 828 | self.store(node.name, self.expr.value) |
paul@36 | 829 | else: |
paul@36 | 830 | self.store(node.name, self.expr) |
paul@0 | 831 | return None |
paul@0 | 832 | |
paul@0 | 833 | visitAssTuple = visitAssList |
paul@0 | 834 | |
paul@2 | 835 | visitAugAssign = NOP |
paul@2 | 836 | |
paul@4 | 837 | visitBackquote = NOP |
paul@4 | 838 | |
paul@2 | 839 | visitBitand = NOP |
paul@2 | 840 | |
paul@2 | 841 | visitBitor = NOP |
paul@2 | 842 | |
paul@4 | 843 | visitBitxor = NOP |
paul@4 | 844 | |
paul@4 | 845 | visitBreak = NOP |
paul@4 | 846 | |
paul@0 | 847 | visitCallFunc = NOP |
paul@0 | 848 | |
paul@0 | 849 | def visitClass(self, node): |
paul@12 | 850 | if self.namespaces: |
paul@41 | 851 | print "Class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) |
paul@0 | 852 | else: |
paul@24 | 853 | cls = Class(node.name, self.get_parent().full_name(), self, node) |
paul@37 | 854 | |
paul@37 | 855 | # Visit the base class expressions, attempting to find concrete |
paul@37 | 856 | # definitions of classes. |
paul@37 | 857 | |
paul@0 | 858 | for base in node.bases: |
paul@36 | 859 | expr = self.dispatch(base) |
paul@36 | 860 | if isinstance(expr, Attr): |
paul@36 | 861 | if expr.assignments != 1: |
paul@39 | 862 | raise InspectError(self.full_name(), node, |
paul@40 | 863 | "Base class %r for %r is not constant." % (base, cls.full_name())) |
paul@36 | 864 | else: |
paul@36 | 865 | cls.add_base(expr.value) |
paul@36 | 866 | else: # if expr is None: |
paul@39 | 867 | raise InspectError(self.full_name(), node, |
paul@40 | 868 | "Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name())) |
paul@0 | 869 | |
paul@21 | 870 | # Make a back reference from the node for code generation. |
paul@21 | 871 | |
paul@39 | 872 | node.unit = cls |
paul@21 | 873 | |
paul@0 | 874 | # Make an entry for the class. |
paul@0 | 875 | |
paul@0 | 876 | self.store(node.name, cls) |
paul@0 | 877 | |
paul@12 | 878 | self.namespaces.append(cls) |
paul@0 | 879 | self.dispatch(node.code) |
paul@12 | 880 | self.namespaces.pop() |
paul@0 | 881 | |
paul@19 | 882 | return None |
paul@0 | 883 | |
paul@4 | 884 | visitCompare = NOP |
paul@4 | 885 | |
paul@21 | 886 | def visitConst(self, node): |
paul@21 | 887 | const = Const(node.value) |
paul@22 | 888 | self.constant_values[node.value] = const |
paul@24 | 889 | return const |
paul@0 | 890 | |
paul@4 | 891 | visitContinue = NOP |
paul@4 | 892 | |
paul@4 | 893 | visitDecorators = NOP |
paul@4 | 894 | |
paul@0 | 895 | visitDict = NOP |
paul@0 | 896 | |
paul@0 | 897 | visitDiscard = NOP |
paul@0 | 898 | |
paul@4 | 899 | visitDiv = NOP |
paul@4 | 900 | |
paul@4 | 901 | visitEllipsis = NOP |
paul@4 | 902 | |
paul@4 | 903 | visitExec = NOP |
paul@4 | 904 | |
paul@4 | 905 | visitExpression = NOP |
paul@4 | 906 | |
paul@4 | 907 | visitFloorDiv = NOP |
paul@4 | 908 | |
paul@34 | 909 | def visitFor(self, node): |
paul@34 | 910 | self.in_loop = 1 |
paul@34 | 911 | self.NOP(node) |
paul@34 | 912 | self.in_loop = 0 |
paul@0 | 913 | |
paul@0 | 914 | def visitFrom(self, node): |
paul@0 | 915 | if self.importer is None: |
paul@39 | 916 | raise InspectError(self.full_name(), node, |
paul@39 | 917 | "Please use the micropython.Importer class for code which uses the 'from' statement.") |
paul@0 | 918 | |
paul@0 | 919 | module = self.importer.load(node.modname, 1) |
paul@0 | 920 | |
paul@35 | 921 | #if module is None: |
paul@35 | 922 | # print "Warning:", node.modname, "not imported." |
paul@0 | 923 | |
paul@0 | 924 | for name, alias in node.names: |
paul@0 | 925 | if name != "*": |
paul@4 | 926 | if module is not None and module.namespace.has_key(name): |
paul@24 | 927 | attr = module[name] |
paul@36 | 928 | self.store(alias or name, attr.value) |
paul@2 | 929 | if isinstance(attr, Module) and not attr.loaded: |
paul@2 | 930 | self.importer.load(attr.name) |
paul@24 | 931 | |
paul@24 | 932 | # Support the import of names from missing modules. |
paul@24 | 933 | |
paul@3 | 934 | else: |
paul@24 | 935 | self.store(alias or name, UnresolvedName(name, node.modname, self)) |
paul@3 | 936 | else: |
paul@3 | 937 | if module is not None: |
paul@3 | 938 | for n in module.namespace.keys(): |
paul@24 | 939 | attr = module[n] |
paul@36 | 940 | self.store(n, attr.value) |
paul@3 | 941 | if isinstance(attr, Module) and not attr.loaded: |
paul@3 | 942 | self.importer.load(attr.name) |
paul@0 | 943 | |
paul@0 | 944 | return None |
paul@0 | 945 | |
paul@0 | 946 | def visitFunction(self, node): |
paul@3 | 947 | function = Function( |
paul@3 | 948 | node.name, |
paul@7 | 949 | self.get_parent().full_name(), |
paul@3 | 950 | node.argnames, |
paul@17 | 951 | (node.flags & 4 != 0), |
paul@17 | 952 | (node.flags & 8 != 0), |
paul@24 | 953 | self, |
paul@17 | 954 | node |
paul@3 | 955 | ) |
paul@0 | 956 | |
paul@21 | 957 | # Make a back reference from the node for code generation. |
paul@21 | 958 | |
paul@39 | 959 | node.unit = function |
paul@21 | 960 | |
paul@12 | 961 | self.namespaces.append(function) |
paul@15 | 962 | |
paul@12 | 963 | # Current namespace is the function. |
paul@12 | 964 | # Previous namespace is the class. |
paul@15 | 965 | |
paul@12 | 966 | if node.name == "__init__" and isinstance(self.namespaces[-2], Class): |
paul@0 | 967 | self.in_init = 1 |
paul@15 | 968 | |
paul@12 | 969 | self.dispatch(node.code) |
paul@12 | 970 | self.in_init = 0 |
paul@12 | 971 | self.namespaces.pop() |
paul@3 | 972 | |
paul@3 | 973 | self.store(node.name, function) |
paul@0 | 974 | return None |
paul@0 | 975 | |
paul@4 | 976 | visitGenExpr = NOP |
paul@4 | 977 | |
paul@4 | 978 | visitGenExprFor = NOP |
paul@4 | 979 | |
paul@4 | 980 | visitGenExprIf = NOP |
paul@4 | 981 | |
paul@4 | 982 | visitGenExprInner = NOP |
paul@4 | 983 | |
paul@0 | 984 | def visitGetattr(self, node): |
paul@0 | 985 | expr = self.dispatch(node.expr) |
paul@36 | 986 | if isinstance(expr, Attr): |
paul@36 | 987 | value = expr.value |
paul@36 | 988 | if isinstance(value, Module): |
paul@36 | 989 | return value.namespace.get(node.attrname) |
paul@36 | 990 | elif isinstance(value, UnresolvedName): |
paul@36 | 991 | return UnresolvedName(node.attrname, value.full_name(), self) |
paul@15 | 992 | return builtins.get(node.attrname) |
paul@4 | 993 | |
paul@12 | 994 | def visitGlobal(self, node): |
paul@12 | 995 | if self.namespaces: |
paul@12 | 996 | for name in node.names: |
paul@12 | 997 | self.namespaces[-1].make_global(name) |
paul@37 | 998 | |
paul@37 | 999 | # Record a global entry for the name in the module. |
paul@37 | 1000 | |
paul@15 | 1001 | if not self.has_key(name): |
paul@16 | 1002 | self[name] = Global() |
paul@0 | 1003 | |
paul@0 | 1004 | def visitIf(self, node): |
paul@0 | 1005 | for test, body in node.tests: |
paul@0 | 1006 | self.dispatch(body) |
paul@0 | 1007 | if node.else_ is not None: |
paul@0 | 1008 | self.dispatch(node.else_) |
paul@0 | 1009 | return None |
paul@0 | 1010 | |
paul@12 | 1011 | visitIfExp = NOP |
paul@12 | 1012 | |
paul@0 | 1013 | def visitImport(self, node): |
paul@0 | 1014 | if self.importer is None: |
paul@39 | 1015 | raise InspectError(self.full_name(), node, |
paul@39 | 1016 | "Please use the micropython.Importer class for code which uses the 'import' statement.") |
paul@0 | 1017 | |
paul@0 | 1018 | for name, alias in node.names: |
paul@2 | 1019 | if alias is not None: |
paul@24 | 1020 | self.store(alias, self.importer.load(name, 1) or UnresolvedName(None, name, self)) |
paul@2 | 1021 | else: |
paul@24 | 1022 | self.store(name.split(".")[0], self.importer.load(name) or UnresolvedName(None, name.split(".")[0], self)) |
paul@0 | 1023 | |
paul@0 | 1024 | return None |
paul@0 | 1025 | |
paul@4 | 1026 | visitInvert = NOP |
paul@4 | 1027 | |
paul@4 | 1028 | visitKeyword = NOP |
paul@4 | 1029 | |
paul@4 | 1030 | visitLambda = NOP |
paul@4 | 1031 | |
paul@1 | 1032 | visitLeftShift = NOP |
paul@1 | 1033 | |
paul@0 | 1034 | visitList = NOP |
paul@0 | 1035 | |
paul@4 | 1036 | visitListComp = NOP |
paul@4 | 1037 | |
paul@4 | 1038 | visitListCompFor = NOP |
paul@4 | 1039 | |
paul@4 | 1040 | visitListCompIf = NOP |
paul@4 | 1041 | |
paul@4 | 1042 | visitMod = NOP |
paul@4 | 1043 | |
paul@0 | 1044 | def visitModule(self, node): |
paul@0 | 1045 | return self.dispatch(node.node) |
paul@0 | 1046 | |
paul@2 | 1047 | visitMul = NOP |
paul@2 | 1048 | |
paul@0 | 1049 | def visitName(self, node): |
paul@0 | 1050 | name = node.name |
paul@36 | 1051 | if self.namespaces and self.namespaces[-1].has_key(name): |
paul@35 | 1052 | return self.namespaces[-1][name] |
paul@13 | 1053 | elif self.has_key(name): |
paul@12 | 1054 | return self[name] |
paul@15 | 1055 | elif builtins.has_key(name): |
paul@15 | 1056 | return builtins[name] |
paul@0 | 1057 | else: |
paul@0 | 1058 | return None |
paul@0 | 1059 | |
paul@0 | 1060 | visitNot = NOP |
paul@0 | 1061 | |
paul@0 | 1062 | visitOr = NOP |
paul@0 | 1063 | |
paul@0 | 1064 | visitPass = NOP |
paul@0 | 1065 | |
paul@4 | 1066 | visitPower = NOP |
paul@4 | 1067 | |
paul@4 | 1068 | visitPrint = NOP |
paul@4 | 1069 | |
paul@4 | 1070 | visitPrintnl = NOP |
paul@4 | 1071 | |
paul@0 | 1072 | visitRaise = NOP |
paul@0 | 1073 | |
paul@4 | 1074 | visitReturn = NOP |
paul@4 | 1075 | |
paul@1 | 1076 | visitRightShift = NOP |
paul@1 | 1077 | |
paul@4 | 1078 | visitSlice = NOP |
paul@4 | 1079 | |
paul@23 | 1080 | visitSliceobj = NOP |
paul@23 | 1081 | |
paul@0 | 1082 | def visitStmt(self, node): |
paul@0 | 1083 | for n in node.nodes: |
paul@0 | 1084 | self.dispatch(n) |
paul@0 | 1085 | return None |
paul@0 | 1086 | |
paul@4 | 1087 | visitSub = NOP |
paul@4 | 1088 | |
paul@0 | 1089 | visitSubscript = NOP |
paul@0 | 1090 | |
paul@0 | 1091 | def visitTryExcept(self, node): |
paul@0 | 1092 | self.dispatch(node.body) |
paul@0 | 1093 | for name, var, n in node.handlers: |
paul@0 | 1094 | self.dispatch(n) |
paul@0 | 1095 | if node.else_ is not None: |
paul@0 | 1096 | self.dispatch(node.else_) |
paul@0 | 1097 | return None |
paul@0 | 1098 | |
paul@4 | 1099 | visitTryFinally = NOP |
paul@4 | 1100 | |
paul@1 | 1101 | visitTuple = NOP |
paul@1 | 1102 | |
paul@4 | 1103 | visitUnaryAdd = NOP |
paul@4 | 1104 | |
paul@4 | 1105 | visitUnarySub = NOP |
paul@4 | 1106 | |
paul@34 | 1107 | def visitWhile(self, node): |
paul@34 | 1108 | self.in_loop = 1 |
paul@34 | 1109 | self.NOP(node) |
paul@34 | 1110 | self.in_loop = 0 |
paul@4 | 1111 | |
paul@4 | 1112 | visitWith = NOP |
paul@4 | 1113 | |
paul@4 | 1114 | visitYield = NOP |
paul@4 | 1115 | |
paul@16 | 1116 | class Global: |
paul@16 | 1117 | |
paul@16 | 1118 | """ |
paul@16 | 1119 | A reference to an object assigned to a global from outside the module |
paul@16 | 1120 | top-level. |
paul@16 | 1121 | """ |
paul@16 | 1122 | |
paul@16 | 1123 | pass |
paul@16 | 1124 | |
paul@4 | 1125 | # Built-in types initialisation. |
paul@4 | 1126 | |
paul@15 | 1127 | class Builtins(Module): |
paul@15 | 1128 | |
paul@15 | 1129 | "The special built-in types module." |
paul@15 | 1130 | |
paul@15 | 1131 | def __init__(self): |
paul@15 | 1132 | Module.__init__(self, "__builtins__") |
paul@44 | 1133 | self.loaded = 1 |
paul@44 | 1134 | self.module = None |
paul@15 | 1135 | |
paul@15 | 1136 | for key in ['ArithmeticError', 'AssertionError', 'AttributeError', |
paul@15 | 1137 | 'BaseException', 'DeprecationWarning', 'EOFError', 'Ellipsis', |
paul@15 | 1138 | 'EnvironmentError', 'Exception', 'False', 'FloatingPointError', |
paul@15 | 1139 | 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', |
paul@15 | 1140 | 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', |
paul@15 | 1141 | 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', |
paul@15 | 1142 | 'None', 'NotImplemented', 'NotImplementedError', 'OSError', |
paul@15 | 1143 | 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', |
paul@15 | 1144 | 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', |
paul@15 | 1145 | 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', |
paul@15 | 1146 | 'TabError', 'True', 'TypeError', 'UnboundLocalError', |
paul@15 | 1147 | 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', |
paul@15 | 1148 | 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', |
paul@15 | 1149 | 'ValueError', 'Warning', 'ZeroDivisionError', |
paul@25 | 1150 | 'basestring', 'bool', 'buffer', 'complex', 'dict', 'file', 'float', |
paul@44 | 1151 | 'frozenset', 'int', # 'list', |
paul@44 | 1152 | 'long', 'object', 'set', 'slice', 'str', |
paul@25 | 1153 | 'tuple', 'type', 'unicode', 'xrange']: |
paul@44 | 1154 | self.store(key, Class(key, self.full_name(), self)) |
paul@44 | 1155 | |
paul@44 | 1156 | # NOTE: Temporary measure - provide detailed built-ins. |
paul@44 | 1157 | |
paul@44 | 1158 | cls = Class("list", self.full_name(), self) |
paul@44 | 1159 | cls.set("__iter__", Function("__iter__", cls.full_name(), [], 0, 0, cls)) |
paul@44 | 1160 | self.store("list", cls) |
paul@44 | 1161 | |
paul@44 | 1162 | cls = Class("listiterator", self.full_name(), self) |
paul@44 | 1163 | cls.set("next", Function("next", cls.full_name(), [], 0, 0, cls)) |
paul@44 | 1164 | self.store("listiterator", cls) |
paul@15 | 1165 | |
paul@25 | 1166 | # NOTE: Incomplete: some functions have more than one parameter. |
paul@25 | 1167 | |
paul@25 | 1168 | for key in ['__import__', 'abs', 'all', 'any', 'callable', 'chr', |
paul@25 | 1169 | 'classmethod', 'cmp', 'compile', 'delattr', 'dir', 'divmod', |
paul@25 | 1170 | 'enumerate', 'eval', 'execfile', 'filter', 'getattr', 'globals', |
paul@25 | 1171 | 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'isinstance', |
paul@25 | 1172 | 'issubclass', 'iter', 'len', 'locals', 'map', 'max', 'min', 'oct', |
paul@25 | 1173 | 'open', 'ord', 'pow', 'property', 'range', 'raw_input', 'reduce', |
paul@25 | 1174 | 'reload', 'repr', 'reversed', 'round', 'setattr', 'sorted', |
paul@25 | 1175 | 'staticmethod', 'sum', 'super', 'unichr', 'vars', 'zip']: |
paul@24 | 1176 | self[key] = Function(key, self.full_name(), ['arg'], 0, 0, self) |
paul@16 | 1177 | |
paul@44 | 1178 | def store(self, name, obj): |
paul@44 | 1179 | self.set(name, obj) |
paul@44 | 1180 | self.all_objects.add(obj) |
paul@44 | 1181 | |
paul@44 | 1182 | def vacuum(self): |
paul@44 | 1183 | pass |
paul@44 | 1184 | |
paul@15 | 1185 | builtins = Builtins() |
paul@0 | 1186 | |
paul@0 | 1187 | # vim: tabstop=4 expandtab shiftwidth=4 |