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