paul@83 | 1 | #!/usr/bin/env python |
paul@83 | 2 | |
paul@83 | 3 | """ |
paul@83 | 4 | Data classes. |
paul@83 | 5 | |
paul@83 | 6 | Copyright (C) 2007, 2008 Paul Boddie <paul@boddie.org.uk> |
paul@83 | 7 | |
paul@83 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@83 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@83 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@83 | 11 | version. |
paul@83 | 12 | |
paul@83 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@83 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@83 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@83 | 16 | details. |
paul@83 | 17 | |
paul@83 | 18 | You should have received a copy of the GNU General Public License along with |
paul@83 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@83 | 20 | |
paul@83 | 21 | -------- |
paul@83 | 22 | |
paul@83 | 23 | The central classes in this module are the following: |
paul@83 | 24 | |
paul@83 | 25 | * Class |
paul@83 | 26 | * Function |
paul@83 | 27 | * Module |
paul@83 | 28 | |
paul@83 | 29 | All of the above support the Naming interface either explicitly or through |
paul@83 | 30 | general conformance, meaning that all can be asked to provide their 'full_name' |
paul@83 | 31 | using the method of that name. |
paul@83 | 32 | |
paul@83 | 33 | Additionally, all of the above also support a dictionary interface in order to |
paul@83 | 34 | access names within their defined scopes. Specific methods also exist in order |
paul@83 | 35 | to distinguish between certain kinds of attributes: |
paul@83 | 36 | |
paul@83 | 37 | * Class: (class|all_class|instance|all)_attributes |
paul@83 | 38 | * Function: parameters, locals, all_locals |
paul@83 | 39 | * Module: module_attributes |
paul@83 | 40 | |
paul@83 | 41 | These specific methods are useful in certain situations. |
paul@83 | 42 | |
paul@83 | 43 | The above classes also provide a 'node' attribute, indicating the AST node where |
paul@83 | 44 | each such object is defined. |
paul@83 | 45 | """ |
paul@83 | 46 | |
paul@83 | 47 | from micropython.common import * |
paul@83 | 48 | |
paul@83 | 49 | # Mix-ins and abstract classes. |
paul@83 | 50 | |
paul@83 | 51 | class NamespaceDict: |
paul@83 | 52 | |
paul@83 | 53 | "A mix-in providing dictionary methods." |
paul@83 | 54 | |
paul@83 | 55 | def __init__(self, global_namespace=None): |
paul@83 | 56 | self.namespace = {} |
paul@83 | 57 | self.globals = set() |
paul@83 | 58 | self.global_namespace = global_namespace |
paul@83 | 59 | |
paul@83 | 60 | def __getitem__(self, name): |
paul@83 | 61 | return self.namespace[name] |
paul@83 | 62 | |
paul@83 | 63 | def get(self, name, default=None): |
paul@83 | 64 | return self.namespace.get(name, default) |
paul@83 | 65 | |
paul@83 | 66 | def __setitem__(self, name, value): |
paul@83 | 67 | self.set(name, value) |
paul@83 | 68 | |
paul@83 | 69 | def set(self, name, value, single_assignment=1): |
paul@83 | 70 | |
paul@83 | 71 | """ |
paul@83 | 72 | A more powerful set operation, making 'name' refer to 'value' whilst |
paul@83 | 73 | indicating whether a 'single_assignment' (true by default) occurs in |
paul@83 | 74 | this operation (or whether the operation covers potentially many |
paul@83 | 75 | assignments in the lifetime of a program). |
paul@83 | 76 | """ |
paul@83 | 77 | |
paul@83 | 78 | if name in self.globals: |
paul@83 | 79 | self.global_namespace.set(name, value, 0) |
paul@83 | 80 | else: |
paul@83 | 81 | attr = self._set(name, value) |
paul@83 | 82 | |
paul@83 | 83 | # NOTE: Insist on assignments with known values. |
paul@83 | 84 | |
paul@83 | 85 | if value is not None: |
paul@83 | 86 | attr.update(value, single_assignment) |
paul@83 | 87 | |
paul@83 | 88 | def set_module(self, name, value): |
paul@83 | 89 | |
paul@83 | 90 | """ |
paul@83 | 91 | A specialised set operation, making 'name' refer to 'value' in the |
paul@83 | 92 | context of making a module reference available in association with |
paul@83 | 93 | 'name' as part of the import of that module or a submodule of that |
paul@83 | 94 | module. |
paul@83 | 95 | """ |
paul@83 | 96 | |
paul@83 | 97 | attr = self._set(name, value) |
paul@83 | 98 | if attr.assignments is None: |
paul@83 | 99 | attr.assignments = 1 |
paul@83 | 100 | attr.assignment_values.add(value) |
paul@83 | 101 | |
paul@83 | 102 | def _set(self, name, value): |
paul@83 | 103 | |
paul@83 | 104 | "The underlying set operation associating 'name' with 'value'." |
paul@83 | 105 | |
paul@83 | 106 | if not self.namespace.has_key(name): |
paul@83 | 107 | self.namespace[name] = Attr(None, self, name, value) |
paul@83 | 108 | return self.namespace[name] |
paul@83 | 109 | |
paul@83 | 110 | def __delitem__(self, name): |
paul@83 | 111 | del self.namespace[name] |
paul@83 | 112 | |
paul@83 | 113 | def has_key(self, name): |
paul@83 | 114 | return self.namespace.has_key(name) |
paul@83 | 115 | |
paul@83 | 116 | def keys(self): |
paul@83 | 117 | return self.namespace.keys() |
paul@83 | 118 | |
paul@83 | 119 | def values(self): |
paul@83 | 120 | return self.namespace.values() |
paul@83 | 121 | |
paul@83 | 122 | def items(self): |
paul@83 | 123 | return self.namespace.items() |
paul@83 | 124 | |
paul@83 | 125 | def make_global(self, name): |
paul@83 | 126 | if not self.namespace.has_key(name): |
paul@83 | 127 | self.globals.add(name) |
paul@83 | 128 | else: |
paul@83 | 129 | raise InspectError(self.full_name(), self.node, "Name %r is both global and local in %r" % (name, self.full_name())) |
paul@83 | 130 | |
paul@83 | 131 | def get_assignments(self, name): |
paul@83 | 132 | if self.assignments.has_key(name): |
paul@83 | 133 | return max(self.assignments[name], len(self.assignment_values[name])) |
paul@83 | 134 | else: |
paul@83 | 135 | return None |
paul@83 | 136 | |
paul@83 | 137 | def attributes_as_list(self): |
paul@83 | 138 | self.finalise_attributes() |
paul@83 | 139 | l = [None] * len(self.keys()) |
paul@83 | 140 | for attr in self.values(): |
paul@83 | 141 | l[attr.position] = attr |
paul@83 | 142 | return l |
paul@83 | 143 | |
paul@83 | 144 | def finalise_attributes(self): |
paul@83 | 145 | |
paul@83 | 146 | "Make sure all attributes are fully defined." |
paul@83 | 147 | |
paul@83 | 148 | # The default action is to assign attribute positions sequentially. |
paul@83 | 149 | |
paul@83 | 150 | for i, attr in enumerate(self.values()): |
paul@83 | 151 | attr.position = i |
paul@83 | 152 | |
paul@85 | 153 | # Program data structures. There are two separate kinds of structures: those |
paul@85 | 154 | # with context, which are the values manipulated by programs, and those without |
paul@85 | 155 | # context, which are typically constant things which are stored alongside the |
paul@85 | 156 | # program but which are wrapped in context-dependent structures in the running |
paul@85 | 157 | # program. |
paul@83 | 158 | |
paul@83 | 159 | class Attr: |
paul@83 | 160 | |
paul@83 | 161 | "An attribute entry having a parent as context." |
paul@83 | 162 | |
paul@83 | 163 | def __init__(self, position, parent, name, value=None, assignments=None): |
paul@83 | 164 | self.position = position |
paul@83 | 165 | self.parent = parent |
paul@83 | 166 | self.name = name |
paul@83 | 167 | self.value = value |
paul@83 | 168 | |
paul@83 | 169 | # Number of assignments per name. |
paul@83 | 170 | |
paul@83 | 171 | self.assignments = assignments |
paul@83 | 172 | self.assignment_values = set() |
paul@83 | 173 | |
paul@83 | 174 | def update(self, value, single_assignment): |
paul@83 | 175 | |
paul@83 | 176 | """ |
paul@83 | 177 | Update the attribute, adding the 'value' provided to the known values |
paul@83 | 178 | associated with the attribute, changing the number of assignments |
paul@83 | 179 | according to the 'single_assignment' status of the operation, where |
paul@83 | 180 | a true value indicates that only one assignment is associated with the |
paul@83 | 181 | update, and a false value indicates that potentially many assignments |
paul@83 | 182 | may be involved. |
paul@83 | 183 | """ |
paul@83 | 184 | |
paul@83 | 185 | if self.assignments is None: |
paul@83 | 186 | if single_assignment: |
paul@83 | 187 | self.assignments = 1 |
paul@83 | 188 | else: |
paul@83 | 189 | self.assignments = AtLeast(1) |
paul@83 | 190 | else: |
paul@83 | 191 | if single_assignment: |
paul@83 | 192 | self.assignments += 1 |
paul@83 | 193 | else: |
paul@83 | 194 | self.assignments += AtLeast(1) |
paul@83 | 195 | self.assignment_values.add(value) |
paul@83 | 196 | |
paul@83 | 197 | def __repr__(self): |
paul@83 | 198 | return "Attr(%r, %r, %r, %r, %r)" % (self.position, self.parent, self.name, self.value, self.assignments) |
paul@83 | 199 | |
paul@86 | 200 | # Instances are special in that they need to be wrapped together with context in |
paul@86 | 201 | # a running program, but they are not generally constant. |
paul@86 | 202 | |
paul@86 | 203 | class Instance: |
paul@86 | 204 | |
paul@86 | 205 | "A placeholder indicating the involvement of an instance." |
paul@86 | 206 | |
paul@86 | 207 | def __repr__(self): |
paul@86 | 208 | return "Instance()" |
paul@86 | 209 | |
paul@85 | 210 | class Constant: |
paul@85 | 211 | |
paul@85 | 212 | "A superclass for all constant or context-free structures." |
paul@85 | 213 | |
paul@85 | 214 | pass |
paul@85 | 215 | |
paul@86 | 216 | class Const(Constant, Instance): |
paul@83 | 217 | |
paul@83 | 218 | "A constant object with no context." |
paul@83 | 219 | |
paul@83 | 220 | def __init__(self, value): |
paul@83 | 221 | self.value = value |
paul@83 | 222 | |
paul@83 | 223 | # Image generation details. |
paul@83 | 224 | |
paul@83 | 225 | self.location = None |
paul@83 | 226 | |
paul@83 | 227 | def __repr__(self): |
paul@83 | 228 | if self.location is not None: |
paul@83 | 229 | return "Const(%r, location=%r)" % (self.value, self.location) |
paul@83 | 230 | else: |
paul@83 | 231 | return "Const(%r)" % self.value |
paul@83 | 232 | |
paul@83 | 233 | # Support constants as dictionary keys in order to build constant tables. |
paul@83 | 234 | |
paul@83 | 235 | def __eq__(self, other): |
paul@83 | 236 | return self.value == other.value |
paul@83 | 237 | |
paul@83 | 238 | def __hash__(self): |
paul@83 | 239 | return hash(self.value) |
paul@83 | 240 | |
paul@83 | 241 | def value_type_name(self): |
paul@83 | 242 | return "__builtins__." + self.value.__class__.__name__ |
paul@83 | 243 | |
paul@85 | 244 | class Class(NamespaceDict, Naming, Constant): |
paul@83 | 245 | |
paul@83 | 246 | "An inspected class." |
paul@83 | 247 | |
paul@83 | 248 | def __init__(self, name, parent, global_namespace=None, node=None): |
paul@83 | 249 | |
paul@83 | 250 | """ |
paul@83 | 251 | Initialise the class with the given 'name', 'parent' object, optional |
paul@83 | 252 | 'global_namespace' and optional AST 'node'. |
paul@83 | 253 | """ |
paul@83 | 254 | |
paul@83 | 255 | NamespaceDict.__init__(self, global_namespace) |
paul@83 | 256 | self.name = name |
paul@83 | 257 | self.parent = parent |
paul@83 | 258 | self.node = node |
paul@83 | 259 | |
paul@83 | 260 | # Superclasses, descendants and attributes. |
paul@83 | 261 | |
paul@83 | 262 | self.bases = [] |
paul@83 | 263 | self.descendants = set() |
paul@83 | 264 | self.instattr = set() # instance attributes |
paul@83 | 265 | self.relocated = set() # attributes which do not have the same |
paul@83 | 266 | # position as those of the same name in |
paul@83 | 267 | # some superclasses |
paul@83 | 268 | |
paul@83 | 269 | # Caches. |
paul@83 | 270 | |
paul@83 | 271 | self.all_instattr = None # cache for instance_attributes |
paul@83 | 272 | self.all_instattr_names = None # from all_instattr |
paul@83 | 273 | self.all_classattr = None # cache for all_class_attributes |
paul@83 | 274 | self.all_classattr_names = None # from all_classattr |
paul@83 | 275 | self.allattr = None # cache for all_attributes |
paul@83 | 276 | self.allattr_names = None # from allattr |
paul@83 | 277 | |
paul@83 | 278 | # Add this class to its attributes. |
paul@83 | 279 | |
paul@83 | 280 | self.set("__class__", self) |
paul@83 | 281 | |
paul@83 | 282 | # Image generation details. |
paul@83 | 283 | |
paul@83 | 284 | self.location = None |
paul@83 | 285 | self.code_location = None |
paul@83 | 286 | self.instantiator = None |
paul@83 | 287 | |
paul@83 | 288 | # Program-related details. |
paul@83 | 289 | |
paul@83 | 290 | self.stack_usage = 0 |
paul@83 | 291 | self.stack_temp_usage = 0 |
paul@83 | 292 | self.stack_local_usage = 0 |
paul@83 | 293 | |
paul@83 | 294 | def __repr__(self): |
paul@83 | 295 | if self.location is not None: |
paul@83 | 296 | return "Class(%r, %r, location=%r)" % (self.name, self.parent, self.location) |
paul@83 | 297 | else: |
paul@83 | 298 | return "Class(%r, %r)" % (self.name, self.parent) |
paul@83 | 299 | |
paul@83 | 300 | def finalise_attributes(self): |
paul@83 | 301 | |
paul@83 | 302 | "Make sure that all attributes are fully defined." |
paul@83 | 303 | |
paul@83 | 304 | self.finalise_class_attributes() |
paul@83 | 305 | self.finalise_instance_attributes() |
paul@83 | 306 | |
paul@83 | 307 | def get_instantiator(self): |
paul@83 | 308 | |
paul@83 | 309 | "Return a function which can be used to instantiate the class." |
paul@83 | 310 | |
paul@83 | 311 | if self.instantiator is None: |
paul@83 | 312 | init_method = self.all_class_attributes()["__init__"].value |
paul@83 | 313 | self.instantiator = init_method.function_from_method() |
paul@83 | 314 | return self.instantiator |
paul@83 | 315 | |
paul@83 | 316 | # Class-specific methods. |
paul@83 | 317 | |
paul@83 | 318 | def add_base(self, base): |
paul@83 | 319 | self.bases.append(base) |
paul@83 | 320 | base.add_descendant(self) |
paul@83 | 321 | |
paul@83 | 322 | def add_instance_attribute(self, name): |
paul@83 | 323 | self.instattr.add(name) |
paul@83 | 324 | |
paul@83 | 325 | def add_descendant(self, cls): |
paul@83 | 326 | self.descendants.add(cls) |
paul@83 | 327 | for base in self.bases: |
paul@83 | 328 | base.add_descendant(cls) |
paul@83 | 329 | |
paul@83 | 330 | "Return the attribute names provided by this class only." |
paul@83 | 331 | |
paul@83 | 332 | class_attribute_names = NamespaceDict.keys |
paul@83 | 333 | |
paul@83 | 334 | def class_attributes(self): |
paul@83 | 335 | |
paul@83 | 336 | "Return class attributes provided by this class only." |
paul@83 | 337 | |
paul@83 | 338 | self.finalise_class_attributes() |
paul@83 | 339 | return dict(self) |
paul@83 | 340 | |
paul@83 | 341 | def all_class_attribute_names(self): |
paul@83 | 342 | |
paul@83 | 343 | "Return the attribute names provided by classes in this hierarchy." |
paul@83 | 344 | |
paul@83 | 345 | if self.all_classattr_names is None: |
paul@83 | 346 | self.all_class_attributes() |
paul@83 | 347 | return self.all_classattr_names |
paul@83 | 348 | |
paul@83 | 349 | def all_class_attributes(self): |
paul@83 | 350 | |
paul@83 | 351 | "Return all class attributes, indicating the class which provides them." |
paul@83 | 352 | |
paul@83 | 353 | self.finalise_class_attributes() |
paul@83 | 354 | return self.all_classattr |
paul@83 | 355 | |
paul@83 | 356 | def finalise_class_attributes(self): |
paul@83 | 357 | |
paul@83 | 358 | "Make sure that the class attributes are fully defined." |
paul@83 | 359 | |
paul@83 | 360 | if self.all_classattr is None: |
paul@83 | 361 | self.all_classattr = {} |
paul@83 | 362 | clsattr = {} |
paul@83 | 363 | |
paul@83 | 364 | # Record provisional position information for attributes of this |
paul@83 | 365 | # class. |
paul@83 | 366 | |
paul@83 | 367 | for name in self.class_attributes().keys(): |
paul@83 | 368 | clsattr[name] = set() # position not yet defined |
paul@83 | 369 | |
paul@83 | 370 | reversed_bases = self.bases[:] |
paul@83 | 371 | reversed_bases.reverse() |
paul@83 | 372 | |
paul@83 | 373 | # For the bases in reverse order, acquire class attribute details. |
paul@83 | 374 | |
paul@83 | 375 | for cls in reversed_bases: |
paul@83 | 376 | for name, attr in cls.all_class_attributes().items(): |
paul@83 | 377 | self.all_classattr[name] = attr |
paul@83 | 378 | |
paul@83 | 379 | # Record previous attribute information. |
paul@83 | 380 | |
paul@83 | 381 | if clsattr.has_key(name): |
paul@83 | 382 | clsattr[name].add(attr.position) |
paul@83 | 383 | |
paul@83 | 384 | # Record class attributes provided by this class and its bases, |
paul@83 | 385 | # along with their positions. |
paul@83 | 386 | |
paul@83 | 387 | self.all_classattr.update(self.class_attributes()) |
paul@83 | 388 | |
paul@83 | 389 | if clsattr: |
paul@83 | 390 | for i, name in enumerate(self._get_position_list(clsattr)): |
paul@83 | 391 | self.all_classattr[name].position = i |
paul@83 | 392 | |
paul@83 | 393 | return self.all_classattr |
paul@83 | 394 | |
paul@83 | 395 | def instance_attribute_names(self): |
paul@83 | 396 | |
paul@83 | 397 | "Return the instance attribute names provided by the class." |
paul@83 | 398 | |
paul@83 | 399 | if self.all_instattr_names is None: |
paul@83 | 400 | self.instance_attributes() |
paul@83 | 401 | return self.all_instattr_names |
paul@83 | 402 | |
paul@83 | 403 | def instance_attributes(self): |
paul@83 | 404 | |
paul@83 | 405 | "Return instance-only attributes for instances of this class." |
paul@83 | 406 | |
paul@83 | 407 | self.finalise_instance_attributes() |
paul@83 | 408 | return self.all_instattr |
paul@83 | 409 | |
paul@83 | 410 | def finalise_instance_attributes(self): |
paul@83 | 411 | |
paul@83 | 412 | "Make sure that the instance attributes are fully defined." |
paul@83 | 413 | |
paul@83 | 414 | if self.all_instattr is None: |
paul@83 | 415 | self.all_instattr = {} |
paul@83 | 416 | instattr = {} |
paul@83 | 417 | |
paul@83 | 418 | # Record provisional position information for attributes of this |
paul@83 | 419 | # instance. |
paul@83 | 420 | |
paul@83 | 421 | for name in self.instattr: |
paul@83 | 422 | instattr[name] = set() # position not yet defined |
paul@83 | 423 | |
paul@83 | 424 | reversed_bases = self.bases[:] |
paul@83 | 425 | reversed_bases.reverse() |
paul@83 | 426 | |
paul@83 | 427 | # For the bases in reverse order, acquire instance attribute |
paul@83 | 428 | # details. |
paul@83 | 429 | |
paul@83 | 430 | for cls in reversed_bases: |
paul@83 | 431 | for name, attr in cls.instance_attributes().items(): |
paul@83 | 432 | |
paul@83 | 433 | # Record previous attribute information. |
paul@83 | 434 | |
paul@83 | 435 | if instattr.has_key(name): |
paul@83 | 436 | instattr[name].add(attr.position) |
paul@83 | 437 | |
paul@83 | 438 | # Cache the attributes by converting the positioned attributes into |
paul@83 | 439 | # a dictionary. |
paul@83 | 440 | |
paul@83 | 441 | if not instattr: |
paul@83 | 442 | self.all_instattr = {} |
paul@83 | 443 | else: |
paul@83 | 444 | self.all_instattr = self._get_attributes(instattr) |
paul@83 | 445 | |
paul@83 | 446 | self.all_instattr_names = self.all_instattr.keys() |
paul@83 | 447 | |
paul@83 | 448 | return self.all_instattr |
paul@83 | 449 | |
paul@83 | 450 | def _get_position_list(self, positions): |
paul@83 | 451 | |
paul@83 | 452 | """ |
paul@83 | 453 | Return a list of attribute names for the given 'positions' mapping from |
paul@83 | 454 | names to positions, indicating the positions of the attributes in the |
paul@83 | 455 | final instance structure. |
paul@83 | 456 | """ |
paul@83 | 457 | |
paul@83 | 458 | position_items = positions.items() |
paul@83 | 459 | namearray = [None] * len(position_items) |
paul@83 | 460 | |
paul@83 | 461 | # Get the positions in ascending order of list size, with lists |
paul@83 | 462 | # of the same size ordered according to their smallest position |
paul@83 | 463 | # value. |
paul@83 | 464 | |
paul@83 | 465 | position_items.sort(self._cmp_positions) |
paul@83 | 466 | |
paul@83 | 467 | # Get the names in position order. |
paul@83 | 468 | |
paul@83 | 469 | held = [] |
paul@83 | 470 | |
paul@83 | 471 | for name, pos in position_items: |
paul@83 | 472 | pos = list(pos) |
paul@83 | 473 | pos.sort() |
paul@83 | 474 | if pos and pos[0] < len(namearray) and namearray[pos[0]] is None: |
paul@83 | 475 | namearray[pos[0]] = name |
paul@83 | 476 | else: |
paul@83 | 477 | if pos: |
paul@83 | 478 | self.relocated.add(name) |
paul@83 | 479 | held.append((name, pos)) |
paul@83 | 480 | |
paul@83 | 481 | for i, attr in enumerate(namearray): |
paul@83 | 482 | if attr is None: |
paul@83 | 483 | name, pos = held.pop() |
paul@83 | 484 | namearray[i] = name |
paul@83 | 485 | |
paul@83 | 486 | #print self.name, positions |
paul@83 | 487 | #print "->", namearray |
paul@83 | 488 | return namearray |
paul@83 | 489 | |
paul@83 | 490 | def _get_attributes(self, positions): |
paul@83 | 491 | |
paul@83 | 492 | """ |
paul@83 | 493 | For the given 'positions' mapping from names to positions, return a |
paul@83 | 494 | dictionary mapping names to Attr instances incorporating information |
paul@83 | 495 | about their positions in the final instance structure. |
paul@83 | 496 | """ |
paul@83 | 497 | |
paul@83 | 498 | d = {} |
paul@83 | 499 | for i, name in enumerate(self._get_position_list(positions)): |
paul@83 | 500 | d[name] = Attr(i, Instance(), name, None) |
paul@83 | 501 | return d |
paul@83 | 502 | |
paul@83 | 503 | def _cmp_positions(self, a, b): |
paul@83 | 504 | |
paul@83 | 505 | "Compare name plus position list operands 'a' and 'b'." |
paul@83 | 506 | |
paul@83 | 507 | name_a, list_a = a |
paul@83 | 508 | name_b, list_b = b |
paul@83 | 509 | if len(list_a) < len(list_b): |
paul@83 | 510 | return -1 |
paul@83 | 511 | elif len(list_a) > len(list_b): |
paul@83 | 512 | return 1 |
paul@83 | 513 | elif not list_a: |
paul@83 | 514 | return 0 |
paul@83 | 515 | else: |
paul@83 | 516 | return cmp(min(list_a), min(list_b)) |
paul@83 | 517 | |
paul@83 | 518 | def all_attribute_names(self): |
paul@83 | 519 | |
paul@83 | 520 | """ |
paul@83 | 521 | Return the names of all attributes provided by instances of this class. |
paul@83 | 522 | """ |
paul@83 | 523 | |
paul@83 | 524 | self.allattr_names = self.allattr_names or self.all_attributes().keys() |
paul@83 | 525 | return self.allattr_names |
paul@83 | 526 | |
paul@83 | 527 | def all_attributes(self): |
paul@83 | 528 | |
paul@83 | 529 | """ |
paul@83 | 530 | Return all attributes for an instance, indicating either the class which |
paul@83 | 531 | provides them or that the instance itself provides them. |
paul@83 | 532 | """ |
paul@83 | 533 | |
paul@83 | 534 | if self.allattr is None: |
paul@83 | 535 | self.allattr = {} |
paul@83 | 536 | self.allattr.update(self.all_class_attributes()) |
paul@83 | 537 | for name, attr in self.instance_attributes().items(): |
paul@83 | 538 | if self.allattr.has_key(name): |
paul@83 | 539 | print "Instance attribute %r in %r overrides class attribute." % (name, self) |
paul@83 | 540 | self.allattr[name] = attr |
paul@83 | 541 | return self.allattr |
paul@83 | 542 | |
paul@85 | 543 | class Function(NamespaceDict, Naming, Constant): |
paul@83 | 544 | |
paul@83 | 545 | "An inspected function." |
paul@83 | 546 | |
paul@83 | 547 | def __init__(self, name, parent, argnames, defaults, has_star, has_dstar, global_namespace=None, node=None): |
paul@83 | 548 | |
paul@83 | 549 | """ |
paul@83 | 550 | Initialise the function with the given 'name', 'parent', list of |
paul@83 | 551 | 'argnames', list of 'defaults', the 'has_star' flag (indicating the |
paul@83 | 552 | presence of a * parameter), the 'has_dstar' flag (indicating the |
paul@83 | 553 | presence of a ** parameter), optional 'global_namespace', and optional |
paul@83 | 554 | AST 'node'. |
paul@83 | 555 | """ |
paul@83 | 556 | |
paul@83 | 557 | NamespaceDict.__init__(self, global_namespace) |
paul@83 | 558 | self.name = name |
paul@83 | 559 | self.parent = parent |
paul@83 | 560 | self.argnames = argnames |
paul@83 | 561 | self.defaults = defaults |
paul@83 | 562 | self.has_star = has_star |
paul@83 | 563 | self.has_dstar = has_dstar |
paul@83 | 564 | self.node = node |
paul@83 | 565 | |
paul@83 | 566 | # Initialise the positional names. |
paul@83 | 567 | |
paul@83 | 568 | self.positional_names = self.argnames[:] |
paul@83 | 569 | if has_dstar: |
paul@83 | 570 | self.dstar_name = self.positional_names[-1] |
paul@83 | 571 | del self.positional_names[-1] |
paul@83 | 572 | if has_star: |
paul@83 | 573 | self.star_name = self.positional_names[-1] |
paul@83 | 574 | del self.positional_names[-1] |
paul@83 | 575 | |
paul@83 | 576 | # Initialise default storage. |
paul@83 | 577 | # NOTE: This must be initialised separately due to the reliance on node |
paul@83 | 578 | # NOTE: visiting. |
paul@83 | 579 | |
paul@83 | 580 | self.default_attrs = [] |
paul@83 | 581 | |
paul@83 | 582 | # Caches. |
paul@83 | 583 | |
paul@83 | 584 | self.localnames = None # cache for locals |
paul@83 | 585 | |
paul@83 | 586 | # Add parameters to the namespace. |
paul@83 | 587 | |
paul@83 | 588 | self._add_parameters(argnames) |
paul@83 | 589 | |
paul@83 | 590 | # Image generation details. |
paul@83 | 591 | |
paul@83 | 592 | self.location = None |
paul@83 | 593 | self.code_location = None |
paul@83 | 594 | |
paul@83 | 595 | # Program-related details. |
paul@83 | 596 | |
paul@83 | 597 | self.stack_usage = 0 |
paul@83 | 598 | self.stack_temp_usage = 0 |
paul@83 | 599 | self.stack_local_usage = 0 |
paul@83 | 600 | |
paul@83 | 601 | def _add_parameters(self, argnames): |
paul@83 | 602 | for name in argnames: |
paul@83 | 603 | if isinstance(name, tuple): |
paul@83 | 604 | self._add_parameters(name) |
paul@83 | 605 | else: |
paul@83 | 606 | self.set(name, None) |
paul@83 | 607 | |
paul@83 | 608 | def __repr__(self): |
paul@83 | 609 | if self.location is not None: |
paul@83 | 610 | return "Function(%r, %r, %r, %r, %r, %r, location=%r)" % ( |
paul@83 | 611 | self.name, self.parent, self.argnames, self.defaults, self.has_star, self.has_dstar, self.location |
paul@83 | 612 | ) |
paul@83 | 613 | else: |
paul@83 | 614 | return "Function(%r, %r, %r, %r, %r, %r)" % ( |
paul@83 | 615 | self.name, self.parent, self.argnames, self.defaults, self.has_star, self.has_dstar |
paul@83 | 616 | ) |
paul@83 | 617 | |
paul@83 | 618 | def store_default(self, value): |
paul@83 | 619 | attr = Attr(None, self, None, value) |
paul@83 | 620 | attr.update(value, 1) |
paul@83 | 621 | self.default_attrs.append(attr) |
paul@83 | 622 | |
paul@83 | 623 | def make_global(self, name): |
paul@83 | 624 | if name not in self.argnames and not self.has_key(name): |
paul@83 | 625 | self.globals.add(name) |
paul@83 | 626 | else: |
paul@83 | 627 | raise InspectError(self.full_name(), self.node, "Name %r is global and local in %r" % (name, self.full_name())) |
paul@83 | 628 | |
paul@83 | 629 | def parameters(self): |
paul@83 | 630 | |
paul@83 | 631 | """ |
paul@83 | 632 | Return a dictionary mapping parameter names to their position in the |
paul@83 | 633 | parameter list. |
paul@83 | 634 | """ |
paul@83 | 635 | |
paul@83 | 636 | parameters = {} |
paul@83 | 637 | for i, name in enumerate(self.argnames): |
paul@83 | 638 | parameters[name] = i |
paul@83 | 639 | return parameters |
paul@83 | 640 | |
paul@83 | 641 | def all_locals(self): |
paul@83 | 642 | |
paul@83 | 643 | "Return a dictionary mapping names to local and parameter details." |
paul@83 | 644 | |
paul@83 | 645 | return dict(self) |
paul@83 | 646 | |
paul@83 | 647 | def locals(self): |
paul@83 | 648 | |
paul@83 | 649 | "Return a dictionary mapping names to local details." |
paul@83 | 650 | |
paul@83 | 651 | if self.localnames is None: |
paul@83 | 652 | self.localnames = {} |
paul@83 | 653 | self.localnames.update(self.all_locals()) |
paul@83 | 654 | for name in self.argnames: |
paul@83 | 655 | del self.localnames[name] |
paul@83 | 656 | return self.localnames |
paul@83 | 657 | |
paul@83 | 658 | def is_method(self): |
paul@83 | 659 | |
paul@83 | 660 | "Return whether this function is a method." |
paul@83 | 661 | |
paul@83 | 662 | return isinstance(self.parent, Class) |
paul@83 | 663 | |
paul@83 | 664 | def is_relocated(self, name): |
paul@83 | 665 | |
paul@83 | 666 | """ |
paul@83 | 667 | Determine whether the given attribute 'name' is relocated for instances |
paul@83 | 668 | having this function as a method. |
paul@83 | 669 | """ |
paul@83 | 670 | |
paul@83 | 671 | for cls in self.parent.descendants: |
paul@83 | 672 | if name in cls.relocated: |
paul@83 | 673 | return 1 |
paul@83 | 674 | return 0 |
paul@83 | 675 | |
paul@83 | 676 | def finalise_attributes(self): |
paul@83 | 677 | |
paul@83 | 678 | """ |
paul@83 | 679 | Make sure all attributes (locals) are fully defined. Note that locals |
paul@83 | 680 | are not attributes in the sense of class, module or instance attributes. |
paul@83 | 681 | Defaults are also finalised by this method. |
paul@83 | 682 | """ |
paul@83 | 683 | |
paul@83 | 684 | for i, default in enumerate(self.default_attrs): |
paul@83 | 685 | default.position = i |
paul@83 | 686 | |
paul@83 | 687 | i = None |
paul@83 | 688 | for i, name in enumerate(self.argnames): |
paul@83 | 689 | self[name].position = i |
paul@83 | 690 | |
paul@83 | 691 | if i is not None: |
paul@83 | 692 | j = i |
paul@83 | 693 | else: |
paul@83 | 694 | j = 0 |
paul@83 | 695 | |
paul@83 | 696 | i = -1 |
paul@83 | 697 | for i, attr in enumerate(self.locals().values()): |
paul@83 | 698 | attr.position = i + j |
paul@83 | 699 | |
paul@83 | 700 | self.stack_local_usage = i + 1 |
paul@83 | 701 | |
paul@83 | 702 | def function_from_method(self): |
paul@83 | 703 | |
paul@83 | 704 | "Make a function from a method." |
paul@83 | 705 | |
paul@83 | 706 | function = Function(self.name, self.parent, self.argnames[1:], self.defaults, |
paul@83 | 707 | self.has_star, self.has_dstar, self.global_namespace, self.node) |
paul@83 | 708 | function.default_attrs = self.default_attrs |
paul@83 | 709 | return function |
paul@83 | 710 | |
paul@85 | 711 | class UnresolvedName(NamespaceDict, Constant): |
paul@83 | 712 | |
paul@83 | 713 | "A module, class or function which was mentioned but could not be imported." |
paul@83 | 714 | |
paul@83 | 715 | def __init__(self, name, parent_name, global_namespace=None): |
paul@83 | 716 | NamespaceDict.__init__(self, global_namespace) |
paul@83 | 717 | self.name = name |
paul@83 | 718 | self.parent_name = parent_name |
paul@83 | 719 | |
paul@83 | 720 | def all_class_attributes(self): |
paul@83 | 721 | return {} |
paul@83 | 722 | |
paul@83 | 723 | def instance_attributes(self): |
paul@83 | 724 | return {} |
paul@83 | 725 | |
paul@83 | 726 | def __repr__(self): |
paul@83 | 727 | return "UnresolvedName(%r, %r)" % (self.name, self.parent_name) |
paul@83 | 728 | |
paul@83 | 729 | def full_name(self): |
paul@83 | 730 | if self.name is not None: |
paul@83 | 731 | return self.parent_name + "." + self.name |
paul@83 | 732 | else: |
paul@83 | 733 | return self.parent_name |
paul@83 | 734 | |
paul@85 | 735 | class Module(NamespaceDict, Constant): |
paul@83 | 736 | |
paul@83 | 737 | "An inspected module's core details." |
paul@83 | 738 | |
paul@83 | 739 | def __init__(self, name): |
paul@83 | 740 | NamespaceDict.__init__(self, self) |
paul@83 | 741 | self.name = name |
paul@83 | 742 | |
paul@83 | 743 | # Original location details. |
paul@83 | 744 | |
paul@83 | 745 | self.node = None |
paul@83 | 746 | |
paul@83 | 747 | # Complete lists of classes and functions. |
paul@83 | 748 | |
paul@83 | 749 | self.all_objects = set() |
paul@83 | 750 | |
paul@83 | 751 | # Keyword records. |
paul@83 | 752 | |
paul@83 | 753 | self.keyword_names = set() |
paul@83 | 754 | |
paul@83 | 755 | # Image generation details. |
paul@83 | 756 | |
paul@83 | 757 | self.location = None |
paul@83 | 758 | self.code_location = None |
paul@83 | 759 | |
paul@83 | 760 | # Program-related details. |
paul@83 | 761 | |
paul@83 | 762 | self.stack_usage = 0 |
paul@83 | 763 | self.stack_temp_usage = 0 |
paul@83 | 764 | self.stack_local_usage = 0 |
paul@83 | 765 | |
paul@83 | 766 | def full_name(self): |
paul@83 | 767 | return self.name |
paul@83 | 768 | |
paul@83 | 769 | def __repr__(self): |
paul@83 | 770 | if self.location is not None: |
paul@83 | 771 | return "Module(%r, location=%r)" % (self.name, self.location) |
paul@83 | 772 | else: |
paul@83 | 773 | return "Module(%r)" % self.name |
paul@83 | 774 | |
paul@83 | 775 | # Attribute methods. |
paul@83 | 776 | |
paul@83 | 777 | "Return the module attribute names provided by the module." |
paul@83 | 778 | |
paul@83 | 779 | module_attribute_names = NamespaceDict.keys |
paul@83 | 780 | |
paul@83 | 781 | def module_attributes(self): |
paul@83 | 782 | |
paul@83 | 783 | "Return a dictionary mapping names to module attributes." |
paul@83 | 784 | |
paul@83 | 785 | return dict(self) |
paul@83 | 786 | |
paul@83 | 787 | # vim: tabstop=4 expandtab shiftwidth=4 |