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 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 abandon_branch(self): 336 self.get_namespace()._abandon_branch() 337 338 def shelve_branch(self): 339 self.get_namespace()._shelve_branch() 340 341 def merge_branches(self): 342 self.get_namespace()._merge_branches() 343 344 def define_attribute_user(self, node): 345 346 """ 347 Define 'node' as the user of attributes, indicating the point where the 348 user is defined. 349 """ 350 351 self.get_namespace()._define_attribute_user(node) 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 NOP_ABANDON(self, node): 378 self.abandon_branch() 379 self.NOP(node) 380 381 def OP(self, node): 382 for n in node.getChildNodes(): 383 self.dispatch(n) 384 return Instance() 385 386 def _visitUnary(self, node): 387 388 "Accounting method for the unary operator 'node'." 389 390 method = unary_methods[node.__class__.__name__] 391 self.use_name(method) 392 return self.OP(node) 393 394 def _visitBinary(self, node): 395 396 "Accounting method for the binary operator 'node'." 397 398 left_method, right_method = binary_methods[node.__class__.__name__] 399 self.use_name(left_method) 400 self.use_name(right_method) 401 return self.OP(node) 402 403 def _visitFunction(self, node, name): 404 405 """ 406 Return a function object for the function defined by 'node' with the 407 given 'name'. If a lambda expression is being visited, 'name' should be 408 None. 409 """ 410 411 # Define the function object. 412 413 function = Function( 414 name, 415 self.get_namespace(), 416 node.argnames, 417 node.defaults, 418 (node.flags & 4 != 0), 419 (node.flags & 8 != 0), 420 self, 421 node 422 ) 423 424 self.add_object(function, any_scope=1) 425 426 # Make a back reference from the node for code generation. 427 428 node.unit = function 429 430 # Process the defaults. 431 432 for n in node.defaults: 433 self.expr = self.dispatch(n) 434 function.store_default(self.expr) 435 436 self.functions.append((node, self.namespaces + [function])) 437 438 if name is not None: 439 self.store(name, function) 440 else: 441 self.store_lambda(function) 442 443 # Where defaults exist, an instance needs creating. Thus, it makes 444 # no sense to return a reference to the function here, since the 445 # recipient will not be referencing the function itself. 446 447 if node.defaults: 448 return Instance() # indicates no known target 449 450 return function 451 452 def _visitFunctionBody(self, node, namespaces): 453 454 "Enter the function." 455 456 # Current namespace is the function. 457 # Previous namespace is the class. 458 459 if len(namespaces) > 1 and isinstance(namespaces[-2], Class): 460 if namespaces[-1].name == "__init__": 461 self.in_init = 1 462 self.in_method = 1 463 464 self.namespaces = namespaces 465 self.dispatch(node.code) 466 467 self.in_init = 0 468 self.in_method = 0 469 470 # Specific handler methods. 471 472 visitAdd = _visitBinary 473 474 visitAnd = OP 475 476 visitAssert = NOP 477 478 def visitAssign(self, node): 479 self.expr = self.dispatch(node.expr) 480 for n in node.nodes: 481 self.dispatch(n) 482 return None 483 484 def visitAssAttr(self, node): 485 expr = self.dispatch(node.expr) 486 487 # Record the attribute on the presumed target. 488 489 if isinstance(expr, Attr): 490 if expr.name == "self": 491 if not self.store_class_attr(node.attrname): 492 self.store_instance_attr(node.attrname) 493 elif isinstance(expr.get_value(), Module): 494 self.store_module_attr(node.attrname, expr.get_value()) 495 print "Warning: attribute %r of module %r set outside the module." % (node.attrname, expr.get_value().name) 496 497 # Note usage of the attribute where a local is involved. 498 499 if expr.parent is self.get_namespace(): 500 node._attrnames = self.use_attribute(expr.name, node.attrname) 501 502 return None 503 504 def visitAssList(self, node): 505 506 # Declare names which will be used by generated code. 507 508 self.use_name("__getitem__") 509 510 # Process the assignment. 511 512 for i, n in enumerate(node.nodes): 513 self.dispatch(n) 514 self.importer.make_constant(i) # for __getitem__(i) at run-time 515 return None 516 517 def visitAssName(self, node): 518 if hasattr(node, "flags") and node.flags == "OP_DELETE": 519 print "Warning: deletion of attribute %r in %r is not supported." % (node.name, self.full_name()) 520 #raise InspectError(self.full_name(), node, "Deletion of attribute %r is not supported." % node.name) 521 522 self.store(node.name, self.expr) 523 self.define_attribute_user(node) 524 525 # Ensure the presence of the given name in this namespace. 526 527 self.use_specific_attribute(None, node.name) 528 return None 529 530 visitAssTuple = visitAssList 531 532 def visitAugAssign(self, node): 533 534 # Accounting. 535 536 aug_method, (left_method, right_method) = augassign_methods[node.op] 537 self.use_name(aug_method) 538 self.use_name(left_method) 539 self.use_name(right_method) 540 541 # Process the assignment. 542 543 self.expr = self.dispatch(node.expr) 544 545 # NOTE: Similar to micropython.ast handler code. 546 # NOTE: Slices and subscripts not supported. 547 548 if isinstance(node.node, compiler.ast.Name): 549 self.visitAssName(node.node) 550 elif isinstance(node.node, compiler.ast.Getattr): 551 self.visitAssAttr(node.node) 552 else: 553 raise InspectError(self.full_name(), node, "AugAssign(Slice or Subscript)") 554 555 return None 556 557 visitBackquote = OP 558 559 visitBitand = _visitBinary 560 561 visitBitor = _visitBinary 562 563 visitBitxor = _visitBinary 564 565 visitBreak = NOP_ABANDON 566 567 visitCallFunc = OP 568 569 def visitClass(self, node): 570 571 """ 572 Register the class at the given 'node' subject to the restrictions 573 mentioned in the module docstring. 574 """ 575 576 if self.namespaces: 577 print "Class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) 578 return None 579 else: 580 cls = Class(node.name, self.get_namespace(), self, node) 581 582 # Visit the base class expressions, attempting to find concrete 583 # definitions of classes. 584 585 for base in node.bases: 586 expr = self.dispatch(base) 587 if isinstance(expr, Attr): 588 if expr.assignments != 1: 589 raise InspectError(self.full_name(), node, 590 "Base class %r for %r is not constant." % (base, cls.full_name())) 591 else: 592 cls.add_base(expr.get_value()) 593 else: # if expr is None: 594 raise InspectError(self.full_name(), node, 595 "Base class %r for %r is not found: it may be hidden in some way." % (base, cls.full_name())) 596 597 # NOTE: Potentially dubious measure to permit __init__ availability. 598 # If no bases exist, adopt the 'object' class. 599 600 if not node.bases and not (self.name == "__builtins__" and node.name == "object") : 601 expr = self.dispatch(compiler.ast.Name("object")) 602 cls.add_base(expr.get_value()) 603 604 # Make a back reference from the node for code generation. 605 606 node.unit = cls 607 608 # Make an entry for the class. 609 610 self.store(node.name, cls) 611 self.add_object(cls) 612 613 # Process the class body. 614 615 self.namespaces.append(cls) 616 self.dispatch(node.code) 617 self.finalise_namespace(cls) 618 self.namespaces.pop() 619 620 return cls 621 622 def visitCompare(self, node): 623 624 # Accounting. 625 # NOTE: Replicates some code in micropython.ast.visitCompare. 626 627 for op in node.ops: 628 op_name, next_node = op 629 methods = comparison_methods[op_name] 630 if methods is not None: 631 self.use_name(methods[0]) 632 self.use_name(methods[1]) 633 elif op_name.endswith("in"): 634 self.use_name("__contains__") 635 636 return self.OP(node) 637 638 def visitConst(self, node): 639 640 # Register the constant, if necessary, returning the resulting object. 641 642 self.use_name(self.importer.get_constant_type_name(node.value)) 643 return self.importer.make_constant(node.value) 644 645 visitContinue = NOP_ABANDON 646 647 visitDecorators = NOP 648 649 visitDict = OP 650 651 visitDiscard = NOP 652 653 visitDiv = _visitBinary 654 655 visitEllipsis = NOP 656 657 visitExec = NOP 658 659 visitExpression = OP 660 661 visitFloorDiv = _visitBinary 662 663 def visitFor(self, node): 664 self.new_branchpoint() 665 666 # Declare names which will be used by generated code. 667 668 self.use_name("__iter__") 669 self.use_name("next") 670 671 self.in_loop = 1 672 self.dispatch(node.assign) 673 self.dispatch(node.list) 674 675 # Enter the loop. 676 # Propagate attribute usage to branches. 677 678 self.new_branch() 679 self.dispatch(node.body) 680 self.shelve_branch() 681 self.in_loop = 0 682 683 # Maintain a branch for the else clause or the current retained usage 684 # where execution avoids the conditional clauses. 685 686 self.new_branch() 687 if node.else_ is not None: 688 self.dispatch(node.else_) 689 self.shelve_branch() 690 691 self.merge_branches() 692 return None 693 694 def visitFrom(self, node): 695 module = self.importer.load(node.modname, 1) 696 697 #if module is None: 698 # print "Warning:", node.modname, "not imported." 699 700 for name, alias in node.names: 701 if name != "*": 702 if module is not None and module.has_key(name): 703 attr = module[name] 704 self.store(alias or name, attr) 705 if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: 706 self.importer.load(attr.get_value().name) 707 708 # Support the import of names from missing modules. 709 710 else: 711 self.store(alias or name, UnresolvedName(name, node.modname, self)) 712 else: 713 if module is not None: 714 for n in module.keys(): 715 attr = module[n] 716 self.store(n, attr) 717 if isinstance(attr.get_value(), Module) and not attr.get_value().loaded: 718 self.importer.load(attr.get_value().name) 719 720 return None 721 722 def visitFunction(self, node): 723 return self._visitFunction(node, node.name) 724 725 visitGenExpr = OP 726 727 visitGenExprFor = NOP 728 729 visitGenExprIf = NOP 730 731 visitGenExprInner = NOP 732 733 def visitGetattr(self, node): 734 expr = self.dispatch(node.expr) 735 attrname = node.attrname 736 737 # Attempt to identify the nature of the attribute. 738 739 if isinstance(expr, Attr): 740 value = expr.get_value() 741 742 # Get the attribute and record its usage. 743 744 if isinstance(value, (Class, Module)): 745 attr = value.get(attrname) 746 self.use_specific_attribute(value.full_name(), attrname) 747 748 elif isinstance(value, UnresolvedName): 749 attr = UnresolvedName(attrname, value.full_name(), self) 750 751 else: 752 attr = None 753 754 # Note usage of the attribute where a local is involved. 755 756 if expr.parent is self.get_namespace(): 757 node._attrnames = self.use_attribute(expr.name, attrname) 758 else: 759 self.use_name(attrname) 760 761 elif self.builtins is not None: 762 attr = self.builtins.get(attrname) 763 self.use_specific_attribute(self.builtins.full_name(), attrname) 764 765 else: 766 attr = UnresolvedName(attrname, value.full_name(), self) 767 768 return attr 769 770 def visitGlobal(self, node): 771 if self.namespaces: 772 for name in node.names: 773 ns = self.namespaces[-1] 774 if not ns.make_global(name): 775 raise InspectError(ns.full_name(), node, "Name %r is global and local in %r" % (name, ns.full_name())) 776 777 # The name is recorded in an earlier process. 778 779 def visitIf(self, node): 780 self.new_branchpoint() 781 782 # Propagate attribute usage to branches. 783 784 for test, body in node.tests: 785 self.dispatch(test) 786 787 self.new_branch() 788 self.dispatch(body) 789 self.shelve_branch() 790 791 # Maintain a branch for the else clause or the current retained usage 792 # where execution avoids the conditional clauses. 793 794 self.new_branch() 795 if node.else_ is not None: 796 self.dispatch(node.else_) 797 self.shelve_branch() 798 799 self.merge_branches() 800 return None 801 802 visitIfExp = NOP 803 804 def visitImport(self, node): 805 for name, alias in node.names: 806 if alias is not None: 807 module = self.importer.load(name, 1) or UnresolvedName(None, name, self) 808 self.store(alias, module) 809 else: 810 module = self.importer.load(name) or UnresolvedName(None, name.split(".")[0], self) 811 self.store(name.split(".")[0], module) 812 813 return None 814 815 visitInvert = _visitUnary 816 817 def visitKeyword(self, node): 818 self.dispatch(node.expr) 819 self.importer.make_constant(node.name) 820 self.keyword_names.add(node.name) 821 return None 822 823 def visitLambda(self, node): 824 self.use_name(None) # lambda functions have no names but are assumed to be invoked 825 return self._visitFunction(node, None) 826 827 visitLeftShift = _visitBinary 828 829 visitList = OP 830 831 visitListComp = OP 832 833 visitListCompFor = NOP 834 835 visitListCompIf = NOP 836 837 visitMod = _visitBinary 838 839 def visitModule(self, node): 840 841 # Make a back reference from the node for code generation. 842 843 node.unit = self 844 return self.dispatch(node.node) 845 846 visitMul = _visitBinary 847 848 def visitName(self, node): 849 name = node.name 850 851 # Constants. 852 853 if self.importer.predefined_constants.has_key(name): 854 attr = self.importer.get_predefined_constant(name) 855 856 # Locals. 857 858 elif self.namespaces and self.namespaces[-1].has_key(name): 859 attr = self.namespaces[-1][name] 860 861 # Note usage of the local (potentially a class attribute). 862 863 self.use_specific_attribute(None, name) 864 865 # Globals. 866 867 elif self.has_key(name): 868 attr = self[name] 869 870 # Note usage of the module attribute. 871 872 self.use_specific_attribute(self.full_name(), name) 873 874 # Builtins. 875 876 elif self.builtins is not None and self.builtins.has_key(name): 877 attr = self.builtins[name] 878 self.use_specific_attribute(self.builtins.full_name(), name) 879 880 # Unknown. 881 882 else: 883 attr = None 884 self.use_name(name) 885 886 return attr 887 888 visitNot = OP 889 890 visitOr = OP 891 892 visitPass = NOP 893 894 visitPower = _visitBinary 895 896 visitPrint = NOP 897 898 visitPrintnl = NOP 899 900 visitRaise = NOP_ABANDON 901 902 visitReturn = NOP_ABANDON 903 904 visitRightShift = _visitBinary 905 906 visitSlice = OP 907 908 visitSliceobj = OP 909 910 def visitStmt(self, node): 911 for n in node.nodes: 912 self.dispatch(n) 913 return None 914 915 visitSub = _visitBinary 916 917 def visitSubscript(self, node): 918 self.use_name("__getitem__") 919 self.OP(node) 920 921 def visitTryExcept(self, node): 922 self.dispatch(node.body) 923 924 self.new_branchpoint() 925 926 for name, var, n in node.handlers: 927 self.new_branch() 928 929 # Establish the local for the handler. 930 931 if var is not None: 932 self.dispatch(var) 933 if n is not None: 934 self.dispatch(n) 935 936 self.shelve_branch() 937 938 if node.else_ is not None: 939 self.new_branch() 940 self.dispatch(node.else_) 941 self.shelve_branch() 942 943 self.merge_branches() 944 return None 945 946 visitTryFinally = NOP 947 948 visitTuple = OP 949 950 visitUnaryAdd = _visitUnary 951 952 visitUnarySub = _visitUnary 953 954 def visitWhile(self, node): 955 self.new_branchpoint() 956 957 # Propagate attribute usage to branches. 958 959 self.in_loop = 1 960 self.dispatch(node.test) 961 self.new_branch() 962 self.dispatch(node.body) 963 self.shelve_branch() 964 self.in_loop = 0 965 966 # Maintain a branch for the else clause or the current retained usage 967 # where execution avoids the conditional clauses. 968 969 self.new_branch() 970 if node.else_ is not None: 971 self.dispatch(node.else_) 972 self.shelve_branch() 973 974 self.merge_branches() 975 return None 976 977 visitWith = NOP 978 979 visitYield = NOP 980 981 # vim: tabstop=4 expandtab shiftwidth=4