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, loop_node=None): 333 self.get_namespace()._new_branch(loop_node) 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 NOP(self, node): 370 for n in node.getChildNodes(): 371 self.dispatch(n) 372 return None 373 374 def NOP_ABANDON(self, node): 375 self.NOP(node) 376 self.abandon_branch() 377 378 def OP(self, node): 379 for n in node.getChildNodes(): 380 self.dispatch(n) 381 return Instance() 382 383 # Generic support for classes of operations. 384 385 def _visitUnary(self, node): 386 387 "Accounting method for the unary operator 'node'." 388 389 method = unary_methods[node.__class__.__name__] 390 self.use_name(method) 391 return self.OP(node) 392 393 def _visitBinary(self, node): 394 395 "Accounting method for the binary operator 'node'." 396 397 left_method, right_method = binary_methods[node.__class__.__name__] 398 self.use_name(left_method) 399 self.use_name(right_method) 400 return self.OP(node) 401 402 def _visitFunction(self, node, name): 403 404 """ 405 Return a function object for the function defined by 'node' with the 406 given 'name'. If a lambda expression is being visited, 'name' should be 407 None. 408 """ 409 410 # Define the function object. 411 412 function = Function( 413 name, 414 self.get_namespace(), 415 node.argnames, 416 node.defaults, 417 (node.flags & 4 != 0), 418 (node.flags & 8 != 0), 419 self, 420 node 421 ) 422 423 self.add_object(function, any_scope=1) 424 425 # Make a back reference from the node for code generation. 426 427 node.unit = function 428 429 # Process the defaults. 430 431 for n in node.defaults: 432 self.expr = self.dispatch(n) 433 function.store_default(self.expr) 434 435 self.functions.append((node, self.namespaces + [function])) 436 437 if name is not None: 438 self.store(name, function) 439 else: 440 self.store_lambda(function) 441 442 # Where defaults exist, an instance needs creating. Thus, it makes 443 # no sense to return a reference to the function here, since the 444 # recipient will not be referencing the function itself. 445 446 if node.defaults: 447 return Instance() # indicates no known target 448 449 return function 450 451 def _visitFunctionBody(self, node, namespaces): 452 453 "Enter the function." 454 455 # Current namespace is the function. 456 # Previous namespace is the class. 457 458 if len(namespaces) > 1 and isinstance(namespaces[-2], Class): 459 if namespaces[-1].name == "__init__": 460 self.in_init = 1 461 self.in_method = 1 462 463 self.namespaces = namespaces 464 self.dispatch(node.code) 465 466 self.in_init = 0 467 self.in_method = 0 468 469 # Specific handler methods. 470 471 visitAdd = _visitBinary 472 473 visitAnd = OP 474 475 visitAssert = NOP 476 477 def visitAssign(self, node): 478 self.expr = self.dispatch(node.expr) 479 for n in node.nodes: 480 self.dispatch(n) 481 return None 482 483 def visitAssAttr(self, node): 484 expr = self.dispatch(node.expr) 485 486 # Record the attribute on the presumed target. 487 488 if isinstance(expr, Attr): 489 if expr.name == "self": 490 if not self.store_class_attr(node.attrname): 491 self.store_instance_attr(node.attrname) 492 elif isinstance(expr.get_value(), Module): 493 self.store_module_attr(node.attrname, expr.get_value()) 494 print "Warning: attribute %r of module %r set outside the module." % (node.attrname, expr.get_value().name) 495 496 # Note usage of the attribute where a local is involved. 497 498 if expr.parent is self.get_namespace(): 499 node._attrusers = self.use_attribute(expr.name, node.attrname) 500 node._username = expr.name 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._attrusers = self.use_attribute(expr.name, attrname) 758 node._username = expr.name 759 else: 760 self.use_name(attrname) 761 762 elif self.builtins is not None: 763 attr = self.builtins.get(attrname) 764 self.use_specific_attribute(self.builtins.full_name(), attrname) 765 766 else: 767 attr = UnresolvedName(attrname, value.full_name(), self) 768 769 return attr 770 771 def visitGlobal(self, node): 772 if self.namespaces: 773 for name in node.names: 774 ns = self.namespaces[-1] 775 if not ns.make_global(name): 776 raise InspectError(ns.full_name(), node, "Name %r is global and local in %r" % (name, ns.full_name())) 777 778 # The name is recorded in an earlier process. 779 780 def visitIf(self, node): 781 self.new_branchpoint() 782 783 # Propagate attribute usage to branches. 784 785 for test, body in node.tests: 786 self.dispatch(test) 787 788 self.new_branch() 789 self.dispatch(body) 790 self.shelve_branch() 791 792 # Maintain a branch for the else clause or the current retained usage 793 # where execution avoids the conditional clauses. 794 795 self.new_branch() 796 if node.else_ is not None: 797 self.dispatch(node.else_) 798 self.shelve_branch() 799 800 self.merge_branches() 801 return None 802 803 visitIfExp = NOP 804 805 def visitImport(self, node): 806 for name, alias in node.names: 807 if alias is not None: 808 module = self.importer.load(name, 1) or UnresolvedName(None, name, self) 809 self.store(alias, module) 810 else: 811 module = self.importer.load(name) or UnresolvedName(None, name.split(".")[0], self) 812 self.store(name.split(".")[0], module) 813 814 return None 815 816 visitInvert = _visitUnary 817 818 def visitKeyword(self, node): 819 self.dispatch(node.expr) 820 self.importer.make_constant(node.name) 821 self.keyword_names.add(node.name) 822 return None 823 824 def visitLambda(self, node): 825 self.use_name(None) # lambda functions have no names but are assumed to be invoked 826 return self._visitFunction(node, None) 827 828 visitLeftShift = _visitBinary 829 830 visitList = OP 831 832 visitListComp = OP 833 834 visitListCompFor = NOP 835 836 visitListCompIf = NOP 837 838 visitMod = _visitBinary 839 840 def visitModule(self, node): 841 842 # Make a back reference from the node for code generation. 843 844 node.unit = self 845 return self.dispatch(node.node) 846 847 visitMul = _visitBinary 848 849 def visitName(self, node): 850 name = node.name 851 852 # Constants. 853 854 if self.importer.predefined_constants.has_key(name): 855 attr = self.importer.get_predefined_constant(name) 856 857 # Locals. 858 859 elif self.namespaces and self.namespaces[-1].has_key(name): 860 attr = self.namespaces[-1][name] 861 862 # Note usage of the local (potentially a class attribute). 863 864 self.use_specific_attribute(None, name) 865 866 # Globals. 867 868 elif self.has_key(name): 869 attr = self[name] 870 871 # Note usage of the module attribute. 872 873 self.use_specific_attribute(self.full_name(), name) 874 875 # Builtins. 876 877 elif self.builtins is not None and self.builtins.has_key(name): 878 attr = self.builtins[name] 879 self.use_specific_attribute(self.builtins.full_name(), name) 880 881 # Unknown. 882 883 else: 884 attr = None 885 self.use_name(name) 886 887 return attr 888 889 visitNot = OP 890 891 visitOr = OP 892 893 visitPass = NOP 894 895 visitPower = _visitBinary 896 897 visitPrint = NOP 898 899 visitPrintnl = NOP 900 901 visitRaise = NOP_ABANDON 902 903 visitReturn = NOP_ABANDON 904 905 visitRightShift = _visitBinary 906 907 visitSlice = OP 908 909 visitSliceobj = OP 910 911 def visitStmt(self, node): 912 for n in node.nodes: 913 self.dispatch(n) 914 return None 915 916 visitSub = _visitBinary 917 918 def visitSubscript(self, node): 919 self.use_name("__getitem__") 920 self.OP(node) 921 922 def visitTryExcept(self, node): 923 self.dispatch(node.body) 924 925 self.new_branchpoint() 926 927 for name, var, n in node.handlers: 928 self.new_branch() 929 930 # Establish the local for the handler. 931 932 if var is not None: 933 self.dispatch(var) 934 if n is not None: 935 self.dispatch(n) 936 937 self.shelve_branch() 938 939 if node.else_ is not None: 940 self.new_branch() 941 self.dispatch(node.else_) 942 self.shelve_branch() 943 944 self.merge_branches() 945 return None 946 947 visitTryFinally = NOP 948 949 visitTuple = OP 950 951 visitUnaryAdd = _visitUnary 952 953 visitUnarySub = _visitUnary 954 955 def visitWhile(self, node): 956 self.new_branchpoint() 957 958 # Propagate attribute usage to branches. 959 960 self.in_loop = 1 961 self.dispatch(node.test) 962 self.new_branch(node) 963 self.dispatch(node.body) 964 self.shelve_branch() 965 self.in_loop = 0 966 967 # Maintain a branch for the else clause or the current retained usage 968 # where execution avoids the conditional clauses. 969 # NOTE: Consider merging here before the else clause. 970 971 self.new_branch() 972 if node.else_ is not None: 973 self.dispatch(node.else_) 974 self.shelve_branch() 975 976 self.merge_branches() 977 return None 978 979 visitWith = NOP 980 981 visitYield = NOP 982 983 # vim: tabstop=4 expandtab shiftwidth=4