paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | The micropython package for processing Python source code. The code originates |
paul@0 | 5 | from the simplify package but has had various details related to that package |
paul@0 | 6 | removed. |
paul@0 | 7 | |
paul@491 | 8 | Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 9 | |
paul@0 | 10 | This program is free software; you can redistribute it and/or modify it under |
paul@0 | 11 | the terms of the GNU General Public License as published by the Free Software |
paul@0 | 12 | Foundation; either version 3 of the License, or (at your option) any later |
paul@0 | 13 | version. |
paul@0 | 14 | |
paul@0 | 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@0 | 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@0 | 17 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@0 | 18 | details. |
paul@0 | 19 | |
paul@0 | 20 | You should have received a copy of the GNU General Public License along with |
paul@0 | 21 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@0 | 22 | |
paul@0 | 23 | -------- |
paul@0 | 24 | |
paul@280 | 25 | To use this module, an importer should be constructed. Here, the standard path |
paul@280 | 26 | for module searching is employed: |
paul@0 | 27 | |
paul@0 | 28 | importer = Importer(sys.path) |
paul@0 | 29 | |
paul@189 | 30 | To generate programs, the above importer should be supplied in the |
paul@189 | 31 | initialisation of a program instance, and then various methods are called: |
paul@189 | 32 | |
paul@189 | 33 | program = Program(importer) |
paul@189 | 34 | image = program.get_raw_image() |
paul@189 | 35 | |
paul@189 | 36 | Such importer and program objects are the most convenient mechanism through |
paul@189 | 37 | which the functionality of the micropython package may be accessed. |
paul@0 | 38 | """ |
paul@0 | 39 | |
paul@470 | 40 | from micropython.data import * |
paul@555 | 41 | from micropython.errors import * |
paul@622 | 42 | from micropython.objectset import ObjectSet |
paul@554 | 43 | from micropython.program import Location |
paul@555 | 44 | from micropython.types import * |
paul@17 | 45 | import micropython.ast |
paul@479 | 46 | import micropython.native |
paul@154 | 47 | import micropython.opt |
paul@0 | 48 | import micropython.inspect |
paul@7 | 49 | import micropython.table |
paul@491 | 50 | import bisect |
paul@149 | 51 | import os |
paul@598 | 52 | import sys |
paul@228 | 53 | |
paul@0 | 54 | try: |
paul@0 | 55 | set |
paul@0 | 56 | except NameError: |
paul@0 | 57 | from sets import Set as set |
paul@0 | 58 | |
paul@175 | 59 | class Program: |
paul@0 | 60 | |
paul@175 | 61 | "This class supports the generation of a program image." |
paul@0 | 62 | |
paul@154 | 63 | supported_optimisations = micropython.opt.Optimiser.supported_optimisations |
paul@63 | 64 | |
paul@175 | 65 | def __init__(self, importer, optimisations=None): |
paul@0 | 66 | |
paul@0 | 67 | """ |
paul@175 | 68 | Initialise the program representation with an 'importer' which is able |
paul@175 | 69 | to locate and load Python modules. |
paul@35 | 70 | |
paul@167 | 71 | The optional 'optimisations' cause certain techniques to be used in |
paul@167 | 72 | reducing program size and improving program efficiency. |
paul@0 | 73 | """ |
paul@0 | 74 | |
paul@175 | 75 | self.importer = importer |
paul@175 | 76 | self.optimisations = optimisations or set() |
paul@479 | 77 | self.native = micropython.native.NativeLibrary(self) |
paul@0 | 78 | |
paul@60 | 79 | # Remember the tables once generated. |
paul@60 | 80 | |
paul@60 | 81 | self.objtable = None |
paul@60 | 82 | self.paramtable = None |
paul@60 | 83 | |
paul@93 | 84 | # Main program information. |
paul@93 | 85 | |
paul@93 | 86 | self.code = None |
paul@93 | 87 | self.code_location = None |
paul@93 | 88 | |
paul@625 | 89 | # A record of nodes for which no attribute target could be found. |
paul@625 | 90 | |
paul@625 | 91 | self.unknown_target_nodes = [] |
paul@626 | 92 | self.independent_target_nodes = [] |
paul@625 | 93 | |
paul@175 | 94 | def get_importer(self): |
paul@175 | 95 | return self.importer |
paul@137 | 96 | |
paul@169 | 97 | # Access to finalised program information. |
paul@169 | 98 | |
paul@353 | 99 | def finalise(self): |
paul@353 | 100 | |
paul@353 | 101 | "Finalise the program." |
paul@353 | 102 | |
paul@581 | 103 | self.importer.complete_modules() |
paul@581 | 104 | |
paul@353 | 105 | # Need the tables to finalise. |
paul@353 | 106 | |
paul@353 | 107 | objtable = self.get_object_table() |
paul@353 | 108 | self.get_parameter_table() |
paul@353 | 109 | |
paul@353 | 110 | self.importer.vacuum(objtable) |
paul@353 | 111 | |
paul@353 | 112 | # Now remove unneeded things from the tables. |
paul@353 | 113 | |
paul@437 | 114 | objtable = self.get_object_table(reset=1) |
paul@353 | 115 | self.get_parameter_table(reset=1) |
paul@353 | 116 | |
paul@437 | 117 | self.importer.finalise(objtable) |
paul@437 | 118 | |
paul@167 | 119 | def get_image(self, with_builtins=0): |
paul@9 | 120 | |
paul@265 | 121 | """ |
paul@265 | 122 | Return the program image including built-in objects if 'with_builtins' |
paul@265 | 123 | is specified and set to a true value. |
paul@265 | 124 | """ |
paul@9 | 125 | |
paul@137 | 126 | if self.code is not None: |
paul@137 | 127 | return self.code |
paul@137 | 128 | |
paul@280 | 129 | # Optimise and regenerate the object table. |
paul@280 | 130 | |
paul@353 | 131 | self.finalise() |
paul@173 | 132 | self.code = [] |
paul@18 | 133 | |
paul@82 | 134 | # Append constants to the image. |
paul@82 | 135 | |
paul@265 | 136 | for const in self.importer.constants(): |
paul@173 | 137 | self.code.append(const) |
paul@82 | 138 | |
paul@479 | 139 | # Generate each module. |
paul@479 | 140 | |
paul@175 | 141 | last_module = self.importer.modules_ordered[-1] |
paul@139 | 142 | |
paul@175 | 143 | for module in self.importer.modules_ordered: |
paul@431 | 144 | suppress_builtins = not with_builtins and module.name in ("__builtins__", "native") |
paul@15 | 145 | |
paul@55 | 146 | # Position the module in the image and make a translation. |
paul@55 | 147 | |
paul@167 | 148 | trans = micropython.ast.Translation(module, self) |
paul@55 | 149 | |
paul@37 | 150 | # Add header details. |
paul@37 | 151 | |
paul@173 | 152 | self.code.append(module) |
paul@37 | 153 | |
paul@15 | 154 | # Append module attributes to the image. |
paul@15 | 155 | |
paul@9 | 156 | attributes = module.module_attributes() |
paul@173 | 157 | self.code += module.attributes_as_list() |
paul@9 | 158 | |
paul@15 | 159 | # Append classes and functions to the image. |
paul@15 | 160 | |
paul@9 | 161 | for obj in module.all_objects: |
paul@470 | 162 | if isinstance(obj, Class): |
paul@54 | 163 | |
paul@22 | 164 | # Add header details. |
paul@22 | 165 | |
paul@173 | 166 | self.code.append(obj) |
paul@15 | 167 | |
paul@15 | 168 | # Append class attributes to the image. |
paul@15 | 169 | |
paul@9 | 170 | attributes = obj.class_attributes() |
paul@173 | 171 | self.code += obj.attributes_as_list() |
paul@9 | 172 | |
paul@203 | 173 | # Omit built-in function code where requested. |
paul@203 | 174 | |
paul@253 | 175 | if suppress_builtins and obj.astnode.doc is None: |
paul@203 | 176 | continue |
paul@203 | 177 | |
paul@137 | 178 | # Generate the instantiator/initialiser. |
paul@137 | 179 | # Append the function code to the image. |
paul@15 | 180 | |
paul@137 | 181 | code = trans.get_instantiator_code(obj) |
paul@173 | 182 | self.code += code |
paul@137 | 183 | |
paul@137 | 184 | # Class-level code is generated separately at the module |
paul@137 | 185 | # level, and the code location is set within the code |
paul@137 | 186 | # generation process for the module. |
paul@9 | 187 | |
paul@470 | 188 | elif isinstance(obj, Function): |
paul@15 | 189 | |
paul@22 | 190 | # Add header details. |
paul@22 | 191 | |
paul@173 | 192 | self.code.append(obj) |
paul@9 | 193 | |
paul@66 | 194 | # Append any default values to the image. |
paul@331 | 195 | # Only do this for functions which are not dynamic. |
paul@66 | 196 | |
paul@331 | 197 | if not obj.is_dynamic(): |
paul@173 | 198 | self.code += obj.default_attrs |
paul@66 | 199 | |
paul@180 | 200 | # Omit built-in function code where requested. |
paul@180 | 201 | |
paul@253 | 202 | if suppress_builtins and obj.astnode.doc is None: |
paul@180 | 203 | pass |
paul@180 | 204 | |
paul@15 | 205 | # Append the function code to the image. |
paul@15 | 206 | |
paul@180 | 207 | else: |
paul@180 | 208 | code = trans.get_code(obj) |
paul@180 | 209 | self.code += code |
paul@180 | 210 | |
paul@180 | 211 | # Omit built-in module code where requested. |
paul@180 | 212 | |
paul@205 | 213 | if suppress_builtins: |
paul@180 | 214 | pass |
paul@15 | 215 | |
paul@15 | 216 | # Append the module top-level code to the image. |
paul@15 | 217 | |
paul@180 | 218 | else: |
paul@242 | 219 | code = trans.get_module_code() |
paul@180 | 220 | self.code += code |
paul@15 | 221 | |
paul@479 | 222 | # Generate the native library once we know how much of it is used. |
paul@479 | 223 | |
paul@479 | 224 | self.code += self.native.get_native_code() |
paul@479 | 225 | |
paul@173 | 226 | return self.code |
paul@173 | 227 | |
paul@265 | 228 | def get_raw_image(self, architecture=None, with_builtins=0): |
paul@173 | 229 | |
paul@173 | 230 | "Return the raw image representation of the program." |
paul@173 | 231 | |
paul@265 | 232 | architecture = architecture or micropython.rsvp |
paul@265 | 233 | |
paul@173 | 234 | self.get_image(with_builtins) |
paul@173 | 235 | |
paul@173 | 236 | objtable = self.get_object_table() |
paul@173 | 237 | paramtable = self.get_parameter_table() |
paul@173 | 238 | |
paul@173 | 239 | # Position the objects. |
paul@173 | 240 | |
paul@173 | 241 | pos = 0 |
paul@215 | 242 | |
paul@173 | 243 | for item in self.code: |
paul@265 | 244 | arch_item = architecture.get_object(item) |
paul@180 | 245 | |
paul@265 | 246 | # Get the raw version for the architecture. |
paul@183 | 247 | |
paul@265 | 248 | if arch_item is not None: |
paul@412 | 249 | pos = arch_item.set_location(pos, objtable, with_builtins) |
paul@173 | 250 | else: |
paul@173 | 251 | pos += 1 |
paul@173 | 252 | |
paul@173 | 253 | # Generate the raw code. |
paul@173 | 254 | |
paul@173 | 255 | self.raw_code = [] |
paul@173 | 256 | |
paul@173 | 257 | for item in self.code: |
paul@265 | 258 | arch_item = architecture.get_object(item) |
paul@173 | 259 | |
paul@265 | 260 | # Get the raw version for the architecture. |
paul@139 | 261 | |
paul@265 | 262 | if arch_item is not None: |
paul@562 | 263 | arch_item.finalise_location(with_builtins) |
paul@265 | 264 | self.raw_code += arch_item.as_raw(objtable, paramtable, with_builtins) |
paul@562 | 265 | arch_item.finalise_body_location(with_builtins) |
paul@173 | 266 | else: |
paul@173 | 267 | self.raw_code.append(item) |
paul@173 | 268 | |
paul@173 | 269 | # Fix the module locations. |
paul@173 | 270 | |
paul@175 | 271 | for module in self.importer.modules_ordered: |
paul@173 | 272 | |
paul@431 | 273 | if not with_builtins and module.name in ("__builtins__", "native"): |
paul@173 | 274 | continue |
paul@173 | 275 | |
paul@175 | 276 | self.code_location = self.importer.modules["__main__"].code_location |
paul@173 | 277 | return self.raw_code |
paul@173 | 278 | |
paul@280 | 279 | def get_object_table(self, reset=0): |
paul@7 | 280 | |
paul@7 | 281 | "Return a table with details of attributes for classes and modules." |
paul@7 | 282 | |
paul@280 | 283 | if self.objtable is None or reset: |
paul@171 | 284 | |
paul@132 | 285 | t = self.objtable = micropython.table.ObjectTable() |
paul@470 | 286 | |
paul@470 | 287 | # First, get all active modules and classes. |
paul@470 | 288 | |
paul@470 | 289 | all_objects = set() |
paul@470 | 290 | |
paul@175 | 291 | for module in self.importer.get_modules(): |
paul@470 | 292 | all_objects.add(module) |
paul@470 | 293 | for obj in module.all_objects: |
paul@470 | 294 | if isinstance(obj, Class): |
paul@470 | 295 | all_objects.add(obj) |
paul@470 | 296 | |
paul@470 | 297 | # Then, visit the modules and classes. |
paul@470 | 298 | |
paul@470 | 299 | for obj in all_objects: |
paul@418 | 300 | |
paul@418 | 301 | # Add module attributes and module identity information. |
paul@418 | 302 | |
paul@470 | 303 | if isinstance(obj, Module): |
paul@470 | 304 | full_name = obj.full_name() |
paul@490 | 305 | attributes = {"#" + full_name : obj} |
paul@470 | 306 | attributes.update(obj.module_attributes()) |
paul@470 | 307 | t.add(full_name, attributes) |
paul@135 | 308 | |
paul@135 | 309 | # Add class and instance attributes for all classes, together |
paul@135 | 310 | # with descendant information. |
paul@135 | 311 | |
paul@470 | 312 | elif isinstance(obj, Class): |
paul@470 | 313 | |
paul@628 | 314 | full_name = obj.full_name() |
paul@628 | 315 | |
paul@470 | 316 | # Prevent ambiguous classes. |
paul@243 | 317 | |
paul@628 | 318 | #if obj.module.has_key(obj.name) and obj.module[obj.name].defines_ambiguous_class(): |
paul@628 | 319 | # raise TableGenerationError, "Class %r in module %r is ambiguously defined." % (obj.name, obj.module.full_name()) |
paul@470 | 320 | |
paul@470 | 321 | # Define a table entry for the class. |
paul@243 | 322 | |
paul@490 | 323 | attributes = {"#" + full_name : obj} |
paul@470 | 324 | attributes.update(obj.all_attributes()) |
paul@470 | 325 | |
paul@470 | 326 | # Filter out unused classes. |
paul@243 | 327 | |
paul@470 | 328 | for name, descendant in obj.all_descendants().items(): |
paul@470 | 329 | if descendant in all_objects: |
paul@491 | 330 | attributes["#" + name] = descendant |
paul@243 | 331 | |
paul@628 | 332 | # Merge ambiguous classes. |
paul@628 | 333 | |
paul@628 | 334 | if t.has(full_name): |
paul@628 | 335 | old_attributes = t.get(full_name) |
paul@628 | 336 | old_attributes.update(attributes) |
paul@628 | 337 | attributes = old_attributes |
paul@628 | 338 | |
paul@470 | 339 | t.add(full_name, attributes) |
paul@60 | 340 | |
paul@60 | 341 | return self.objtable |
paul@7 | 342 | |
paul@286 | 343 | def get_parameter_table(self, reset=0): |
paul@7 | 344 | |
paul@7 | 345 | "Return a table with details of parameters for functions and methods." |
paul@7 | 346 | |
paul@60 | 347 | # Need the object table to get at class details. |
paul@60 | 348 | |
paul@286 | 349 | if self.paramtable is None or reset: |
paul@132 | 350 | t = self.paramtable = micropython.table.ParameterTable() |
paul@71 | 351 | |
paul@71 | 352 | # Visit each module, getting function and method details. |
paul@71 | 353 | |
paul@175 | 354 | for module in self.importer.get_modules(): |
paul@60 | 355 | for obj in module.all_objects: |
paul@470 | 356 | if isinstance(obj, Function): |
paul@60 | 357 | t.add(obj.full_name(), obj.parameters()) |
paul@60 | 358 | |
paul@60 | 359 | # Classes are callable, too. |
paul@61 | 360 | # Take details of the appropriate __init__ method to make an |
paul@61 | 361 | # entry for an instantiation function for the class. |
paul@60 | 362 | |
paul@470 | 363 | elif isinstance(obj, Class): |
paul@212 | 364 | t.add(obj.get_instantiator().full_name(), obj.get_instantiator().parameters()) |
paul@60 | 365 | |
paul@71 | 366 | # Filter out all parameter table entries not referenced by keyword |
paul@71 | 367 | # arguments. |
paul@71 | 368 | |
paul@71 | 369 | keyword_names = set() |
paul@71 | 370 | |
paul@175 | 371 | for module in self.importer.get_modules(): |
paul@71 | 372 | keyword_names.update(module.keyword_names) |
paul@71 | 373 | |
paul@71 | 374 | for function_name, parameters in t.table.items(): |
paul@71 | 375 | for name in parameters.keys(): |
paul@71 | 376 | if name in keyword_names: |
paul@71 | 377 | break |
paul@71 | 378 | else: |
paul@71 | 379 | del t.table[function_name] |
paul@71 | 380 | |
paul@60 | 381 | return self.paramtable |
paul@7 | 382 | |
paul@491 | 383 | def object_at(self, pos): |
paul@491 | 384 | |
paul@491 | 385 | "Return the object whose code can be found at 'pos'." |
paul@491 | 386 | |
paul@491 | 387 | i = bisect.bisect_left(self.code, Location(pos)) |
paul@491 | 388 | if i > 0: |
paul@491 | 389 | return self.code[i-1] |
paul@491 | 390 | else: |
paul@491 | 391 | return None |
paul@491 | 392 | |
paul@175 | 393 | class Importer: |
paul@175 | 394 | |
paul@175 | 395 | "An import machine, searching for and loading modules." |
paul@175 | 396 | |
paul@175 | 397 | predefined_constants = { |
paul@175 | 398 | "None" : None, |
paul@175 | 399 | "True" : True, |
paul@175 | 400 | "False" : False, |
paul@203 | 401 | #"Ellipsis" : Ellipsis, |
paul@175 | 402 | "NotImplemented" : NotImplemented |
paul@175 | 403 | } |
paul@175 | 404 | |
paul@280 | 405 | names_always_used = [ |
paul@463 | 406 | "__call__" |
paul@280 | 407 | ] |
paul@280 | 408 | |
paul@175 | 409 | def __init__(self, path=None, verbose=0, optimisations=None): |
paul@175 | 410 | |
paul@175 | 411 | """ |
paul@175 | 412 | Initialise the importer with the given search 'path' - a list of |
paul@175 | 413 | directories to search for Python modules. |
paul@175 | 414 | |
paul@175 | 415 | The optional 'verbose' parameter causes output concerning the activities |
paul@175 | 416 | of the object to be produced if set to a true value (not the default). |
paul@175 | 417 | |
paul@175 | 418 | The optional 'optimisations' cause certain techniques to be used in |
paul@175 | 419 | reducing program size and improving program efficiency. |
paul@175 | 420 | """ |
paul@175 | 421 | |
paul@175 | 422 | self.path = path or [os.getcwd()] |
paul@175 | 423 | self.verbose = verbose |
paul@175 | 424 | self.optimisations = optimisations or set() |
paul@175 | 425 | |
paul@175 | 426 | self.modules = {} |
paul@175 | 427 | self.modules_ordered = [] |
paul@175 | 428 | self.loading = set() |
paul@175 | 429 | |
paul@562 | 430 | # Importers responsible for initially importing modules. |
paul@562 | 431 | |
paul@562 | 432 | self.importers = {} |
paul@562 | 433 | |
paul@581 | 434 | # Modules involved in circular imports. |
paul@581 | 435 | |
paul@581 | 436 | self.circular_imports = set() |
paul@581 | 437 | |
paul@175 | 438 | # Constant records. |
paul@175 | 439 | |
paul@175 | 440 | self.constant_values = {} |
paul@440 | 441 | self.constants_used = set() |
paul@440 | 442 | self.constant_references = {} |
paul@175 | 443 | self.init_predefined_constants() |
paul@175 | 444 | |
paul@280 | 445 | # Attribute usage. |
paul@175 | 446 | |
paul@344 | 447 | self.attributes_used = set() |
paul@250 | 448 | self.name_references = {} |
paul@281 | 449 | self.specific_name_references = {} |
paul@403 | 450 | |
paul@403 | 451 | # Attribute coverage calculated during collection. |
paul@403 | 452 | |
paul@403 | 453 | self.inferred_name_references = {} |
paul@403 | 454 | |
paul@403 | 455 | # Attribute coverage status during collection. |
paul@403 | 456 | |
paul@289 | 457 | self.attribute_users_visited = set() |
paul@345 | 458 | self.attributes_to_visit = {} |
paul@175 | 459 | |
paul@598 | 460 | # Attribute usage type deduction failures. |
paul@598 | 461 | |
paul@598 | 462 | self.attribute_usage_failures = set() |
paul@598 | 463 | |
paul@175 | 464 | # Status information. |
paul@175 | 465 | |
paul@591 | 466 | self.completed = False |
paul@591 | 467 | self.vacuumed = False |
paul@591 | 468 | self.finalised = False |
paul@175 | 469 | |
paul@175 | 470 | def get_modules(self): |
paul@175 | 471 | |
paul@175 | 472 | "Return all modules known to the importer." |
paul@175 | 473 | |
paul@175 | 474 | return self.modules.values() |
paul@175 | 475 | |
paul@228 | 476 | def get_module(self, name): |
paul@228 | 477 | |
paul@228 | 478 | "Return the module with the given 'name'." |
paul@228 | 479 | |
paul@228 | 480 | return self.modules[name] |
paul@228 | 481 | |
paul@581 | 482 | def complete_modules(self): |
paul@581 | 483 | |
paul@581 | 484 | "Complete the processing of modules." |
paul@581 | 485 | |
paul@581 | 486 | if self.completed: |
paul@581 | 487 | return |
paul@581 | 488 | |
paul@581 | 489 | self.get_module("__builtins__").complete() |
paul@581 | 490 | self.get_module("__main__").complete() |
paul@581 | 491 | |
paul@591 | 492 | self.completed = True |
paul@581 | 493 | |
paul@175 | 494 | # General maintenance. |
paul@175 | 495 | |
paul@280 | 496 | def vacuum(self, objtable): |
paul@175 | 497 | |
paul@175 | 498 | "Tidy up the modules." |
paul@175 | 499 | |
paul@175 | 500 | if self.vacuumed: |
paul@175 | 501 | return |
paul@175 | 502 | |
paul@280 | 503 | # Complete the list of attribute names used in the program. |
paul@280 | 504 | |
paul@280 | 505 | self.collect_attributes(objtable) |
paul@280 | 506 | |
paul@175 | 507 | for name, module in self.modules.items(): |
paul@175 | 508 | if module.loaded: |
paul@175 | 509 | module.vacuum() |
paul@175 | 510 | else: |
paul@175 | 511 | del self.modules[name] |
paul@175 | 512 | |
paul@591 | 513 | self.vacuumed = True |
paul@175 | 514 | |
paul@437 | 515 | def finalise(self, objtable): |
paul@175 | 516 | |
paul@280 | 517 | "Finalise the program (which should have been vacuumed first)." |
paul@175 | 518 | |
paul@175 | 519 | if self.finalised: |
paul@175 | 520 | return |
paul@175 | 521 | |
paul@287 | 522 | # Reset any previously compiled information. |
paul@287 | 523 | |
paul@287 | 524 | for module in self.get_modules(): |
paul@287 | 525 | module.unfinalise() |
paul@287 | 526 | |
paul@287 | 527 | # Prepare module information again. |
paul@287 | 528 | |
paul@175 | 529 | for module in self.get_modules(): |
paul@437 | 530 | module.finalise(objtable) |
paul@175 | 531 | |
paul@591 | 532 | self.finalised = True |
paul@175 | 533 | |
paul@175 | 534 | # Name accounting. |
paul@175 | 535 | |
paul@404 | 536 | def use_name(self, name, from_name, value=None): |
paul@250 | 537 | |
paul@250 | 538 | """ |
paul@250 | 539 | Register the given 'name' as being used in the program from within an |
paul@404 | 540 | object with the specified 'from_name'. If the optional 'value' is given, |
paul@404 | 541 | note an assignment. |
paul@250 | 542 | """ |
paul@250 | 543 | |
paul@250 | 544 | if not self.name_references.has_key(from_name): |
paul@250 | 545 | self.name_references[from_name] = set() |
paul@280 | 546 | |
paul@404 | 547 | attrnames = ObjectSet([name]) |
paul@570 | 548 | |
paul@570 | 549 | # Note the assignment in association with the given attribute name. |
paul@570 | 550 | |
paul@565 | 551 | if value is not None: |
paul@565 | 552 | attrnames[name].add(value) |
paul@570 | 553 | |
paul@570 | 554 | # Only a single set of usage is recorded here, but other situations |
paul@570 | 555 | # may involve multiple usage observations. |
paul@570 | 556 | |
paul@382 | 557 | usage = (attrnames,) |
paul@382 | 558 | self.name_references[from_name].add((None, None, usage)) |
paul@381 | 559 | |
paul@381 | 560 | def use_names(self, user, name, usage, from_name): |
paul@280 | 561 | |
paul@280 | 562 | """ |
paul@381 | 563 | For the given attribute 'user' (which may be None if no specific user is |
paul@381 | 564 | given), register for the given 'name' the given attribute 'usage' |
paul@381 | 565 | (combinations of attribute names), noting the scope of this usage as |
paul@381 | 566 | being the program object with the specified 'from_name'. |
paul@280 | 567 | """ |
paul@280 | 568 | |
paul@280 | 569 | if not self.name_references.has_key(from_name): |
paul@280 | 570 | self.name_references[from_name] = set() |
paul@381 | 571 | |
paul@382 | 572 | self.name_references[from_name].add((user, name, usage)) |
paul@280 | 573 | |
paul@285 | 574 | def use_specific_name(self, objname, attrname, from_name): |
paul@281 | 575 | |
paul@281 | 576 | """ |
paul@285 | 577 | Register the given 'objname' (for an object) whose 'attrname' is being |
paul@285 | 578 | used in the program from within an object with the specified |
paul@285 | 579 | 'from_name'. |
paul@281 | 580 | """ |
paul@281 | 581 | |
paul@281 | 582 | if not self.specific_name_references.has_key(from_name): |
paul@281 | 583 | self.specific_name_references[from_name] = set() |
paul@285 | 584 | self.specific_name_references[from_name].add((objname, attrname)) |
paul@281 | 585 | |
paul@281 | 586 | # Name accounting products. |
paul@281 | 587 | |
paul@344 | 588 | def uses_attribute(self, objname, name): |
paul@250 | 589 | |
paul@280 | 590 | """ |
paul@344 | 591 | Return whether the attribute of the object with the given 'objname' |
paul@344 | 592 | having the given 'name' is used as an attribute in the program. |
paul@280 | 593 | """ |
paul@280 | 594 | |
paul@344 | 595 | return (objname + "." + name) in self.attributes_used |
paul@280 | 596 | |
paul@280 | 597 | def use_attribute(self, objname, name): |
paul@175 | 598 | |
paul@280 | 599 | """ |
paul@280 | 600 | Indicate that in the object with the given 'objname', the attribute of |
paul@280 | 601 | the given 'name' is used. |
paul@280 | 602 | """ |
paul@280 | 603 | |
paul@344 | 604 | self.attributes_used.add(objname + "." + name) |
paul@250 | 605 | |
paul@345 | 606 | def use_object(self, objname): |
paul@345 | 607 | |
paul@345 | 608 | "Indicate that the object with the given 'objname' is used." |
paul@345 | 609 | |
paul@345 | 610 | self.attributes_used.add(objname) |
paul@345 | 611 | |
paul@280 | 612 | def collect_attributes(self, objtable): |
paul@280 | 613 | |
paul@280 | 614 | "Collect attribute references for the entire program." |
paul@280 | 615 | |
paul@345 | 616 | # Include names which may not be explicitly used in programs. |
paul@345 | 617 | # NOTE: Potentially declare these when inspecting. |
paul@345 | 618 | |
paul@345 | 619 | for attrname in self.names_always_used: |
paul@345 | 620 | for objname in objtable.all_possible_objects([attrname]): |
paul@345 | 621 | |
paul@345 | 622 | # Record attributes of objects for potential visiting. |
paul@345 | 623 | |
paul@345 | 624 | self.add_attribute_to_visit(objname, attrname) |
paul@345 | 625 | |
paul@367 | 626 | # Visit all modules, since some may employ initialisation code which has |
paul@367 | 627 | # some kind of side-effect. |
paul@283 | 628 | |
paul@367 | 629 | for name in self.modules.keys(): |
paul@367 | 630 | self._collect_attributes(name, objtable) |
paul@283 | 631 | |
paul@345 | 632 | def add_attribute_to_visit(self, objname, attrname): |
paul@351 | 633 | |
paul@351 | 634 | """ |
paul@351 | 635 | Queue an attribute of the object with the given 'objname', having the |
paul@351 | 636 | given 'attrname', to the list for potential visiting if the specified |
paul@351 | 637 | object is actually referenced. |
paul@351 | 638 | """ |
paul@351 | 639 | |
paul@345 | 640 | if not self.attributes_to_visit.has_key(objname): |
paul@345 | 641 | self.attributes_to_visit[objname] = set() |
paul@345 | 642 | self.attributes_to_visit[objname].add(attrname) |
paul@280 | 643 | |
paul@403 | 644 | def _collect_attributes_from(self, from_name, objname, attrname, objtable): |
paul@403 | 645 | |
paul@403 | 646 | """ |
paul@403 | 647 | Record the association between 'from_name' and the attribute of |
paul@403 | 648 | 'objname' with the given 'attrname'. Then collect attributes for the |
paul@403 | 649 | referenced attribute using 'objtable'. |
paul@403 | 650 | """ |
paul@403 | 651 | |
paul@403 | 652 | if not self.inferred_name_references.has_key(from_name): |
paul@403 | 653 | self.inferred_name_references[from_name] = set() |
paul@403 | 654 | |
paul@403 | 655 | self.inferred_name_references[from_name].add((objname, attrname)) |
paul@403 | 656 | self._collect_attributes(objname + "." + attrname, objtable) |
paul@403 | 657 | |
paul@280 | 658 | def _collect_attributes(self, from_name, objtable): |
paul@250 | 659 | |
paul@280 | 660 | """ |
paul@280 | 661 | Given an object called 'from_name', find all names referenced from such |
paul@403 | 662 | an object according to the register of names, using 'objtable' to infer |
paul@403 | 663 | types. |
paul@280 | 664 | """ |
paul@280 | 665 | |
paul@289 | 666 | if from_name in self.attribute_users_visited: |
paul@280 | 667 | return |
paul@280 | 668 | |
paul@301 | 669 | self.attribute_users_visited.add(from_name) |
paul@301 | 670 | |
paul@440 | 671 | # Get constant references. |
paul@440 | 672 | |
paul@440 | 673 | for const in self.constant_references.get(from_name, []): |
paul@440 | 674 | self.constants_used.add(const) |
paul@440 | 675 | |
paul@416 | 676 | # The getattr function is a special case: it can potentially reference |
paul@416 | 677 | # any known attribute. Since accessor attributes must be known |
paul@416 | 678 | # constants, the intersection of known constants and attributes is used |
paul@416 | 679 | # to build a set of objects that might be referenced by getattr. |
paul@416 | 680 | |
paul@416 | 681 | if from_name == "__builtins__.getattr": |
paul@416 | 682 | all_attributes = set(objtable.attribute_names()) |
paul@416 | 683 | all_string_constants = set([const.get_value() for const in self.constants() if const.value_type_name() == "__builtins__.str"]) |
paul@416 | 684 | all_attribute_constants = all_attributes.intersection(all_string_constants) |
paul@416 | 685 | |
paul@416 | 686 | # Get the types supporting each attribute and visit the referenced |
paul@416 | 687 | # objects. |
paul@416 | 688 | |
paul@416 | 689 | all_objtypes = set() |
paul@416 | 690 | |
paul@416 | 691 | for attrname in all_attribute_constants: |
paul@416 | 692 | objtypes = objtable.any_possible_objects_plus_status([attrname]) |
paul@416 | 693 | all_objtypes.update(objtypes) |
paul@416 | 694 | |
paul@416 | 695 | # Attribute assignment does not take place, so an empty list of |
paul@416 | 696 | # values is given. |
paul@416 | 697 | |
paul@416 | 698 | self._collect_attributes_for_types(from_name, objtable, all_objtypes, |
paul@416 | 699 | [{attrname : []} for attrname in all_attribute_constants]) |
paul@416 | 700 | |
paul@281 | 701 | # Get name references and find possible objects which support such |
paul@281 | 702 | # combinations of attribute names. |
paul@281 | 703 | |
paul@382 | 704 | for user, name, usage in self.name_references.get(from_name, []): |
paul@381 | 705 | |
paul@381 | 706 | # Using all attribute names for a particular name, attempt to get |
paul@381 | 707 | # specific object types. |
paul@381 | 708 | |
paul@598 | 709 | all_objtypes = get_object_types_for_usage(usage, objtable, name, from_name, True, self) |
paul@525 | 710 | if not all_objtypes: |
paul@598 | 711 | all_objtypes = get_object_types_for_usage(usage, objtable, name, from_name, False, self) |
paul@382 | 712 | |
paul@466 | 713 | # Where the name through which the attributes are accessed is the |
paul@466 | 714 | # special "self" name, restrict the possibilities to types |
paul@466 | 715 | # appropriate for the method involved. |
paul@466 | 716 | |
paul@466 | 717 | if name == "self" and user and user.unit and user.unit.is_method(): |
paul@466 | 718 | cls = user.unit.parent |
paul@522 | 719 | valid_objtypes = filter_using_self(all_objtypes, cls) |
paul@466 | 720 | else: |
paul@466 | 721 | valid_objtypes = all_objtypes |
paul@466 | 722 | |
paul@437 | 723 | # Investigate the object types. |
paul@420 | 724 | |
paul@466 | 725 | self._collect_attributes_for_types(from_name, objtable, valid_objtypes, usage) |
paul@175 | 726 | |
paul@281 | 727 | # Get specific name references and visit the referenced objects. |
paul@281 | 728 | |
paul@285 | 729 | for objname, attrname in self.specific_name_references.get(from_name, []): |
paul@285 | 730 | self.use_attribute(objname, attrname) |
paul@403 | 731 | self._collect_attributes_from(from_name, objname, attrname, objtable) |
paul@281 | 732 | |
paul@332 | 733 | # Where the object has an __init__ attribute, assume that it is an |
paul@332 | 734 | # initialiser which is called at some point, and collect attributes used |
paul@332 | 735 | # in this initialiser. |
paul@332 | 736 | |
paul@332 | 737 | if "__init__" in objtable.table.get(from_name, []): |
paul@338 | 738 | self.use_attribute(from_name, "__init__") |
paul@403 | 739 | self._collect_attributes_from(from_name, from_name, "__init__", objtable) |
paul@332 | 740 | |
paul@384 | 741 | # Visit attributes on this object that were queued in case of the object |
paul@384 | 742 | # being referenced. |
paul@345 | 743 | |
paul@345 | 744 | attributes_to_visit = self.attributes_to_visit.get(from_name, []) |
paul@345 | 745 | |
paul@345 | 746 | if attributes_to_visit: |
paul@345 | 747 | del self.attributes_to_visit[from_name] |
paul@345 | 748 | |
paul@345 | 749 | for attrname in attributes_to_visit: |
paul@345 | 750 | self.use_attribute(from_name, attrname) |
paul@403 | 751 | self._collect_attributes_from(from_name, from_name, attrname, objtable) |
paul@345 | 752 | |
paul@416 | 753 | def _collect_attributes_for_types(self, from_name, objtable, objtypes, usage): |
paul@416 | 754 | |
paul@416 | 755 | """ |
paul@416 | 756 | For the unit known as 'from_name' and using the 'objtable' to validate |
paul@416 | 757 | each attribute, identify and attempt to visit attributes found for each |
paul@416 | 758 | of the suggested object types given by 'objtypes' and the 'usage' |
paul@416 | 759 | provided. |
paul@416 | 760 | """ |
paul@416 | 761 | |
paul@416 | 762 | for objname, is_static in objtypes: |
paul@416 | 763 | for attrnames in usage: |
paul@416 | 764 | for attrname, attrvalues in attrnames.items(): |
paul@416 | 765 | |
paul@416 | 766 | # Test for the presence of an attribute on the suggested |
paul@416 | 767 | # object type. |
paul@416 | 768 | |
paul@416 | 769 | try: |
paul@416 | 770 | attr = objtable.access(objname, attrname) |
paul@416 | 771 | except TableError: |
paul@507 | 772 | #print >>sys.stderr, "Warning: object type %r does not support attribute %r" % (objname, attrname) |
paul@416 | 773 | continue |
paul@416 | 774 | |
paul@416 | 775 | # Get the real identity of the attribute in order to |
paul@416 | 776 | # properly collect usage from it. |
paul@416 | 777 | |
paul@416 | 778 | parent = attr.parent |
paul@520 | 779 | |
paul@520 | 780 | # NOTE: At this point, parent should never be None. |
paul@520 | 781 | |
paul@421 | 782 | if parent is None: |
paul@421 | 783 | continue |
paul@520 | 784 | |
paul@520 | 785 | # Instances provide the same identity as the object name. |
paul@520 | 786 | |
paul@470 | 787 | elif isinstance(parent, Instance): |
paul@416 | 788 | parentname = objname |
paul@520 | 789 | |
paul@520 | 790 | # In general, the fully qualified name is obtained. |
paul@520 | 791 | |
paul@416 | 792 | else: |
paul@416 | 793 | parentname = parent.full_name() |
paul@416 | 794 | |
paul@416 | 795 | # Test for assignment. |
paul@416 | 796 | |
paul@416 | 797 | if attrvalues: |
paul@427 | 798 | |
paul@520 | 799 | # NOTE: Here, an instance can actually represent any |
paul@520 | 800 | # NOTE: kind of object. |
paul@427 | 801 | |
paul@470 | 802 | if isinstance(parent, Instance): |
paul@520 | 803 | |
paul@520 | 804 | # Get the parent object using the special |
paul@520 | 805 | # table entry. |
paul@515 | 806 | |
paul@535 | 807 | parent = objtable.get_object(objname) |
paul@515 | 808 | |
paul@520 | 809 | # Permit assignment to known instance attributes |
paul@520 | 810 | # only. |
paul@515 | 811 | |
paul@520 | 812 | if not (isinstance(parent, Class) and |
paul@520 | 813 | parent.instance_attributes().has_key(attrname)): |
paul@515 | 814 | |
paul@520 | 815 | print >>sys.stderr, "Warning: potential assignment to instance attribute %s of %s not permitted" % ( |
paul@520 | 816 | attrname, parent.full_name()) |
paul@515 | 817 | |
paul@520 | 818 | # Assignment to a known attribute is permitted. |
paul@520 | 819 | |
paul@520 | 820 | elif parent.has_key(attrname): |
paul@427 | 821 | for attrvalue in attrvalues: |
paul@427 | 822 | parent.set(attrname, attrvalue, 0) |
paul@416 | 823 | |
paul@520 | 824 | # Assignment to an unknown attribute is not permitted. |
paul@520 | 825 | |
paul@520 | 826 | else: |
paul@520 | 827 | print >>sys.stderr, "Warning: potential assignment to static attribute %s of %s not permitted" % ( |
paul@520 | 828 | attrname, parent.full_name()) |
paul@520 | 829 | |
paul@416 | 830 | # Visit attributes of objects known to be used. |
paul@416 | 831 | |
paul@416 | 832 | if parentname in self.attributes_used: |
paul@416 | 833 | self.use_attribute(parentname, attrname) |
paul@416 | 834 | self._collect_attributes_from(from_name, parentname, attrname, objtable) |
paul@416 | 835 | |
paul@416 | 836 | # Record attributes of other objects for potential visiting. |
paul@416 | 837 | |
paul@416 | 838 | else: |
paul@416 | 839 | self.add_attribute_to_visit(parentname, attrname) |
paul@416 | 840 | |
paul@598 | 841 | def add_usage_failure(self, all_attributes, unit_name, name, attrnames): |
paul@598 | 842 | |
paul@598 | 843 | """ |
paul@598 | 844 | Record a type deduction failure based on 'all_attributes' (where true |
paul@598 | 845 | indicates that all attribute names were required; false indicating that |
paul@598 | 846 | any were required) within the given 'unit_name' for the variable of the |
paul@598 | 847 | given 'name' and for the specified 'attrnames'. |
paul@598 | 848 | """ |
paul@598 | 849 | |
paul@598 | 850 | attrnames = tuple(attrnames) |
paul@598 | 851 | self.attribute_usage_failures.add((unit_name, name, attrnames, all_attributes)) |
paul@598 | 852 | |
paul@175 | 853 | # Constant accounting. |
paul@175 | 854 | |
paul@440 | 855 | def use_constant(self, const, from_name): |
paul@440 | 856 | |
paul@440 | 857 | """ |
paul@440 | 858 | Register the given 'const' as being used in the program from within an |
paul@440 | 859 | object with the specified 'from_name'. |
paul@440 | 860 | """ |
paul@440 | 861 | |
paul@440 | 862 | if not self.constant_references.has_key(from_name): |
paul@440 | 863 | self.constant_references[from_name] = set() |
paul@440 | 864 | |
paul@440 | 865 | self.constant_references[from_name].add(const) |
paul@440 | 866 | |
paul@175 | 867 | def init_predefined_constants(self): |
paul@175 | 868 | |
paul@175 | 869 | "Ensure the predefined constants." |
paul@175 | 870 | |
paul@175 | 871 | for name, value in self.predefined_constants.items(): |
paul@440 | 872 | self.constants_used.add(self.make_constant(value)) |
paul@175 | 873 | |
paul@175 | 874 | def get_predefined_constant(self, name): |
paul@175 | 875 | |
paul@175 | 876 | "Return the predefined constant for the given 'name'." |
paul@175 | 877 | |
paul@175 | 878 | return self.make_constant(self.predefined_constants[name]) |
paul@175 | 879 | |
paul@175 | 880 | def get_constant(self, value): |
paul@175 | 881 | |
paul@175 | 882 | "Return a constant for the given 'value'." |
paul@175 | 883 | |
paul@470 | 884 | const = Const(value) |
paul@176 | 885 | return self.constant_values[const] |
paul@175 | 886 | |
paul@252 | 887 | def get_constant_type_name(self, value): |
paul@415 | 888 | |
paul@415 | 889 | "Return the type name for the given constant 'value'." |
paul@415 | 890 | |
paul@252 | 891 | return value.__class__.__name__ |
paul@252 | 892 | |
paul@175 | 893 | def make_constant(self, value): |
paul@175 | 894 | |
paul@175 | 895 | "Make and return a constant for the given 'value'." |
paul@175 | 896 | |
paul@219 | 897 | # Make a constant object and return it. |
paul@219 | 898 | |
paul@470 | 899 | const = Const(value) |
paul@176 | 900 | if not self.constant_values.has_key(const): |
paul@176 | 901 | self.constant_values[const] = const |
paul@176 | 902 | return self.constant_values[const] |
paul@175 | 903 | |
paul@175 | 904 | def constants(self): |
paul@175 | 905 | |
paul@175 | 906 | "Return a list of constants." |
paul@175 | 907 | |
paul@440 | 908 | return self.constants_used |
paul@175 | 909 | |
paul@9 | 910 | # Import methods. |
paul@9 | 911 | |
paul@0 | 912 | def find_in_path(self, name): |
paul@0 | 913 | |
paul@0 | 914 | """ |
paul@0 | 915 | Find the given module 'name' in the search path, returning None where no |
paul@0 | 916 | such module could be found, or a 2-tuple from the 'find' method |
paul@0 | 917 | otherwise. |
paul@0 | 918 | """ |
paul@0 | 919 | |
paul@0 | 920 | for d in self.path: |
paul@0 | 921 | m = self.find(d, name) |
paul@0 | 922 | if m: return m |
paul@0 | 923 | return None |
paul@0 | 924 | |
paul@0 | 925 | def find(self, d, name): |
paul@0 | 926 | |
paul@0 | 927 | """ |
paul@0 | 928 | In the directory 'd', find the given module 'name', where 'name' can |
paul@0 | 929 | either refer to a single file module or to a package. Return None if the |
paul@0 | 930 | 'name' cannot be associated with either a file or a package directory, |
paul@0 | 931 | or a 2-tuple from '_find_package' or '_find_module' otherwise. |
paul@0 | 932 | """ |
paul@0 | 933 | |
paul@0 | 934 | m = self._find_package(d, name) |
paul@0 | 935 | if m: return m |
paul@0 | 936 | m = self._find_module(d, name) |
paul@0 | 937 | if m: return m |
paul@0 | 938 | return None |
paul@0 | 939 | |
paul@0 | 940 | def _find_module(self, d, name): |
paul@0 | 941 | |
paul@0 | 942 | """ |
paul@0 | 943 | In the directory 'd', find the given module 'name', returning None where |
paul@0 | 944 | no suitable file exists in the directory, or a 2-tuple consisting of |
paul@0 | 945 | None (indicating that no package directory is involved) and a filename |
paul@0 | 946 | indicating the location of the module. |
paul@0 | 947 | """ |
paul@0 | 948 | |
paul@0 | 949 | name_py = name + os.extsep + "py" |
paul@0 | 950 | filename = self._find_file(d, name_py) |
paul@0 | 951 | if filename: |
paul@0 | 952 | return None, filename |
paul@0 | 953 | return None |
paul@0 | 954 | |
paul@0 | 955 | def _find_package(self, d, name): |
paul@0 | 956 | |
paul@0 | 957 | """ |
paul@0 | 958 | In the directory 'd', find the given package 'name', returning None |
paul@0 | 959 | where no suitable package directory exists, or a 2-tuple consisting of |
paul@0 | 960 | a directory (indicating the location of the package directory itself) |
paul@0 | 961 | and a filename indicating the location of the __init__.py module which |
paul@0 | 962 | declares the package's top-level contents. |
paul@0 | 963 | """ |
paul@0 | 964 | |
paul@0 | 965 | filename = self._find_file(d, name) |
paul@0 | 966 | if filename: |
paul@0 | 967 | init_py = "__init__" + os.path.extsep + "py" |
paul@0 | 968 | init_py_filename = self._find_file(filename, init_py) |
paul@0 | 969 | if init_py_filename: |
paul@0 | 970 | return filename, init_py_filename |
paul@0 | 971 | return None |
paul@0 | 972 | |
paul@0 | 973 | def _find_file(self, d, filename): |
paul@0 | 974 | |
paul@0 | 975 | """ |
paul@0 | 976 | Return the filename obtained when searching the directory 'd' for the |
paul@0 | 977 | given 'filename', or None if no actual file exists for the filename. |
paul@0 | 978 | """ |
paul@0 | 979 | |
paul@0 | 980 | filename = os.path.join(d, filename) |
paul@0 | 981 | if os.path.exists(filename): |
paul@0 | 982 | return filename |
paul@0 | 983 | else: |
paul@0 | 984 | return None |
paul@0 | 985 | |
paul@562 | 986 | def load(self, name, return_leaf=0, importer=None): |
paul@0 | 987 | |
paul@0 | 988 | """ |
paul@0 | 989 | Load the module or package with the given 'name'. Return an object |
paul@0 | 990 | referencing the loaded module or package, or None if no such module or |
paul@0 | 991 | package exists. |
paul@562 | 992 | |
paul@562 | 993 | If the given 'importer' is specified, it will be associated with the |
paul@562 | 994 | imported module if it is responsible for importing the module for the |
paul@562 | 995 | first time. |
paul@0 | 996 | """ |
paul@0 | 997 | |
paul@363 | 998 | if return_leaf: |
paul@363 | 999 | name_for_return = name |
paul@363 | 1000 | else: |
paul@363 | 1001 | name_for_return = name.split(".")[0] |
paul@363 | 1002 | |
paul@566 | 1003 | # Loaded modules are returned immediately. |
paul@566 | 1004 | # Modules may be known but not yet loading (having been registered as |
paul@566 | 1005 | # submodules), loading, loaded, or completely unknown. |
paul@566 | 1006 | |
paul@566 | 1007 | if self.modules.has_key(name) and self.modules[name].loaded: |
paul@566 | 1008 | if self.verbose: |
paul@566 | 1009 | print >>sys.stderr, "Cached (%s)" % name |
paul@363 | 1010 | return self.modules[name_for_return] |
paul@363 | 1011 | |
paul@35 | 1012 | if self.verbose: |
paul@516 | 1013 | print >>sys.stderr, "Loading", name |
paul@0 | 1014 | |
paul@0 | 1015 | # Split the name into path components, and try to find the uppermost in |
paul@0 | 1016 | # the search path. |
paul@0 | 1017 | |
paul@0 | 1018 | path = name.split(".") |
paul@581 | 1019 | path_so_far = [] |
paul@581 | 1020 | top = module = None |
paul@0 | 1021 | |
paul@581 | 1022 | for p in path: |
paul@0 | 1023 | |
paul@581 | 1024 | # Get the module's filesystem details. |
paul@0 | 1025 | |
paul@581 | 1026 | if not path_so_far: |
paul@581 | 1027 | m = self.find_in_path(p) |
paul@1 | 1028 | else: |
paul@581 | 1029 | m = self.find(d, p) |
paul@0 | 1030 | |
paul@581 | 1031 | if not m: |
paul@581 | 1032 | if self.verbose: |
paul@581 | 1033 | print >>sys.stderr, "Not found (%s)" % p |
paul@581 | 1034 | return None # NOTE: Import error. |
paul@0 | 1035 | |
paul@581 | 1036 | # Get the module itself. |
paul@0 | 1037 | |
paul@581 | 1038 | d, filename = m |
paul@581 | 1039 | path_so_far.append(p) |
paul@581 | 1040 | module_name = ".".join(path_so_far) |
paul@581 | 1041 | submodule = self.load_from_file(filename, module_name, importer) |
paul@0 | 1042 | |
paul@581 | 1043 | if module is None: |
paul@581 | 1044 | top = submodule |
paul@581 | 1045 | else: |
paul@0 | 1046 | # Store the submodule within its parent module. |
paul@0 | 1047 | |
paul@32 | 1048 | module.set_module(p, submodule) |
paul@581 | 1049 | |
paul@581 | 1050 | module = submodule |
paul@0 | 1051 | |
paul@581 | 1052 | # Stop descending if no package was found. |
paul@545 | 1053 | |
paul@581 | 1054 | if not d: |
paul@581 | 1055 | break |
paul@545 | 1056 | |
paul@0 | 1057 | # Return either the deepest or the uppermost module. |
paul@0 | 1058 | |
paul@0 | 1059 | if return_leaf: |
paul@0 | 1060 | return module |
paul@0 | 1061 | else: |
paul@0 | 1062 | return top |
paul@0 | 1063 | |
paul@562 | 1064 | def load_from_file(self, name, module_name=None, importer=None): |
paul@0 | 1065 | |
paul@0 | 1066 | """ |
paul@0 | 1067 | Load the module with the given 'name' (which may be a full module path). |
paul@562 | 1068 | |
paul@562 | 1069 | If the given 'importer' is specified, it will be associated with the |
paul@562 | 1070 | imported module if it is responsible for importing the module for the |
paul@562 | 1071 | first time. |
paul@0 | 1072 | """ |
paul@0 | 1073 | |
paul@0 | 1074 | if module_name is None: |
paul@0 | 1075 | module_name = "__main__" |
paul@0 | 1076 | |
paul@1 | 1077 | module = self.add_module(module_name) |
paul@2 | 1078 | if not module.loaded and module not in self.loading: |
paul@2 | 1079 | self.loading.add(module) |
paul@545 | 1080 | if self.verbose: |
paul@545 | 1081 | print >>sys.stderr, "Parsing", name |
paul@2 | 1082 | module.parse(name) |
paul@545 | 1083 | if self.verbose: |
paul@545 | 1084 | print >>sys.stderr, "Loaded", name |
paul@2 | 1085 | self.loading.remove(module) |
paul@591 | 1086 | module.loaded = True |
paul@0 | 1087 | |
paul@562 | 1088 | # Record each module as imported by any importer. |
paul@562 | 1089 | |
paul@562 | 1090 | if importer: |
paul@562 | 1091 | if not self.importers.has_key(importer): |
paul@562 | 1092 | self.importers[importer] = [] |
paul@562 | 1093 | self.importers[importer].append(module) |
paul@562 | 1094 | |
paul@0 | 1095 | # Record the module. |
paul@0 | 1096 | |
paul@345 | 1097 | self.use_object(module.full_name()) |
paul@516 | 1098 | #print >>sys.stderr, "Loaded", module_name, "with namespace", module.namespace.keys() |
paul@0 | 1099 | return module |
paul@0 | 1100 | |
paul@1 | 1101 | def add_module(self, module_name): |
paul@1 | 1102 | |
paul@1 | 1103 | """ |
paul@1 | 1104 | Return the module with the given 'module_name', adding a new module |
paul@1 | 1105 | object if one does not already exist. |
paul@1 | 1106 | """ |
paul@1 | 1107 | |
paul@1 | 1108 | if not self.modules.has_key(module_name): |
paul@15 | 1109 | self.modules[module_name] = module = micropython.inspect.InspectedModule(module_name, self) |
paul@128 | 1110 | self.modules_ordered.append(module) |
paul@1 | 1111 | else: |
paul@1 | 1112 | module = self.modules[module_name] |
paul@1 | 1113 | return module |
paul@1 | 1114 | |
paul@1 | 1115 | def add_submodules(self, pathname, module): |
paul@1 | 1116 | |
paul@1 | 1117 | """ |
paul@1 | 1118 | Work around insufficient __all__ declarations and examine the directory |
paul@1 | 1119 | with the given 'pathname', adding submodules to the given 'module'. |
paul@1 | 1120 | """ |
paul@1 | 1121 | |
paul@1 | 1122 | for filename in os.listdir(pathname): |
paul@50 | 1123 | submodule, ext = os.path.splitext(filename) |
paul@579 | 1124 | if submodule == "__init__" or ext not in ("", ".py"): |
paul@50 | 1125 | continue |
paul@32 | 1126 | module.set_module(submodule, self.add_module(module.name + "." + submodule)) |
paul@1 | 1127 | |
paul@0 | 1128 | # vim: tabstop=4 expandtab shiftwidth=4 |