1 #!/usr/bin/env python 2 3 """ 4 Inspect source files, obtaining details of classes and attributes. 5 6 Copyright (C) 2007, 2008, 2009 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. However, there may be benefits in 71 considering a local to be constant within a single invocation. 72 """ 73 74 from micropython.common import * 75 from micropython.data import * 76 import compiler.ast 77 from compiler.visitor import ASTVisitor 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) 97 self.visitor = self 98 99 # Import machinery links. 100 101 self.importer = importer 102 self.optimisations = importer.optimisations 103 self.builtins = self.importer.modules.get("__builtins__") 104 self.loaded = 0 105 106 # Current expression state. 107 108 self.expr = None 109 110 # Namespace state. 111 112 self.in_init = 0 # Find instance attributes in __init__ methods. 113 self.in_method = 0 # Find instance attributes in all methods. 114 self.in_loop = 0 # Note loop "membership", affecting assignments. 115 self.namespaces = [] 116 self.module = None 117 self.functions = [] 118 119 def parse(self, filename): 120 121 "Parse the file having the given 'filename'." 122 123 module = compiler.parseFile(filename) 124 self.process(module) 125 126 def process(self, module): 127 128 "Process the given 'module'." 129 130 self.astnode = self.module = module 131 132 # Add __name__ to the namespace by adding an explicit assignment to the 133 # module. 134 135 module.node.nodes.insert(0, compiler.ast.Assign( 136 [compiler.ast.AssName("__name__", 0)], 137 compiler.ast.Const(self.full_name()) 138 )) 139 140 # First, visit module-level code, recording global names. 141 142 processed = self.dispatch(module) 143 144 # Then, for each function, detect and record globals declared in those 145 # functions. 146 147 for node, namespaces in self.functions: 148 self.process_globals(node) 149 150 self.finalise_namespace(self) 151 152 # Then, visit each function, recording other names. 153 154 for node, namespaces in self.functions: 155 self._visitFunctionBody(node, namespaces) 156 self.finalise_namespace(namespaces[-1]) 157 158 # Add references to other modules declared using the __all__ global. 159 160 if self.has_key("__all__"): 161 all = self["__all__"] 162 if isinstance(all, compiler.ast.List): 163 for n in all.nodes: 164 self.store(n.value, self.importer.add_module(self.name + "." + n.value)) 165 166 return processed 167 168 def process_globals(self, node): 169 170 """ 171 Within the given 'node', process global declarations, adjusting the 172 module namespace. 173 """ 174 175 for n in node.getChildNodes(): 176 if isinstance(n, compiler.ast.Global): 177 for name in n.names: 178 if not self.has_key(name): 179 self[name] = None 180 else: 181 self.process_globals(n) 182 183 def finalise_namespace(self, namespace): 184 185 "Finalise the given 'namespace'." 186 187 for names in namespace.get_all_attribute_usage(): 188 self.importer.use_names(names, namespace.full_name()) 189 190 def vacuum(self): 191 192 """ 193 Vacuum the module namespace, removing unreferenced objects and unused 194 names. 195 """ 196 197 if self.should_optimise_unused_objects(): 198 self.vacuum_object(self) 199 200 all_objects = list(self.all_objects) 201 202 for obj in all_objects: 203 if isinstance(obj, Class): 204 self.vacuum_object(obj) 205 206 def vacuum_object(self, obj, delete_all=0): 207 208 "Vacuum the given object 'obj'." 209 210 for name, attr in obj.items(): 211 212 # Only consider deleting entire unused objects or things accessible 213 # via names which are never used. 214 215 if delete_all or not self.importer.uses_attribute(obj, name): 216 del obj[name] 217 218 # Delete any unambiguous attribute value. Such values can only 219 # have been defined within the object and therefore are not 220 # redefined by other code regions. 221 222 if attr.assignments == 1: 223 value = attr.get_value() 224 225 if value is not obj and value in self.all_objects: 226 self.all_objects.remove(value) 227 228 # Delete class contents. 229 230 if isinstance(value, Class): 231 self.vacuum_object(value, 1) 232 233 def unfinalise(self): 234 235 "Reset finalised information for the module." 236 237 for obj in self.all_objects: 238 obj.unfinalise_attributes() 239 240 def finalise(self): 241 242 "Finalise the module." 243 244 for obj in self.all_objects: 245 obj.finalise_attributes() 246 247 def add_object(self, obj, any_scope=0): 248 249 """ 250 Record 'obj' if non-local or if the optional 'any_scope' is set to a 251 true value. 252 """ 253 254 if any_scope or not (self.namespaces and isinstance(self.namespaces[-1], Function)): 255 self.all_objects.add(obj) 256 257 # Optimisation tests. 258 259 def should_optimise_unused_objects(self): 260 return "unused_objects" in self.optimisations 261 262 # Namespace methods. 263 264 def store(self, name, obj): 265 266 "Record attribute or local 'name', storing 'obj'." 267 268 if not self.namespaces: 269 self.set(name, obj, not self.in_loop) 270 else: 271 self.namespaces[-1].set(name, obj, not self.in_loop) 272 273 def store_lambda(self, obj): 274 275 "Store a lambda function 'obj'." 276 277 self.add_object(obj) 278 279 def store_module_attr(self, name, module): 280 281 """ 282 Record module attribute 'name' in the given 'module' using the current 283 expression. 284 """ 285 286 module.set(name, self.expr, 0) 287 288 def store_class_attr(self, name): 289 290 """ 291 Record class attribute 'name' in the current class using the current 292 expression. 293 """ 294 295 if self.in_method and self.namespaces[-2].has_key(name): 296 self.namespaces[-2].set(name, self.expr, 0) 297 return 1 298 299 return 0 300 301 def store_instance_attr(self, name): 302 303 "Record instance attribute 'name' in the current class." 304 305 if self.in_method: 306 307 # Current namespace is the function. 308 # Previous namespace is the class. 309 310 self.namespaces[-2].add_instance_attribute(name) 311 312 def get_namespace(self): 313 314 "Return the parent (or most recent) namespace currently exposed." 315 316 return (self.namespaces[-1:] or [self])[0] 317 318 def use_name(self, name): 319 320 "Use the given 'name' within the current namespace/unit." 321 322 unit = self.get_namespace() 323 self.importer.use_name(name, unit.full_name()) 324 325 # Attribute usage methods. 326 # These are convenience methods which refer to the specific namespace's 327 # implementation of these operations. 328 329 def new_branchpoint(self): 330 self.get_namespace()._new_branchpoint() 331 332 def new_branch(self): 333 self.get_namespace()._new_branch() 334 335 def shelve_branch(self): 336 self.get_namespace()._shelve_branch() 337 338 def merge_branches(self): 339 self.get_namespace()._merge_branches() 340 341 def define_attribute_user(self, node): 342 343 """ 344 Define 'node' as the user of attributes, indicating the point where the 345 user is defined. 346 """ 347 348 self.get_namespace()._define_attribute_user(node) 349 350 def reset_all_attributes(self): 351 self.get_namespace()._reset_all_attributes() 352 353 def use_attribute(self, name, attrname): 354 return self.get_namespace()._use_attribute(name, attrname) 355 356 # Specific attribute usage, nominating specific attributes which can be 357 # resolved during inspection. 358 359 def use_specific_attribute(self, objname, attrname): 360 from_name = self.get_namespace().full_name() 361 objname = objname or from_name 362 self.importer.use_specific_name(objname, attrname, from_name) 363 364 # Visitor methods. 365 366 def default(self, node, *args): 367 raise InspectError(self.full_name(), node, "Node class %r is not supported." % node.__class__) 368 369 def dispatch(self, node, *args): 370 return ASTVisitor.dispatch(self, node, *args) 371 372 def NOP(self, node): 373 for n in node.getChildNodes(): 374 self.dispatch(n) 375 return None 376 377 def OP(self, node): 378 for n in node.getChildNodes(): 379 self.dispatch(n) 380 return Instance() 381 382 def _visitUnary(self, node): 383 384 "Accounting method for the unary operator 'node'." 385 386 method = unary_methods[node.__class__.__name__] 387 self.use_name(method) 388 return self.OP(node) 389 390 def _visitBinary(self, node): 391 392 "Accounting method for the binary operator 'node'." 393 394 left_method, right_method = binary_methods[node.__class__.__name__] 395 self.use_name(left_method) 396 self.use_name(right_method) 397 return self.OP(node) 398 399 def _visitFunction(self, node, name): 400 401 """ 402 Return a function object for the function defined by 'node' with the 403 given 'name'. If a lambda expression is being visited, 'name' should be 404 None. 405 """ 406 407 # Define the function object. 408 409 function = Function( 410 name, 411 self.get_namespace(), 412 node.argnames, 413 node.defaults, 414 (node.flags & 4 != 0), 415 (node.flags & 8 != 0), 416 self, 417 node 418 ) 419 420 self.add_object(function, any_scope=1) 421 422 # Make a back reference from the node for code generation. 423 424 node.unit = function 425 426 # Process the defaults. 427 428 for n in node.defaults: 429 self.expr = self.dispatch(n) 430 function.store_default(self.expr) 431 432 self.functions.append((node, self.namespaces + [function])) 433 434 if name is not None: 435 self.store(name, function) 436 else: 437 self.store_lambda(function) 438 439 # Where defaults exist, an instance needs creating. Thus, it makes 440 # no sense to return a reference to the function here, since the 441 # recipient will not be referencing the function itself. 442 443 if node.defaults: 444 return Instance() # indicates no known target 445 446 return function 447 448 def _visitFunctionBody(self, node, namespaces): 449 450 "Enter the function." 451 452 # Current namespace is the function. 453 # Previous namespace is the class. 454 455 if len(namespaces) > 1 and isinstance(namespaces[-2], Class): 456 if namespaces[-1].name == "__init__": 457 self.in_init = 1 458 self.in_method = 1 459 460 self.namespaces = namespaces 461 self.dispatch(node.code) 462 463 self.in_init = 0 464 self.in_method = 0 465 466 # Specific handler methods. 467 468 visitAdd = _visitBinary 469 470 visitAnd = OP 471 472 visitAssert = NOP 473 474 def visitAssign(self, node): 475 self.expr = self.dispatch(node.expr) 476 for n in node.nodes: 477 self.dispatch(n) 478 return None 479 480 def visitAssAttr(self, node): 481 expr = self.dispatch(node.expr) 482 483 # Record the attribute on the presumed target. 484 485 if isinstance(expr, Attr): 486 if expr.name == "self": 487 if not self.store_class_attr(node.attrname): 488 self.store_instance_attr(node.attrname) 489 elif isinstance(expr.get_value(), Module): 490 self.store_module_attr(node.attrname, expr.get_value()) 491 print "Warning: attribute %r of module %r set outside the module." % (node.attrname, expr.get_value().name) 492 493 # Note usage of the attribute where a local is involved. 494 495 if expr.parent is self.get_namespace(): 496 node._attrnames = self.use_attribute(expr.name, node.attrname) 497 498 return None 499 500 def visitAssList(self, node): 501 502 # Declare names which will be used by generated code. 503 504 self.use_name("__getitem__") 505 506 # Process the assignment. 507 508 for i, n in enumerate(node.nodes): 509 self.dispatch(n) 510 self.importer.make_constant(i) # for __getitem__(i) at run-time 511 return None 512 513 def visitAssName(self, node): 514 if hasattr(node, "flags") and node.flags == "OP_DELETE": 515 raise InspectError(self.full_name(), node, "Deletion of attribute %r is not supported." % node.name) 516 517 self.store(node.name, self.expr) 518 self.define_attribute_user(node) 519 520 # Ensure the presence of the given name in this namespace. 521 522 self.use_specific_attribute(None, node.name) 523 return None 524 525 visitAssTuple = visitAssList 526 527 def visitAugAssign(self, node): 528 529 # Accounting. 530 531 aug_method, (left_method, right_method) = augassign_methods[node.op] 532 self.use_name(aug_method) 533 self.use_name(left_method) 534 self.use_name(right_method) 535 536 # Process the assignment. 537 538 self.expr = self.dispatch(node.expr) 539 540 # NOTE: Similar to micropython.ast handler code. 541 # NOTE: Slices and subscripts not supported. 542 543 if isinstance(node.node, compiler.ast.Name): 544 self.visitAssName(node.node) 545 elif isinstance(node.node, compiler.ast.Getattr): 546 self.visitAssAttr(node.node) 547 else: 548 raise InspectError(self.full_name(), node, "AugAssign(Slice or Subscript)") 549 550 return None 551 552 visitBackquote = OP 553 554 visitBitand = _visitBinary 555 556 visitBitor = _visitBinary 557 558 visitBitxor = _visitBinary 559 560 def visitBreak(self, node): 561 self.reset_all_attributes() 562 563 visitCallFunc = OP 564 565 def visitClass(self, node): 566 567 """ 568 Register the class at the given 'node' subject to the restrictions 569 mentioned in the module docstring. 570 """ 571 572 if self.namespaces: 573 print "Class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) 574 return None 575 else: 576 cls = Class(node.name, self.get_namespace(), self, node) 577 578 # Visit the base class expressions, attempting to find concrete 579 # definitions of classes. 580 581 for base in node.bases: 582 expr = self.dispatch(base) 583 if isinstance(expr, Attr): 584 if expr.assignments != 1: 585 raise InspectError(self.full_name(), node, 586 "Base class %r for %r is not constant." % (base, cls.full_name())) 587 else: 588 cls.add_base(expr.get_value()) 589 else: # if expr is None: 590 raise InspectError(self.full_name(), node, 591 "Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name())) 592 593 # NOTE: Potentially dubious measure to permit __init__ availability. 594 # If no bases exist, adopt the 'object' class. 595 596 if not node.bases and not (self.name == "__builtins__" and node.name == "object") : 597 expr = self.dispatch(compiler.ast.Name("object")) 598 cls.add_base(expr.get_value()) 599 600 # Make a back reference from the node for code generation. 601 602 node.unit = cls 603 604 # Make an entry for the class. 605 606 self.store(node.name, cls) 607 self.add_object(cls) 608 609 # Process the class body. 610 611 self.namespaces.append(cls) 612 self.dispatch(node.code) 613 self.finalise_namespace(cls) 614 self.namespaces.pop() 615 616 return cls 617 618 def visitCompare(self, node): 619 620 # Accounting. 621 # NOTE: Replicates some code in micropython.ast.visitCompare. 622 623 for op in node.ops: 624 op_name, next_node = op 625 methods = comparison_methods[op_name] 626 if methods is not None: 627 self.use_name(methods[0]) 628 self.use_name(methods[1]) 629 elif op_name.endswith("in"): 630 self.use_name("__contains__") 631 632 return self.OP(node) 633 634 def visitConst(self, node): 635 636 # Register the constant, if necessary, returning the resulting object. 637 638 self.use_name(self.importer.get_constant_type_name(node.value)) 639 return self.importer.make_constant(node.value) 640 641 def visitContinue(self, node): 642 self.reset_all_attributes() 643 644 visitDecorators = NOP 645 646 visitDict = OP 647 648 visitDiscard = NOP 649 650 visitDiv = _visitBinary 651 652 visitEllipsis = NOP 653 654 visitExec = NOP 655 656 visitExpression = OP 657 658 visitFloorDiv = _visitBinary 659 660 def visitFor(self, node): 661 self.new_branchpoint() 662 663 # Declare names which will be used by generated code. 664 665 self.use_name("__iter__") 666 self.use_name("next") 667 668 self.in_loop = 1 669 self.dispatch(node.assign) 670 self.dispatch(node.list) 671 672 # Enter the loop. 673 # Propagate attribute usage to branches. 674 675 self.new_branch() 676 self.dispatch(node.body) 677 self.shelve_branch() 678 self.in_loop = 0 679 680 # Maintain a branch for the else clause or the current retained usage 681 # where execution avoids the conditional clauses. 682 683 self.new_branch() 684 if node.else_ is not None: 685 self.dispatch(node.else_) 686 self.shelve_branch() 687 688 self.merge_branches() 689 return None 690 691 def visitFrom(self, node): 692 module = self.importer.load(node.modname, 1) 693 694 #if module is None: 695 # print "Warning:", node.modname, "not imported." 696 697 for name, alias in node.names: 698 if name != "*": 699 if module is not None and module.has_key(name): 700 attr = module[name] 701 self.store(alias or name, attr) 702 if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: 703 self.importer.load(attr.get_value().name) 704 705 # Support the import of names from missing modules. 706 707 else: 708 self.store(alias or name, UnresolvedName(name, node.modname, self)) 709 else: 710 if module is not None: 711 for n in module.keys(): 712 attr = module[n] 713 self.store(n, attr) 714 if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: 715 self.importer.load(attr.get_value().name) 716 717 return None 718 719 def visitFunction(self, node): 720 return self._visitFunction(node, node.name) 721 722 visitGenExpr = OP 723 724 visitGenExprFor = NOP 725 726 visitGenExprIf = NOP 727 728 visitGenExprInner = NOP 729 730 def visitGetattr(self, node): 731 expr = self.dispatch(node.expr) 732 attrname = node.attrname 733 734 # Attempt to identify the nature of the attribute. 735 736 if isinstance(expr, Attr): 737 value = expr.get_value() 738 739 # Get the attribute and record its usage. 740 741 if isinstance(value, (Class, Module)): 742 attr = value.get(attrname) 743 self.use_specific_attribute(value.full_name(), attrname) 744 745 elif isinstance(value, UnresolvedName): 746 attr = UnresolvedName(attrname, value.full_name(), self) 747 748 else: 749 attr = None 750 751 # Note usage of the attribute where a local is involved. 752 753 if expr.parent is self.get_namespace(): 754 node._attrnames = self.use_attribute(expr.name, attrname) 755 else: 756 self.use_name(attrname) 757 758 elif self.builtins is not None: 759 attr = self.builtins.get(attrname) 760 self.use_specific_attribute(self.builtins.full_name(), attrname) 761 762 else: 763 attr = UnresolvedName(attrname, value.full_name(), self) 764 765 return attr 766 767 def visitGlobal(self, node): 768 if self.namespaces: 769 for name in node.names: 770 ns = self.namespaces[-1] 771 if not ns.make_global(name): 772 raise InspectError(ns.full_name(), node, "Name %r is global and local in %r" % (name, ns.full_name())) 773 774 # The name is recorded in an earlier process. 775 776 def visitIf(self, node): 777 self.new_branchpoint() 778 779 # Propagate attribute usage to branches. 780 781 for test, body in node.tests: 782 self.dispatch(test) 783 784 self.new_branch() 785 self.dispatch(body) 786 self.shelve_branch() 787 788 # Maintain a branch for the else clause or the current retained usage 789 # where execution avoids the conditional clauses. 790 791 self.new_branch() 792 if node.else_ is not None: 793 self.dispatch(node.else_) 794 self.shelve_branch() 795 796 self.merge_branches() 797 return None 798 799 visitIfExp = NOP 800 801 def visitImport(self, node): 802 for name, alias in node.names: 803 if alias is not None: 804 module = self.importer.load(name, 1) or UnresolvedName(None, name, self) 805 self.store(alias, module) 806 else: 807 module = self.importer.load(name) or UnresolvedName(None, name.split(".")[0], self) 808 self.store(name.split(".")[0], module) 809 810 return None 811 812 visitInvert = _visitUnary 813 814 def visitKeyword(self, node): 815 self.dispatch(node.expr) 816 self.importer.make_constant(node.name) 817 self.keyword_names.add(node.name) 818 return None 819 820 def visitLambda(self, node): 821 self.use_name(None) # lambda functions have no names but are assumed to be invoked 822 return self._visitFunction(node, None) 823 824 visitLeftShift = _visitBinary 825 826 visitList = OP 827 828 visitListComp = OP 829 830 visitListCompFor = NOP 831 832 visitListCompIf = NOP 833 834 visitMod = _visitBinary 835 836 def visitModule(self, node): 837 838 # Make a back reference from the node for code generation. 839 840 node.unit = self 841 return self.dispatch(node.node) 842 843 visitMul = _visitBinary 844 845 def visitName(self, node): 846 name = node.name 847 848 # Constants. 849 850 if self.importer.predefined_constants.has_key(name): 851 attr = self.importer.get_predefined_constant(name) 852 853 # Locals. 854 855 elif self.namespaces and self.namespaces[-1].has_key(name): 856 attr = self.namespaces[-1][name] 857 858 # Note usage of the local (potentially a class attribute). 859 860 self.use_specific_attribute(None, name) 861 862 # Globals. 863 864 elif self.has_key(name): 865 attr = self[name] 866 867 # Note usage of the module attribute. 868 869 self.use_specific_attribute(self.full_name(), name) 870 871 # Builtins. 872 873 elif self.builtins is not None and self.builtins.has_key(name): 874 attr = self.builtins[name] 875 self.use_specific_attribute(self.builtins.full_name(), name) 876 877 # Unknown. 878 879 else: 880 attr = None 881 self.use_name(name) 882 883 return attr 884 885 visitNot = OP 886 887 visitOr = OP 888 889 visitPass = NOP 890 891 visitPower = _visitBinary 892 893 visitPrint = NOP 894 895 visitPrintnl = NOP 896 897 visitRaise = NOP 898 899 visitReturn = NOP 900 901 visitRightShift = _visitBinary 902 903 visitSlice = OP 904 905 visitSliceobj = OP 906 907 def visitStmt(self, node): 908 for n in node.nodes: 909 self.dispatch(n) 910 return None 911 912 visitSub = _visitBinary 913 914 def visitSubscript(self, node): 915 self.use_name("__getitem__") 916 self.OP(node) 917 918 def visitTryExcept(self, node): 919 self.dispatch(node.body) 920 921 self.new_branchpoint() 922 923 for name, var, n in node.handlers: 924 self.new_branch() 925 926 # Establish the local for the handler. 927 928 if var is not None: 929 self.dispatch(var) 930 if n is not None: 931 self.dispatch(n) 932 933 self.shelve_branch() 934 935 if node.else_ is not None: 936 self.new_branch() 937 self.dispatch(node.else_) 938 self.shelve_branch() 939 940 self.merge_branches() 941 return None 942 943 visitTryFinally = NOP 944 945 visitTuple = OP 946 947 visitUnaryAdd = _visitUnary 948 949 visitUnarySub = _visitUnary 950 951 def visitWhile(self, node): 952 self.new_branchpoint() 953 954 # Propagate attribute usage to branches. 955 956 self.in_loop = 1 957 self.dispatch(node.test) 958 self.new_branch() 959 self.dispatch(node.body) 960 self.shelve_branch() 961 self.in_loop = 0 962 963 # Maintain a branch for the else clause or the current retained usage 964 # where execution avoids the conditional clauses. 965 966 self.new_branch() 967 if node.else_ is not None: 968 self.dispatch(node.else_) 969 self.shelve_branch() 970 971 self.merge_branches() 972 return None 973 974 visitWith = NOP 975 976 visitYield = NOP 977 978 # vim: tabstop=4 expandtab shiftwidth=4