PageRenderTime 118ms CodeModel.GetById 37ms app.highlight 69ms RepoModel.GetById 1ms app.codeStats 0ms

/External.LCA_RESTRICTED/Languages/CPython/27/Lib/lib2to3/tests/test_fixers.py

http://github.com/IronLanguages/main
Python | 4529 lines | 4452 code | 72 blank | 5 comment | 36 complexity | 10926e53ce2f87d4767fc70ff8b03837 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1""" Test suite for the fixer modules """
   2
   3# Python imports
   4import os
   5import unittest
   6from itertools import chain
   7from operator import itemgetter
   8
   9# Local imports
  10from lib2to3 import pygram, pytree, refactor, fixer_util
  11from lib2to3.tests import support
  12
  13
  14class FixerTestCase(support.TestCase):
  15
  16    # Other test cases can subclass this class and replace "fixer_pkg" with
  17    # their own.
  18    def setUp(self, fix_list=None, fixer_pkg="lib2to3", options=None):
  19        if fix_list is None:
  20            fix_list = [self.fixer]
  21        self.refactor = support.get_refactorer(fixer_pkg, fix_list, options)
  22        self.fixer_log = []
  23        self.filename = u"<string>"
  24
  25        for fixer in chain(self.refactor.pre_order,
  26                           self.refactor.post_order):
  27            fixer.log = self.fixer_log
  28
  29    def _check(self, before, after):
  30        before = support.reformat(before)
  31        after = support.reformat(after)
  32        tree = self.refactor.refactor_string(before, self.filename)
  33        self.assertEqual(after, unicode(tree))
  34        return tree
  35
  36    def check(self, before, after, ignore_warnings=False):
  37        tree = self._check(before, after)
  38        self.assertTrue(tree.was_changed)
  39        if not ignore_warnings:
  40            self.assertEqual(self.fixer_log, [])
  41
  42    def warns(self, before, after, message, unchanged=False):
  43        tree = self._check(before, after)
  44        self.assertTrue(message in "".join(self.fixer_log))
  45        if not unchanged:
  46            self.assertTrue(tree.was_changed)
  47
  48    def warns_unchanged(self, before, message):
  49        self.warns(before, before, message, unchanged=True)
  50
  51    def unchanged(self, before, ignore_warnings=False):
  52        self._check(before, before)
  53        if not ignore_warnings:
  54            self.assertEqual(self.fixer_log, [])
  55
  56    def assert_runs_after(self, *names):
  57        fixes = [self.fixer]
  58        fixes.extend(names)
  59        r = support.get_refactorer("lib2to3", fixes)
  60        (pre, post) = r.get_fixers()
  61        n = "fix_" + self.fixer
  62        if post and post[-1].__class__.__module__.endswith(n):
  63            # We're the last fixer to run
  64            return
  65        if pre and pre[-1].__class__.__module__.endswith(n) and not post:
  66            # We're the last in pre and post is empty
  67            return
  68        self.fail("Fixer run order (%s) is incorrect; %s should be last."\
  69               %(", ".join([x.__class__.__module__ for x in (pre+post)]), n))
  70
  71class Test_ne(FixerTestCase):
  72    fixer = "ne"
  73
  74    def test_basic(self):
  75        b = """if x <> y:
  76            pass"""
  77
  78        a = """if x != y:
  79            pass"""
  80        self.check(b, a)
  81
  82    def test_no_spaces(self):
  83        b = """if x<>y:
  84            pass"""
  85
  86        a = """if x!=y:
  87            pass"""
  88        self.check(b, a)
  89
  90    def test_chained(self):
  91        b = """if x<>y<>z:
  92            pass"""
  93
  94        a = """if x!=y!=z:
  95            pass"""
  96        self.check(b, a)
  97
  98class Test_has_key(FixerTestCase):
  99    fixer = "has_key"
 100
 101    def test_1(self):
 102        b = """x = d.has_key("x") or d.has_key("y")"""
 103        a = """x = "x" in d or "y" in d"""
 104        self.check(b, a)
 105
 106    def test_2(self):
 107        b = """x = a.b.c.d.has_key("x") ** 3"""
 108        a = """x = ("x" in a.b.c.d) ** 3"""
 109        self.check(b, a)
 110
 111    def test_3(self):
 112        b = """x = a.b.has_key(1 + 2).__repr__()"""
 113        a = """x = (1 + 2 in a.b).__repr__()"""
 114        self.check(b, a)
 115
 116    def test_4(self):
 117        b = """x = a.b.has_key(1 + 2).__repr__() ** -3 ** 4"""
 118        a = """x = (1 + 2 in a.b).__repr__() ** -3 ** 4"""
 119        self.check(b, a)
 120
 121    def test_5(self):
 122        b = """x = a.has_key(f or g)"""
 123        a = """x = (f or g) in a"""
 124        self.check(b, a)
 125
 126    def test_6(self):
 127        b = """x = a + b.has_key(c)"""
 128        a = """x = a + (c in b)"""
 129        self.check(b, a)
 130
 131    def test_7(self):
 132        b = """x = a.has_key(lambda: 12)"""
 133        a = """x = (lambda: 12) in a"""
 134        self.check(b, a)
 135
 136    def test_8(self):
 137        b = """x = a.has_key(a for a in b)"""
 138        a = """x = (a for a in b) in a"""
 139        self.check(b, a)
 140
 141    def test_9(self):
 142        b = """if not a.has_key(b): pass"""
 143        a = """if b not in a: pass"""
 144        self.check(b, a)
 145
 146    def test_10(self):
 147        b = """if not a.has_key(b).__repr__(): pass"""
 148        a = """if not (b in a).__repr__(): pass"""
 149        self.check(b, a)
 150
 151    def test_11(self):
 152        b = """if not a.has_key(b) ** 2: pass"""
 153        a = """if not (b in a) ** 2: pass"""
 154        self.check(b, a)
 155
 156class Test_apply(FixerTestCase):
 157    fixer = "apply"
 158
 159    def test_1(self):
 160        b = """x = apply(f, g + h)"""
 161        a = """x = f(*g + h)"""
 162        self.check(b, a)
 163
 164    def test_2(self):
 165        b = """y = apply(f, g, h)"""
 166        a = """y = f(*g, **h)"""
 167        self.check(b, a)
 168
 169    def test_3(self):
 170        b = """z = apply(fs[0], g or h, h or g)"""
 171        a = """z = fs[0](*g or h, **h or g)"""
 172        self.check(b, a)
 173
 174    def test_4(self):
 175        b = """apply(f, (x, y) + t)"""
 176        a = """f(*(x, y) + t)"""
 177        self.check(b, a)
 178
 179    def test_5(self):
 180        b = """apply(f, args,)"""
 181        a = """f(*args)"""
 182        self.check(b, a)
 183
 184    def test_6(self):
 185        b = """apply(f, args, kwds,)"""
 186        a = """f(*args, **kwds)"""
 187        self.check(b, a)
 188
 189    # Test that complex functions are parenthesized
 190
 191    def test_complex_1(self):
 192        b = """x = apply(f+g, args)"""
 193        a = """x = (f+g)(*args)"""
 194        self.check(b, a)
 195
 196    def test_complex_2(self):
 197        b = """x = apply(f*g, args)"""
 198        a = """x = (f*g)(*args)"""
 199        self.check(b, a)
 200
 201    def test_complex_3(self):
 202        b = """x = apply(f**g, args)"""
 203        a = """x = (f**g)(*args)"""
 204        self.check(b, a)
 205
 206    # But dotted names etc. not
 207
 208    def test_dotted_name(self):
 209        b = """x = apply(f.g, args)"""
 210        a = """x = f.g(*args)"""
 211        self.check(b, a)
 212
 213    def test_subscript(self):
 214        b = """x = apply(f[x], args)"""
 215        a = """x = f[x](*args)"""
 216        self.check(b, a)
 217
 218    def test_call(self):
 219        b = """x = apply(f(), args)"""
 220        a = """x = f()(*args)"""
 221        self.check(b, a)
 222
 223    # Extreme case
 224    def test_extreme(self):
 225        b = """x = apply(a.b.c.d.e.f, args, kwds)"""
 226        a = """x = a.b.c.d.e.f(*args, **kwds)"""
 227        self.check(b, a)
 228
 229    # XXX Comments in weird places still get lost
 230    def test_weird_comments(self):
 231        b = """apply(   # foo
 232          f, # bar
 233          args)"""
 234        a = """f(*args)"""
 235        self.check(b, a)
 236
 237    # These should *not* be touched
 238
 239    def test_unchanged_1(self):
 240        s = """apply()"""
 241        self.unchanged(s)
 242
 243    def test_unchanged_2(self):
 244        s = """apply(f)"""
 245        self.unchanged(s)
 246
 247    def test_unchanged_3(self):
 248        s = """apply(f,)"""
 249        self.unchanged(s)
 250
 251    def test_unchanged_4(self):
 252        s = """apply(f, args, kwds, extras)"""
 253        self.unchanged(s)
 254
 255    def test_unchanged_5(self):
 256        s = """apply(f, *args, **kwds)"""
 257        self.unchanged(s)
 258
 259    def test_unchanged_6(self):
 260        s = """apply(f, *args)"""
 261        self.unchanged(s)
 262
 263    def test_unchanged_7(self):
 264        s = """apply(func=f, args=args, kwds=kwds)"""
 265        self.unchanged(s)
 266
 267    def test_unchanged_8(self):
 268        s = """apply(f, args=args, kwds=kwds)"""
 269        self.unchanged(s)
 270
 271    def test_unchanged_9(self):
 272        s = """apply(f, args, kwds=kwds)"""
 273        self.unchanged(s)
 274
 275    def test_space_1(self):
 276        a = """apply(  f,  args,   kwds)"""
 277        b = """f(*args, **kwds)"""
 278        self.check(a, b)
 279
 280    def test_space_2(self):
 281        a = """apply(  f  ,args,kwds   )"""
 282        b = """f(*args, **kwds)"""
 283        self.check(a, b)
 284
 285class Test_intern(FixerTestCase):
 286    fixer = "intern"
 287
 288    def test_prefix_preservation(self):
 289        b = """x =   intern(  a  )"""
 290        a = """import sys\nx =   sys.intern(  a  )"""
 291        self.check(b, a)
 292
 293        b = """y = intern("b" # test
 294              )"""
 295        a = """import sys\ny = sys.intern("b" # test
 296              )"""
 297        self.check(b, a)
 298
 299        b = """z = intern(a+b+c.d,   )"""
 300        a = """import sys\nz = sys.intern(a+b+c.d,   )"""
 301        self.check(b, a)
 302
 303    def test(self):
 304        b = """x = intern(a)"""
 305        a = """import sys\nx = sys.intern(a)"""
 306        self.check(b, a)
 307
 308        b = """z = intern(a+b+c.d,)"""
 309        a = """import sys\nz = sys.intern(a+b+c.d,)"""
 310        self.check(b, a)
 311
 312        b = """intern("y%s" % 5).replace("y", "")"""
 313        a = """import sys\nsys.intern("y%s" % 5).replace("y", "")"""
 314        self.check(b, a)
 315
 316    # These should not be refactored
 317
 318    def test_unchanged(self):
 319        s = """intern(a=1)"""
 320        self.unchanged(s)
 321
 322        s = """intern(f, g)"""
 323        self.unchanged(s)
 324
 325        s = """intern(*h)"""
 326        self.unchanged(s)
 327
 328        s = """intern(**i)"""
 329        self.unchanged(s)
 330
 331        s = """intern()"""
 332        self.unchanged(s)
 333
 334class Test_reduce(FixerTestCase):
 335    fixer = "reduce"
 336
 337    def test_simple_call(self):
 338        b = "reduce(a, b, c)"
 339        a = "from functools import reduce\nreduce(a, b, c)"
 340        self.check(b, a)
 341
 342    def test_bug_7253(self):
 343        # fix_tuple_params was being bad and orphaning nodes in the tree.
 344        b = "def x(arg): reduce(sum, [])"
 345        a = "from functools import reduce\ndef x(arg): reduce(sum, [])"
 346        self.check(b, a)
 347
 348    def test_call_with_lambda(self):
 349        b = "reduce(lambda x, y: x + y, seq)"
 350        a = "from functools import reduce\nreduce(lambda x, y: x + y, seq)"
 351        self.check(b, a)
 352
 353    def test_unchanged(self):
 354        s = "reduce(a)"
 355        self.unchanged(s)
 356
 357        s = "reduce(a, b=42)"
 358        self.unchanged(s)
 359
 360        s = "reduce(a, b, c, d)"
 361        self.unchanged(s)
 362
 363        s = "reduce(**c)"
 364        self.unchanged(s)
 365
 366        s = "reduce()"
 367        self.unchanged(s)
 368
 369class Test_print(FixerTestCase):
 370    fixer = "print"
 371
 372    def test_prefix_preservation(self):
 373        b = """print 1,   1+1,   1+1+1"""
 374        a = """print(1,   1+1,   1+1+1)"""
 375        self.check(b, a)
 376
 377    def test_idempotency(self):
 378        s = """print()"""
 379        self.unchanged(s)
 380
 381        s = """print('')"""
 382        self.unchanged(s)
 383
 384    def test_idempotency_print_as_function(self):
 385        self.refactor.driver.grammar = pygram.python_grammar_no_print_statement
 386        s = """print(1, 1+1, 1+1+1)"""
 387        self.unchanged(s)
 388
 389        s = """print()"""
 390        self.unchanged(s)
 391
 392        s = """print('')"""
 393        self.unchanged(s)
 394
 395    def test_1(self):
 396        b = """print 1, 1+1, 1+1+1"""
 397        a = """print(1, 1+1, 1+1+1)"""
 398        self.check(b, a)
 399
 400    def test_2(self):
 401        b = """print 1, 2"""
 402        a = """print(1, 2)"""
 403        self.check(b, a)
 404
 405    def test_3(self):
 406        b = """print"""
 407        a = """print()"""
 408        self.check(b, a)
 409
 410    def test_4(self):
 411        # from bug 3000
 412        b = """print whatever; print"""
 413        a = """print(whatever); print()"""
 414        self.check(b, a)
 415
 416    def test_5(self):
 417        b = """print; print whatever;"""
 418        a = """print(); print(whatever);"""
 419        self.check(b, a)
 420
 421    def test_tuple(self):
 422        b = """print (a, b, c)"""
 423        a = """print((a, b, c))"""
 424        self.check(b, a)
 425
 426    # trailing commas
 427
 428    def test_trailing_comma_1(self):
 429        b = """print 1, 2, 3,"""
 430        a = """print(1, 2, 3, end=' ')"""
 431        self.check(b, a)
 432
 433    def test_trailing_comma_2(self):
 434        b = """print 1, 2,"""
 435        a = """print(1, 2, end=' ')"""
 436        self.check(b, a)
 437
 438    def test_trailing_comma_3(self):
 439        b = """print 1,"""
 440        a = """print(1, end=' ')"""
 441        self.check(b, a)
 442
 443    # >> stuff
 444
 445    def test_vargs_without_trailing_comma(self):
 446        b = """print >>sys.stderr, 1, 2, 3"""
 447        a = """print(1, 2, 3, file=sys.stderr)"""
 448        self.check(b, a)
 449
 450    def test_with_trailing_comma(self):
 451        b = """print >>sys.stderr, 1, 2,"""
 452        a = """print(1, 2, end=' ', file=sys.stderr)"""
 453        self.check(b, a)
 454
 455    def test_no_trailing_comma(self):
 456        b = """print >>sys.stderr, 1+1"""
 457        a = """print(1+1, file=sys.stderr)"""
 458        self.check(b, a)
 459
 460    def test_spaces_before_file(self):
 461        b = """print >>  sys.stderr"""
 462        a = """print(file=sys.stderr)"""
 463        self.check(b, a)
 464
 465    def test_with_future_print_function(self):
 466        s = "from __future__ import print_function\n" \
 467            "print('Hai!', end=' ')"
 468        self.unchanged(s)
 469
 470        b = "print 'Hello, world!'"
 471        a = "print('Hello, world!')"
 472        self.check(b, a)
 473
 474
 475class Test_exec(FixerTestCase):
 476    fixer = "exec"
 477
 478    def test_prefix_preservation(self):
 479        b = """  exec code in ns1,   ns2"""
 480        a = """  exec(code, ns1,   ns2)"""
 481        self.check(b, a)
 482
 483    def test_basic(self):
 484        b = """exec code"""
 485        a = """exec(code)"""
 486        self.check(b, a)
 487
 488    def test_with_globals(self):
 489        b = """exec code in ns"""
 490        a = """exec(code, ns)"""
 491        self.check(b, a)
 492
 493    def test_with_globals_locals(self):
 494        b = """exec code in ns1, ns2"""
 495        a = """exec(code, ns1, ns2)"""
 496        self.check(b, a)
 497
 498    def test_complex_1(self):
 499        b = """exec (a.b()) in ns"""
 500        a = """exec((a.b()), ns)"""
 501        self.check(b, a)
 502
 503    def test_complex_2(self):
 504        b = """exec a.b() + c in ns"""
 505        a = """exec(a.b() + c, ns)"""
 506        self.check(b, a)
 507
 508    # These should not be touched
 509
 510    def test_unchanged_1(self):
 511        s = """exec(code)"""
 512        self.unchanged(s)
 513
 514    def test_unchanged_2(self):
 515        s = """exec (code)"""
 516        self.unchanged(s)
 517
 518    def test_unchanged_3(self):
 519        s = """exec(code, ns)"""
 520        self.unchanged(s)
 521
 522    def test_unchanged_4(self):
 523        s = """exec(code, ns1, ns2)"""
 524        self.unchanged(s)
 525
 526class Test_repr(FixerTestCase):
 527    fixer = "repr"
 528
 529    def test_prefix_preservation(self):
 530        b = """x =   `1 + 2`"""
 531        a = """x =   repr(1 + 2)"""
 532        self.check(b, a)
 533
 534    def test_simple_1(self):
 535        b = """x = `1 + 2`"""
 536        a = """x = repr(1 + 2)"""
 537        self.check(b, a)
 538
 539    def test_simple_2(self):
 540        b = """y = `x`"""
 541        a = """y = repr(x)"""
 542        self.check(b, a)
 543
 544    def test_complex(self):
 545        b = """z = `y`.__repr__()"""
 546        a = """z = repr(y).__repr__()"""
 547        self.check(b, a)
 548
 549    def test_tuple(self):
 550        b = """x = `1, 2, 3`"""
 551        a = """x = repr((1, 2, 3))"""
 552        self.check(b, a)
 553
 554    def test_nested(self):
 555        b = """x = `1 + `2``"""
 556        a = """x = repr(1 + repr(2))"""
 557        self.check(b, a)
 558
 559    def test_nested_tuples(self):
 560        b = """x = `1, 2 + `3, 4``"""
 561        a = """x = repr((1, 2 + repr((3, 4))))"""
 562        self.check(b, a)
 563
 564class Test_except(FixerTestCase):
 565    fixer = "except"
 566
 567    def test_prefix_preservation(self):
 568        b = """
 569            try:
 570                pass
 571            except (RuntimeError, ImportError),    e:
 572                pass"""
 573        a = """
 574            try:
 575                pass
 576            except (RuntimeError, ImportError) as    e:
 577                pass"""
 578        self.check(b, a)
 579
 580    def test_simple(self):
 581        b = """
 582            try:
 583                pass
 584            except Foo, e:
 585                pass"""
 586        a = """
 587            try:
 588                pass
 589            except Foo as e:
 590                pass"""
 591        self.check(b, a)
 592
 593    def test_simple_no_space_before_target(self):
 594        b = """
 595            try:
 596                pass
 597            except Foo,e:
 598                pass"""
 599        a = """
 600            try:
 601                pass
 602            except Foo as e:
 603                pass"""
 604        self.check(b, a)
 605
 606    def test_tuple_unpack(self):
 607        b = """
 608            def foo():
 609                try:
 610                    pass
 611                except Exception, (f, e):
 612                    pass
 613                except ImportError, e:
 614                    pass"""
 615
 616        a = """
 617            def foo():
 618                try:
 619                    pass
 620                except Exception as xxx_todo_changeme:
 621                    (f, e) = xxx_todo_changeme.args
 622                    pass
 623                except ImportError as e:
 624                    pass"""
 625        self.check(b, a)
 626
 627    def test_multi_class(self):
 628        b = """
 629            try:
 630                pass
 631            except (RuntimeError, ImportError), e:
 632                pass"""
 633
 634        a = """
 635            try:
 636                pass
 637            except (RuntimeError, ImportError) as e:
 638                pass"""
 639        self.check(b, a)
 640
 641    def test_list_unpack(self):
 642        b = """
 643            try:
 644                pass
 645            except Exception, [a, b]:
 646                pass"""
 647
 648        a = """
 649            try:
 650                pass
 651            except Exception as xxx_todo_changeme:
 652                [a, b] = xxx_todo_changeme.args
 653                pass"""
 654        self.check(b, a)
 655
 656    def test_weird_target_1(self):
 657        b = """
 658            try:
 659                pass
 660            except Exception, d[5]:
 661                pass"""
 662
 663        a = """
 664            try:
 665                pass
 666            except Exception as xxx_todo_changeme:
 667                d[5] = xxx_todo_changeme
 668                pass"""
 669        self.check(b, a)
 670
 671    def test_weird_target_2(self):
 672        b = """
 673            try:
 674                pass
 675            except Exception, a.foo:
 676                pass"""
 677
 678        a = """
 679            try:
 680                pass
 681            except Exception as xxx_todo_changeme:
 682                a.foo = xxx_todo_changeme
 683                pass"""
 684        self.check(b, a)
 685
 686    def test_weird_target_3(self):
 687        b = """
 688            try:
 689                pass
 690            except Exception, a().foo:
 691                pass"""
 692
 693        a = """
 694            try:
 695                pass
 696            except Exception as xxx_todo_changeme:
 697                a().foo = xxx_todo_changeme
 698                pass"""
 699        self.check(b, a)
 700
 701    def test_bare_except(self):
 702        b = """
 703            try:
 704                pass
 705            except Exception, a:
 706                pass
 707            except:
 708                pass"""
 709
 710        a = """
 711            try:
 712                pass
 713            except Exception as a:
 714                pass
 715            except:
 716                pass"""
 717        self.check(b, a)
 718
 719    def test_bare_except_and_else_finally(self):
 720        b = """
 721            try:
 722                pass
 723            except Exception, a:
 724                pass
 725            except:
 726                pass
 727            else:
 728                pass
 729            finally:
 730                pass"""
 731
 732        a = """
 733            try:
 734                pass
 735            except Exception as a:
 736                pass
 737            except:
 738                pass
 739            else:
 740                pass
 741            finally:
 742                pass"""
 743        self.check(b, a)
 744
 745    def test_multi_fixed_excepts_before_bare_except(self):
 746        b = """
 747            try:
 748                pass
 749            except TypeError, b:
 750                pass
 751            except Exception, a:
 752                pass
 753            except:
 754                pass"""
 755
 756        a = """
 757            try:
 758                pass
 759            except TypeError as b:
 760                pass
 761            except Exception as a:
 762                pass
 763            except:
 764                pass"""
 765        self.check(b, a)
 766
 767    def test_one_line_suites(self):
 768        b = """
 769            try: raise TypeError
 770            except TypeError, e:
 771                pass
 772            """
 773        a = """
 774            try: raise TypeError
 775            except TypeError as e:
 776                pass
 777            """
 778        self.check(b, a)
 779        b = """
 780            try:
 781                raise TypeError
 782            except TypeError, e: pass
 783            """
 784        a = """
 785            try:
 786                raise TypeError
 787            except TypeError as e: pass
 788            """
 789        self.check(b, a)
 790        b = """
 791            try: raise TypeError
 792            except TypeError, e: pass
 793            """
 794        a = """
 795            try: raise TypeError
 796            except TypeError as e: pass
 797            """
 798        self.check(b, a)
 799        b = """
 800            try: raise TypeError
 801            except TypeError, e: pass
 802            else: function()
 803            finally: done()
 804            """
 805        a = """
 806            try: raise TypeError
 807            except TypeError as e: pass
 808            else: function()
 809            finally: done()
 810            """
 811        self.check(b, a)
 812
 813    # These should not be touched:
 814
 815    def test_unchanged_1(self):
 816        s = """
 817            try:
 818                pass
 819            except:
 820                pass"""
 821        self.unchanged(s)
 822
 823    def test_unchanged_2(self):
 824        s = """
 825            try:
 826                pass
 827            except Exception:
 828                pass"""
 829        self.unchanged(s)
 830
 831    def test_unchanged_3(self):
 832        s = """
 833            try:
 834                pass
 835            except (Exception, SystemExit):
 836                pass"""
 837        self.unchanged(s)
 838
 839class Test_raise(FixerTestCase):
 840    fixer = "raise"
 841
 842    def test_basic(self):
 843        b = """raise Exception, 5"""
 844        a = """raise Exception(5)"""
 845        self.check(b, a)
 846
 847    def test_prefix_preservation(self):
 848        b = """raise Exception,5"""
 849        a = """raise Exception(5)"""
 850        self.check(b, a)
 851
 852        b = """raise   Exception,    5"""
 853        a = """raise   Exception(5)"""
 854        self.check(b, a)
 855
 856    def test_with_comments(self):
 857        b = """raise Exception, 5 # foo"""
 858        a = """raise Exception(5) # foo"""
 859        self.check(b, a)
 860
 861        b = """raise E, (5, 6) % (a, b) # foo"""
 862        a = """raise E((5, 6) % (a, b)) # foo"""
 863        self.check(b, a)
 864
 865        b = """def foo():
 866                    raise Exception, 5, 6 # foo"""
 867        a = """def foo():
 868                    raise Exception(5).with_traceback(6) # foo"""
 869        self.check(b, a)
 870
 871    def test_None_value(self):
 872        b = """raise Exception(5), None, tb"""
 873        a = """raise Exception(5).with_traceback(tb)"""
 874        self.check(b, a)
 875
 876    def test_tuple_value(self):
 877        b = """raise Exception, (5, 6, 7)"""
 878        a = """raise Exception(5, 6, 7)"""
 879        self.check(b, a)
 880
 881    def test_tuple_detection(self):
 882        b = """raise E, (5, 6) % (a, b)"""
 883        a = """raise E((5, 6) % (a, b))"""
 884        self.check(b, a)
 885
 886    def test_tuple_exc_1(self):
 887        b = """raise (((E1, E2), E3), E4), V"""
 888        a = """raise E1(V)"""
 889        self.check(b, a)
 890
 891    def test_tuple_exc_2(self):
 892        b = """raise (E1, (E2, E3), E4), V"""
 893        a = """raise E1(V)"""
 894        self.check(b, a)
 895
 896    # These should produce a warning
 897
 898    def test_string_exc(self):
 899        s = """raise 'foo'"""
 900        self.warns_unchanged(s, "Python 3 does not support string exceptions")
 901
 902    def test_string_exc_val(self):
 903        s = """raise "foo", 5"""
 904        self.warns_unchanged(s, "Python 3 does not support string exceptions")
 905
 906    def test_string_exc_val_tb(self):
 907        s = """raise "foo", 5, 6"""
 908        self.warns_unchanged(s, "Python 3 does not support string exceptions")
 909
 910    # These should result in traceback-assignment
 911
 912    def test_tb_1(self):
 913        b = """def foo():
 914                    raise Exception, 5, 6"""
 915        a = """def foo():
 916                    raise Exception(5).with_traceback(6)"""
 917        self.check(b, a)
 918
 919    def test_tb_2(self):
 920        b = """def foo():
 921                    a = 5
 922                    raise Exception, 5, 6
 923                    b = 6"""
 924        a = """def foo():
 925                    a = 5
 926                    raise Exception(5).with_traceback(6)
 927                    b = 6"""
 928        self.check(b, a)
 929
 930    def test_tb_3(self):
 931        b = """def foo():
 932                    raise Exception,5,6"""
 933        a = """def foo():
 934                    raise Exception(5).with_traceback(6)"""
 935        self.check(b, a)
 936
 937    def test_tb_4(self):
 938        b = """def foo():
 939                    a = 5
 940                    raise Exception,5,6
 941                    b = 6"""
 942        a = """def foo():
 943                    a = 5
 944                    raise Exception(5).with_traceback(6)
 945                    b = 6"""
 946        self.check(b, a)
 947
 948    def test_tb_5(self):
 949        b = """def foo():
 950                    raise Exception, (5, 6, 7), 6"""
 951        a = """def foo():
 952                    raise Exception(5, 6, 7).with_traceback(6)"""
 953        self.check(b, a)
 954
 955    def test_tb_6(self):
 956        b = """def foo():
 957                    a = 5
 958                    raise Exception, (5, 6, 7), 6
 959                    b = 6"""
 960        a = """def foo():
 961                    a = 5
 962                    raise Exception(5, 6, 7).with_traceback(6)
 963                    b = 6"""
 964        self.check(b, a)
 965
 966class Test_throw(FixerTestCase):
 967    fixer = "throw"
 968
 969    def test_1(self):
 970        b = """g.throw(Exception, 5)"""
 971        a = """g.throw(Exception(5))"""
 972        self.check(b, a)
 973
 974    def test_2(self):
 975        b = """g.throw(Exception,5)"""
 976        a = """g.throw(Exception(5))"""
 977        self.check(b, a)
 978
 979    def test_3(self):
 980        b = """g.throw(Exception, (5, 6, 7))"""
 981        a = """g.throw(Exception(5, 6, 7))"""
 982        self.check(b, a)
 983
 984    def test_4(self):
 985        b = """5 + g.throw(Exception, 5)"""
 986        a = """5 + g.throw(Exception(5))"""
 987        self.check(b, a)
 988
 989    # These should produce warnings
 990
 991    def test_warn_1(self):
 992        s = """g.throw("foo")"""
 993        self.warns_unchanged(s, "Python 3 does not support string exceptions")
 994
 995    def test_warn_2(self):
 996        s = """g.throw("foo", 5)"""
 997        self.warns_unchanged(s, "Python 3 does not support string exceptions")
 998
 999    def test_warn_3(self):
1000        s = """g.throw("foo", 5, 6)"""
1001        self.warns_unchanged(s, "Python 3 does not support string exceptions")
1002
1003    # These should not be touched
1004
1005    def test_untouched_1(self):
1006        s = """g.throw(Exception)"""
1007        self.unchanged(s)
1008
1009    def test_untouched_2(self):
1010        s = """g.throw(Exception(5, 6))"""
1011        self.unchanged(s)
1012
1013    def test_untouched_3(self):
1014        s = """5 + g.throw(Exception(5, 6))"""
1015        self.unchanged(s)
1016
1017    # These should result in traceback-assignment
1018
1019    def test_tb_1(self):
1020        b = """def foo():
1021                    g.throw(Exception, 5, 6)"""
1022        a = """def foo():
1023                    g.throw(Exception(5).with_traceback(6))"""
1024        self.check(b, a)
1025
1026    def test_tb_2(self):
1027        b = """def foo():
1028                    a = 5
1029                    g.throw(Exception, 5, 6)
1030                    b = 6"""
1031        a = """def foo():
1032                    a = 5
1033                    g.throw(Exception(5).with_traceback(6))
1034                    b = 6"""
1035        self.check(b, a)
1036
1037    def test_tb_3(self):
1038        b = """def foo():
1039                    g.throw(Exception,5,6)"""
1040        a = """def foo():
1041                    g.throw(Exception(5).with_traceback(6))"""
1042        self.check(b, a)
1043
1044    def test_tb_4(self):
1045        b = """def foo():
1046                    a = 5
1047                    g.throw(Exception,5,6)
1048                    b = 6"""
1049        a = """def foo():
1050                    a = 5
1051                    g.throw(Exception(5).with_traceback(6))
1052                    b = 6"""
1053        self.check(b, a)
1054
1055    def test_tb_5(self):
1056        b = """def foo():
1057                    g.throw(Exception, (5, 6, 7), 6)"""
1058        a = """def foo():
1059                    g.throw(Exception(5, 6, 7).with_traceback(6))"""
1060        self.check(b, a)
1061
1062    def test_tb_6(self):
1063        b = """def foo():
1064                    a = 5
1065                    g.throw(Exception, (5, 6, 7), 6)
1066                    b = 6"""
1067        a = """def foo():
1068                    a = 5
1069                    g.throw(Exception(5, 6, 7).with_traceback(6))
1070                    b = 6"""
1071        self.check(b, a)
1072
1073    def test_tb_7(self):
1074        b = """def foo():
1075                    a + g.throw(Exception, 5, 6)"""
1076        a = """def foo():
1077                    a + g.throw(Exception(5).with_traceback(6))"""
1078        self.check(b, a)
1079
1080    def test_tb_8(self):
1081        b = """def foo():
1082                    a = 5
1083                    a + g.throw(Exception, 5, 6)
1084                    b = 6"""
1085        a = """def foo():
1086                    a = 5
1087                    a + g.throw(Exception(5).with_traceback(6))
1088                    b = 6"""
1089        self.check(b, a)
1090
1091class Test_long(FixerTestCase):
1092    fixer = "long"
1093
1094    def test_1(self):
1095        b = """x = long(x)"""
1096        a = """x = int(x)"""
1097        self.check(b, a)
1098
1099    def test_2(self):
1100        b = """y = isinstance(x, long)"""
1101        a = """y = isinstance(x, int)"""
1102        self.check(b, a)
1103
1104    def test_3(self):
1105        b = """z = type(x) in (int, long)"""
1106        a = """z = type(x) in (int, int)"""
1107        self.check(b, a)
1108
1109    def test_unchanged(self):
1110        s = """long = True"""
1111        self.unchanged(s)
1112
1113        s = """s.long = True"""
1114        self.unchanged(s)
1115
1116        s = """def long(): pass"""
1117        self.unchanged(s)
1118
1119        s = """class long(): pass"""
1120        self.unchanged(s)
1121
1122        s = """def f(long): pass"""
1123        self.unchanged(s)
1124
1125        s = """def f(g, long): pass"""
1126        self.unchanged(s)
1127
1128        s = """def f(x, long=True): pass"""
1129        self.unchanged(s)
1130
1131    def test_prefix_preservation(self):
1132        b = """x =   long(  x  )"""
1133        a = """x =   int(  x  )"""
1134        self.check(b, a)
1135
1136
1137class Test_execfile(FixerTestCase):
1138    fixer = "execfile"
1139
1140    def test_conversion(self):
1141        b = """execfile("fn")"""
1142        a = """exec(compile(open("fn").read(), "fn", 'exec'))"""
1143        self.check(b, a)
1144
1145        b = """execfile("fn", glob)"""
1146        a = """exec(compile(open("fn").read(), "fn", 'exec'), glob)"""
1147        self.check(b, a)
1148
1149        b = """execfile("fn", glob, loc)"""
1150        a = """exec(compile(open("fn").read(), "fn", 'exec'), glob, loc)"""
1151        self.check(b, a)
1152
1153        b = """execfile("fn", globals=glob)"""
1154        a = """exec(compile(open("fn").read(), "fn", 'exec'), globals=glob)"""
1155        self.check(b, a)
1156
1157        b = """execfile("fn", locals=loc)"""
1158        a = """exec(compile(open("fn").read(), "fn", 'exec'), locals=loc)"""
1159        self.check(b, a)
1160
1161        b = """execfile("fn", globals=glob, locals=loc)"""
1162        a = """exec(compile(open("fn").read(), "fn", 'exec'), globals=glob, locals=loc)"""
1163        self.check(b, a)
1164
1165    def test_spacing(self):
1166        b = """execfile( "fn" )"""
1167        a = """exec(compile(open( "fn" ).read(), "fn", 'exec'))"""
1168        self.check(b, a)
1169
1170        b = """execfile("fn",  globals = glob)"""
1171        a = """exec(compile(open("fn").read(), "fn", 'exec'),  globals = glob)"""
1172        self.check(b, a)
1173
1174
1175class Test_isinstance(FixerTestCase):
1176    fixer = "isinstance"
1177
1178    def test_remove_multiple_items(self):
1179        b = """isinstance(x, (int, int, int))"""
1180        a = """isinstance(x, int)"""
1181        self.check(b, a)
1182
1183        b = """isinstance(x, (int, float, int, int, float))"""
1184        a = """isinstance(x, (int, float))"""
1185        self.check(b, a)
1186
1187        b = """isinstance(x, (int, float, int, int, float, str))"""
1188        a = """isinstance(x, (int, float, str))"""
1189        self.check(b, a)
1190
1191        b = """isinstance(foo() + bar(), (x(), y(), x(), int, int))"""
1192        a = """isinstance(foo() + bar(), (x(), y(), x(), int))"""
1193        self.check(b, a)
1194
1195    def test_prefix_preservation(self):
1196        b = """if    isinstance(  foo(), (  bar, bar, baz )) : pass"""
1197        a = """if    isinstance(  foo(), (  bar, baz )) : pass"""
1198        self.check(b, a)
1199
1200    def test_unchanged(self):
1201        self.unchanged("isinstance(x, (str, int))")
1202
1203class Test_dict(FixerTestCase):
1204    fixer = "dict"
1205
1206    def test_prefix_preservation(self):
1207        b = "if   d. keys  (  )  : pass"
1208        a = "if   list(d. keys  (  ))  : pass"
1209        self.check(b, a)
1210
1211        b = "if   d. items  (  )  : pass"
1212        a = "if   list(d. items  (  ))  : pass"
1213        self.check(b, a)
1214
1215        b = "if   d. iterkeys  ( )  : pass"
1216        a = "if   iter(d. keys  ( ))  : pass"
1217        self.check(b, a)
1218
1219        b = "[i for i in    d.  iterkeys(  )  ]"
1220        a = "[i for i in    d.  keys(  )  ]"
1221        self.check(b, a)
1222
1223        b = "if   d. viewkeys  ( )  : pass"
1224        a = "if   d. keys  ( )  : pass"
1225        self.check(b, a)
1226
1227        b = "[i for i in    d.  viewkeys(  )  ]"
1228        a = "[i for i in    d.  keys(  )  ]"
1229        self.check(b, a)
1230
1231    def test_trailing_comment(self):
1232        b = "d.keys() # foo"
1233        a = "list(d.keys()) # foo"
1234        self.check(b, a)
1235
1236        b = "d.items()  # foo"
1237        a = "list(d.items())  # foo"
1238        self.check(b, a)
1239
1240        b = "d.iterkeys()  # foo"
1241        a = "iter(d.keys())  # foo"
1242        self.check(b, a)
1243
1244        b = """[i for i in d.iterkeys() # foo
1245               ]"""
1246        a = """[i for i in d.keys() # foo
1247               ]"""
1248        self.check(b, a)
1249
1250        b = """[i for i in d.iterkeys() # foo
1251               ]"""
1252        a = """[i for i in d.keys() # foo
1253               ]"""
1254        self.check(b, a)
1255
1256        b = "d.viewitems()  # foo"
1257        a = "d.items()  # foo"
1258        self.check(b, a)
1259
1260    def test_unchanged(self):
1261        for wrapper in fixer_util.consuming_calls:
1262            s = "s = %s(d.keys())" % wrapper
1263            self.unchanged(s)
1264
1265            s = "s = %s(d.values())" % wrapper
1266            self.unchanged(s)
1267
1268            s = "s = %s(d.items())" % wrapper
1269            self.unchanged(s)
1270
1271    def test_01(self):
1272        b = "d.keys()"
1273        a = "list(d.keys())"
1274        self.check(b, a)
1275
1276        b = "a[0].foo().keys()"
1277        a = "list(a[0].foo().keys())"
1278        self.check(b, a)
1279
1280    def test_02(self):
1281        b = "d.items()"
1282        a = "list(d.items())"
1283        self.check(b, a)
1284
1285    def test_03(self):
1286        b = "d.values()"
1287        a = "list(d.values())"
1288        self.check(b, a)
1289
1290    def test_04(self):
1291        b = "d.iterkeys()"
1292        a = "iter(d.keys())"
1293        self.check(b, a)
1294
1295    def test_05(self):
1296        b = "d.iteritems()"
1297        a = "iter(d.items())"
1298        self.check(b, a)
1299
1300    def test_06(self):
1301        b = "d.itervalues()"
1302        a = "iter(d.values())"
1303        self.check(b, a)
1304
1305    def test_07(self):
1306        s = "list(d.keys())"
1307        self.unchanged(s)
1308
1309    def test_08(self):
1310        s = "sorted(d.keys())"
1311        self.unchanged(s)
1312
1313    def test_09(self):
1314        b = "iter(d.keys())"
1315        a = "iter(list(d.keys()))"
1316        self.check(b, a)
1317
1318    def test_10(self):
1319        b = "foo(d.keys())"
1320        a = "foo(list(d.keys()))"
1321        self.check(b, a)
1322
1323    def test_11(self):
1324        b = "for i in d.keys(): print i"
1325        a = "for i in list(d.keys()): print i"
1326        self.check(b, a)
1327
1328    def test_12(self):
1329        b = "for i in d.iterkeys(): print i"
1330        a = "for i in d.keys(): print i"
1331        self.check(b, a)
1332
1333    def test_13(self):
1334        b = "[i for i in d.keys()]"
1335        a = "[i for i in list(d.keys())]"
1336        self.check(b, a)
1337
1338    def test_14(self):
1339        b = "[i for i in d.iterkeys()]"
1340        a = "[i for i in d.keys()]"
1341        self.check(b, a)
1342
1343    def test_15(self):
1344        b = "(i for i in d.keys())"
1345        a = "(i for i in list(d.keys()))"
1346        self.check(b, a)
1347
1348    def test_16(self):
1349        b = "(i for i in d.iterkeys())"
1350        a = "(i for i in d.keys())"
1351        self.check(b, a)
1352
1353    def test_17(self):
1354        b = "iter(d.iterkeys())"
1355        a = "iter(d.keys())"
1356        self.check(b, a)
1357
1358    def test_18(self):
1359        b = "list(d.iterkeys())"
1360        a = "list(d.keys())"
1361        self.check(b, a)
1362
1363    def test_19(self):
1364        b = "sorted(d.iterkeys())"
1365        a = "sorted(d.keys())"
1366        self.check(b, a)
1367
1368    def test_20(self):
1369        b = "foo(d.iterkeys())"
1370        a = "foo(iter(d.keys()))"
1371        self.check(b, a)
1372
1373    def test_21(self):
1374        b = "print h.iterkeys().next()"
1375        a = "print iter(h.keys()).next()"
1376        self.check(b, a)
1377
1378    def test_22(self):
1379        b = "print h.keys()[0]"
1380        a = "print list(h.keys())[0]"
1381        self.check(b, a)
1382
1383    def test_23(self):
1384        b = "print list(h.iterkeys().next())"
1385        a = "print list(iter(h.keys()).next())"
1386        self.check(b, a)
1387
1388    def test_24(self):
1389        b = "for x in h.keys()[0]: print x"
1390        a = "for x in list(h.keys())[0]: print x"
1391        self.check(b, a)
1392
1393    def test_25(self):
1394        b = "d.viewkeys()"
1395        a = "d.keys()"
1396        self.check(b, a)
1397
1398    def test_26(self):
1399        b = "d.viewitems()"
1400        a = "d.items()"
1401        self.check(b, a)
1402
1403    def test_27(self):
1404        b = "d.viewvalues()"
1405        a = "d.values()"
1406        self.check(b, a)
1407
1408    def test_14(self):
1409        b = "[i for i in d.viewkeys()]"
1410        a = "[i for i in d.keys()]"
1411        self.check(b, a)
1412
1413    def test_15(self):
1414        b = "(i for i in d.viewkeys())"
1415        a = "(i for i in d.keys())"
1416        self.check(b, a)
1417
1418    def test_17(self):
1419        b = "iter(d.viewkeys())"
1420        a = "iter(d.keys())"
1421        self.check(b, a)
1422
1423    def test_18(self):
1424        b = "list(d.viewkeys())"
1425        a = "list(d.keys())"
1426        self.check(b, a)
1427
1428    def test_19(self):
1429        b = "sorted(d.viewkeys())"
1430        a = "sorted(d.keys())"
1431        self.check(b, a)
1432
1433class Test_xrange(FixerTestCase):
1434    fixer = "xrange"
1435
1436    def test_prefix_preservation(self):
1437        b = """x =    xrange(  10  )"""
1438        a = """x =    range(  10  )"""
1439        self.check(b, a)
1440
1441        b = """x = xrange(  1  ,  10   )"""
1442        a = """x = range(  1  ,  10   )"""
1443        self.check(b, a)
1444
1445        b = """x = xrange(  0  ,  10 ,  2 )"""
1446        a = """x = range(  0  ,  10 ,  2 )"""
1447        self.check(b, a)
1448
1449    def test_single_arg(self):
1450        b = """x = xrange(10)"""
1451        a = """x = range(10)"""
1452        self.check(b, a)
1453
1454    def test_two_args(self):
1455        b = """x = xrange(1, 10)"""
1456        a = """x = range(1, 10)"""
1457        self.check(b, a)
1458
1459    def test_three_args(self):
1460        b = """x = xrange(0, 10, 2)"""
1461        a = """x = range(0, 10, 2)"""
1462        self.check(b, a)
1463
1464    def test_wrap_in_list(self):
1465        b = """x = range(10, 3, 9)"""
1466        a = """x = list(range(10, 3, 9))"""
1467        self.check(b, a)
1468
1469        b = """x = foo(range(10, 3, 9))"""
1470        a = """x = foo(list(range(10, 3, 9)))"""
1471        self.check(b, a)
1472
1473        b = """x = range(10, 3, 9) + [4]"""
1474        a = """x = list(range(10, 3, 9)) + [4]"""
1475        self.check(b, a)
1476
1477        b = """x = range(10)[::-1]"""
1478        a = """x = list(range(10))[::-1]"""
1479        self.check(b, a)
1480
1481        b = """x = range(10)  [3]"""
1482        a = """x = list(range(10))  [3]"""
1483        self.check(b, a)
1484
1485    def test_xrange_in_for(self):
1486        b = """for i in xrange(10):\n    j=i"""
1487        a = """for i in range(10):\n    j=i"""
1488        self.check(b, a)
1489
1490        b = """[i for i in xrange(10)]"""
1491        a = """[i for i in range(10)]"""
1492        self.check(b, a)
1493
1494    def test_range_in_for(self):
1495        self.unchanged("for i in range(10): pass")
1496        self.unchanged("[i for i in range(10)]")
1497
1498    def test_in_contains_test(self):
1499        self.unchanged("x in range(10, 3, 9)")
1500
1501    def test_in_consuming_context(self):
1502        for call in fixer_util.consuming_calls:
1503            self.unchanged("a = %s(range(10))" % call)
1504
1505class Test_xrange_with_reduce(FixerTestCase):
1506
1507    def setUp(self):
1508        super(Test_xrange_with_reduce, self).setUp(["xrange", "reduce"])
1509
1510    def test_double_transform(self):
1511        b = """reduce(x, xrange(5))"""
1512        a = """from functools import reduce
1513reduce(x, range(5))"""
1514        self.check(b, a)
1515
1516class Test_raw_input(FixerTestCase):
1517    fixer = "raw_input"
1518
1519    def test_prefix_preservation(self):
1520        b = """x =    raw_input(   )"""
1521        a = """x =    input(   )"""
1522        self.check(b, a)
1523
1524        b = """x = raw_input(   ''   )"""
1525        a = """x = input(   ''   )"""
1526        self.check(b, a)
1527
1528    def test_1(self):
1529        b = """x = raw_input()"""
1530        a = """x = input()"""
1531        self.check(b, a)
1532
1533    def test_2(self):
1534        b = """x = raw_input('')"""
1535        a = """x = input('')"""
1536        self.check(b, a)
1537
1538    def test_3(self):
1539        b = """x = raw_input('prompt')"""
1540        a = """x = input('prompt')"""
1541        self.check(b, a)
1542
1543    def test_4(self):
1544        b = """x = raw_input(foo(a) + 6)"""
1545        a = """x = input(foo(a) + 6)"""
1546        self.check(b, a)
1547
1548    def test_5(self):
1549        b = """x = raw_input(invite).split()"""
1550        a = """x = input(invite).split()"""
1551        self.check(b, a)
1552
1553    def test_6(self):
1554        b = """x = raw_input(invite) . split ()"""
1555        a = """x = input(invite) . split ()"""
1556        self.check(b, a)
1557
1558    def test_8(self):
1559        b = "x = int(raw_input())"
1560        a = "x = int(input())"
1561        self.check(b, a)
1562
1563class Test_funcattrs(FixerTestCase):
1564    fixer = "funcattrs"
1565
1566    attrs = ["closure", "doc", "name", "defaults", "code", "globals", "dict"]
1567
1568    def test(self):
1569        for attr in self.attrs:
1570            b = "a.func_%s" % attr
1571            a = "a.__%s__" % attr
1572            self.check(b, a)
1573
1574            b = "self.foo.func_%s.foo_bar" % attr
1575            a = "self.foo.__%s__.foo_bar" % attr
1576            self.check(b, a)
1577
1578    def test_unchanged(self):
1579        for attr in self.attrs:
1580            s = "foo(func_%s + 5)" % attr
1581            self.unchanged(s)
1582
1583            s = "f(foo.__%s__)" % attr
1584            self.unchanged(s)
1585
1586            s = "f(foo.__%s__.foo)" % attr
1587            self.unchanged(s)
1588
1589class Test_xreadlines(FixerTestCase):
1590    fixer = "xreadlines"
1591
1592    def test_call(self):
1593        b = "for x in f.xreadlines(): pass"
1594        a = "for x in f: pass"
1595        self.check(b, a)
1596
1597        b = "for x in foo().xreadlines(): pass"
1598        a = "for x in foo(): pass"
1599        self.check(b, a)
1600
1601        b = "for x in (5 + foo()).xreadlines(): pass"
1602        a = "for x in (5 + foo()): pass"
1603        self.check(b, a)
1604
1605    def test_attr_ref(self):
1606        b = "foo(f.xreadlines + 5)"
1607        a = "foo(f.__iter__ + 5)"
1608        self.check(b, a)
1609
1610        b = "foo(f().xreadlines + 5)"
1611        a = "foo(f().__iter__ + 5)"
1612        self.check(b, a)
1613
1614        b = "foo((5 + f()).xreadlines + 5)"
1615        a = "foo((5 + f()).__iter__ + 5)"
1616        self.check(b, a)
1617
1618    def test_unchanged(self):
1619        s = "for x in f.xreadlines(5): pass"
1620        self.unchanged(s)
1621
1622        s = "for x in f.xreadlines(k=5): pass"
1623        self.unchanged(s)
1624
1625        s = "for x in f.xreadlines(*k, **v): pass"
1626        self.unchanged(s)
1627
1628        s = "foo(xreadlines)"
1629        self.unchanged(s)
1630
1631
1632class ImportsFixerTests:
1633
1634    def test_import_module(self):
1635        for old, new in self.modules.items():
1636            b = "import %s" % old
1637            a = "import %s" % new
1638            self.check(b, a)
1639
1640            b = "import foo, %s, bar" % old
1641            a = "import foo, %s, bar" % new
1642            self.check(b, a)
1643
1644    def test_import_from(self):
1645        for old, new in self.modules.items():
1646            b = "from %s import foo" % old
1647            a = "from %s import foo" % new
1648            self.check(b, a)
1649
1650            b = "from %s import foo, bar" % old
1651            a = "from %s import foo, bar" % new
1652            self.check(b, a)
1653
1654            b = "from %s import (yes, no)" % old
1655            a = "from %s import (yes, no)" % new
1656            self.check(b, a)
1657
1658    def test_import_module_as(self):
1659        for old, new in self.modules.items():
1660            b = "import %s as foo_bar" % old
1661            a = "import %s as foo_bar" % new
1662            self.check(b, a)
1663
1664            b = "import %s as foo_bar" % old
1665            a = "import %s as foo_bar" % new
1666            self.check(b, a)
1667
1668    def test_import_from_as(self):
1669        for old, new in self.modules.items():
1670            b = "from %s import foo as bar" % old
1671            a = "from %s import foo as bar" % new
1672            self.check(b, a)
1673
1674    def test_star(self):
1675        for old, new in self.modules.items():
1676            b = "from %s import *" % old
1677            a = "from %s import *" % new
1678            self.check(b, a)
1679
1680    def test_import_module_usage(self):
1681        for old, new in self.modules.items():
1682            b = """
1683                import %s
1684                foo(%s.bar)
1685                """ % (old, old)
1686            a = """
1687                import %s
1688                foo(%s.bar)
1689                """ % (new, new)
1690            self.check(b, a)
1691
1692            b = """
1693                from %s import x
1694                %s = 23
1695                """ % (old, old)
1696            a = """
1697                from %s import x
1698                %s = 23
1699                """ % (new, old)
1700            self.check(b, a)
1701
1702            s = """
1703                def f():
1704                    %s.method()
1705                """ % (old,)
1706            self.unchanged(s)
1707
1708            # test nested usage
1709            b = """
1710                import %s
1711                %s.bar(%s.foo)
1712                """ % (old, old, old)
1713            a = """
1714                import %s
1715                %s.bar(%s.foo)
1716                """ % (new, new, new)
1717            self.check(b, a)
1718
1719            b = """
1720                import %s
1721                x.%s
1722                """ % (old, old)
1723            a = """
1724                import %s
1725                x.%s
1726                """ % (new, old)
1727            self.check(b, a)
1728
1729
1730class Test_imports(FixerTestCase, ImportsFixerTests):
1731    fixer = "imports"
1732    from ..fixes.fix_imports import MAPPING as modules
1733
1734    def test_multiple_imports(self):
1735        b = """import urlparse, cStringIO"""
1736        a = """import urllib.parse, io"""
1737        self.check(b, a)
1738
1739    def test_multiple_imports_as(self):
1740        b = """
1741            import copy_reg as bar, HTMLParser as foo, urlparse
1742            s = urlparse.spam(bar.foo())
1743            """
1744        a = """
1745            import copyreg as bar, html.parser as foo, urllib.parse
1746            s = urllib.parse.spam(bar.foo())
1747            """
1748        self.check(b, a)
1749
1750
1751class Test_imports2(FixerTestCase, ImportsFixerTests):
1752    fixer = "imports2"
1753    from ..fixes.fix_imports2 import MAPPING as modules
1754
1755
1756class Test_imports_fixer_order(FixerTestCase, ImportsFixerTests):
1757
1758    def setUp(self):
1759        super(Test_imports_fixer_order, self).setUp(['imports', 'imports2'])
1760        from ..fixes.fix_imports2 import MAPPING as mapping2
1761        self.modules = mapping2.copy()
1762        from ..fixes.fix_imports import MAPPING as mapping1
1763        for key in ('dbhash', 'dumbdbm', 'dbm', 'gdbm'):
1764            self.modules[key] = mapping1[key]
1765
1766    def test_after_local_imports_refactoring(self):
1767        for fix in ("imports", "imports2"):
1768            self.fixer = fix
1769            self.assert_runs_after("import")
1770
1771
1772class Test_urllib(FixerTestCase):
1773    fixer = "urllib"
1774    from ..fixes.fix_urllib import MAPPING as modules
1775
1776    def test_import_module(self):
1777        for old, changes in self.modules.items():
1778            b = "import %s" % old
1779            a = "import %s" % ", ".join(map(itemgetter(0), changes))
1780            self.check(b, a)
1781
1782    def test_import_from(self):
1783        for old, changes in self.modules.items():
1784            all_members = []
1785            for new, members in changes:
1786                for member in members:
1787                    all_members.append(member)
1788                    b = "from %s import %s" % (old, member)
1789                    a = "from %s import %s" % (new, member)
1790                    self.check(b, a)
1791
1792                    s = "from foo import %s" % member
1793                    self.unchanged(s)
1794
1795                b = "from %s import %s" % (old, ", ".join(members))
1796                a = "from %s import %s" % (new, ", ".join(members))
1797                self.check(b, a)
1798
1799                s = "from foo import %s" % ", ".join(members)
1800                self.unchanged(s)
1801
1802            # test the breaking of a module into multiple replacements
1803            b = "from %s import %s" % (old, ", ".join(all_members))
1804            a = "\n".join(["from %s import %s" % (new, ", ".join(members))
1805                            for (new, members) in changes])
1806            self.check(b, a)
1807
1808    def test_import_module_as(self):
1809        for old in self.modules:
1810            s = "import %s as foo" % old
1811            self.warns_unchanged(s, "This module is now multiple modules")
1812
1813    def test_import_from_as(self):
1814        for old, changes in self.modules.items():
1815            for new, members in changes:
1816                for member in members:
1817                    b = "from %s import %s as foo_bar" % (old, member)
1818                    a = "from %s import %s as foo_bar" % (new, member)
1819                    self.check(b, a)
1820                    b = "from %s import %s as blah, %s" % (old, member, member)
1821                    a = "from %s import %s as blah, %s" % (new, member, member)
1822                    self.check(b, a)
1823
1824    def test_star(self):
1825        for old in self.modules:
1826            s = "from %s import *" % old
1827            self.warns_unchanged(s, "Cannot handle star imports")
1828
1829    def test_indented(self):
1830        b = """
1831def foo():
1832    from urllib import urlencode, urlopen
1833"""
1834        a = """
1835def foo():
1836    from urllib.parse import urlencode
1837    from urllib.request import urlopen
1838"""
1839        self.check(b, a)
1840
1841        b = """
1842def foo():
1843    other()
1844    from urllib import urlencode, urlopen
1845"""
1846        a = """
1847def foo():
1848    other()
1849    from urllib.parse import urlencode
1850    from urllib.request import urlopen
1851"""
1852        self.check(b, a)
1853
1854
1855
1856    def test_import_module_usage(self):
1857        for old, changes in self.modules.items():
1858            for new, members in changes:
1859                for member in members:
1860                    new_import = ", ".join([n for (n, mems)
1861                                            in self.modules[old]])
1862                    b = """
1863                        import %s
1864                        foo(%s.%s)
1865                        """ % (old, old, member)
1866                    a = """
1867

Large files files are truncated, but you can click here to view the full file