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