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