1 """Python abstract syntax node definitions 2 3 This file was originally generated by Tools/compiler/astgen.py 4 """ 5 from compiler.consts import CO_VARARGS, CO_VARKEYWORDS 6 7 def flatten(seq): 8 l = [] 9 for elt in seq: 10 if isinstance(elt, (tuple, list)): 11 for elt2 in flatten(elt): 12 l.append(elt2) 13 else: 14 l.append(elt) 15 return l 16 17 def flatten_nodes(seq): 18 return [n for n in flatten(seq) if isinstance(n, Node)] 19 20 def flatten_statement(seq): 21 l = [] 22 for elt in seq: 23 if isinstance(elt, Stmt): 24 l += flatten_statement(elt) 25 else: 26 l.append(elt) 27 return l 28 29 def flatten_assignment(node): 30 l = [] 31 if isinstance(node, (AssList, AssTuple)): 32 for n in node.nodes: 33 l += flatten_assignment(n) 34 else: 35 l.append(node) 36 return l 37 38 def is_deletion(node): 39 return isinstance(node, (AssAttr, AssName)) and node.flags == "OP_DELETE" 40 41 def docstring(s): 42 if s.find("\n") != -1: 43 if s.find("'''") != -1: 44 return '"""%s"""' % s.replace('"""', '\\"\\"\\"') 45 else: 46 return "'''%s'''" % s.replace("'''", "\\'\\'\\'") 47 else: 48 return repr(s) 49 50 def indent(s): 51 return s.replace("\n", "\n\t") 52 53 def get_defaults(node): 54 55 "Return a list of (argument name, default) tuples for the 'node'." 56 57 star = (node.flags & 4 != 0) and 1 or 0 58 dstar = (node.flags & 8 != 0) and 1 or 0 59 argnames = node.argnames[:] 60 61 # Add stars to star and dstar parameters. 62 63 if star: 64 argnames[-dstar-star] = "*%s" % argnames[-dstar-star] 65 if dstar: 66 argnames[-dstar] = "**%s" % argnames[-dstar] 67 68 # Map defaults to parameters. 69 70 defaults = [None] * (len(node.argnames) - star - dstar - len(node.defaults)) + list(node.defaults) + [None] * (star + dstar) 71 return zip(argnames, defaults) 72 73 def decode_function(node): 74 return [(default and "%s=%s" % (argname, default) or argname) for (argname, default) in get_defaults(node)] 75 76 nodes = {} 77 78 class OperatorUser: 79 80 "Operator-related node." 81 82 pass 83 84 class Operator(OperatorUser): 85 86 "Operator node." 87 88 pass 89 90 class Node: 91 92 "Abstract base class for ast nodes." 93 94 def getChildren(self): 95 pass # implemented by subclasses 96 97 def __iter__(self): 98 for n in self.getChildren(): 99 yield n 100 101 def getChildNodes(self): 102 pass # implemented by subclasses 103 104 class EmptyNode(Node): 105 pass 106 107 class Expression(Node): 108 # Expression is an artificial node class to support "eval" 109 nodes["expression"] = "Expression" 110 def __init__(self, node): 111 self.node = node 112 113 def getChildren(self): 114 return self.node, 115 116 def getChildNodes(self): 117 return self.node, 118 119 def __repr__(self): 120 return "Expression(%r)" % (self.node,) 121 122 def __str__(self): 123 return str(self.node) 124 125 class Add(Node, Operator): 126 def __init__(self, leftright, lineno=None): 127 self.left = leftright[0] 128 self.right = leftright[1] 129 self.lineno = lineno 130 131 def getChildren(self): 132 return self.left, self.right 133 134 def getChildNodes(self): 135 return self.left, self.right 136 137 def __repr__(self): 138 return "Add((%r, %r))" % (self.left, self.right) 139 140 def __str__(self): 141 return "(%s + %s)" % (self.left, self.right) 142 143 class And(Node): 144 def __init__(self, nodes, lineno=None): 145 self.nodes = nodes 146 self.lineno = lineno 147 148 def getChildren(self): 149 return tuple(flatten(self.nodes)) 150 151 def getChildNodes(self): 152 nodelist = [] 153 nodelist.extend(flatten_nodes(self.nodes)) 154 return tuple(nodelist) 155 156 def __repr__(self): 157 return "And(%r)" % (self.nodes,) 158 159 def __str__(self): 160 return "(%s)" % " and ".join(map(str, self.nodes)) 161 162 class AssAttr(Node): 163 def __init__(self, expr, attrname, flags, lineno=None): 164 self.expr = expr 165 self.attrname = attrname 166 self.flags = flags 167 self.lineno = lineno 168 169 def getChildren(self): 170 return self.expr, self.attrname, self.flags 171 172 def getChildNodes(self): 173 return self.expr, 174 175 def __repr__(self): 176 return "AssAttr(%r, %r, %r)" % (self.expr, self.attrname, self.flags) 177 178 def __str__(self): 179 return "%s%s.%s" % (self.flags == "OP_DELETE" and "del " or "", self.expr, self.attrname) 180 181 class AssList(Node): 182 def __init__(self, nodes, lineno=None): 183 self.nodes = nodes 184 self.lineno = lineno 185 186 def getChildren(self): 187 return tuple(flatten(self.nodes)) 188 189 def getChildNodes(self): 190 nodelist = [] 191 nodelist.extend(flatten_nodes(self.nodes)) 192 return tuple(nodelist) 193 194 def __repr__(self): 195 return "AssList(%r)" % (self.nodes,) 196 197 def __str__(self): 198 nodes = flatten_assignment(self) 199 if nodes and is_deletion(nodes[0]): 200 return "; ".join(map(str, self.nodes)) 201 else: 202 return "[%s]" % ", ".join(map(str, self.nodes)) 203 204 class AssName(Node): 205 def __init__(self, name, flags, lineno=None): 206 self.name = name 207 self.flags = flags 208 self.lineno = lineno 209 210 def getChildren(self): 211 return self.name, self.flags 212 213 def getChildNodes(self): 214 return () 215 216 def __repr__(self): 217 return "AssName(%r, %r)" % (self.name, self.flags) 218 219 def __str__(self): 220 return "%s%s" % (self.flags == "OP_DELETE" and "del " or "", self.name) 221 222 class AssTuple(Node): 223 def __init__(self, nodes, lineno=None): 224 self.nodes = nodes 225 self.lineno = lineno 226 227 def getChildren(self): 228 return tuple(flatten(self.nodes)) 229 230 def getChildNodes(self): 231 nodelist = [] 232 nodelist.extend(flatten_nodes(self.nodes)) 233 return tuple(nodelist) 234 235 def __repr__(self): 236 return "AssTuple(%r)" % (self.nodes,) 237 238 def __str__(self): 239 nodes = flatten_assignment(self) 240 if nodes and is_deletion(nodes[0]): 241 return "; ".join(map(str, self.nodes)) 242 else: 243 return "(%s)" % ", ".join(map(str, self.nodes)) 244 245 class Assert(Node): 246 def __init__(self, test, fail, lineno=None): 247 self.test = test 248 self.fail = fail 249 self.lineno = lineno 250 251 def getChildren(self): 252 children = [] 253 children.append(self.test) 254 children.append(self.fail) 255 return tuple(children) 256 257 def getChildNodes(self): 258 nodelist = [] 259 nodelist.append(self.test) 260 if self.fail is not None: 261 nodelist.append(self.fail) 262 return tuple(nodelist) 263 264 def __repr__(self): 265 return "Assert(%r, %r)" % (self.test, self.fail) 266 267 def __str__(self): 268 return "assert %s%s" % (self.test, self.fail and ", %s" % self.fail or "") 269 270 class Assign(Node): 271 def __init__(self, nodes, expr, lineno=None): 272 self.nodes = nodes 273 self.expr = expr 274 self.lineno = lineno 275 276 def getChildren(self): 277 children = [] 278 children.extend(flatten(self.nodes)) 279 children.append(self.expr) 280 return tuple(children) 281 282 def getChildNodes(self): 283 nodelist = [] 284 nodelist.extend(flatten_nodes(self.nodes)) 285 nodelist.append(self.expr) 286 return tuple(nodelist) 287 288 def __repr__(self): 289 return "Assign(%r, %r)" % (self.nodes, self.expr) 290 291 def __str__(self): 292 return "%s = %s" % (", ".join(map(str, self.nodes)), self.expr) 293 294 class AugAssign(Node, OperatorUser): 295 def __init__(self, node, op, expr, lineno=None): 296 self.node = node 297 self.op = op 298 self.expr = expr 299 self.lineno = lineno 300 301 def getChildren(self): 302 return self.node, self.op, self.expr 303 304 def getChildNodes(self): 305 return self.node, self.expr 306 307 def __repr__(self): 308 return "AugAssign(%r, %r, %r)" % (self.node, self.op, self.expr) 309 310 def __str__(self): 311 return "%s %s %s" % (self.node, self.op, self.expr) 312 313 class Backquote(Node): 314 def __init__(self, expr, lineno=None): 315 self.expr = expr 316 self.lineno = lineno 317 318 def getChildren(self): 319 return self.expr, 320 321 def getChildNodes(self): 322 return self.expr, 323 324 def __repr__(self): 325 return "Backquote(%r)" % (self.expr,) 326 327 def __str__(self): 328 return "`%s`" % self.expr 329 330 class Bitand(Node, Operator): 331 def __init__(self, nodes, lineno=None): 332 self.nodes = nodes 333 self.lineno = lineno 334 335 def getChildren(self): 336 return tuple(flatten(self.nodes)) 337 338 def getChildNodes(self): 339 nodelist = [] 340 nodelist.extend(flatten_nodes(self.nodes)) 341 return tuple(nodelist) 342 343 def __repr__(self): 344 return "Bitand(%r)" % (self.nodes,) 345 346 def __str__(self): 347 return "(%s)" % " & ".join(map(str, self.nodes)) 348 349 class Bitor(Node, Operator): 350 def __init__(self, nodes, lineno=None): 351 self.nodes = nodes 352 self.lineno = lineno 353 354 def getChildren(self): 355 return tuple(flatten(self.nodes)) 356 357 def getChildNodes(self): 358 nodelist = [] 359 nodelist.extend(flatten_nodes(self.nodes)) 360 return tuple(nodelist) 361 362 def __repr__(self): 363 return "Bitor(%r)" % (self.nodes,) 364 365 def __str__(self): 366 return "(%s)" % " | ".join(map(str, self.nodes)) 367 368 class Bitxor(Node, Operator): 369 def __init__(self, nodes, lineno=None): 370 self.nodes = nodes 371 self.lineno = lineno 372 373 def getChildren(self): 374 return tuple(flatten(self.nodes)) 375 376 def getChildNodes(self): 377 nodelist = [] 378 nodelist.extend(flatten_nodes(self.nodes)) 379 return tuple(nodelist) 380 381 def __repr__(self): 382 return "Bitxor(%r)" % (self.nodes,) 383 384 def __str__(self): 385 return "(%s)" % " ^ ".join(map(str, self.nodes)) 386 387 class Break(Node): 388 def __init__(self, lineno=None): 389 self.lineno = lineno 390 391 def getChildren(self): 392 return () 393 394 def getChildNodes(self): 395 return () 396 397 def __repr__(self): 398 return "Break()" 399 400 def __str__(self): 401 return "break" 402 403 class CallFunc(Node): 404 def __init__(self, node, args, star_args = None, dstar_args = None, lineno=None): 405 self.node = node 406 self.args = args 407 self.star_args = star_args 408 self.dstar_args = dstar_args 409 self.lineno = lineno 410 411 def getChildren(self): 412 children = [] 413 children.append(self.node) 414 children.extend(flatten(self.args)) 415 children.append(self.star_args) 416 children.append(self.dstar_args) 417 return tuple(children) 418 419 def getChildNodes(self): 420 nodelist = [] 421 nodelist.append(self.node) 422 nodelist.extend(flatten_nodes(self.args)) 423 if self.star_args is not None: 424 nodelist.append(self.star_args) 425 if self.dstar_args is not None: 426 nodelist.append(self.dstar_args) 427 return tuple(nodelist) 428 429 def __repr__(self): 430 args = [] 431 if self.dstar_args: 432 args.insert(0, repr(self.dstar_args)) 433 if args or self.star_args: 434 args.insert(0, repr(self.star_args)) 435 return "CallFunc(%r, %r%s)" % (self.node, self.args, args and (", %s" % ", ".join(args)) or "") 436 437 def __str__(self): 438 star_args = self.star_args and ["*%s" % self.star_args] or [] 439 dstar_args = self.dstar_args and ["**%s" % self.dstar_args] or [] 440 return "%s(%s)" % (self.node, ", ".join(map(str, tuple(self.args) + tuple(star_args) + tuple(dstar_args)))) 441 442 class Class(Node): 443 def __init__(self, name, bases, doc, code, decorators = None, lineno=None): 444 self.name = name 445 self.bases = bases 446 self.doc = doc 447 self.code = code 448 self.decorators = decorators 449 self.lineno = lineno 450 451 def getChildren(self): 452 children = [] 453 children.append(self.name) 454 children.extend(flatten(self.bases)) 455 children.append(self.doc) 456 children.append(self.code) 457 children.append(self.decorators) 458 return tuple(children) 459 460 def getChildNodes(self): 461 nodelist = [] 462 nodelist.extend(flatten_nodes(self.bases)) 463 nodelist.append(self.code) 464 if self.decorators is not None: 465 nodelist.append(self.decorators) 466 return tuple(nodelist) 467 468 def __repr__(self): 469 return "Class(%r, %r, %r, %r, %r)" % (self.name, self.bases, self.doc, self.code, self.decorators) 470 471 def __str__(self): 472 return "%sclass %s%s:%s%s\n" % ( 473 self.decorators and "%s\n" % "\n".join([("@%s" % decorator) for decorator in self.decorators]) or "", 474 self.name, 475 self.bases and "(%s)" % ", ".join(map(str, self.bases)) or "", 476 self.doc and "\n\t" + docstring(self.doc) or "", 477 indent("\n%s" % self.code) 478 ) 479 480 class Compare(Node, OperatorUser): 481 def __init__(self, expr, ops, lineno=None): 482 self.expr = expr 483 self.ops = ops 484 self.lineno = lineno 485 486 def getChildren(self): 487 children = [] 488 children.append(self.expr) 489 children.extend(flatten(self.ops)) 490 return tuple(children) 491 492 def getChildNodes(self): 493 nodelist = [] 494 nodelist.append(self.expr) 495 nodelist.extend(flatten_nodes(self.ops)) 496 return tuple(nodelist) 497 498 def __repr__(self): 499 return "Compare(%r, %r)" % (self.expr, self.ops) 500 501 def __str__(self): 502 return "%s %s" % (self.expr, " ".join([("%s %s" % op) for op in self.ops])) 503 504 class Const(Node): 505 def __init__(self, value, lineno=None): 506 self.value = value 507 self.lineno = lineno 508 509 def getChildren(self): 510 return self.value, 511 512 def getChildNodes(self): 513 return () 514 515 def __repr__(self): 516 return "Const(%r)" % (self.value,) 517 518 def __str__(self): 519 return repr(self.value) 520 521 class Continue(Node): 522 def __init__(self, lineno=None): 523 self.lineno = lineno 524 525 def getChildren(self): 526 return () 527 528 def getChildNodes(self): 529 return () 530 531 def __repr__(self): 532 return "Continue()" 533 534 def __str__(self): 535 return "continue" 536 537 class Decorators(Node): 538 def __init__(self, nodes, lineno=None): 539 self.nodes = nodes 540 self.lineno = lineno 541 542 def getChildren(self): 543 return tuple(flatten(self.nodes)) 544 545 def getChildNodes(self): 546 nodelist = [] 547 nodelist.extend(flatten_nodes(self.nodes)) 548 return tuple(nodelist) 549 550 def __repr__(self): 551 return "Decorators(%r)" % (self.nodes,) 552 553 def __str__(self): 554 return "\n".join([("@%s" % node) for node in self.nodes]) 555 556 class Dict(Node): 557 def __init__(self, items, lineno=None): 558 self.items = items 559 self.lineno = lineno 560 561 def getChildren(self): 562 return tuple(flatten(self.items)) 563 564 def getChildNodes(self): 565 nodelist = [] 566 nodelist.extend(flatten_nodes(self.items)) 567 return tuple(nodelist) 568 569 def __repr__(self): 570 return "Dict(%r)" % (self.items,) 571 572 def __str__(self): 573 return "{%s}" % ", ".join([("%s : %s" % (key, value)) for (key, value) in self.items]) 574 575 class Discard(Node): 576 def __init__(self, expr, lineno=None): 577 self.expr = expr 578 self.lineno = lineno 579 580 def getChildren(self): 581 return self.expr, 582 583 def getChildNodes(self): 584 return self.expr, 585 586 def __repr__(self): 587 return "Discard(%r)" % (self.expr,) 588 589 def __str__(self): 590 return str(self.expr) 591 592 class Div(Node, Operator): 593 def __init__(self, leftright, lineno=None): 594 self.left = leftright[0] 595 self.right = leftright[1] 596 self.lineno = lineno 597 598 def getChildren(self): 599 return self.left, self.right 600 601 def getChildNodes(self): 602 return self.left, self.right 603 604 def __repr__(self): 605 return "Div((%r, %r))" % (self.left, self.right) 606 607 def __str__(self): 608 return "(%s / %s)" % (self.left, self.right) 609 610 class Ellipsis(Node): 611 def __init__(self, lineno=None): 612 self.lineno = lineno 613 614 def getChildren(self): 615 return () 616 617 def getChildNodes(self): 618 return () 619 620 def __repr__(self): 621 return "Ellipsis()" 622 623 def __str__(self): 624 return "..." 625 626 class Exec(Node): 627 def __init__(self, expr, locals, globals, lineno=None): 628 self.expr = expr 629 self.locals = locals 630 self.globals = globals 631 self.lineno = lineno 632 633 def getChildren(self): 634 children = [] 635 children.append(self.expr) 636 children.append(self.locals) 637 children.append(self.globals) 638 return tuple(children) 639 640 def getChildNodes(self): 641 nodelist = [] 642 nodelist.append(self.expr) 643 if self.locals is not None: 644 nodelist.append(self.locals) 645 if self.globals is not None: 646 nodelist.append(self.globals) 647 return tuple(nodelist) 648 649 def __repr__(self): 650 return "Exec(%r, %r, %r)" % (self.expr, self.locals, self.globals) 651 652 def __str__(self): 653 return "exec %s%s%s" % (self.expr, self.locals and "in %s" % self.locals or "", 654 self.globals and ", %s" % self.globals or "") 655 656 class FloorDiv(Node, Operator): 657 def __init__(self, leftright, lineno=None): 658 self.left = leftright[0] 659 self.right = leftright[1] 660 self.lineno = lineno 661 662 def getChildren(self): 663 return self.left, self.right 664 665 def getChildNodes(self): 666 return self.left, self.right 667 668 def __repr__(self): 669 return "FloorDiv((%r, %r))" % (self.left, self.right) 670 671 def __str__(self): 672 return "(%s // %s)" % (self.left, self.right) 673 674 class For(Node): 675 def __init__(self, assign, list, body, else_, lineno=None): 676 self.assign = assign 677 self.list = list 678 self.body = body 679 self.else_ = else_ 680 self.lineno = lineno 681 682 def getChildren(self): 683 children = [] 684 children.append(self.assign) 685 children.append(self.list) 686 children.append(self.body) 687 children.append(self.else_) 688 return tuple(children) 689 690 def getChildNodes(self): 691 nodelist = [] 692 nodelist.append(self.assign) 693 nodelist.append(self.list) 694 nodelist.append(self.body) 695 if self.else_ is not None: 696 nodelist.append(self.else_) 697 return tuple(nodelist) 698 699 def __repr__(self): 700 return "For(%r, %r, %r, %r)" % (self.assign, self.list, self.body, self.else_) 701 702 def __str__(self): 703 return "for %s in %s:%s%s" % ( 704 self.assign, self.list, 705 indent("\n%s" % self.body), 706 self.else_ and "\nelse:%s" % indent("\n%s" % self.else_) or "" 707 ) 708 709 class From(Node): 710 def __init__(self, modname, names, level, lineno=None): 711 self.modname = modname 712 self.names = names 713 self.level = level 714 self.lineno = lineno 715 716 def getChildren(self): 717 return self.modname, self.names, self.level 718 719 def getChildNodes(self): 720 return () 721 722 def __repr__(self): 723 return "From(%r, %r, %r)" % (self.modname, self.names, self.level) 724 725 def __str__(self): 726 return "from %s import %s" % (self.modname, 727 ", ".join([(alias and "%s as %s" % (name, alias) or name) for (name, alias) in self.names])) 728 729 class Function(Node): 730 def __init__(self, decorators, name, argnames, defaults, flags, doc, code, lineno=None): 731 self.decorators = decorators 732 self.name = name 733 self.argnames = argnames 734 self.defaults = defaults 735 self.flags = flags 736 self.doc = doc 737 self.code = code 738 self.lineno = lineno 739 self.varargs = self.kwargs = None 740 if flags & CO_VARARGS: 741 self.varargs = 1 742 if flags & CO_VARKEYWORDS: 743 self.kwargs = 1 744 745 def getChildren(self): 746 children = [] 747 children.append(self.decorators) 748 children.append(self.name) 749 children.append(self.argnames) 750 children.extend(flatten(self.defaults)) 751 children.append(self.flags) 752 children.append(self.doc) 753 children.append(self.code) 754 return tuple(children) 755 756 def getChildNodes(self): 757 nodelist = [] 758 if self.decorators is not None: 759 nodelist.append(self.decorators) 760 nodelist.extend(flatten_nodes(self.defaults)) 761 nodelist.append(self.code) 762 return tuple(nodelist) 763 764 def __repr__(self): 765 return "Function(%r, %r, %r, %r, %r, %r, %r)" % (self.decorators, self.name, self.argnames, self.defaults, self.flags, self.doc, self.code) 766 767 def __str__(self): 768 parameters = decode_function(self) 769 770 return "%sdef %s(%s):%s%s\n" % ( 771 self.decorators and "%s\n" % "\n".join([("@%s" % decorator) for decorator in self.decorators]) or "", 772 self.name, 773 ", ".join(parameters), 774 self.doc and "\n\n\t%s\n" % docstring(self.doc) or "", 775 indent("\n%s" % self.code) 776 ) 777 778 class GenExpr(Node): 779 def __init__(self, code, lineno=None): 780 self.code = code 781 self.lineno = lineno 782 self.argnames = ['.0'] 783 self.varargs = self.kwargs = None 784 785 def getChildren(self): 786 return self.code, 787 788 def getChildNodes(self): 789 return self.code, 790 791 def __repr__(self): 792 return "GenExpr(%r)" % (self.code,) 793 794 def __str__(self): 795 return str(self.code) 796 797 class GenExprFor(Node): 798 def __init__(self, assign, iter, ifs, lineno=None): 799 self.assign = assign 800 self.iter = iter 801 self.ifs = ifs 802 self.lineno = lineno 803 self.is_outmost = False 804 805 def getChildren(self): 806 children = [] 807 children.append(self.assign) 808 children.append(self.iter) 809 children.extend(flatten(self.ifs)) 810 return tuple(children) 811 812 def getChildNodes(self): 813 nodelist = [] 814 nodelist.append(self.assign) 815 nodelist.append(self.iter) 816 nodelist.extend(flatten_nodes(self.ifs)) 817 return tuple(nodelist) 818 819 def __repr__(self): 820 return "GenExprFor(%r, %r, %r)" % (self.assign, self.iter, self.ifs) 821 822 def __str__(self): 823 return "for %s in %s%s" % ( 824 self.assign, self.iter, 825 self.ifs and " ".join(map(str, self.ifs)) or "" 826 ) 827 828 class GenExprIf(Node): 829 def __init__(self, test, lineno=None): 830 self.test = test 831 self.lineno = lineno 832 833 def getChildren(self): 834 return self.test, 835 836 def getChildNodes(self): 837 return self.test, 838 839 def __repr__(self): 840 return "GenExprIf(%r)" % (self.test,) 841 842 def __str__(self): 843 return "if %s" % self.test 844 845 class GenExprInner(Node): 846 def __init__(self, expr, quals, lineno=None): 847 self.expr = expr 848 self.quals = quals 849 self.lineno = lineno 850 851 def getChildren(self): 852 children = [] 853 children.append(self.expr) 854 children.extend(flatten(self.quals)) 855 return tuple(children) 856 857 def getChildNodes(self): 858 nodelist = [] 859 nodelist.append(self.expr) 860 nodelist.extend(flatten_nodes(self.quals)) 861 return tuple(nodelist) 862 863 def __repr__(self): 864 return "GenExprInner(%r, %r)" % (self.expr, self.quals) 865 866 def __str__(self): 867 return "%s %s" % (self.expr, " ".join(map(str, self.quals))) 868 869 class Getattr(Node): 870 def __init__(self, expr, attrname, lineno=None): 871 self.expr = expr 872 self.attrname = attrname 873 self.lineno = lineno 874 875 def getChildren(self): 876 return self.expr, self.attrname 877 878 def getChildNodes(self): 879 return self.expr, 880 881 def __repr__(self): 882 return "Getattr(%r, %r)" % (self.expr, self.attrname) 883 884 def __str__(self): 885 return "%s.%s" % (self.expr, self.attrname) 886 887 class Global(Node): 888 def __init__(self, names, lineno=None): 889 self.names = names 890 self.lineno = lineno 891 892 def getChildren(self): 893 return self.names, 894 895 def getChildNodes(self): 896 return () 897 898 def __repr__(self): 899 return "Global(%r)" % (self.names,) 900 901 def __str__(self): 902 return "global %s" % ", ".join(map(str, self.names)) 903 904 class If(Node): 905 def __init__(self, tests, else_, lineno=None): 906 self.tests = tests 907 self.else_ = else_ 908 self.lineno = lineno 909 910 def getChildren(self): 911 children = [] 912 children.extend(flatten(self.tests)) 913 children.append(self.else_) 914 return tuple(children) 915 916 def getChildNodes(self): 917 nodelist = [] 918 nodelist.extend(flatten_nodes(self.tests)) 919 if self.else_ is not None: 920 nodelist.append(self.else_) 921 return tuple(nodelist) 922 923 def __repr__(self): 924 return "If(%r, %r)" % (self.tests, self.else_) 925 926 def __str__(self): 927 tests = [("%sif %s:%s" % (i > 0 and "el" or "", test, indent("\n%s" % body))) for (i, (test, body)) in enumerate(self.tests)] 928 return "%s%s" % ( 929 "\n".join(tests), 930 self.else_ and "\nelse:%s" % indent("\n%s" % self.else_) or "" 931 ) 932 933 class IfExp(Node): 934 def __init__(self, test, then, else_, lineno=None): 935 self.test = test 936 self.then = then 937 self.else_ = else_ 938 self.lineno = lineno 939 940 def getChildren(self): 941 return self.test, self.then, self.else_ 942 943 def getChildNodes(self): 944 return self.test, self.then, self.else_ 945 946 def __repr__(self): 947 return "IfExp(%r, %r, %r)" % (self.test, self.then, self.else_) 948 949 def __str__(self): 950 return "%s if %s else %s" % (self.then, self.test, self.else_) 951 952 class Import(Node): 953 def __init__(self, names, lineno=None): 954 self.names = names 955 self.lineno = lineno 956 957 def getChildren(self): 958 return self.names, 959 960 def getChildNodes(self): 961 return () 962 963 def __repr__(self): 964 return "Import(%r)" % (self.names,) 965 966 def __str__(self): 967 return "import %s" % ( 968 ", ".join([(alias and "%s as %s" % (name, alias) or name) for (name, alias) in self.names])) 969 970 class Invert(Node, Operator): 971 def __init__(self, expr, lineno=None): 972 self.expr = expr 973 self.lineno = lineno 974 975 def getChildren(self): 976 return self.expr, 977 978 def getChildNodes(self): 979 return self.expr, 980 981 def __repr__(self): 982 return "Invert(%r)" % (self.expr,) 983 984 def __str__(self): 985 return "~%s" % self.expr 986 987 class Keyword(Node): 988 def __init__(self, name, expr, lineno=None): 989 self.name = name 990 self.expr = expr 991 self.lineno = lineno 992 993 def getChildren(self): 994 return self.name, self.expr 995 996 def getChildNodes(self): 997 return self.expr, 998 999 def __repr__(self): 1000 return "Keyword(%r, %r)" % (self.name, self.expr) 1001 1002 def __str__(self): 1003 return "%s=%s" % (self.name, self.expr) 1004 1005 class Lambda(Node): 1006 def __init__(self, argnames, defaults, flags, code, lineno=None): 1007 self.argnames = argnames 1008 self.defaults = defaults 1009 self.flags = flags 1010 self.code = code 1011 self.lineno = lineno 1012 self.varargs = self.kwargs = None 1013 if flags & CO_VARARGS: 1014 self.varargs = 1 1015 if flags & CO_VARKEYWORDS: 1016 self.kwargs = 1 1017 1018 def getChildren(self): 1019 children = [] 1020 children.append(self.argnames) 1021 children.extend(flatten(self.defaults)) 1022 children.append(self.flags) 1023 children.append(self.code) 1024 return tuple(children) 1025 1026 def getChildNodes(self): 1027 nodelist = [] 1028 nodelist.extend(flatten_nodes(self.defaults)) 1029 nodelist.append(self.code) 1030 return tuple(nodelist) 1031 1032 def __repr__(self): 1033 return "Lambda(%r, %r, %r, %r)" % (self.argnames, self.defaults, self.flags, self.code) 1034 1035 def __str__(self): 1036 parameters = decode_function(self) 1037 return "lambda %s: %s" % (", ".join(parameters), self.code) 1038 1039 class LeftShift(Node, Operator): 1040 def __init__(self, leftright, lineno=None): 1041 self.left = leftright[0] 1042 self.right = leftright[1] 1043 self.lineno = lineno 1044 1045 def getChildren(self): 1046 return self.left, self.right 1047 1048 def getChildNodes(self): 1049 return self.left, self.right 1050 1051 def __repr__(self): 1052 return "LeftShift((%r, %r))" % (self.left, self.right) 1053 1054 def __str__(self): 1055 return "(%s << %s)" % (self.left, self.right) 1056 1057 class List(Node): 1058 def __init__(self, nodes, lineno=None): 1059 self.nodes = nodes 1060 self.lineno = lineno 1061 1062 def getChildren(self): 1063 return tuple(flatten(self.nodes)) 1064 1065 def getChildNodes(self): 1066 nodelist = [] 1067 nodelist.extend(flatten_nodes(self.nodes)) 1068 return tuple(nodelist) 1069 1070 def __repr__(self): 1071 return "List(%r)" % (self.nodes,) 1072 1073 def __str__(self): 1074 return "[%s]" % ", ".join(map(str, self.nodes)) 1075 1076 class ListComp(Node): 1077 def __init__(self, expr, quals, lineno=None): 1078 self.expr = expr 1079 self.quals = quals 1080 self.lineno = lineno 1081 1082 def getChildren(self): 1083 children = [] 1084 children.append(self.expr) 1085 children.extend(flatten(self.quals)) 1086 return tuple(children) 1087 1088 def getChildNodes(self): 1089 nodelist = [] 1090 nodelist.append(self.expr) 1091 nodelist.extend(flatten_nodes(self.quals)) 1092 return tuple(nodelist) 1093 1094 def __repr__(self): 1095 return "ListComp(%r, %r)" % (self.expr, self.quals) 1096 1097 def __str__(self): 1098 return "[%s %s]" % (self.expr, " ".join(map(str, self.quals))) 1099 1100 class ListCompFor(Node): 1101 def __init__(self, assign, list, ifs, lineno=None): 1102 self.assign = assign 1103 self.list = list 1104 self.ifs = ifs 1105 self.lineno = lineno 1106 1107 def getChildren(self): 1108 children = [] 1109 children.append(self.assign) 1110 children.append(self.list) 1111 children.extend(flatten(self.ifs)) 1112 return tuple(children) 1113 1114 def getChildNodes(self): 1115 nodelist = [] 1116 nodelist.append(self.assign) 1117 nodelist.append(self.list) 1118 nodelist.extend(flatten_nodes(self.ifs)) 1119 return tuple(nodelist) 1120 1121 def __repr__(self): 1122 return "ListCompFor(%r, %r, %r)" % (self.assign, self.list, self.ifs) 1123 1124 def __str__(self): 1125 return "for %s in %s%s" % ( 1126 self.assign, self.list, 1127 self.ifs and " ".join(map(str, self.ifs)) or "" 1128 ) 1129 1130 class ListCompIf(Node): 1131 def __init__(self, test, lineno=None): 1132 self.test = test 1133 self.lineno = lineno 1134 1135 def getChildren(self): 1136 return self.test, 1137 1138 def getChildNodes(self): 1139 return self.test, 1140 1141 def __repr__(self): 1142 return "ListCompIf(%r)" % (self.test,) 1143 1144 def __str__(self): 1145 return " if %s" % self.test 1146 1147 class SetComp(Node): 1148 def __init__(self, expr, quals, lineno=None): 1149 self.expr = expr 1150 self.quals = quals 1151 self.lineno = lineno 1152 1153 def getChildren(self): 1154 children = [] 1155 children.append(self.expr) 1156 children.extend(flatten(self.quals)) 1157 return tuple(children) 1158 1159 def getChildNodes(self): 1160 nodelist = [] 1161 nodelist.append(self.expr) 1162 nodelist.extend(flatten_nodes(self.quals)) 1163 return tuple(nodelist) 1164 1165 def __repr__(self): 1166 return "SetComp(%r, %r)" % (self.expr, self.quals) 1167 1168 def __str__(self): 1169 return "{%s %s}" % (self.expr, " ".join(map(str, self.quals))) 1170 1171 class DictComp(Node): 1172 def __init__(self, key, value, quals, lineno=None): 1173 self.key = key 1174 self.value = value 1175 self.quals = quals 1176 self.lineno = lineno 1177 1178 def getChildren(self): 1179 children = [] 1180 children.append(self.key) 1181 children.append(self.value) 1182 children.extend(flatten(self.quals)) 1183 return tuple(children) 1184 1185 def getChildNodes(self): 1186 nodelist = [] 1187 nodelist.append(self.key) 1188 nodelist.append(self.value) 1189 nodelist.extend(flatten_nodes(self.quals)) 1190 return tuple(nodelist) 1191 1192 def __repr__(self): 1193 return "DictComp(%r, %r, %r)" % (self.key, self.value, self.quals) 1194 1195 def __str__(self): 1196 return "{%s : %s %s}" % (self.key, self.value, " ".join(map(str, self.quals))) 1197 1198 class Mod(Node, Operator): 1199 def __init__(self, leftright, lineno=None): 1200 self.left = leftright[0] 1201 self.right = leftright[1] 1202 self.lineno = lineno 1203 1204 def getChildren(self): 1205 return self.left, self.right 1206 1207 def getChildNodes(self): 1208 return self.left, self.right 1209 1210 def __repr__(self): 1211 return "Mod((%r, %r))" % (self.left, self.right) 1212 1213 def __str__(self): 1214 return "(%s %% %s)" % (self.left, self.right) 1215 1216 class Module(Node): 1217 def __init__(self, doc, node, lineno=None): 1218 self.doc = doc 1219 self.node = node 1220 self.lineno = lineno 1221 1222 def getChildren(self): 1223 return self.doc, self.node 1224 1225 def getChildNodes(self): 1226 return self.node, 1227 1228 def __repr__(self): 1229 return "Module(%r, %r)" % (self.doc, self.node) 1230 1231 def __str__(self): 1232 return "%s%s" % (self.doc and "%s\n\n" % docstring(self.doc) or "", self.node) 1233 1234 class Mul(Node, Operator): 1235 def __init__(self, leftright, lineno=None): 1236 self.left = leftright[0] 1237 self.right = leftright[1] 1238 self.lineno = lineno 1239 1240 def getChildren(self): 1241 return self.left, self.right 1242 1243 def getChildNodes(self): 1244 return self.left, self.right 1245 1246 def __repr__(self): 1247 return "Mul((%r, %r))" % (self.left, self.right) 1248 1249 def __str__(self): 1250 return "(%s * %s)" % (self.left, self.right) 1251 1252 class Name(Node): 1253 def __init__(self, name, lineno=None): 1254 self.name = name 1255 self.lineno = lineno 1256 1257 def getChildren(self): 1258 return self.name, 1259 1260 def getChildNodes(self): 1261 return () 1262 1263 def __repr__(self): 1264 return "Name(%r)" % (self.name,) 1265 1266 def __str__(self): 1267 return str(self.name) 1268 1269 class Not(Node): 1270 def __init__(self, expr, lineno=None): 1271 self.expr = expr 1272 self.lineno = lineno 1273 1274 def getChildren(self): 1275 return self.expr, 1276 1277 def getChildNodes(self): 1278 return self.expr, 1279 1280 def __repr__(self): 1281 return "Not(%r)" % (self.expr,) 1282 1283 def __str__(self): 1284 return "not %s" % self.expr 1285 1286 class Or(Node): 1287 def __init__(self, nodes, lineno=None): 1288 self.nodes = nodes 1289 self.lineno = lineno 1290 1291 def getChildren(self): 1292 return tuple(flatten(self.nodes)) 1293 1294 def getChildNodes(self): 1295 nodelist = [] 1296 nodelist.extend(flatten_nodes(self.nodes)) 1297 return tuple(nodelist) 1298 1299 def __repr__(self): 1300 return "Or(%r)" % (self.nodes,) 1301 1302 def __str__(self): 1303 return "(%s)" % " or ".join(map(str, self.nodes)) 1304 1305 class Pass(Node): 1306 def __init__(self, lineno=None): 1307 self.lineno = lineno 1308 1309 def getChildren(self): 1310 return () 1311 1312 def getChildNodes(self): 1313 return () 1314 1315 def __repr__(self): 1316 return "Pass()" 1317 1318 def __str__(self): 1319 return "pass" 1320 1321 class Power(Node, Operator): 1322 def __init__(self, leftright, lineno=None): 1323 self.left = leftright[0] 1324 self.right = leftright[1] 1325 self.lineno = lineno 1326 1327 def getChildren(self): 1328 return self.left, self.right 1329 1330 def getChildNodes(self): 1331 return self.left, self.right 1332 1333 def __repr__(self): 1334 return "Power((%r, %r))" % (self.left, self.right) 1335 1336 def __str__(self): 1337 return "(%s ** %s)" % (self.left, self.right) 1338 1339 class Print(Node): 1340 def __init__(self, nodes, dest, lineno=None): 1341 self.nodes = nodes 1342 self.dest = dest 1343 self.lineno = lineno 1344 1345 def getChildren(self): 1346 children = [] 1347 children.extend(flatten(self.nodes)) 1348 children.append(self.dest) 1349 return tuple(children) 1350 1351 def getChildNodes(self): 1352 nodelist = [] 1353 nodelist.extend(flatten_nodes(self.nodes)) 1354 if self.dest is not None: 1355 nodelist.append(self.dest) 1356 return tuple(nodelist) 1357 1358 def __repr__(self): 1359 return "Print(%r, %r)" % (self.nodes, self.dest) 1360 1361 def __str__(self): 1362 dest = self.dest and [">>%s" % self.dest] or [] 1363 return "print %s," % ", ".join(map(str, dest + self.nodes)) 1364 1365 class Printnl(Node): 1366 def __init__(self, nodes, dest, lineno=None): 1367 self.nodes = nodes 1368 self.dest = dest 1369 self.lineno = lineno 1370 1371 def getChildren(self): 1372 children = [] 1373 children.extend(flatten(self.nodes)) 1374 children.append(self.dest) 1375 return tuple(children) 1376 1377 def getChildNodes(self): 1378 nodelist = [] 1379 nodelist.extend(flatten_nodes(self.nodes)) 1380 if self.dest is not None: 1381 nodelist.append(self.dest) 1382 return tuple(nodelist) 1383 1384 def __repr__(self): 1385 return "Printnl(%r, %r)" % (self.nodes, self.dest) 1386 1387 def __str__(self): 1388 dest = self.dest and [">>%s" % self.dest] or [] 1389 return "print %s" % ", ".join(map(str, dest + self.nodes)) 1390 1391 class Raise(Node): 1392 def __init__(self, expr1, expr2, expr3, lineno=None): 1393 self.expr1 = expr1 1394 self.expr2 = expr2 1395 self.expr3 = expr3 1396 self.lineno = lineno 1397 1398 def getChildren(self): 1399 children = [] 1400 children.append(self.expr1) 1401 children.append(self.expr2) 1402 children.append(self.expr3) 1403 return tuple(children) 1404 1405 def getChildNodes(self): 1406 nodelist = [] 1407 if self.expr1 is not None: 1408 nodelist.append(self.expr1) 1409 if self.expr2 is not None: 1410 nodelist.append(self.expr2) 1411 if self.expr3 is not None: 1412 nodelist.append(self.expr3) 1413 return tuple(nodelist) 1414 1415 def __repr__(self): 1416 return "Raise(%r, %r, %r)" % (self.expr1, self.expr2, self.expr3) 1417 1418 def __str__(self): 1419 args = self.expr1 and [self.expr1] or [] 1420 args += self.expr2 and [self.expr2] or [] 1421 args += self.expr3 and [self.expr3] or [] 1422 return "raise %s" % ", ".join(map(str, args)) 1423 1424 class Return(Node): 1425 def __init__(self, value, lineno=None): 1426 self.value = value 1427 self.lineno = lineno 1428 1429 def getChildren(self): 1430 return self.value, 1431 1432 def getChildNodes(self): 1433 return self.value, 1434 1435 def __repr__(self): 1436 return "Return(%r)" % (self.value,) 1437 1438 def __str__(self): 1439 return "return %s" % self.value 1440 1441 class RightShift(Node, Operator): 1442 def __init__(self, leftright, lineno=None): 1443 self.left = leftright[0] 1444 self.right = leftright[1] 1445 self.lineno = lineno 1446 1447 def getChildren(self): 1448 return self.left, self.right 1449 1450 def getChildNodes(self): 1451 return self.left, self.right 1452 1453 def __repr__(self): 1454 return "RightShift((%r, %r))" % (self.left, self.right) 1455 1456 def __str__(self): 1457 return "(%s >> %s)" % (self.left, self.right) 1458 1459 class Set(Node): 1460 def __init__(self, nodes, lineno=None): 1461 self.nodes = nodes 1462 self.lineno = lineno 1463 1464 def getChildren(self): 1465 return tuple(flatten(self.nodes)) 1466 1467 def getChildNodes(self): 1468 nodelist = [] 1469 nodelist.extend(flatten_nodes(self.nodes)) 1470 return tuple(nodelist) 1471 1472 def __repr__(self): 1473 return "Set(%r)" % (self.nodes,) 1474 1475 def __str__(self): 1476 return "{%s}" % ", ".join(map(str, self.nodes)) 1477 1478 class Slice(Node, OperatorUser): 1479 def __init__(self, expr, flags, lower, upper, lineno=None): 1480 self.expr = expr 1481 self.flags = flags 1482 self.lower = lower 1483 self.upper = upper 1484 self.lineno = lineno 1485 1486 def getChildren(self): 1487 children = [] 1488 children.append(self.expr) 1489 children.append(self.flags) 1490 children.append(self.lower) 1491 children.append(self.upper) 1492 return tuple(children) 1493 1494 def getChildNodes(self): 1495 nodelist = [] 1496 nodelist.append(self.expr) 1497 if self.lower is not None: 1498 nodelist.append(self.lower) 1499 if self.upper is not None: 1500 nodelist.append(self.upper) 1501 return tuple(nodelist) 1502 1503 def __repr__(self): 1504 return "Slice(%r, %r, %r, %r)" % (self.expr, self.flags, self.lower, self.upper) 1505 1506 def __str__(self): 1507 args = [self.lower or "", self.upper or ""] 1508 return "%s%s[%s]" % (self.flags == "OP_DELETE" and "del " or "", self.expr, ":".join(map(str, args))) 1509 1510 class Sliceobj(Node): 1511 def __init__(self, nodes, lineno=None): 1512 self.nodes = nodes 1513 self.lineno = lineno 1514 1515 def getChildren(self): 1516 return tuple(flatten(self.nodes)) 1517 1518 def getChildNodes(self): 1519 nodelist = [] 1520 nodelist.extend(flatten_nodes(self.nodes)) 1521 return tuple(nodelist) 1522 1523 def __repr__(self): 1524 return "Sliceobj(%r)" % (self.nodes,) 1525 1526 def __str__(self): 1527 return ":".join(map(str, self.nodes)) 1528 1529 class Stmt(Node): 1530 def __init__(self, nodes, lineno=None): 1531 self.nodes = nodes 1532 self.lineno = lineno 1533 1534 def getChildren(self): 1535 return tuple(flatten(self.nodes)) 1536 1537 def getChildNodes(self): 1538 nodelist = [] 1539 nodelist.extend(flatten_nodes(self.nodes)) 1540 return tuple(nodelist) 1541 1542 def __repr__(self): 1543 return "Stmt(%r)" % (self.nodes,) 1544 1545 def __str__(self): 1546 return "\n".join(map(str, flatten_statement(self.nodes))) 1547 1548 class Sub(Node, Operator): 1549 def __init__(self, leftright, lineno=None): 1550 self.left = leftright[0] 1551 self.right = leftright[1] 1552 self.lineno = lineno 1553 1554 def getChildren(self): 1555 return self.left, self.right 1556 1557 def getChildNodes(self): 1558 return self.left, self.right 1559 1560 def __repr__(self): 1561 return "Sub((%r, %r))" % (self.left, self.right) 1562 1563 def __str__(self): 1564 return "(%s - %s)" % (self.left, self.right) 1565 1566 class Subscript(Node, OperatorUser): 1567 def __init__(self, expr, flags, subs, lineno=None): 1568 self.expr = expr 1569 self.flags = flags 1570 self.subs = subs 1571 self.lineno = lineno 1572 1573 def getChildren(self): 1574 children = [] 1575 children.append(self.expr) 1576 children.append(self.flags) 1577 children.extend(flatten(self.subs)) 1578 return tuple(children) 1579 1580 def getChildNodes(self): 1581 nodelist = [] 1582 nodelist.append(self.expr) 1583 nodelist.extend(flatten_nodes(self.subs)) 1584 return tuple(nodelist) 1585 1586 def __repr__(self): 1587 return "Subscript(%r, %r, %r)" % (self.expr, self.flags, self.subs) 1588 1589 def __str__(self): 1590 return "%s%s[%s]" % (self.flags == "OP_DELETE" and "del " or "", self.expr, ",".join(map(str, self.subs))) 1591 1592 class TryExcept(Node): 1593 def __init__(self, body, handlers, else_, lineno=None): 1594 self.body = body 1595 self.handlers = handlers 1596 self.else_ = else_ 1597 self.lineno = lineno 1598 1599 def getChildren(self): 1600 children = [] 1601 children.append(self.body) 1602 children.extend(flatten(self.handlers)) 1603 children.append(self.else_) 1604 return tuple(children) 1605 1606 def getChildNodes(self): 1607 nodelist = [] 1608 nodelist.append(self.body) 1609 nodelist.extend(flatten_nodes(self.handlers)) 1610 if self.else_ is not None: 1611 nodelist.append(self.else_) 1612 return tuple(nodelist) 1613 1614 def __repr__(self): 1615 return "TryExcept(%r, %r, %r)" % (self.body, self.handlers, self.else_) 1616 1617 def __str__(self): 1618 handlers = [ 1619 ("\nexcept%s%s:%s" % (spec and " %s" % spec or "", assign and ", %s" % assign or "", indent("\n%s" % statement))) 1620 for (spec, assign, statement) in self.handlers 1621 ] 1622 1623 return "try:%s%s%s" % ( 1624 indent("\n%s" % self.body), 1625 "".join(handlers), 1626 self.else_ and "\nelse:%s" % indent("\n%s" % self.else_) or "" 1627 ) 1628 1629 class TryFinally(Node): 1630 def __init__(self, body, final, lineno=None): 1631 self.body = body 1632 self.final = final 1633 self.lineno = lineno 1634 1635 def getChildren(self): 1636 return self.body, self.final 1637 1638 def getChildNodes(self): 1639 return self.body, self.final 1640 1641 def __repr__(self): 1642 return "TryFinally(%r, %r)" % (self.body, self.final) 1643 1644 def __str__(self): 1645 return "try:%s\nfinally:%s" % ( 1646 indent("\n%s" % self.body), 1647 indent("\n%s" % self.final) 1648 ) 1649 1650 class Tuple(Node): 1651 def __init__(self, nodes, lineno=None): 1652 self.nodes = nodes 1653 self.lineno = lineno 1654 1655 def getChildren(self): 1656 return tuple(flatten(self.nodes)) 1657 1658 def getChildNodes(self): 1659 nodelist = [] 1660 nodelist.extend(flatten_nodes(self.nodes)) 1661 return tuple(nodelist) 1662 1663 def __repr__(self): 1664 return "Tuple(%r)" % (self.nodes,) 1665 1666 def __str__(self): 1667 return "(%s)" % ", ".join(map(str, self.nodes)) 1668 1669 class UnaryAdd(Node, Operator): 1670 def __init__(self, expr, lineno=None): 1671 self.expr = expr 1672 self.lineno = lineno 1673 1674 def getChildren(self): 1675 return self.expr, 1676 1677 def getChildNodes(self): 1678 return self.expr, 1679 1680 def __repr__(self): 1681 return "UnaryAdd(%r)" % (self.expr,) 1682 1683 def __str__(self): 1684 return "+%s" % self.expr 1685 1686 class UnarySub(Node, Operator): 1687 def __init__(self, expr, lineno=None): 1688 self.expr = expr 1689 self.lineno = lineno 1690 1691 def getChildren(self): 1692 return self.expr, 1693 1694 def getChildNodes(self): 1695 return self.expr, 1696 1697 def __repr__(self): 1698 return "UnarySub(%r)" % (self.expr,) 1699 1700 def __str__(self): 1701 return "-%s" % self.expr 1702 1703 class While(Node): 1704 def __init__(self, test, body, else_, lineno=None): 1705 self.test = test 1706 self.body = body 1707 self.else_ = else_ 1708 self.lineno = lineno 1709 1710 def getChildren(self): 1711 children = [] 1712 children.append(self.test) 1713 children.append(self.body) 1714 children.append(self.else_) 1715 return tuple(children) 1716 1717 def getChildNodes(self): 1718 nodelist = [] 1719 nodelist.append(self.test) 1720 nodelist.append(self.body) 1721 if self.else_ is not None: 1722 nodelist.append(self.else_) 1723 return tuple(nodelist) 1724 1725 def __repr__(self): 1726 return "While(%r, %r, %r)" % (self.test, self.body, self.else_) 1727 1728 def __str__(self): 1729 return "while %s:%s%s" % ( 1730 self.test, 1731 indent("\n%s" % self.body), 1732 self.else_ and "\nelse:%s" % indent("\n%s" % self.else_) or "" 1733 ) 1734 1735 class With(Node): 1736 def __init__(self, expr, vars, body, lineno=None): 1737 self.expr = expr 1738 self.vars = vars 1739 self.body = body 1740 self.lineno = lineno 1741 1742 def getChildren(self): 1743 children = [] 1744 children.append(self.expr) 1745 children.append(self.vars) 1746 children.append(self.body) 1747 return tuple(children) 1748 1749 def getChildNodes(self): 1750 nodelist = [] 1751 nodelist.append(self.expr) 1752 if self.vars is not None: 1753 nodelist.append(self.vars) 1754 nodelist.append(self.body) 1755 return tuple(nodelist) 1756 1757 def __repr__(self): 1758 return "With(%r, %r, %r)" % (self.expr, self.vars, self.body) 1759 1760 def __str__(self): 1761 return "with %s%s:%s" % ( 1762 self.expr, 1763 self.vars and " as %s" % ", ".join(map(str, self.vars)), 1764 indent("\n%s" % self.body), 1765 ) 1766 1767 class Yield(Node): 1768 def __init__(self, value, lineno=None): 1769 self.value = value 1770 self.lineno = lineno 1771 1772 def getChildren(self): 1773 return self.value, 1774 1775 def getChildNodes(self): 1776 return self.value, 1777 1778 def __repr__(self): 1779 return "Yield(%r)" % (self.value,) 1780 1781 def __str__(self): 1782 return "yield %s" % self.value 1783 1784 for name, obj in globals().items(): 1785 if isinstance(obj, type) and issubclass(obj, Node): 1786 nodes[name.lower()] = obj