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