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