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