1 #!/usr/bin/env python 2 3 """ 4 Inspect source files, obtaining details of classes and attributes. 5 6 Copyright (C) 2007, 2008, 2009, 2010, 2011 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 21 -------- 22 23 The results of inspecting a module are as follows: 24 25 Constants 26 --------- 27 28 All constants identified within the code shall be registered. 29 30 Classes 31 ------- 32 33 All global classes shall be registered; local classes (within functions) or 34 nested classes (within classes) are not currently registered. 35 36 Base classes must be detected and constant. 37 38 All classes without bases are made to inherit from __builtins__.object in order 39 to support some standard methods. 40 41 Functions 42 --------- 43 44 All functions and lambda definitions shall be registered. 45 46 Namespaces 47 ---------- 48 49 Modules define their own "global" namespace, within which classes, functions 50 and lambda definitions establish a hierarchy of namespaces. 51 52 Only local, global and built-in namespaces are recognised; closures are not 53 supported. 54 55 Assignments 56 ----------- 57 58 Name assignment and attribute assignment involving modules and classes cause 59 names to be associated with values within namespaces. 60 61 Any assignments within loops are considered to cause the targets of such 62 assignments to provide non-constant values. 63 64 Assignments to names are only really considered to cause the targets of such 65 assignments to provide constant values if the targets reside in the module 66 namespace or in class namespaces, subject to the above conditions. 67 68 Assignments to names within functions are not generally considered to cause the 69 targets of such assignments to provide constant values since functions can be 70 invoked many times with different inputs. This affects particularly the 71 definition of functions or lambdas within functions. However, there may be 72 benefits in considering a local to be constant within a single invocation. 73 """ 74 75 from micropython.common import * 76 from micropython.data import * 77 import compiler.ast 78 79 # Program visitors. 80 81 class InspectedModule(ASTVisitor, Module): 82 83 """ 84 An inspected module, providing core details via the Module superclass, but 85 capable of being used as an AST visitor. 86 """ 87 88 def __init__(self, name, importer): 89 90 """ 91 Initialise this visitor with a module 'name' and an 'importer' which is 92 used to provide access to other modules when required. 93 """ 94 95 ASTVisitor.__init__(self) 96 Module.__init__(self, name, importer) 97 self.visitor = self 98 99 # Import machinery links. 100 101 self.optimisations = self.importer.optimisations 102 self.builtins = self.importer.modules.get("__builtins__") 103 self.loaded = 0 104 105 # Current expression state. 106 107 self.expr = None 108 self.in_assignment = 0 # For slice and subscript handling. 109 110 # Namespace state. 111 112 self.in_method = 0 # Find instance attributes in all methods. 113 self.in_function = 0 # Note function presence, affecting definitions. 114 self.in_loop = 0 # Note loop "membership", affecting assignments. 115 self.namespaces = [] 116 self.functions = [] 117 118 def parse(self, filename): 119 120 "Parse the file having the given 'filename'." 121 122 module = compiler.parseFile(filename) 123 self.process(module) 124 125 def process(self, module): 126 127 "Process the given 'module'." 128 129 self.astnode = module 130 131 # Add __name__ to the namespace. 132 133 self.store("__name__", self._visitConst(self.full_name())) 134 135 # First, visit module-level code, recording global names. 136 137 processed = self.dispatch(module) 138 139 # Then, for each function, detect and record globals declared in those 140 # functions. 141 142 for node, namespaces in self.functions: 143 self.process_globals(node) 144 145 self.finalise_attribute_usage() 146 147 # Then, visit each function, recording other names. 148 149 for node, namespaces in self.functions: 150 self._visitFunctionBody(node, namespaces) 151 namespaces[-1].finalise_attribute_usage() 152 153 # Add references to other modules declared using the __all__ global. 154 155 if self.has_key("__all__"): 156 all = self["__all__"] 157 if isinstance(all, compiler.ast.List): 158 for n in all.nodes: 159 self.store(n.value, self.importer.add_module(self.name + "." + n.value)) 160 161 return processed 162 163 def process_globals(self, node): 164 165 """ 166 Within the given 'node', process global declarations, adjusting the 167 module namespace. 168 """ 169 170 for n in node.getChildNodes(): 171 if isinstance(n, compiler.ast.Global): 172 for name in n.names: 173 if not self.has_key(name): 174 self[name] = make_instance() 175 else: 176 self.process_globals(n) 177 178 def vacuum(self): 179 180 """ 181 Vacuum the module namespace, removing unreferenced objects and unused 182 names. 183 """ 184 185 if self.should_optimise_unused_objects(): 186 self.vacuum_object(self) 187 188 all_objects = list(self.all_objects) 189 190 for obj in all_objects: 191 self.vacuum_object(obj) 192 193 def vacuum_object(self, obj, delete_all=0): 194 195 "Vacuum the given object 'obj'." 196 197 # Get all constant objects in apparent use. 198 199 if delete_all: 200 obj_objects = set() 201 else: 202 obj_objects = [] 203 for name, attr in obj.items_for_vacuum(): 204 205 # Get constant objects for attributes in use. 206 207 if self.importer.uses_attribute(obj.full_name(), name) and \ 208 attr is not None and attr.is_constant(): 209 210 value = attr.get_value() 211 obj_objects.append(value) 212 213 # Now vacuum unused attributes and objects not in use. 214 215 for name, attr in obj.items_for_vacuum(): 216 217 # Only consider deleting entire unused objects or things accessible 218 # via names which are never used. 219 220 if delete_all or not self.importer.uses_attribute(obj.full_name(), name): 221 obj.vacuum_item(name) 222 223 # Delete any unambiguous attribute value. Such values can only 224 # have been defined within the object and therefore are not 225 # redefined by other code regions. 226 227 if attr is not None and attr.is_constant(): 228 value = attr.get_value() 229 230 # The value must have this object as a parent. 231 # However, it must not be shared by several names. 232 233 if value is not obj and value.parent is obj and \ 234 value in self.all_objects and value not in obj_objects: 235 236 self.all_objects.remove(value) 237 238 # Delete class contents and lambdas from functions. 239 240 self.vacuum_object(value, 1) 241 242 def unfinalise(self): 243 244 "Reset finalised information for the module." 245 246 for obj in self.all_objects: 247 obj.unfinalise_attributes() 248 249 def finalise(self, objtable): 250 251 "Finalise the module." 252 253 for obj in self.all_objects: 254 obj.finalise(objtable) 255 256 self.finalise_users(objtable) 257 258 def add_object(self, obj, any_scope=0): 259 260 """ 261 Record 'obj' if non-local or if the optional 'any_scope' is set to a 262 true value. 263 """ 264 265 if any_scope or not (self.namespaces and isinstance(self.namespaces[-1], Function)): 266 self.all_objects.add(obj) 267 268 # Optimisation tests. 269 270 def should_optimise_unused_objects(self): 271 return "unused_objects" in self.optimisations 272 273 # Namespace methods. 274 275 def in_class(self, namespaces=None): 276 namespaces = namespaces or self.namespaces 277 return len(namespaces) > 1 and isinstance(namespaces[-2], Class) 278 279 def store(self, name, obj): 280 281 "Record attribute or local 'name', storing 'obj'." 282 283 # Store in the module. 284 285 if not self.namespaces: 286 if self.in_loop and self.used_in_scope(name, "builtins"): 287 raise InspectError("Name %r already used as a built-in." % name) 288 else: 289 self.set(name, obj, not self.in_loop) 290 291 # Or store locally. 292 293 else: 294 locals = self.namespaces[-1] 295 296 if self.in_loop and locals.used_in_scope(name, "global") and not name in locals.globals: 297 raise InspectError("Name %r already used as global." % name) 298 elif self.in_loop and locals.used_in_scope(name, "builtins"): 299 raise InspectError("Name %r already used as a built-in." % name) 300 else: 301 locals.set(name, obj, not self.in_loop) 302 303 def store_lambda(self, obj): 304 305 "Store a lambda function 'obj'." 306 307 self.add_object(obj) 308 self.get_namespace().add_lambda(obj) 309 310 def store_module_attr(self, name, module): 311 312 """ 313 Record module attribute 'name' in the given 'module' using the current 314 expression. 315 """ 316 317 module.set(name, self.expr, 0) 318 self.use_specific_attribute(module.full_name(), name) 319 320 def store_class_attr(self, name, cls): 321 322 """ 323 Record class attribute 'name' in the given class 'cls' using the current 324 expression. 325 """ 326 327 cls.set(name, self.expr, 0) 328 self.use_specific_attribute(cls.full_name(), name) 329 330 def store_instance_attr(self, name): 331 332 "Record instance attribute 'name' in the current class." 333 334 if self.in_method: 335 336 # Current namespace is the function. 337 # Previous namespace is the class. 338 339 cls = self.namespaces[-2] 340 cls.add_instance_attribute(name) 341 342 # NOTE: The instance attribute, although defined in a specific 343 # NOTE: class, obviously appears in all descendant classes. 344 345 self.use_specific_attribute(cls.full_name(), name) 346 347 def get_namespace(self): 348 349 "Return the parent (or most recent) namespace currently exposed." 350 351 return (self.namespaces[-1:] or [self])[0] 352 353 def use_name(self, name, node=None, value=None): 354 355 """ 356 Use the given 'name' within the current namespace/unit, either in 357 conjunction with a particular object (if 'node' is specified and not 358 None) or unconditionally. 359 """ 360 361 if node is not None and isinstance(node, compiler.ast.Name): 362 self.use_attribute(node.name, name, value) 363 364 # For general name usage, declare usage of the given name from this 365 # particular unit. 366 367 else: 368 unit = self.get_namespace() 369 self.importer.use_name(name, unit.full_name(), value) 370 371 def use_constant(self, const): 372 373 "Use the given 'const' within the current namespace/unit." 374 375 unit = self.get_namespace() 376 self.importer.use_constant(const, unit.full_name()) 377 378 # Attribute usage methods. 379 # These are convenience methods which refer to the specific namespace's 380 # implementation of these operations. 381 382 def new_branchpoint(self, loop_node=None): 383 self.get_namespace()._new_branchpoint(loop_node) 384 385 def new_branch(self, node): 386 self.get_namespace()._new_branch(node) 387 388 def abandon_branch(self): 389 self.get_namespace()._abandon_branch() 390 391 def suspend_broken_branch(self): 392 self.get_namespace()._suspend_broken_branch() 393 394 def suspend_continuing_branch(self): 395 self.get_namespace()._suspend_continuing_branch() 396 397 def shelve_branch(self): 398 self.get_namespace()._shelve_branch() 399 400 def merge_branches(self): 401 self.get_namespace()._merge_branches() 402 403 def resume_broken_branches(self): 404 self.get_namespace()._resume_broken_branches() 405 406 def resume_continuing_branches(self): 407 self.get_namespace()._resume_continuing_branches() 408 409 def define_attribute_user(self, node): 410 411 """ 412 Define 'node' as the user of attributes, indicating the point where the 413 user is defined. 414 """ 415 416 self.get_namespace()._define_attribute_user(node) 417 418 def use_attribute(self, name, attrname, value=None): 419 420 """ 421 Note usage on the attribute user 'name' of the attribute 'attrname', 422 noting an assignment if 'value' is specified. 423 """ 424 425 return self.get_namespace()._use_attribute(name, attrname, value) 426 427 def use_specific_attribute(self, objname, attrname, from_name=None): 428 429 """ 430 Note usage on the object having the given 'objname' of the attribute 431 'attrname'. If 'objname' is None, the current namespace is chosen as the 432 object providing the attribute. 433 """ 434 435 return self.get_namespace()._use_specific_attribute(objname, attrname, from_name) 436 437 # Visitor methods. 438 439 def default(self, node, *args): 440 raise InspectError("Node class %r is not supported." % node.__class__) 441 442 def NOP(self, node): 443 for n in node.getChildNodes(): 444 self.dispatch(n) 445 446 def NOP_ABANDON(self, node): 447 self.NOP(node) 448 self.abandon_branch() 449 450 def TEST_NOP(self, node): 451 self.use_name("__bool__", node) 452 self.NOP(node) 453 454 def OP(self, node): 455 for n in node.getChildNodes(): 456 self.dispatch(n) 457 return make_instance() 458 459 def TEST_OP(self, node): 460 self.use_name("__bool__", node) 461 return self.OP(node) 462 463 # Generic support for classes of operations. 464 465 def _ensureOperators(self): 466 attr, scope, namespace = self._get_with_scope("$operator") 467 if attr is None: 468 module = self.importer.load("operator") 469 self["$operator"] = module 470 else: 471 module = attr.get_value() 472 return module 473 474 def _visitOperator(self, node, operator_name=None): 475 476 "Accounting method for the operator 'node'." 477 478 operator_module = self._ensureOperators() 479 operator_fn = operator_functions[operator_name or node.__class__.__name__] 480 self.use_specific_attribute(operator_module.full_name(), operator_fn) 481 return self.OP(node) 482 483 _visitBinary = _visitOperator 484 _visitUnary = _visitOperator 485 486 def _visitAttr(self, expr, attrname, node): 487 488 """ 489 Process the attribute provided by the given 'expr' with the given 490 'attrname' and involving the given 'node'. 491 """ 492 493 # Attempt to identify the nature of the attribute. 494 495 if isinstance(expr, Attr): 496 value = expr.get_value() 497 498 # Get the attribute and record its usage. 499 500 if isinstance(value, (Class, Module)): 501 502 # Check for class.__class__. 503 504 if attrname == "__class__" and isinstance(value, Class): 505 attr = type_class 506 else: 507 attr = value.get(attrname) or make_instance() 508 self.use_specific_attribute(value.full_name(), attrname) 509 510 elif isinstance(value, UnresolvedName): 511 attr = UnresolvedName(attrname, value.full_name(), self) 512 513 # The actual attribute is not readily identifiable and is assumed 514 # to be an instance. 515 516 else: 517 attr = make_instance() 518 519 # Note usage of the attribute where a local is involved. 520 521 self._visitAttrUser(expr, attrname, node) 522 523 # No particular attribute has been identified, thus a general instance 524 # is assumed. 525 526 else: 527 attr = make_instance() 528 self.use_name(attrname, node) 529 530 return attr 531 532 def _visitAttrUser(self, expr, attrname, node, value=None): 533 534 """ 535 Note usage of the attribute provided by 'expr' with the given 'attrname' 536 where a local is involved, annotating the given 'node'. If the optional 537 'value' is given, note an assignment for future effects on attributes 538 where such attributes are inferred from the usage. 539 """ 540 541 # Access to attribute via a local. 542 543 if expr.parent is self.get_namespace(): 544 545 # NOTE: Revisiting of nodes may occur for loops. 546 547 if not hasattr(node, "_attrusers"): 548 node._attrusers = set() 549 550 node._attrusers.update(self.use_attribute(expr.name, attrname, value)) 551 node._username = expr.name 552 else: 553 self.use_name(attrname, node.expr, value) 554 555 def _visitConst(self, value): 556 557 """ 558 Register the constant given by 'value', if necessary, returning the 559 resulting object. The type name is noted as being used, thus preserving 560 the class in any generated program. 561 """ 562 563 self.use_specific_attribute("__builtins__", self.importer.get_constant_type_name(value)) 564 const = self.importer.make_constant(value) 565 self.use_constant(const) 566 return const 567 568 def _visitFunction(self, node, name): 569 570 """ 571 Return a function object for the function defined by 'node' with the 572 given 'name'. If a lambda expression is being visited, 'name' should be 573 None. 574 """ 575 576 # Define the function object. 577 578 function = Function( 579 name, 580 self.get_namespace(), 581 node.argnames, 582 node.defaults, 583 (node.flags & 4 != 0), 584 (node.flags & 8 != 0), 585 self.in_loop or self.in_function, 586 self, 587 node 588 ) 589 590 self.add_object(function, any_scope=1) 591 592 # Make a back reference from the node for code generation. 593 594 node.unit = function 595 596 # Process the defaults. 597 598 for n in node.defaults: 599 self.expr = self.dispatch(n) 600 function.store_default(self.expr) 601 602 # Note attribute usage where tuple parameters are involved. 603 604 if function.tuple_parameters(): 605 self.use_name("__getitem__", node) 606 607 # Record the namespace context of the function for later processing. 608 609 self.functions.append((node, self.namespaces + [function])) 610 611 # Store the function. 612 613 if name is not None: 614 self.store(name, function) 615 else: 616 self.store_lambda(function) 617 618 # Test the defaults and assess whether an dynamic object will result. 619 620 function.make_dynamic() 621 return function 622 623 def _visitFunctionBody(self, node, namespaces): 624 625 "Enter the function." 626 627 # Current namespace is the function. 628 # Previous namespace is the class. 629 630 if self.in_class(namespaces): 631 self.in_method = 1 632 633 in_function = self.in_function 634 in_loop = self.in_loop 635 self.in_function = 1 636 self.in_loop = 0 637 638 self.namespaces = namespaces 639 self.dispatch(node.code) 640 641 self.in_loop = in_loop 642 self.in_function = in_function 643 self.in_method = 0 644 645 # Specific handler methods. 646 647 visitAdd = _visitBinary 648 649 visitAnd = TEST_OP 650 651 visitAssert = NOP 652 653 def visitAssign(self, node): 654 self.expr = self.dispatch(node.expr) 655 self.in_assignment = 1 656 for n in node.nodes: 657 self.dispatch(n) 658 self.in_assignment = 0 659 660 def visitAssAttr(self, node): 661 expr = self.dispatch(node.expr) 662 attrname = node.attrname 663 664 # Record the attribute on the presumed target. 665 666 if isinstance(expr, Attr): 667 value = expr.get_value() 668 669 if expr.name == "self": 670 self.store_instance_attr(attrname) 671 self.use_attribute(expr.name, attrname, value) # NOTE: Impose constraints on the type given the hierarchy. 672 self._visitAttrUser(expr, attrname, node, self.expr) 673 674 elif isinstance(value, Module): 675 self.store_module_attr(attrname, value) 676 print "Warning: attribute %r of module %r set outside the module." % (node.attrname, expr.get_value().name) 677 678 elif isinstance(value, Class): 679 self.store_class_attr(attrname, value) 680 681 # Note usage of the attribute where a local is involved. 682 683 else: 684 self._visitAttrUser(expr, attrname, node, self.expr) 685 686 else: 687 self.use_name(attrname, node) 688 689 def visitAssList(self, node): 690 691 # Declare names which will be used by generated code. 692 693 self.use_name("__getitem__", node) 694 695 # Process the assignment. 696 697 for i, n in enumerate(node.nodes): 698 self.dispatch(n) 699 self._visitConst(i) # for __getitem__(i) at run-time 700 701 def visitAssName(self, node): 702 if hasattr(node, "flags") and node.flags == "OP_DELETE": 703 print "Warning: deletion of attribute %r in %r is not supported." % (node.name, self.full_name()) 704 #raise InspectError("Deletion of attribute %r is not supported." % node.name) 705 706 self.store(node.name, self.expr) 707 self.define_attribute_user(node) 708 709 # Ensure the presence of the given name in this namespace. 710 # NOTE: Consider not registering assignments involving methods, since 711 # NOTE: this is merely creating aliases for such methods. 712 713 if isinstance(self.get_namespace(), (Class, Module)): 714 if not isinstance(self.expr, Attr) or not isinstance(self.expr.get_value(), Function): 715 self.use_specific_attribute(None, node.name) 716 else: 717 fn = self.expr.get_value() 718 ns = self.get_namespace().full_name() 719 self.use_specific_attribute(fn.parent.full_name(), fn.name, "%s.%s" % (ns, node.name)) 720 721 visitAssTuple = visitAssList 722 723 def visitAugAssign(self, node): 724 725 # Accounting. 726 727 operator_fn = operator_functions.get(node.op) 728 operator_module = self._ensureOperators() 729 self.use_specific_attribute(operator_module.full_name(), operator_fn) 730 731 # Process the assignment. 732 733 self.expr = self.dispatch(node.expr) 734 735 # NOTE: Similar to micropython.ast handler code. 736 # NOTE: Slices and subscripts are supported by __setitem__(slice) and 737 # NOTE: not __setslice__. 738 739 if isinstance(node.node, compiler.ast.Name): 740 self.visitAssName(node.node) 741 elif isinstance(node.node, compiler.ast.Getattr): 742 self.visitAssAttr(node.node) 743 else: 744 self.use_specific_attribute("__builtins__", "slice") 745 self.use_name("__setitem__", node) 746 747 visitBackquote = OP 748 749 visitBitand = _visitBinary 750 751 visitBitor = _visitBinary 752 753 visitBitxor = _visitBinary 754 755 def visitBreak(self, node): 756 self.NOP(node) 757 self.suspend_broken_branch() 758 759 visitCallFunc = OP 760 761 def visitClass(self, node): 762 763 """ 764 Register the class at the given 'node' subject to the restrictions 765 mentioned in the module docstring. 766 """ 767 768 if self.namespaces: 769 print "Warning: class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) 770 return 771 else: 772 if self.in_loop: 773 print "Warning: class %r in %r defined in a loop." % (node.name, self.full_name()) 774 775 cls = get_class(node.name, self.get_namespace(), self, node) 776 777 # Make a back reference from the node for code generation. 778 779 node.unit = cls 780 781 # Process base classes in the context of the class's namespace. 782 # This confines references to such classes to the class instead of 783 # the namespace in which it is defined. 784 785 self.namespaces.append(cls) 786 787 # Visit the base class expressions, attempting to find concrete 788 # definitions of classes. 789 790 for base in node.bases: 791 expr = self.dispatch(base) 792 793 # Each base class must be constant and known at compile-time. 794 795 if isinstance(expr, Attr): 796 if expr.assignments != 1: 797 raise InspectError("Base class %r for %r is not constant: %r" % (base, cls.full_name(), expr)) 798 else: 799 cls.add_base(expr.get_value()) 800 801 # Where no expression value is available, the base class is 802 # not identifiable. 803 804 else: 805 raise InspectError("Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name())) 806 807 # NOTE: Potentially dubious measure to permit __init__ availability. 808 # If no bases exist, adopt the 'object' class. 809 810 if not node.bases and not (self.name == "__builtins__" and node.name == "object") : 811 expr = self.dispatch(compiler.ast.Name("object")) 812 cls.add_base(expr.get_value()) 813 814 # Make an entry for the class in the parent namespace. 815 816 self.namespaces.pop() 817 self.store(node.name, cls) 818 self.add_object(cls) 819 820 # Process the class body in its own namespace. 821 # Add __name__ to the namespace. 822 823 self.namespaces.append(cls) 824 self.store("__name__", self._visitConst(node.name)) 825 self.dispatch(node.code) 826 self.namespaces.pop() 827 828 cls.finalise_attribute_usage() 829 return cls 830 831 def visitCompare(self, node): 832 833 # Accounting. 834 # NOTE: Replicates some code in micropython.ast.visitCompare. 835 836 self.use_name("__bool__", node) 837 838 this_node = node 839 840 for op in node.ops: 841 op_name, next_node = op 842 843 # Define name/attribute usage. 844 # Get the applicable operation. 845 846 operator_fn = operator_functions.get(op_name) 847 848 # For operators, reference the specific function involved. 849 850 if operator_fn is not None: 851 operator_module = self._ensureOperators() 852 self.use_specific_attribute(operator_module.full_name(), operator_fn) 853 854 # Define __contains__ usage on the next node. 855 856 elif op_name.endswith("in"): 857 self.use_name("__contains__", next_node) 858 859 this_node = next_node 860 861 return self.OP(node) 862 863 def visitConst(self, node): 864 return self._visitConst(node.value) 865 866 def visitContinue(self, node): 867 self.NOP(node) 868 self.suspend_continuing_branch() 869 870 visitDecorators = NOP 871 872 visitDict = OP 873 874 visitDiscard = NOP 875 876 visitDiv = _visitBinary 877 878 visitEllipsis = NOP 879 880 visitExec = NOP 881 882 visitExpression = OP 883 884 visitFloorDiv = _visitBinary 885 886 def visitFor(self, node): 887 self.new_branchpoint(node) 888 889 # Declare names which will be used by generated code. 890 891 self.use_name("__iter__", node.list) 892 self.use_name("next") 893 self.use_name("StopIteration") 894 895 in_loop = self.in_loop 896 self.in_loop = 1 897 self.dispatch(node.list) 898 899 # NOTE: Could generate AST nodes for the actual operations instead of 900 # NOTE: manually generating code in micropython.ast. 901 902 self.expr = make_instance() # each element is a result of a function call 903 self.dispatch(node.assign) 904 905 # Enter the loop. 906 # Propagate attribute usage to branches. 907 908 self.new_branch(node) 909 self.dispatch(node.body) 910 911 self.resume_continuing_branches() 912 913 self.shelve_branch() 914 915 self.in_loop = in_loop 916 917 # A null branch is used to record a path around the loop. 918 919 self.new_branch(node.else_ or NullBranch()) 920 self.shelve_branch() 921 922 self.merge_branches() 923 924 # The else clause is evaluated outside any branch. 925 926 if node.else_ is not None: 927 self.dispatch(node.else_) 928 929 # Any suspended branches from the loop can now be resumed. 930 931 self.resume_broken_branches() 932 933 def visitFrom(self, node): 934 module = self.importer.load(node.modname, 1) 935 936 #if module is None: 937 # print "Warning:", node.modname, "not imported." 938 939 for name, alias in node.names: 940 if name != "*": 941 if module is not None and module.has_key(name): 942 attr = module[name] 943 self.store(alias or name, attr) 944 self.use_specific_attribute(module.full_name(), name) 945 if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: 946 self.importer.load(attr.get_value().name) 947 948 # Support the import of names from missing modules. 949 950 else: 951 self.store(alias or name, UnresolvedName(name, node.modname, self)) 952 else: 953 if module is not None: 954 for n in module.keys(): 955 attr = module[n] 956 self.store(n, attr) 957 self.use_specific_attribute(module.full_name(), n) 958 if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: 959 self.importer.load(attr.get_value().name) 960 961 def visitFunction(self, node): 962 return self._visitFunction(node, node.name) 963 964 visitGenExpr = OP 965 966 visitGenExprFor = NOP 967 968 visitGenExprIf = NOP 969 970 visitGenExprInner = NOP 971 972 def visitGetattr(self, node): 973 expr = self.dispatch(node.expr) 974 attrname = node.attrname 975 return self._visitAttr(expr, attrname, node) 976 977 def visitGlobal(self, node): 978 if self.namespaces: 979 for name in node.names: 980 ns = self.namespaces[-1] 981 if not ns.make_global(name): 982 raise InspectError("Name %r is global and local in %r" % (name, ns.full_name())) 983 984 # The name is recorded in an earlier process. 985 986 def visitIf(self, node): 987 self.use_name("__bool__", node) 988 self.new_branchpoint() 989 990 # Propagate attribute usage to branches. 991 992 for test, body in node.tests: 993 self.dispatch(test) 994 995 self.new_branch(body) 996 self.dispatch(body) 997 self.shelve_branch() 998 999 # Maintain a branch for the else clause. 1000 1001 self.new_branch(node.else_ or NullBranch()) 1002 if node.else_ is not None: 1003 self.dispatch(node.else_) 1004 self.shelve_branch() 1005 1006 self.merge_branches() 1007 1008 def visitIfExp(self, node): 1009 self.use_name("__bool__", node) 1010 self.new_branchpoint() 1011 1012 # Propagate attribute usage to branches. 1013 1014 self.dispatch(node.test) 1015 1016 self.new_branch(node.then) 1017 self.dispatch(node.then) 1018 self.shelve_branch() 1019 1020 self.new_branch(node.else_) 1021 self.dispatch(node.else_) 1022 self.shelve_branch() 1023 1024 self.merge_branches() 1025 return make_instance() # either outcome is possible 1026 1027 def visitImport(self, node): 1028 for name, alias in node.names: 1029 if alias is not None: 1030 module = self.importer.load(name, 1) or UnresolvedName(None, name, self) 1031 self.store(alias, module) 1032 else: 1033 module = self.importer.load(name) or UnresolvedName(None, name.split(".")[0], self) 1034 self.store(name.split(".")[0], module) 1035 1036 visitInvert = _visitUnary 1037 1038 def visitKeyword(self, node): 1039 self.dispatch(node.expr) 1040 self._visitConst(node.name) 1041 self.keyword_names.add(node.name) 1042 1043 def visitLambda(self, node): 1044 fn = self._visitFunction(node, None) 1045 self.use_specific_attribute(None, fn.name) 1046 return fn 1047 1048 visitLeftShift = _visitBinary 1049 1050 def visitList(self, node): 1051 self.use_specific_attribute("__builtins__", "list") 1052 return self.OP(node) 1053 1054 def visitListComp(self, node): 1055 for qual in node.quals: 1056 self.dispatch(qual) 1057 self.dispatch(node.expr) 1058 return make_instance() 1059 1060 def visitListCompFor(self, node): 1061 self.new_branchpoint() 1062 1063 # Declare names which will be used by generated code. 1064 1065 self.use_name("__iter__", node.list) 1066 self.use_name("next") 1067 1068 in_loop = self.in_loop 1069 self.in_loop = 1 1070 self.dispatch(node.list) 1071 1072 # NOTE: Could generate AST nodes for the actual operations instead of 1073 # NOTE: manually generating code in micropython.ast. 1074 1075 self.expr = make_instance() # each element is a result of a function call 1076 self.dispatch(node.assign) 1077 1078 # Enter the loop. 1079 # Propagate attribute usage to branches. 1080 1081 self.new_branch(node) 1082 1083 for if_ in node.ifs: 1084 self.dispatch(if_) 1085 1086 self.shelve_branch() 1087 self.in_loop = in_loop 1088 1089 self.merge_branches() 1090 1091 visitListCompIf = TEST_NOP 1092 1093 visitMod = _visitBinary 1094 1095 def visitModule(self, node): 1096 1097 # Make a back reference from the node for code generation. 1098 1099 node.unit = self 1100 return self.dispatch(node.node) 1101 1102 visitMul = _visitBinary 1103 1104 def visitName(self, node): 1105 return self.get_namespace().get_using_node(node.name, node) or make_instance() 1106 1107 visitNot = TEST_OP 1108 1109 visitOr = TEST_OP 1110 1111 visitPass = NOP 1112 1113 visitPower = _visitBinary 1114 1115 def _visitPrint(self, node, function_name): 1116 self.NOP(node) 1117 self.use_specific_attribute("__builtins__", function_name) 1118 1119 def visitPrint(self, node): 1120 self._visitPrint(node, "_print") 1121 1122 def visitPrintnl(self, node): 1123 self._visitPrint(node, "_printnl") 1124 1125 visitRaise = NOP_ABANDON 1126 1127 visitReturn = NOP_ABANDON 1128 1129 visitRightShift = _visitBinary 1130 1131 def visitSlice(self, node): 1132 return self._visitOperator(node, self.in_assignment and "AssSlice" or "Slice") 1133 1134 visitSliceobj = OP 1135 1136 def visitStmt(self, node): 1137 for n in node.nodes: 1138 self.dispatch(n) 1139 1140 visitSub = _visitBinary 1141 1142 def visitSubscript(self, node): 1143 return self._visitOperator(node, self.in_assignment and "AssSubscript" or "Subscript") 1144 1145 def visitTryExcept(self, node): 1146 self.dispatch(node.body) 1147 1148 self.new_branchpoint() 1149 1150 for name, var, n in node.handlers: 1151 self.new_branch(node) 1152 1153 # Establish the local for the handler. 1154 1155 if var is not None: 1156 self.dispatch(var) 1157 if n is not None: 1158 self.dispatch(n) 1159 1160 self.shelve_branch() 1161 1162 self.new_branch(node.else_ or NullBranch()) 1163 if node.else_ is not None: 1164 self.dispatch(node.else_) 1165 self.shelve_branch() 1166 1167 self.merge_branches() 1168 1169 visitTryFinally = NOP 1170 1171 visitTuple = OP 1172 1173 visitUnaryAdd = _visitUnary 1174 1175 visitUnarySub = _visitUnary 1176 1177 def visitWhile(self, node): 1178 self.use_name("__bool__", node) 1179 self.new_branchpoint(node) 1180 1181 # Propagate attribute usage to branches. 1182 1183 in_loop = self.in_loop 1184 self.in_loop = 1 1185 1186 # The test is evaluated initially and again in the loop. 1187 1188 self.dispatch(node.test) 1189 1190 self.new_branch(node) 1191 self.dispatch(node.body) 1192 1193 self.resume_continuing_branches() 1194 1195 self.dispatch(node.test) 1196 self.shelve_branch() 1197 1198 self.in_loop = in_loop 1199 1200 # A null branch is used to record a path around the loop. 1201 1202 self.new_branch(node.else_ or NullBranch()) 1203 self.shelve_branch() 1204 1205 self.merge_branches() 1206 1207 # The else clause is evaluated outside any branch. 1208 1209 if node.else_ is not None: 1210 self.dispatch(node.else_) 1211 1212 # Any suspended branches from the loop can now be resumed. 1213 1214 self.resume_broken_branches() 1215 1216 visitWith = NOP 1217 1218 visitYield = NOP 1219 1220 # vim: tabstop=4 expandtab shiftwidth=4