1 #!/usr/bin/env python 2 3 """ 4 Simplify AST structures for easier type propagation and analysis. The code in 5 this module processes AST trees originating from the compiler module and 6 produces a result tree consisting of instruction-oriented program nodes. 7 8 Copyright (C) 2006, 2007 Paul Boddie <paul@boddie.org.uk> 9 10 This software is free software; you can redistribute it and/or 11 modify it under the terms of the GNU General Public License as 12 published by the Free Software Foundation; either version 2 of 13 the License, or (at your option) any later version. 14 15 This software is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU General Public License for more details. 19 20 You should have received a copy of the GNU General Public 21 License along with this library; see the file LICENCE.txt 22 If not, write to the Free Software Foundation, Inc., 23 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 24 25 -------- 26 27 To use this module, the easiest approach is to use the simplify function: 28 29 simplify(filename) 30 31 The more complicated approach involves first instantiating a Simplifier object: 32 33 simplifier = Simplifier() 34 35 Then, applying the simplifier to an AST tree: 36 37 module = compiler.parseFile(filename) 38 simplifier.process(module) 39 """ 40 41 from simplified import * 42 import compiler.ast 43 import os 44 45 class Simplifier(Visitor): 46 47 """ 48 A simplifying visitor for AST nodes. 49 50 Covered: Add, And, AssAttr, AssList, AssName, AssTuple, Assign, AugAssign, 51 Break, CallFunc, Class, Compare, Const, Continue, Dict, Discard, 52 Div, FloorDiv, For, From, Function, Getattr, Global, If, Import, 53 Invert, Keyword, Lambda, List, Mod, Module, Mul, Name, Not, Or, 54 Pass, Power, Print, Printnl, Raise, Return, Slice, Stmt, Sub, 55 Subscript, TryExcept, TryFinally, Tuple, While, UnaryAdd, UnarySub. 56 57 Missing: Assert, Backquote, Bitand, Bitor, Bitxor, Decorators, Ellipsis, 58 Exec, LeftShift, ListComp, ListCompFor, ListCompIf, RightShift, 59 Sliceobj, Yield. 60 """ 61 62 def __init__(self, builtins=0): 63 64 """ 65 Initialise the simplifier with the optional 'builtins' parameter 66 indicating whether the module contains the built-in classes and 67 functions. 68 """ 69 70 Visitor.__init__(self) 71 self.subprograms = [] # Subprograms outside the tree. 72 self.structures = [] # Structures/classes. 73 self.constants = {} # Constants. 74 self.current_subprograms = [] # Current subprograms being processed. 75 self.current_structures = [] # Current structures being processed. 76 self.within_class = 0 # Whether a class is being defined. 77 self.builtins = builtins # Whether the builtins are being processed. 78 79 # Convenience attributes. 80 81 self.subnames = {} 82 83 # For compiler package mechanisms. 84 85 self.visitor = self 86 87 def process(self, node, name): 88 result = self.dispatch(node, name) 89 result.simplifier = self 90 return result 91 92 def dispatch_or_none(self, node, *args): 93 if node is not None: 94 return self.dispatch(node, *args) 95 else: 96 return LoadName(node, name="None") 97 98 # Top-level transformation. 99 100 def visitModule(self, module, name=None): 101 102 """ 103 Process the given 'module', producing a Module object which contains the 104 resulting program nodes. If the optional 'name' is provided, the 'name' 105 attribute is set on the Module object using a value other than None. 106 """ 107 108 result = self.module = Module(module, 1, name=name) 109 module_code = self.dispatch(module.node) 110 111 # NOTE: Constant initialisation necessary for annotation but perhaps 112 # NOTE: redundant in the program. 113 114 init_code = [] 115 for value, constant in self.constants.items(): 116 init_code.append( 117 StoreAttr( 118 lvalue=LoadRef(ref=constant), 119 name="__class__", 120 expr=LoadName(name=constant.typename) 121 ) 122 ) 123 124 # NOTE: Hack to ensure correct initialisation of constants. 125 126 if self.builtins: 127 result.code = module_code + init_code 128 else: 129 result.code = init_code + module_code 130 return result 131 132 # Node transformations. 133 134 def visitAdd(self, add): 135 return self._visitBinary(add, "__add__", "__radd__") 136 137 def visitAnd(self, and_): 138 139 """ 140 Make a subprogram for the 'and_' node and record its contents inside the 141 subprogram. Convert... 142 143 And (test) 144 (test) 145 ... 146 147 ...to: 148 149 Subprogram -> Conditional (test) -> ReturnFromBlock ... 150 (else) -> Conditional (test) -> ReturnFromBlock ... 151 (else) -> ... 152 """ 153 154 subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) 155 self.current_subprograms.append(subprogram) 156 157 # In the subprogram, make instructions which store each operand, test 158 # for each operand's truth status, and if appropriate return from the 159 # subprogram with the value of the operand. 160 161 last = and_.nodes[-1] 162 results = nodes = [] 163 164 for node in and_.nodes: 165 expr = self.dispatch(node) 166 167 # Return from the subprogram where the test is not satisfied. 168 169 if node is not last: 170 nodes += [ 171 StoreTemp(expr=expr), 172 Conditional( 173 test=self._visitNot(LoadTemp()), 174 body=[ 175 ReturnFromBlock( 176 expr=LoadTemp() 177 ) 178 ], 179 else_=[ 180 ReleaseTemp() 181 # Subsequent operations go here! 182 ] 183 ) 184 ] 185 186 # Put subsequent operations in the else section of this conditional. 187 188 nodes = nodes[-1].else_ 189 190 # For the last operation, return the result. 191 192 else: 193 nodes.append(ReturnFromBlock(expr=expr)) 194 195 # Finish the subprogram definition. 196 197 subprogram.code = results 198 199 self.current_subprograms.pop() 200 self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram 201 202 # Make an invocation of the subprogram. 203 204 result = InvokeBlock(and_, 1, produces_result=1) 205 result.expr = LoadRef(ref=subprogram) 206 return result 207 208 # Assignments. 209 210 def visitAssAttr(self, assattr, in_sequence=0): 211 expr = self._visitAssNameOrAttr(assattr, in_sequence) 212 lvalue = self.dispatch(assattr.expr) 213 result = StoreAttr(assattr, 1, name=assattr.attrname, lvalue=lvalue, expr=expr) 214 return result 215 216 def visitAssign(self, assign): 217 result = Assign(assign, 1) 218 store = StoreTemp(expr=self.dispatch(assign.expr)) 219 release = ReleaseTemp() 220 result.code = [store] + self.dispatches(assign.nodes, 0) + [release] 221 return result 222 223 def visitAssList(self, asslist, in_sequence=0): 224 if not in_sequence: 225 expr = LoadTemp() 226 else: 227 expr = InvokeFunction(asslist, expr=LoadAttr(expr=LoadTemp(), name="next")) 228 result = Assign(asslist, 1) 229 store = StoreTemp(expr=InvokeFunction(asslist, expr=LoadAttr(name="__iter__", expr=expr))) 230 release = ReleaseTemp() 231 result.code = [store] + self.dispatches(asslist.nodes, 1) + [release] 232 return result 233 234 visitAssTuple = visitAssList 235 236 def _visitAssNameOrAttr(self, node, in_sequence): 237 if not in_sequence: 238 return LoadTemp() 239 else: 240 return InvokeFunction(node, expr=LoadAttr(expr=LoadTemp(), name="next")) 241 242 def visitAssName(self, assname, in_sequence=0): 243 expr = self._visitAssNameOrAttr(assname, in_sequence) 244 result = StoreName(assname, 1, name=assname.name, expr=expr) 245 return result 246 247 augassign_methods = { 248 "+=" : "__iadd__", "-=" : "__isub__", "*=" : "__imul__", "/=" : "__idiv__", 249 "%=" : "__imod__", "**=" : "__ipow__", "<<=" : "__ilshift__", ">>=" : "__irshift__", 250 "&=" : "__iand__", "^=" : "__ixor__", "|=" : "__ior__" 251 } 252 253 def visitAugAssign(self, augassign): 254 255 """ 256 Convert the augmented assignment... 257 258 AugAssign (node) -> Name | Getattr | Slice | Subscript 259 (op) 260 (expr) 261 262 ...to: 263 264 Assign (code) -> StoreTemp (expr) -> InvokeFunction (expr) -> LoadAttr (expr) -> <name> 265 (name) -> <op> 266 StoreName (name) -> <name> 267 (expr) -> LoadTemp 268 ReleaseTemp 269 """ 270 271 result = Assign(augassign, 1) 272 expr = self.dispatch(augassign.expr) 273 274 # Simple augmented assignment: name += expr 275 276 if isinstance(augassign.node, compiler.ast.Name): 277 result.code = [ 278 StoreTemp( 279 expr=InvokeFunction( # referenced below 280 augassign, 281 args=[expr], 282 star=None, 283 dstar=None, 284 expr=LoadAttr( 285 expr=self.dispatch(augassign.node), 286 name=self.augassign_methods[augassign.op] 287 ) 288 ) 289 ), 290 StoreName( 291 expr=LoadTemp(), 292 name=augassign.node.name), 293 ReleaseTemp() 294 ] 295 296 # Make nice annotations for the viewer. 297 298 augassign._op_call = result.code[0].expr 299 300 # Complicated augmented assignment: lvalue.attr += expr 301 302 elif isinstance(augassign.node, compiler.ast.Getattr): 303 304 # <lvalue> -> setattr(<lvalue>, getattr(<lvalue>, "attr").__xxx__(expr)) 305 306 result.code = [ 307 StoreTemp( 308 index="expr", 309 expr=self.dispatch(augassign.node.expr) 310 ), 311 StoreTemp( 312 expr=InvokeFunction( # referenced below 313 augassign, 314 args=[expr], star=None, dstar=None, 315 expr=LoadAttr( 316 expr=LoadAttr(augassign.node, 1, 317 expr=LoadTemp(index="expr"), 318 name=augassign.node.attrname 319 ), 320 name=self.augassign_methods[augassign.op] 321 ) 322 ) 323 ), 324 StoreAttr( 325 expr=LoadTemp(), 326 lvalue=LoadTemp(index="expr"), 327 name=augassign.node.attrname 328 ), 329 ReleaseTemp(index="expr"), 330 ReleaseTemp() 331 ] 332 333 # Make nice annotations for the viewer. 334 335 augassign._op_call = result.code[1].expr 336 337 # Complicated augassign using slices: lvalue[lower:upper] += expr 338 339 elif isinstance(augassign.node, compiler.ast.Slice): 340 341 # <lvalue>, <lower>, <upper> -> <lvalue>.__setslice__(<lower>, <upper>, <lvalue>.__getslice__(<lower>, <upper>).__xxx__(expr)) 342 343 result.code = [ 344 StoreTemp( 345 index="expr", 346 expr=self.dispatch(augassign.node.expr) 347 ), 348 StoreTemp( 349 index="lower", 350 expr=self.dispatch_or_none(augassign.node.lower) 351 ), 352 StoreTemp( 353 index="upper", 354 expr=self.dispatch_or_none(augassign.node.upper) 355 ), 356 StoreTemp( 357 expr=InvokeFunction( # referenced below 358 augassign, 359 args=[expr], star=None, dstar=None, 360 expr=LoadAttr( 361 expr=self._visitSlice( 362 augassign.node, 363 LoadTemp(index="expr"), 364 LoadTemp(index="lower"), 365 LoadTemp(index="upper"), 366 "OP_APPLY"), 367 name=self.augassign_methods[augassign.op] 368 ) 369 ) 370 ), 371 self._visitSlice( 372 augassign.node, 373 LoadTemp(index="expr"), 374 LoadTemp(index="lower"), 375 LoadTemp(index="upper"), 376 "OP_ASSIGN", 377 LoadTemp() 378 ), 379 ReleaseTemp(index="expr"), 380 ReleaseTemp(index="lower"), 381 ReleaseTemp(index="upper"), 382 ReleaseTemp() 383 ] 384 385 # Make nice annotations for the viewer. 386 387 augassign._op_call = result.code[3].expr 388 389 # Complicated augassign using subscripts: lvalue[subs] += expr 390 391 elif isinstance(augassign.node, compiler.ast.Subscript): 392 393 # <lvalue>, <subs> -> <lvalue>.__setitem__(<subs>, <lvalue>.__getitem__(<subs>).__xxx__(expr)) 394 395 result.code = [ 396 StoreTemp(index="expr", expr=self.dispatch(augassign.node.expr)), 397 StoreTemp(index="subs", expr=self._visitSubscriptSubs(augassign.node, augassign.node.subs)), 398 StoreTemp( 399 expr=InvokeFunction( # referenced below 400 augassign, 401 args=[expr], star=None, dstar=None, 402 expr=LoadAttr( 403 expr=self._visitSubscript( 404 augassign.node, 405 LoadTemp(index="expr"), 406 LoadTemp(index="subs"), 407 "OP_APPLY" 408 ), 409 name=self.augassign_methods[augassign.op] 410 ) 411 ) 412 ), 413 self._visitSubscript( 414 augassign.node, 415 LoadTemp(index="expr"), 416 LoadTemp(index="subs"), 417 "OP_ASSIGN", 418 LoadTemp() 419 ), 420 ReleaseTemp(index="expr"), 421 ReleaseTemp(index="subs"), 422 ReleaseTemp() 423 ] 424 425 # Make nice annotations for the viewer. 426 427 augassign._op_call = result.code[2].expr 428 429 else: 430 raise NotImplementedError, augassign.node.__class__ 431 432 return result 433 434 def visitBreak(self, break_): 435 result = ReturnFromBlock(break_, 1) 436 return result 437 438 def visitCallFunc(self, callfunc): 439 result = InvokeFunction(callfunc, 1, star=None, dstar=None, args=self.dispatches(callfunc.args)) 440 if callfunc.star_args is not None: 441 result.star = self.dispatch(callfunc.star_args) 442 if callfunc.dstar_args is not None: 443 result.dstar = self.dispatch(callfunc.dstar_args) 444 result.expr = self.dispatch(callfunc.node) 445 return result 446 447 def visitClass(self, class_): 448 449 # Add "object" if the class is not "object" and has an empty bases list. 450 451 if class_.name != "object" and not class_.bases: 452 bases = [compiler.ast.Name("object")] 453 else: 454 bases = class_.bases 455 456 structure = Class(name=class_.name, module=self.module, bases=self.dispatches(bases)) 457 self.structures.append(structure) 458 within_class = self.within_class 459 self.within_class = 1 460 461 # Make a subprogram which initialises the class structure. 462 463 subprogram = Subprogram(name=None, module=self.module, structure=structure, params=[], star=None, dstar=None) 464 self.current_subprograms.append(subprogram) 465 self.current_structures.append(structure) # mostly for name construction 466 467 # The class is initialised using the code found inside. 468 469 subprogram.code = self.dispatch(class_.code) + [ReturnFromBlock()] 470 471 self.within_class = within_class 472 self.current_structures.pop() 473 self.current_subprograms.pop() 474 self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram 475 476 # Make a definition of the class associating it with a name. 477 478 result = Assign( 479 code=[ 480 StoreName(class_, 1, # defines the class 481 name=class_.name, 482 expr=LoadRef(ref=structure) 483 ), 484 InvokeBlock( 485 class_, 486 share_locals=0, # override the local sharing usually in InvokeBlock 487 expr=LoadRef(ref=subprogram) 488 ) 489 ] 490 ) 491 return result 492 493 comparison_methods = { 494 "==" : ("__eq__", "__ne__"), 495 "!=" : ("__ne__", "__eq__"), 496 "<" : ("__lt__", "__gt__"), 497 "<=" : ("__le__", "__ge__"), 498 ">=" : ("__ge__", "__le__"), 499 ">" : ("__gt__", "__lt__"), 500 "is" : None, 501 "is not" : None 502 } 503 504 def visitCompare(self, compare): 505 506 """ 507 Make a subprogram for the 'compare' node and record its contents inside 508 the subprogram. Convert... 509 510 Compare (expr) 511 (name/node) 512 ... 513 514 ...to: 515 516 InvokeBlock -> Subprogram -> Conditional (test) -> (body) 517 (else) -> Conditional (test) -> (body) 518 (else) -> ... 519 """ 520 521 subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) 522 self.current_subprograms.append(subprogram) 523 524 # In the subprogram, make instructions which invoke a method on the 525 # first operand of each operand pair and, if appropriate, return with 526 # the value from that method. 527 528 last = compare.ops[-1] 529 previous = self.dispatch(compare.expr) 530 results = nodes = [] 531 532 # For viewing purposes, record invocations on the AST node. 533 534 compare._ops = [] 535 536 for op in compare.ops: 537 op_name, node = op 538 539 # Make a new AST-style node to wrap the operation program nodes. 540 541 new_op = Op(op_name, node) 542 expr = self.dispatch(node) 543 544 # Identify the operation and produce the appropriate method call. 545 546 method_names = self.comparison_methods[op_name] 547 if method_names: 548 first_name, alternative_name = method_names 549 invocation = self._visitBinaryCompareOp(new_op, previous, expr, first_name, alternative_name) 550 551 elif op_name == "is": 552 invocation = InvokeFunction( 553 new_op, 1, 554 expr=LoadName(name="__is__"), 555 args=[previous, expr], 556 star=None, 557 dstar=None) 558 559 elif op_name == "is not": 560 invocation = Not( 561 new_op, 1, 562 expr=InvokeFunction( 563 new_op, 564 expr=LoadName(name="__is__"), 565 args=[previous, expr], 566 star=None, 567 dstar=None) 568 ) 569 else: 570 raise NotImplementedError, op_name 571 572 nodes.append(StoreTemp(expr=invocation)) 573 compare._ops.append(new_op) 574 575 # Return from the subprogram where the test is not satisfied. 576 577 if op is not last: 578 nodes.append( 579 Conditional( 580 test=self._visitNot(LoadTemp()), 581 body=[ 582 ReturnFromBlock(expr=LoadTemp()) 583 ], 584 else_=[ 585 ReleaseTemp() 586 # Subsequent operations go here! 587 ] 588 ) 589 ) 590 591 # Put subsequent operations in the else section of this conditional. 592 593 nodes = nodes[-1].else_ 594 595 # For the last operation, return the result. 596 597 else: 598 nodes.append( 599 ReturnFromBlock(expr=LoadTemp(release=1)) 600 ) 601 602 previous = expr 603 604 # Finish the subprogram definition. 605 606 subprogram.code = results 607 608 self.current_subprograms.pop() 609 self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram 610 611 # Make an invocation of the subprogram. 612 613 result = InvokeBlock(compare, 1, produces_result=1) 614 result.expr = LoadRef(ref=subprogram) 615 return result 616 617 def visitConst(self, const): 618 key = "%s-%s" % (const.value.__class__.__name__, const.value) 619 if not self.constants.has_key(key): 620 self.constants[key] = Constant(name=repr(const.value), value=const.value) 621 result = LoadRef(const, 1, ref=self.constants[key]) 622 return result 623 624 def visitContinue(self, continue_): 625 result = InvokeBlock(continue_, 1, 626 expr=LoadRef(ref=self.current_subprograms[-1]) 627 ) 628 return result 629 630 def visitDict(self, dict): 631 result = InvokeFunction(dict, 1, expr=LoadName(name="dict"), star=None, dstar=None) 632 args = [] 633 for key, value in dict.items: 634 tuple = InvokeFunction(dict, expr=LoadName(name="tuple"), star=None, dstar=None) 635 tuple.set_args([self.dispatch(key), self.dispatch(value)]) 636 args.append(tuple) 637 result.set_args(args) 638 return result 639 640 def visitDiscard(self, discard): 641 return self.dispatch(discard.expr) 642 643 def visitDiv(self, div): 644 return self._visitBinary(div, "__div__", "__rdiv__") 645 646 def visitFloorDiv(self, floordiv): 647 return self._visitBinary(floordiv, "__floordiv__", "__rfloordiv__") 648 649 def visitFor(self, for_): 650 651 """ 652 Make a subprogram for the 'for_' node and record its contents inside the 653 subprogram. Convert... 654 655 For (assign) 656 (body) 657 (else) 658 659 ...to: 660 661 Assign (assign #1) 662 Invoke -> Subprogram -> Try (body) -> (assign #2) 663 (body) 664 Invoke subprogram 665 (handler) -> ... 666 (else) -> ... 667 """ 668 669 subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=0, params=[], star=None, dstar=None) 670 self.current_subprograms.append(subprogram) 671 672 # Always return from conditional sections/subprograms. 673 674 if for_.else_ is not None: 675 else_stmt = self.dispatch(for_.else_) + [ReturnFromBlock()] 676 else: 677 else_stmt = [ReturnFromBlock()] 678 679 # Wrap the assignment in a try...except statement. 680 # Inside the body, add a recursive invocation to the subprogram. 681 682 subprogram.code = [ 683 Try( 684 body=[ 685 Assign( 686 code=[ 687 StoreTemp(expr=InvokeFunction(for_, expr=LoadAttr(expr=LoadTemp(), name="next"))), 688 self.dispatch(for_.assign), 689 ReleaseTemp() 690 ]) 691 ] + self.dispatch(for_.body) + [ 692 InvokeBlock( 693 for_, 694 expr=LoadRef(ref=subprogram) 695 ) 696 ], 697 handler=[ 698 Conditional( 699 test=InvokeFunction( 700 for_, 701 expr=LoadName(name="isinstance"), 702 args=[LoadExc(), LoadName(name="StopIteration")], 703 star=None, 704 dstar=None), 705 body=else_stmt, 706 else_=[Raise(expr=LoadExc())] 707 ) 708 ], 709 else_=[], 710 finally_=[] 711 ), 712 ReturnFromBlock() 713 ] 714 715 # Finish the subprogram definition. 716 717 self.current_subprograms.pop() 718 self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram 719 720 # Obtain an iterator for the sequence involved. 721 # Then, make an invocation of the subprogram. 722 723 result = Assign(for_, 1, 724 code=[ 725 StoreTemp( 726 expr=InvokeFunction( 727 for_, 728 expr=LoadAttr( 729 name="__iter__", 730 expr=self.dispatch(for_.list) 731 ) 732 ) 733 ), 734 InvokeBlock(for_, expr=LoadRef(ref=subprogram)), 735 ReleaseTemp() 736 ] 737 ) 738 739 # Make nice annotations for the viewer. 740 741 for_._iter_call = result.code[0].expr 742 for_._next_call = subprogram.code[0].body[0].code[0].expr 743 744 return result 745 746 def visitFrom(self, from_): 747 result = Assign(from_, 1) 748 code = [] 749 _names = [] 750 code.append( 751 StoreTemp( 752 expr=Import(name=from_.modname, alias=1) 753 ) 754 ) 755 from_._modname = code[-1].expr 756 for name, alias in from_.names: 757 code.append( 758 StoreName( 759 expr=LoadAttr( 760 expr=LoadTemp(), 761 name=name), 762 name=(alias or name) 763 ) 764 ) 765 _names.append(code[-1].expr) 766 code.append(ReleaseTemp()) 767 result.code = code 768 from_._names = _names 769 return result 770 771 def _visitFunction(self, function, subprogram): 772 773 """ 774 A common function generator which transforms the given 'function' node 775 and initialises the given 'subprogram' appropriately. 776 """ 777 778 # Discover star and dstar parameters. 779 780 if function.flags & 4 != 0: 781 has_star = 1 782 else: 783 has_star = 0 784 if function.flags & 8 != 0: 785 has_dstar = 1 786 else: 787 has_dstar = 0 788 789 # Discover the number of defaults and positional parameters. 790 791 ndefaults = len(function.defaults) 792 npositional = len(function.argnames) - has_star - has_dstar 793 794 # Produce star and dstar parameters with appropriate defaults. 795 796 if has_star: 797 star = ( 798 function.argnames[npositional], 799 self.dispatch(compiler.ast.List([])) 800 ) 801 else: 802 star = None 803 if has_dstar: 804 dstar = ( 805 function.argnames[npositional + has_star], 806 self.dispatch(compiler.ast.Dict([])) 807 ) 808 else: 809 dstar = None 810 811 params = [] 812 for i in range(0, npositional - ndefaults): 813 params.append((function.argnames[i], None)) 814 815 # Process defaults. 816 817 for i in range(0, ndefaults): 818 default = function.defaults[i] 819 if default is not None: 820 params.append((function.argnames[npositional - ndefaults + i], self.dispatch(default))) 821 else: 822 params.append((function.argnames[npositional - ndefaults + i], None)) 823 824 subprogram.params = params 825 subprogram.star = star 826 subprogram.dstar = dstar 827 self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram 828 829 def visitFunction(self, function): 830 831 """ 832 Make a subprogram for the 'function' and record it outside the main 833 tree. Produce something like the following: 834 835 StoreName (name) 836 (expr) -> LoadRef (ref) -> Subprogram (params) 837 (star) 838 (dstar) 839 """ 840 841 # NOTE: Making the actual subprogram defining, too. 842 843 subprogram = Subprogram(function, 1, name=function.name, module=self.module, structures=self.current_structures[:], 844 internal=0, returns_value=1, star=None, dstar=None, is_method=self.within_class, original_def=function) 845 846 self.current_subprograms.append(subprogram) 847 within_class = self.within_class 848 self.within_class = 0 849 850 subprogram.code = self.dispatch(function.code) + [ReturnFromFunction()] 851 852 self.within_class = within_class 853 self.current_subprograms.pop() 854 self._visitFunction(function, subprogram) 855 856 # Make a definition of the function associating it with a name. 857 858 result = StoreName(function, 1, name=function.name, expr=LoadRef(ref=subprogram)) 859 return result 860 861 def visitGetattr(self, getattr): 862 result = LoadAttr(getattr, 1, 863 name=getattr.attrname, 864 expr=self.dispatch(getattr.expr) 865 ) 866 return result 867 868 def visitGlobal(self, global_): 869 result = Global(global_, 1, 870 names=global_.names 871 ) 872 return result 873 874 def visitIf(self, if_): 875 876 """ 877 Make conditionals for each test from an 'if_' AST node, adding the body 878 and putting each subsequent test as part of the conditional's else 879 section. 880 881 Convert... 882 883 If (test/body) 884 (test/body) 885 ... 886 (else/body) 887 888 ...to: 889 890 Conditional (test) -> (body) 891 (else) -> Conditional (test) -> (body) 892 (else) -> ... 893 """ 894 895 896 results = nodes = [] 897 898 # Produce something like... 899 # expr.__bool__() ? body 900 901 first = 1 902 for compare, stmt in if_.tests: 903 904 # Set the first as the defining node. 905 906 test = Conditional(if_, first, 907 test=InvokeFunction( 908 if_, 909 expr=LoadAttr( 910 expr=self.dispatch(compare), 911 name="__bool__" 912 ), 913 ) 914 ) 915 test.body = self.dispatch(stmt) 916 nodes.append(test) 917 nodes = test.else_ = [] 918 first = 0 919 920 # Add the compound statement from any else clause to the end. 921 922 if if_.else_ is not None: 923 nodes += self.dispatch(if_.else_) 924 925 result = results[0] 926 return result 927 928 def visitImport(self, import_): 929 result = Assign(import_, 1) 930 code = [] 931 _names = [] 932 for path, alias in import_.names: 933 importer = Import(name=path, alias=alias) 934 top = alias or path.split(".")[0] 935 code.append(StoreName(expr=importer, name=top)) 936 _names.append(code[-1].expr) 937 result.code = code 938 import_._names = _names 939 return result 940 941 def visitInvert(self, invert): 942 return self._visitUnary(invert, "__invert__") 943 944 def visitKeyword(self, keyword): 945 result = Keyword(keyword, 1, 946 name=keyword.name, 947 expr=self.dispatch(keyword.expr) 948 ) 949 return result 950 951 def visitLambda(self, lambda_): 952 953 # Make a subprogram for the function and record it outside the main 954 # tree. 955 # NOTE: Making the actual subprogram defining, too. 956 957 subprogram = Subprogram(lambda_, 1, name=None, module=self.module, internal=0, returns_value=1, star=None, dstar=None, original_def=lambda_) 958 self.current_subprograms.append(subprogram) 959 subprogram.code = [ReturnFromFunction(expr=self.dispatch(lambda_.code))] 960 self.current_subprograms.pop() 961 self._visitFunction(lambda_, subprogram) 962 963 # Get the subprogram reference to the lambda. 964 965 return LoadRef(lambda_, 1, ref=subprogram) 966 967 def visitList(self, list): 968 return self._visitBuiltin(list, "list") 969 970 def visitMod(self, mod): 971 return self._visitBinary(mod, "__mod__", "__rmod__") 972 973 def visitMul(self, mul): 974 return self._visitBinary(mul, "__mul__", "__rmul__") 975 976 def visitName(self, name): 977 result = LoadName(name, 1, name=name.name) 978 return result 979 980 def _visitNot(self, expr, not_=None): 981 invocation = InvokeFunction( 982 not_, # NOTE: May need a real original node. 983 expr=LoadAttr( 984 expr=expr, 985 name="__bool__" 986 ), 987 ) 988 if not_ is not None: 989 result = Not(not_, 1, expr=invocation) 990 else: 991 result = Not(expr=invocation) 992 return result 993 994 def visitNot(self, not_): 995 return self._visitNot(self.dispatch(not_.expr), not_) 996 997 def visitOr(self, or_): 998 999 """ 1000 Make a subprogram for the 'or_' node and record its contents inside the 1001 subprogram. Convert... 1002 1003 Or (test) 1004 (test) 1005 ... 1006 1007 ...to: 1008 1009 Subprogram -> Conditional (test) -> ReturnFromBlock ... 1010 (else) -> Conditional (test) -> ReturnFromBlock ... 1011 (else) -> ... 1012 """ 1013 1014 subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) 1015 self.current_subprograms.append(subprogram) 1016 1017 # In the subprogram, make instructions which store each operand, test 1018 # for each operand's truth status, and if appropriate return from the 1019 # subprogram with the value of the operand. 1020 1021 last = or_.nodes[-1] 1022 results = nodes = [] 1023 1024 for node in or_.nodes: 1025 expr = self.dispatch(node) 1026 1027 # Return from the subprogram where the test is satisfied. 1028 1029 if node is not last: 1030 nodes.append(StoreTemp(expr=expr)) 1031 invocation = InvokeFunction(or_, expr=LoadAttr(expr=LoadTemp(), name="__bool__")) 1032 test = Conditional(test=invocation, body=[ReturnFromBlock(expr=LoadTemp())]) 1033 nodes.append(test) 1034 1035 # Put subsequent operations in the else section of this conditional. 1036 1037 nodes = test.else_ = [ReleaseTemp()] 1038 1039 # For the last operation, return the result. 1040 1041 else: 1042 nodes.append( 1043 ReturnFromBlock(expr=expr) 1044 ) 1045 1046 # Finish the subprogram definition. 1047 1048 subprogram.code = results 1049 1050 self.current_subprograms.pop() 1051 self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram 1052 1053 # Make an invocation of the subprogram. 1054 1055 result = InvokeBlock(or_, 1, produces_result=1) 1056 result.expr = LoadRef(ref=subprogram) 1057 return result 1058 1059 def visitPass(self, pass_): 1060 return Pass(pass_, 1) 1061 1062 def visitPower(self, power): 1063 return self._visitBinary(power, "__pow__", "__rpow__") 1064 1065 def visitPrint(self, print_): 1066 1067 """ 1068 Convert... 1069 1070 Print (dest) -> 1071 (nodes) 1072 1073 ...to: 1074 1075 StoreTemp (index) -> "print" 1076 (expr) -> LoadAttr (expr) -> (dest) 1077 (name) -> "write" 1078 InvokeFunction (expr) -> LoadTemp (index) -> "print" 1079 (args) -> [(node)] 1080 ReleaseTemp (index) -> "print" 1081 """ 1082 1083 if print_.dest is not None: 1084 dest = self.dispatch(print_.dest) 1085 else: 1086 dest = self.dispatch(compiler.ast.Name("stdout")) 1087 1088 result = Assign(print_, 1, 1089 code=[ 1090 StoreTemp( 1091 index="print", 1092 expr=LoadAttr( 1093 expr=dest, 1094 name="write" 1095 ) 1096 ) 1097 ] 1098 ) 1099 1100 for node in print_.nodes: 1101 result.code.append( 1102 InvokeFunction( 1103 print_, 1104 expr=LoadTemp(index="print"), 1105 args=[self.dispatch(node)], 1106 star=None, 1107 dstar=None 1108 ) 1109 ) 1110 1111 result.code.append( 1112 ReleaseTemp(index="print") 1113 ) 1114 1115 return result 1116 1117 def visitPrintnl(self, printnl): 1118 result = self.visitPrint(printnl) 1119 result.code.insert( 1120 len(result.code) - 1, 1121 InvokeFunction( 1122 printnl, 1123 expr=LoadTemp(index="print"), 1124 args=[self.dispatch(compiler.ast.Const("\n"))], 1125 star=None, 1126 dstar=None 1127 ) 1128 ) 1129 return result 1130 1131 def visitRaise(self, raise_): 1132 result = Raise(raise_, 1) 1133 if raise_.expr2 is None: 1134 result.expr = self.dispatch(raise_.expr1) 1135 else: 1136 result.expr = InvokeFunction( 1137 raise_, 1138 expr=self.dispatch(raise_.expr1), 1139 args=[self.dispatch(raise_.expr2)], 1140 star=None, 1141 dstar=None 1142 ) 1143 if raise_.expr3 is not None: 1144 result.traceback = self.dispatch(raise_.expr3) 1145 else: 1146 result.traceback = None 1147 return result 1148 1149 def visitReturn(self, return_): 1150 result = ReturnFromFunction(return_, 1, 1151 expr=self.dispatch(return_.value) 1152 ) 1153 return result 1154 1155 def _visitSlice(self, slice, expr, lower, upper, flags, value=None): 1156 if flags == "OP_ASSIGN": 1157 result = InvokeFunction(slice, 1, 1158 expr=LoadAttr( 1159 expr=expr, 1160 name="__setslice__" 1161 ), 1162 star=None, 1163 dstar=None, 1164 args=[lower, upper, value] 1165 ) 1166 elif flags == "OP_APPLY": 1167 args = [] 1168 result = InvokeFunction(slice, 1, 1169 expr=LoadAttr( 1170 expr=expr, 1171 name="__getslice__" 1172 ), 1173 star=None, 1174 dstar=None, 1175 args=[lower, upper] 1176 ) 1177 elif flags == "OP_DELETE": 1178 args = [] 1179 result = InvokeFunction(slice, 1, 1180 expr=LoadAttr( 1181 expr=expr, 1182 name="__delslice__" 1183 ), 1184 star=None, 1185 dstar=None, 1186 args=[lower, upper] 1187 ) 1188 else: 1189 raise NotImplementedError, flags 1190 1191 return result 1192 1193 def visitSlice(self, slice, in_sequence=0): 1194 return self._visitSlice(slice, self.dispatch(slice.expr), self.dispatch_or_none(slice.lower), 1195 self.dispatch_or_none(slice.upper), slice.flags, self._visitAssNameOrAttr(slice, in_sequence)) 1196 1197 def visitStmt(self, stmt): 1198 return self.dispatches(stmt.nodes) 1199 1200 def visitSub(self, sub): 1201 return self._visitBinary(sub, "__sub__", "__rsub__") 1202 1203 def _visitSubscript(self, subscript, expr, subs, flags, value=None): 1204 if flags == "OP_ASSIGN": 1205 result = InvokeFunction(subscript, 1, 1206 expr=LoadAttr( 1207 expr=expr, 1208 name="__setitem__" 1209 ), 1210 star=None, 1211 dstar=None, 1212 args=[subs, value] 1213 ) 1214 elif flags == "OP_APPLY": 1215 args = [] 1216 result = InvokeFunction(subscript, 1, 1217 expr=LoadAttr( 1218 expr=expr, 1219 name="__getitem__" 1220 ), 1221 star=None, 1222 dstar=None, 1223 args=[subs] 1224 ) 1225 elif flags == "OP_DELETE": 1226 args = [] 1227 result = InvokeFunction(subscript, 1, 1228 expr=LoadAttr( 1229 expr=expr, 1230 name="__delitem__" 1231 ), 1232 star=None, 1233 dstar=None, 1234 args=[subs] 1235 ) 1236 else: 1237 raise NotImplementedError, flags 1238 1239 return result 1240 1241 def _visitSubscriptSubs(self, node, subs): 1242 if len(subs) == 1: 1243 return self.dispatch(subs[0]) 1244 else: 1245 return InvokeFunction(node, 1, 1246 expr=LoadName(name="tuple"), 1247 args=self.dispatches(subs), 1248 star=None, 1249 dstar=None 1250 ) 1251 1252 def visitSubscript(self, subscript, in_sequence=0): 1253 return self._visitSubscript( 1254 subscript, self.dispatch(subscript.expr), self._visitSubscriptSubs(subscript, subscript.subs), subscript.flags, 1255 self._visitAssNameOrAttr(subscript, in_sequence) 1256 ) 1257 1258 def visitTryExcept(self, tryexcept): 1259 1260 """ 1261 Make conditionals for each handler associated with a 'tryexcept' node. 1262 1263 Convert... 1264 1265 TryExcept (body) 1266 (else) 1267 (spec/assign/stmt) 1268 ... 1269 1270 ...to: 1271 1272 Try (body) 1273 (else) 1274 (handler) -> Conditional (test) -> (stmt) 1275 (body) -> ResetExc ... 1276 (else) -> Conditional (test) -> (stmt) 1277 (body) -> ResetExc ... 1278 (else) -> ... 1279 """ 1280 1281 result = Try(tryexcept, 1, body=[], else_=[], finally_=[]) 1282 1283 if tryexcept.body is not None: 1284 result.body = self.dispatch(tryexcept.body) 1285 if tryexcept.else_ is not None: 1286 result.else_ = self.dispatch(tryexcept.else_) 1287 1288 results = nodes = [] 1289 catch_all = 0 1290 1291 for spec, assign, stmt in tryexcept.handlers: 1292 1293 # If no specification exists, produce an unconditional block. 1294 1295 if spec is None: 1296 nodes += self.dispatch(stmt) 1297 catch_all = 1 1298 1299 # Produce an exception value check. 1300 1301 else: 1302 test = Conditional( 1303 isolate_test=1, 1304 test=CheckType(expr=LoadExc(), choices=self._visitTryExcept(spec)) 1305 ) 1306 test.body = [] 1307 1308 if assign is not None: 1309 test.body.append( 1310 Assign( 1311 code=[ 1312 StoreTemp(expr=LoadExc()), 1313 self.dispatch(assign), 1314 ReleaseTemp() 1315 ] 1316 ) 1317 ) 1318 1319 test.body += [ResetExc()] + self.dispatch(stmt) 1320 nodes.append(test) 1321 nodes = test.else_ = [] 1322 1323 # Add a raise operation to deal with unhandled exceptions. 1324 1325 if not catch_all: 1326 nodes.append( 1327 Raise( 1328 expr=LoadExc()) 1329 ) 1330 1331 result.handler = results 1332 return result 1333 1334 def _visitTryExcept(self, spec): 1335 1336 "Return a list of nodes for the given exception type 'spec'." 1337 1338 if isinstance(spec, compiler.ast.Tuple): 1339 nodes = [] 1340 for node in spec.nodes: 1341 nodes += self._visitTryExcept(node) 1342 else: 1343 nodes = [self.dispatch(spec)] 1344 return nodes 1345 1346 def visitTryFinally(self, tryfinally): 1347 result = Try(tryfinally, 1, body=[], else_=[], finally_=[]) 1348 if tryfinally.body is not None: 1349 result.body = self.dispatch(tryfinally.body) 1350 if tryfinally.final is not None: 1351 result.finally_ = self.dispatch(tryfinally.final) 1352 return result 1353 1354 def visitTuple(self, tuple): 1355 return self._visitBuiltin(tuple, "tuple") 1356 1357 def visitUnaryAdd(self, unaryadd): 1358 return self._visitUnary(unaryadd, "__pos__") 1359 1360 def visitUnarySub(self, unarysub): 1361 return self._visitUnary(unarysub, "__neg__") 1362 1363 def visitWhile(self, while_): 1364 1365 """ 1366 Make a subprogram for the 'while' node and record its contents inside the 1367 subprogram. Convert... 1368 1369 While (test) -> (body) 1370 (else) 1371 1372 ...to: 1373 1374 Subprogram -> Conditional (test) -> (body) -> Invoke subprogram 1375 (else) -> Conditional (test) -> ReturnFromBlock ... 1376 (else) -> ... 1377 """ 1378 1379 subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=0, params=[], star=None, dstar=None) 1380 self.current_subprograms.append(subprogram) 1381 1382 # Include a conditional statement in the subprogram. 1383 # Inside the conditional, add a recursive invocation to the subprogram 1384 # if the test condition was satisfied. 1385 # Return within the main section of the loop. 1386 1387 test = Conditional( 1388 test=InvokeFunction( 1389 while_, 1390 expr=LoadAttr( 1391 expr=self.dispatch(while_.test), 1392 name="__bool__"), 1393 ), 1394 body=self.dispatch(while_.body) + [ 1395 InvokeBlock( 1396 while_, 1397 expr=LoadRef(ref=subprogram) 1398 ), 1399 ReturnFromBlock() 1400 ], 1401 else_=[] 1402 ) 1403 1404 # Provide the else section, if present, along with an explicit return. 1405 1406 if while_.else_ is not None: 1407 test.else_ = self.dispatch(while_.else_) + [ReturnFromBlock()] 1408 1409 # Finish the subprogram definition. 1410 1411 subprogram.code = [test] 1412 1413 self.current_subprograms.pop() 1414 self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram 1415 1416 # Make an invocation of the subprogram. 1417 1418 result = InvokeBlock(while_, 1, 1419 expr=LoadRef(ref=subprogram) 1420 ) 1421 1422 # Make nice annotations for the viewer. 1423 1424 while_._test_call = subprogram.code[0].test 1425 1426 return result 1427 1428 # Convenience methods. 1429 1430 def _visitBinary(self, binary, left_name, right_name): 1431 return self._visitBinaryOp(binary, self.dispatch(binary.left), self.dispatch(binary.right), left_name, right_name) 1432 1433 def _visitBinaryCompareOp(self, binary, left, right, left_name, right_name): 1434 1435 """ 1436 Emulate the current mechanisms by producing nodes as follows: 1437 1438 InvokeBlock -> Subprogram -> StoreTemp (expr) -> x.__lt__(y) 1439 Conditional (test) -> __is__(LoadTemp, NotImplemented) 1440 (body) -> ReleaseTemp 1441 StoreTemp (expr) -> y.__gt__(x) 1442 Conditional (test) -> __is__(LoadTemp, NotImplemented) 1443 (body) -> ReturnFromBlock (expr) -> False 1444 (else) -> ReturnFromBlock (expr) -> LoadTemp 1445 (else) -> ReturnFromBlock (expr) -> LoadTemp 1446 """ 1447 1448 subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) 1449 self.current_subprograms.append(subprogram) 1450 1451 subprogram.code = [ 1452 StoreTemp( 1453 expr=InvokeFunction( 1454 binary, 1455 expr=LoadAttr(expr=left, name=left_name), 1456 args=[right], 1457 star=None, 1458 dstar=None) 1459 ), 1460 Conditional( 1461 isolate_test=1, 1462 test=CheckType( 1463 expr=LoadTemp(), choices=[LoadName(name="NotImplementedType")] 1464 ), 1465 body=[ 1466 ReleaseTemp(), 1467 StoreTemp( 1468 expr=InvokeFunction( 1469 binary, 1470 expr=LoadAttr(expr=right, name=right_name), 1471 args=[left], 1472 star=None, 1473 dstar=None) 1474 ), 1475 Conditional( 1476 isolate_test=1, 1477 test=CheckType( 1478 expr=LoadTemp(), choices=[LoadName(name="NotImplementedType")] 1479 ), 1480 body=[ 1481 ReturnFromBlock( 1482 expr=LoadName(name="False") 1483 ) 1484 ], 1485 else_=[ 1486 CheckType( 1487 inverted=1, expr=LoadTemp(), choices=[LoadName(name="NotImplementedType")] 1488 ), 1489 ReturnFromBlock( 1490 expr=LoadTemp() 1491 ) 1492 ] 1493 ) 1494 ], 1495 else_=[ 1496 CheckType( 1497 inverted=1, expr=LoadTemp(), choices=[LoadName(name="NotImplementedType")] 1498 ), 1499 ReturnFromBlock( 1500 expr=LoadTemp() 1501 ) 1502 ] 1503 ) 1504 ] 1505 1506 self.current_subprograms.pop() 1507 self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram 1508 1509 result = InvokeBlock( 1510 binary, 1511 produces_result=1, 1512 expr=LoadRef(ref=subprogram) 1513 ) 1514 1515 # Make nice annotations for the viewer. 1516 1517 binary._left_call = subprogram.code[0].expr 1518 binary._right_call = subprogram.code[1].body[1].expr 1519 1520 return result 1521 1522 def _visitBinaryOp(self, binary, left, right, left_name, right_name): 1523 1524 """ 1525 Emulate the current mechanisms by producing nodes as follows: 1526 1527 InvokeBlock -> Subprogram -> Try (body) -> ReturnFromBlock (expr) -> x.__add__(y) 1528 (else) 1529 (handler) -> Conditional (test) -> CheckType (expr) -> LoadExc 1530 (choices) -> LoadName TypeError 1531 (body) -> ReturnFromBlock (expr) -> y.__radd__(x) 1532 (else) 1533 """ 1534 1535 subprogram = Subprogram(name=None, module=self.module, internal=1, returns_value=1, params=[], star=None, dstar=None) 1536 self.current_subprograms.append(subprogram) 1537 1538 subprogram.code = [ 1539 Try(binary, 1, 1540 body=[ 1541 ReturnFromBlock( 1542 expr=InvokeFunction( 1543 binary, 1544 expr=LoadAttr(expr=left, name=left_name), 1545 args=[right], 1546 star=None, 1547 dstar=None) 1548 ) 1549 ], 1550 else_=[], 1551 finally_=[], 1552 handler=[ 1553 Conditional( 1554 test=CheckType(expr=LoadExc(), choices=[LoadName(name="TypeError")]), 1555 body=[ 1556 ReturnFromBlock( 1557 expr=InvokeFunction( 1558 binary, 1559 expr=LoadAttr(expr=right, name=right_name), 1560 args=[left], 1561 star=None, 1562 dstar=None) 1563 ) 1564 ], 1565 else_=[] 1566 ) 1567 ] 1568 ) 1569 ] 1570 1571 self.current_subprograms.pop() 1572 self.subprograms.append(subprogram); self.subnames[subprogram.full_name()] = subprogram 1573 1574 result = InvokeBlock( 1575 binary, 1576 produces_result=1, 1577 expr=LoadRef(ref=subprogram) 1578 ) 1579 1580 # Make nice annotations for the viewer. 1581 1582 binary._left_call = subprogram.code[0].body[0].expr 1583 binary._right_call = subprogram.code[0].handler[0].body[0].expr 1584 1585 return result 1586 1587 def _visitBuiltin(self, builtin, name): 1588 result = InvokeFunction(builtin, 1, expr=LoadName(name=name), args=self.dispatches(builtin.nodes), star=None, dstar=None) 1589 return result 1590 1591 def _visitUnary(self, unary, name): 1592 result = InvokeFunction(unary, 1, 1593 expr=LoadAttr( 1594 expr=self.dispatch(unary.expr), 1595 name=name 1596 ) 1597 ) 1598 1599 # Make nice annotations for the viewer. 1600 1601 unary._unary_call = result 1602 1603 return result 1604 1605 # Convenience functions. 1606 1607 def simplify(filename, builtins=0, module_name=None): 1608 1609 """ 1610 Simplify the module stored in the file with the given 'filename'. 1611 1612 If the optional 'builtins' parameter is set to a true value (the default 1613 being a false value), then the module is considered as the builtins module. 1614 """ 1615 1616 simplifier = Simplifier(builtins) 1617 module = compiler.parseFile(filename) 1618 compiler.misc.set_filename(filename, module) 1619 if builtins: 1620 name = module_name or "__builtins__" 1621 else: 1622 path, ext = os.path.splitext(filename) 1623 path, name = os.path.split(path) 1624 name = module_name or name 1625 simplified = simplifier.process(module, name) 1626 return simplified 1627 1628 # vim: tabstop=4 expandtab shiftwidth=4