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