/lib-python/2.7/lib2to3/tests/test_fixers.py
Python | 4529 lines | 4452 code | 72 blank | 5 comment | 11 complexity | 10926e53ce2f87d4767fc70ff8b03837 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- """ Test suite for the fixer modules """
- # Python imports
- import os
- import unittest
- from itertools import chain
- from operator import itemgetter
- # Local imports
- from lib2to3 import pygram, pytree, refactor, fixer_util
- from lib2to3.tests import support
- class FixerTestCase(support.TestCase):
- # Other test cases can subclass this class and replace "fixer_pkg" with
- # their own.
- def setUp(self, fix_list=None, fixer_pkg="lib2to3", options=None):
- if fix_list is None:
- fix_list = [self.fixer]
- self.refactor = support.get_refactorer(fixer_pkg, fix_list, options)
- self.fixer_log = []
- self.filename = u"<string>"
- for fixer in chain(self.refactor.pre_order,
- self.refactor.post_order):
- fixer.log = self.fixer_log
- def _check(self, before, after):
- before = support.reformat(before)
- after = support.reformat(after)
- tree = self.refactor.refactor_string(before, self.filename)
- self.assertEqual(after, unicode(tree))
- return tree
- def check(self, before, after, ignore_warnings=False):
- tree = self._check(before, after)
- self.assertTrue(tree.was_changed)
- if not ignore_warnings:
- self.assertEqual(self.fixer_log, [])
- def warns(self, before, after, message, unchanged=False):
- tree = self._check(before, after)
- self.assertTrue(message in "".join(self.fixer_log))
- if not unchanged:
- self.assertTrue(tree.was_changed)
- def warns_unchanged(self, before, message):
- self.warns(before, before, message, unchanged=True)
- def unchanged(self, before, ignore_warnings=False):
- self._check(before, before)
- if not ignore_warnings:
- self.assertEqual(self.fixer_log, [])
- def assert_runs_after(self, *names):
- fixes = [self.fixer]
- fixes.extend(names)
- r = support.get_refactorer("lib2to3", fixes)
- (pre, post) = r.get_fixers()
- n = "fix_" + self.fixer
- if post and post[-1].__class__.__module__.endswith(n):
- # We're the last fixer to run
- return
- if pre and pre[-1].__class__.__module__.endswith(n) and not post:
- # We're the last in pre and post is empty
- return
- self.fail("Fixer run order (%s) is incorrect; %s should be last."\
- %(", ".join([x.__class__.__module__ for x in (pre+post)]), n))
- class Test_ne(FixerTestCase):
- fixer = "ne"
- def test_basic(self):
- b = """if x <> y:
- pass"""
- a = """if x != y:
- pass"""
- self.check(b, a)
- def test_no_spaces(self):
- b = """if x<>y:
- pass"""
- a = """if x!=y:
- pass"""
- self.check(b, a)
- def test_chained(self):
- b = """if x<>y<>z:
- pass"""
- a = """if x!=y!=z:
- pass"""
- self.check(b, a)
- class Test_has_key(FixerTestCase):
- fixer = "has_key"
- def test_1(self):
- b = """x = d.has_key("x") or d.has_key("y")"""
- a = """x = "x" in d or "y" in d"""
- self.check(b, a)
- def test_2(self):
- b = """x = a.b.c.d.has_key("x") ** 3"""
- a = """x = ("x" in a.b.c.d) ** 3"""
- self.check(b, a)
- def test_3(self):
- b = """x = a.b.has_key(1 + 2).__repr__()"""
- a = """x = (1 + 2 in a.b).__repr__()"""
- self.check(b, a)
- def test_4(self):
- b = """x = a.b.has_key(1 + 2).__repr__() ** -3 ** 4"""
- a = """x = (1 + 2 in a.b).__repr__() ** -3 ** 4"""
- self.check(b, a)
- def test_5(self):
- b = """x = a.has_key(f or g)"""
- a = """x = (f or g) in a"""
- self.check(b, a)
- def test_6(self):
- b = """x = a + b.has_key(c)"""
- a = """x = a + (c in b)"""
- self.check(b, a)
- def test_7(self):
- b = """x = a.has_key(lambda: 12)"""
- a = """x = (lambda: 12) in a"""
- self.check(b, a)
- def test_8(self):
- b = """x = a.has_key(a for a in b)"""
- a = """x = (a for a in b) in a"""
- self.check(b, a)
- def test_9(self):
- b = """if not a.has_key(b): pass"""
- a = """if b not in a: pass"""
- self.check(b, a)
- def test_10(self):
- b = """if not a.has_key(b).__repr__(): pass"""
- a = """if not (b in a).__repr__(): pass"""
- self.check(b, a)
- def test_11(self):
- b = """if not a.has_key(b) ** 2: pass"""
- a = """if not (b in a) ** 2: pass"""
- self.check(b, a)
- class Test_apply(FixerTestCase):
- fixer = "apply"
- def test_1(self):
- b = """x = apply(f, g + h)"""
- a = """x = f(*g + h)"""
- self.check(b, a)
- def test_2(self):
- b = """y = apply(f, g, h)"""
- a = """y = f(*g, **h)"""
- self.check(b, a)
- def test_3(self):
- b = """z = apply(fs[0], g or h, h or g)"""
- a = """z = fs[0](*g or h, **h or g)"""
- self.check(b, a)
- def test_4(self):
- b = """apply(f, (x, y) + t)"""
- a = """f(*(x, y) + t)"""
- self.check(b, a)
- def test_5(self):
- b = """apply(f, args,)"""
- a = """f(*args)"""
- self.check(b, a)
- def test_6(self):
- b = """apply(f, args, kwds,)"""
- a = """f(*args, **kwds)"""
- self.check(b, a)
- # Test that complex functions are parenthesized
- def test_complex_1(self):
- b = """x = apply(f+g, args)"""
- a = """x = (f+g)(*args)"""
- self.check(b, a)
- def test_complex_2(self):
- b = """x = apply(f*g, args)"""
- a = """x = (f*g)(*args)"""
- self.check(b, a)
- def test_complex_3(self):
- b = """x = apply(f**g, args)"""
- a = """x = (f**g)(*args)"""
- self.check(b, a)
- # But dotted names etc. not
- def test_dotted_name(self):
- b = """x = apply(f.g, args)"""
- a = """x = f.g(*args)"""
- self.check(b, a)
- def test_subscript(self):
- b = """x = apply(f[x], args)"""
- a = """x = f[x](*args)"""
- self.check(b, a)
- def test_call(self):
- b = """x = apply(f(), args)"""
- a = """x = f()(*args)"""
- self.check(b, a)
- # Extreme case
- def test_extreme(self):
- b = """x = apply(a.b.c.d.e.f, args, kwds)"""
- a = """x = a.b.c.d.e.f(*args, **kwds)"""
- self.check(b, a)
- # XXX Comments in weird places still get lost
- def test_weird_comments(self):
- b = """apply( # foo
- f, # bar
- args)"""
- a = """f(*args)"""
- self.check(b, a)
- # These should *not* be touched
- def test_unchanged_1(self):
- s = """apply()"""
- self.unchanged(s)
- def test_unchanged_2(self):
- s = """apply(f)"""
- self.unchanged(s)
- def test_unchanged_3(self):
- s = """apply(f,)"""
- self.unchanged(s)
- def test_unchanged_4(self):
- s = """apply(f, args, kwds, extras)"""
- self.unchanged(s)
- def test_unchanged_5(self):
- s = """apply(f, *args, **kwds)"""
- self.unchanged(s)
- def test_unchanged_6(self):
- s = """apply(f, *args)"""
- self.unchanged(s)
- def test_unchanged_7(self):
- s = """apply(func=f, args=args, kwds=kwds)"""
- self.unchanged(s)
- def test_unchanged_8(self):
- s = """apply(f, args=args, kwds=kwds)"""
- self.unchanged(s)
- def test_unchanged_9(self):
- s = """apply(f, args, kwds=kwds)"""
- self.unchanged(s)
- def test_space_1(self):
- a = """apply( f, args, kwds)"""
- b = """f(*args, **kwds)"""
- self.check(a, b)
- def test_space_2(self):
- a = """apply( f ,args,kwds )"""
- b = """f(*args, **kwds)"""
- self.check(a, b)
- class Test_intern(FixerTestCase):
- fixer = "intern"
- def test_prefix_preservation(self):
- b = """x = intern( a )"""
- a = """import sys\nx = sys.intern( a )"""
- self.check(b, a)
- b = """y = intern("b" # test
- )"""
- a = """import sys\ny = sys.intern("b" # test
- )"""
- self.check(b, a)
- b = """z = intern(a+b+c.d, )"""
- a = """import sys\nz = sys.intern(a+b+c.d, )"""
- self.check(b, a)
- def test(self):
- b = """x = intern(a)"""
- a = """import sys\nx = sys.intern(a)"""
- self.check(b, a)
- b = """z = intern(a+b+c.d,)"""
- a = """import sys\nz = sys.intern(a+b+c.d,)"""
- self.check(b, a)
- b = """intern("y%s" % 5).replace("y", "")"""
- a = """import sys\nsys.intern("y%s" % 5).replace("y", "")"""
- self.check(b, a)
- # These should not be refactored
- def test_unchanged(self):
- s = """intern(a=1)"""
- self.unchanged(s)
- s = """intern(f, g)"""
- self.unchanged(s)
- s = """intern(*h)"""
- self.unchanged(s)
- s = """intern(**i)"""
- self.unchanged(s)
- s = """intern()"""
- self.unchanged(s)
- class Test_reduce(FixerTestCase):
- fixer = "reduce"
- def test_simple_call(self):
- b = "reduce(a, b, c)"
- a = "from functools import reduce\nreduce(a, b, c)"
- self.check(b, a)
- def test_bug_7253(self):
- # fix_tuple_params was being bad and orphaning nodes in the tree.
- b = "def x(arg): reduce(sum, [])"
- a = "from functools import reduce\ndef x(arg): reduce(sum, [])"
- self.check(b, a)
- def test_call_with_lambda(self):
- b = "reduce(lambda x, y: x + y, seq)"
- a = "from functools import reduce\nreduce(lambda x, y: x + y, seq)"
- self.check(b, a)
- def test_unchanged(self):
- s = "reduce(a)"
- self.unchanged(s)
- s = "reduce(a, b=42)"
- self.unchanged(s)
- s = "reduce(a, b, c, d)"
- self.unchanged(s)
- s = "reduce(**c)"
- self.unchanged(s)
- s = "reduce()"
- self.unchanged(s)
- class Test_print(FixerTestCase):
- fixer = "print"
- def test_prefix_preservation(self):
- b = """print 1, 1+1, 1+1+1"""
- a = """print(1, 1+1, 1+1+1)"""
- self.check(b, a)
- def test_idempotency(self):
- s = """print()"""
- self.unchanged(s)
- s = """print('')"""
- self.unchanged(s)
- def test_idempotency_print_as_function(self):
- self.refactor.driver.grammar = pygram.python_grammar_no_print_statement
- s = """print(1, 1+1, 1+1+1)"""
- self.unchanged(s)
- s = """print()"""
- self.unchanged(s)
- s = """print('')"""
- self.unchanged(s)
- def test_1(self):
- b = """print 1, 1+1, 1+1+1"""
- a = """print(1, 1+1, 1+1+1)"""
- self.check(b, a)
- def test_2(self):
- b = """print 1, 2"""
- a = """print(1, 2)"""
- self.check(b, a)
- def test_3(self):
- b = """print"""
- a = """print()"""
- self.check(b, a)
- def test_4(self):
- # from bug 3000
- b = """print whatever; print"""
- a = """print(whatever); print()"""
- self.check(b, a)
- def test_5(self):
- b = """print; print whatever;"""
- a = """print(); print(whatever);"""
- self.check(b, a)
- def test_tuple(self):
- b = """print (a, b, c)"""
- a = """print((a, b, c))"""
- self.check(b, a)
- # trailing commas
- def test_trailing_comma_1(self):
- b = """print 1, 2, 3,"""
- a = """print(1, 2, 3, end=' ')"""
- self.check(b, a)
- def test_trailing_comma_2(self):
- b = """print 1, 2,"""
- a = """print(1, 2, end=' ')"""
- self.check(b, a)
- def test_trailing_comma_3(self):
- b = """print 1,"""
- a = """print(1, end=' ')"""
- self.check(b, a)
- # >> stuff
- def test_vargs_without_trailing_comma(self):
- b = """print >>sys.stderr, 1, 2, 3"""
- a = """print(1, 2, 3, file=sys.stderr)"""
- self.check(b, a)
- def test_with_trailing_comma(self):
- b = """print >>sys.stderr, 1, 2,"""
- a = """print(1, 2, end=' ', file=sys.stderr)"""
- self.check(b, a)
- def test_no_trailing_comma(self):
- b = """print >>sys.stderr, 1+1"""
- a = """print(1+1, file=sys.stderr)"""
- self.check(b, a)
- def test_spaces_before_file(self):
- b = """print >> sys.stderr"""
- a = """print(file=sys.stderr)"""
- self.check(b, a)
- def test_with_future_print_function(self):
- s = "from __future__ import print_function\n" \
- "print('Hai!', end=' ')"
- self.unchanged(s)
- b = "print 'Hello, world!'"
- a = "print('Hello, world!')"
- self.check(b, a)
- class Test_exec(FixerTestCase):
- fixer = "exec"
- def test_prefix_preservation(self):
- b = """ exec code in ns1, ns2"""
- a = """ exec(code, ns1, ns2)"""
- self.check(b, a)
- def test_basic(self):
- b = """exec code"""
- a = """exec(code)"""
- self.check(b, a)
- def test_with_globals(self):
- b = """exec code in ns"""
- a = """exec(code, ns)"""
- self.check(b, a)
- def test_with_globals_locals(self):
- b = """exec code in ns1, ns2"""
- a = """exec(code, ns1, ns2)"""
- self.check(b, a)
- def test_complex_1(self):
- b = """exec (a.b()) in ns"""
- a = """exec((a.b()), ns)"""
- self.check(b, a)
- def test_complex_2(self):
- b = """exec a.b() + c in ns"""
- a = """exec(a.b() + c, ns)"""
- self.check(b, a)
- # These should not be touched
- def test_unchanged_1(self):
- s = """exec(code)"""
- self.unchanged(s)
- def test_unchanged_2(self):
- s = """exec (code)"""
- self.unchanged(s)
- def test_unchanged_3(self):
- s = """exec(code, ns)"""
- self.unchanged(s)
- def test_unchanged_4(self):
- s = """exec(code, ns1, ns2)"""
- self.unchanged(s)
- class Test_repr(FixerTestCase):
- fixer = "repr"
- def test_prefix_preservation(self):
- b = """x = `1 + 2`"""
- a = """x = repr(1 + 2)"""
- self.check(b, a)
- def test_simple_1(self):
- b = """x = `1 + 2`"""
- a = """x = repr(1 + 2)"""
- self.check(b, a)
- def test_simple_2(self):
- b = """y = `x`"""
- a = """y = repr(x)"""
- self.check(b, a)
- def test_complex(self):
- b = """z = `y`.__repr__()"""
- a = """z = repr(y).__repr__()"""
- self.check(b, a)
- def test_tuple(self):
- b = """x = `1, 2, 3`"""
- a = """x = repr((1, 2, 3))"""
- self.check(b, a)
- def test_nested(self):
- b = """x = `1 + `2``"""
- a = """x = repr(1 + repr(2))"""
- self.check(b, a)
- def test_nested_tuples(self):
- b = """x = `1, 2 + `3, 4``"""
- a = """x = repr((1, 2 + repr((3, 4))))"""
- self.check(b, a)
- class Test_except(FixerTestCase):
- fixer = "except"
- def test_prefix_preservation(self):
- b = """
- try:
- pass
- except (RuntimeError, ImportError), e:
- pass"""
- a = """
- try:
- pass
- except (RuntimeError, ImportError) as e:
- pass"""
- self.check(b, a)
- def test_simple(self):
- b = """
- try:
- pass
- except Foo, e:
- pass"""
- a = """
- try:
- pass
- except Foo as e:
- pass"""
- self.check(b, a)
- def test_simple_no_space_before_target(self):
- b = """
- try:
- pass
- except Foo,e:
- pass"""
- a = """
- try:
- pass
- except Foo as e:
- pass"""
- self.check(b, a)
- def test_tuple_unpack(self):
- b = """
- def foo():
- try:
- pass
- except Exception, (f, e):
- pass
- except ImportError, e:
- pass"""
- a = """
- def foo():
- try:
- pass
- except Exception as xxx_todo_changeme:
- (f, e) = xxx_todo_changeme.args
- pass
- except ImportError as e:
- pass"""
- self.check(b, a)
- def test_multi_class(self):
- b = """
- try:
- pass
- except (RuntimeError, ImportError), e:
- pass"""
- a = """
- try:
- pass
- except (RuntimeError, ImportError) as e:
- pass"""
- self.check(b, a)
- def test_list_unpack(self):
- b = """
- try:
- pass
- except Exception, [a, b]:
- pass"""
- a = """
- try:
- pass
- except Exception as xxx_todo_changeme:
- [a, b] = xxx_todo_changeme.args
- pass"""
- self.check(b, a)
- def test_weird_target_1(self):
- b = """
- try:
- pass
- except Exception, d[5]:
- pass"""
- a = """
- try:
- pass
- except Exception as xxx_todo_changeme:
- d[5] = xxx_todo_changeme
- pass"""
- self.check(b, a)
- def test_weird_target_2(self):
- b = """
- try:
- pass
- except Exception, a.foo:
- pass"""
- a = """
- try:
- pass
- except Exception as xxx_todo_changeme:
- a.foo = xxx_todo_changeme
- pass"""
- self.check(b, a)
- def test_weird_target_3(self):
- b = """
- try:
- pass
- except Exception, a().foo:
- pass"""
- a = """
- try:
- pass
- except Exception as xxx_todo_changeme:
- a().foo = xxx_todo_changeme
- pass"""
- self.check(b, a)
- def test_bare_except(self):
- b = """
- try:
- pass
- except Exception, a:
- pass
- except:
- pass"""
- a = """
- try:
- pass
- except Exception as a:
- pass
- except:
- pass"""
- self.check(b, a)
- def test_bare_except_and_else_finally(self):
- b = """
- try:
- pass
- except Exception, a:
- pass
- except:
- pass
- else:
- pass
- finally:
- pass"""
- a = """
- try:
- pass
- except Exception as a:
- pass
- except:
- pass
- else:
- pass
- finally:
- pass"""
- self.check(b, a)
- def test_multi_fixed_excepts_before_bare_except(self):
- b = """
- try:
- pass
- except TypeError, b:
- pass
- except Exception, a:
- pass
- except:
- pass"""
- a = """
- try:
- pass
- except TypeError as b:
- pass
- except Exception as a:
- pass
- except:
- pass"""
- self.check(b, a)
- def test_one_line_suites(self):
- b = """
- try: raise TypeError
- except TypeError, e:
- pass
- """
- a = """
- try: raise TypeError
- except TypeError as e:
- pass
- """
- self.check(b, a)
- b = """
- try:
- raise TypeError
- except TypeError, e: pass
- """
- a = """
- try:
- raise TypeError
- except TypeError as e: pass
- """
- self.check(b, a)
- b = """
- try: raise TypeError
- except TypeError, e: pass
- """
- a = """
- try: raise TypeError
- except TypeError as e: pass
- """
- self.check(b, a)
- b = """
- try: raise TypeError
- except TypeError, e: pass
- else: function()
- finally: done()
- """
- a = """
- try: raise TypeError
- except TypeError as e: pass
- else: function()
- finally: done()
- """
- self.check(b, a)
- # These should not be touched:
- def test_unchanged_1(self):
- s = """
- try:
- pass
- except:
- pass"""
- self.unchanged(s)
- def test_unchanged_2(self):
- s = """
- try:
- pass
- except Exception:
- pass"""
- self.unchanged(s)
- def test_unchanged_3(self):
- s = """
- try:
- pass
- except (Exception, SystemExit):
- pass"""
- self.unchanged(s)
- class Test_raise(FixerTestCase):
- fixer = "raise"
- def test_basic(self):
- b = """raise Exception, 5"""
- a = """raise Exception(5)"""
- self.check(b, a)
- def test_prefix_preservation(self):
- b = """raise Exception,5"""
- a = """raise Exception(5)"""
- self.check(b, a)
- b = """raise Exception, 5"""
- a = """raise Exception(5)"""
- self.check(b, a)
- def test_with_comments(self):
- b = """raise Exception, 5 # foo"""
- a = """raise Exception(5) # foo"""
- self.check(b, a)
- b = """raise E, (5, 6) % (a, b) # foo"""
- a = """raise E((5, 6) % (a, b)) # foo"""
- self.check(b, a)
- b = """def foo():
- raise Exception, 5, 6 # foo"""
- a = """def foo():
- raise Exception(5).with_traceback(6) # foo"""
- self.check(b, a)
- def test_None_value(self):
- b = """raise Exception(5), None, tb"""
- a = """raise Exception(5).with_traceback(tb)"""
- self.check(b, a)
- def test_tuple_value(self):
- b = """raise Exception, (5, 6, 7)"""
- a = """raise Exception(5, 6, 7)"""
- self.check(b, a)
- def test_tuple_detection(self):
- b = """raise E, (5, 6) % (a, b)"""
- a = """raise E((5, 6) % (a, b))"""
- self.check(b, a)
- def test_tuple_exc_1(self):
- b = """raise (((E1, E2), E3), E4), V"""
- a = """raise E1(V)"""
- self.check(b, a)
- def test_tuple_exc_2(self):
- b = """raise (E1, (E2, E3), E4), V"""
- a = """raise E1(V)"""
- self.check(b, a)
- # These should produce a warning
- def test_string_exc(self):
- s = """raise 'foo'"""
- self.warns_unchanged(s, "Python 3 does not support string exceptions")
- def test_string_exc_val(self):
- s = """raise "foo", 5"""
- self.warns_unchanged(s, "Python 3 does not support string exceptions")
- def test_string_exc_val_tb(self):
- s = """raise "foo", 5, 6"""
- self.warns_unchanged(s, "Python 3 does not support string exceptions")
- # These should result in traceback-assignment
- def test_tb_1(self):
- b = """def foo():
- raise Exception, 5, 6"""
- a = """def foo():
- raise Exception(5).with_traceback(6)"""
- self.check(b, a)
- def test_tb_2(self):
- b = """def foo():
- a = 5
- raise Exception, 5, 6
- b = 6"""
- a = """def foo():
- a = 5
- raise Exception(5).with_traceback(6)
- b = 6"""
- self.check(b, a)
- def test_tb_3(self):
- b = """def foo():
- raise Exception,5,6"""
- a = """def foo():
- raise Exception(5).with_traceback(6)"""
- self.check(b, a)
- def test_tb_4(self):
- b = """def foo():
- a = 5
- raise Exception,5,6
- b = 6"""
- a = """def foo():
- a = 5
- raise Exception(5).with_traceback(6)
- b = 6"""
- self.check(b, a)
- def test_tb_5(self):
- b = """def foo():
- raise Exception, (5, 6, 7), 6"""
- a = """def foo():
- raise Exception(5, 6, 7).with_traceback(6)"""
- self.check(b, a)
- def test_tb_6(self):
- b = """def foo():
- a = 5
- raise Exception, (5, 6, 7), 6
- b = 6"""
- a = """def foo():
- a = 5
- raise Exception(5, 6, 7).with_traceback(6)
- b = 6"""
- self.check(b, a)
- class Test_throw(FixerTestCase):
- fixer = "throw"
- def test_1(self):
- b = """g.throw(Exception, 5)"""
- a = """g.throw(Exception(5))"""
- self.check(b, a)
- def test_2(self):
- b = """g.throw(Exception,5)"""
- a = """g.throw(Exception(5))"""
- self.check(b, a)
- def test_3(self):
- b = """g.throw(Exception, (5, 6, 7))"""
- a = """g.throw(Exception(5, 6, 7))"""
- self.check(b, a)
- def test_4(self):
- b = """5 + g.throw(Exception, 5)"""
- a = """5 + g.throw(Exception(5))"""
- self.check(b, a)
- # These should produce warnings
- def test_warn_1(self):
- s = """g.throw("foo")"""
- self.warns_unchanged(s, "Python 3 does not support string exceptions")
- def test_warn_2(self):
- s = """g.throw("foo", 5)"""
- self.warns_unchanged(s, "Python 3 does not support string exceptions")
- def test_warn_3(self):
- s = """g.throw("foo", 5, 6)"""
- self.warns_unchanged(s, "Python 3 does not support string exceptions")
- # These should not be touched
- def test_untouched_1(self):
- s = """g.throw(Exception)"""
- self.unchanged(s)
- def test_untouched_2(self):
- s = """g.throw(Exception(5, 6))"""
- self.unchanged(s)
- def test_untouched_3(self):
- s = """5 + g.throw(Exception(5, 6))"""
- self.unchanged(s)
- # These should result in traceback-assignment
- def test_tb_1(self):
- b = """def foo():
- g.throw(Exception, 5, 6)"""
- a = """def foo():
- g.throw(Exception(5).with_traceback(6))"""
- self.check(b, a)
- def test_tb_2(self):
- b = """def foo():
- a = 5
- g.throw(Exception, 5, 6)
- b = 6"""
- a = """def foo():
- a = 5
- g.throw(Exception(5).with_traceback(6))
- b = 6"""
- self.check(b, a)
- def test_tb_3(self):
- b = """def foo():
- g.throw(Exception,5,6)"""
- a = """def foo():
- g.throw(Exception(5).with_traceback(6))"""
- self.check(b, a)
- def test_tb_4(self):
- b = """def foo():
- a = 5
- g.throw(Exception,5,6)
- b = 6"""
- a = """def foo():
- a = 5
- g.throw(Exception(5).with_traceback(6))
- b = 6"""
- self.check(b, a)
- def test_tb_5(self):
- b = """def foo():
- g.throw(Exception, (5, 6, 7), 6)"""
- a = """def foo():
- g.throw(Exception(5, 6, 7).with_traceback(6))"""
- self.check(b, a)
- def test_tb_6(self):
- b = """def foo():
- a = 5
- g.throw(Exception, (5, 6, 7), 6)
- b = 6"""
- a = """def foo():
- a = 5
- g.throw(Exception(5, 6, 7).with_traceback(6))
- b = 6"""
- self.check(b, a)
- def test_tb_7(self):
- b = """def foo():
- a + g.throw(Exception, 5, 6)"""
- a = """def foo():
- a + g.throw(Exception(5).with_traceback(6))"""
- self.check(b, a)
- def test_tb_8(self):
- b = """def foo():
- a = 5
- a + g.throw(Exception, 5, 6)
- b = 6"""
- a = """def foo():
- a = 5
- a + g.throw(Exception(5).with_traceback(6))
- b = 6"""
- self.check(b, a)
- class Test_long(FixerTestCase):
- fixer = "long"
- def test_1(self):
- b = """x = long(x)"""
- a = """x = int(x)"""
- self.check(b, a)
- def test_2(self):
- b = """y = isinstance(x, long)"""
- a = """y = isinstance(x, int)"""
- self.check(b, a)
- def test_3(self):
- b = """z = type(x) in (int, long)"""
- a = """z = type(x) in (int, int)"""
- self.check(b, a)
- def test_unchanged(self):
- s = """long = True"""
- self.unchanged(s)
- s = """s.long = True"""
- self.unchanged(s)
- s = """def long(): pass"""
- self.unchanged(s)
- s = """class long(): pass"""
- self.unchanged(s)
- s = """def f(long): pass"""
- self.unchanged(s)
- s = """def f(g, long): pass"""
- self.unchanged(s)
- s = """def f(x, long=True): pass"""
- self.unchanged(s)
- def test_prefix_preservation(self):
- b = """x = long( x )"""
- a = """x = int( x )"""
- self.check(b, a)
- class Test_execfile(FixerTestCase):
- fixer = "execfile"
- def test_conversion(self):
- b = """execfile("fn")"""
- a = """exec(compile(open("fn").read(), "fn", 'exec'))"""
- self.check(b, a)
- b = """execfile("fn", glob)"""
- a = """exec(compile(open("fn").read(), "fn", 'exec'), glob)"""
- self.check(b, a)
- b = """execfile("fn", glob, loc)"""
- a = """exec(compile(open("fn").read(), "fn", 'exec'), glob, loc)"""
- self.check(b, a)
- b = """execfile("fn", globals=glob)"""
- a = """exec(compile(open("fn").read(), "fn", 'exec'), globals=glob)"""
- self.check(b, a)
- b = """execfile("fn", locals=loc)"""
- a = """exec(compile(open("fn").read(), "fn", 'exec'), locals=loc)"""
- self.check(b, a)
- b = """execfile("fn", globals=glob, locals=loc)"""
- a = """exec(compile(open("fn").read(), "fn", 'exec'), globals=glob, locals=loc)"""
- self.check(b, a)
- def test_spacing(self):
- b = """execfile( "fn" )"""
- a = """exec(compile(open( "fn" ).read(), "fn", 'exec'))"""
- self.check(b, a)
- b = """execfile("fn", globals = glob)"""
- a = """exec(compile(open("fn").read(), "fn", 'exec'), globals = glob)"""
- self.check(b, a)
- class Test_isinstance(FixerTestCase):
- fixer = "isinstance"
- def test_remove_multiple_items(self):
- b = """isinstance(x, (int, int, int))"""
- a = """isinstance(x, int)"""
- self.check(b, a)
- b = """isinstance(x, (int, float, int, int, float))"""
- a = """isinstance(x, (int, float))"""
- self.check(b, a)
- b = """isinstance(x, (int, float, int, int, float, str))"""
- a = """isinstance(x, (int, float, str))"""
- self.check(b, a)
- b = """isinstance(foo() + bar(), (x(), y(), x(), int, int))"""
- a = """isinstance(foo() + bar(), (x(), y(), x(), int))"""
- self.check(b, a)
- def test_prefix_preservation(self):
- b = """if isinstance( foo(), ( bar, bar, baz )) : pass"""
- a = """if isinstance( foo(), ( bar, baz )) : pass"""
- self.check(b, a)
- def test_unchanged(self):
- self.unchanged("isinstance(x, (str, int))")
- class Test_dict(FixerTestCase):
- fixer = "dict"
- def test_prefix_preservation(self):
- b = "if d. keys ( ) : pass"
- a = "if list(d. keys ( )) : pass"
- self.check(b, a)
- b = "if d. items ( ) : pass"
- a = "if list(d. items ( )) : pass"
- self.check(b, a)
- b = "if d. iterkeys ( ) : pass"
- a = "if iter(d. keys ( )) : pass"
- self.check(b, a)
- b = "[i for i in d. iterkeys( ) ]"
- a = "[i for i in d. keys( ) ]"
- self.check(b, a)
- b = "if d. viewkeys ( ) : pass"
- a = "if d. keys ( ) : pass"
- self.check(b, a)
- b = "[i for i in d. viewkeys( ) ]"
- a = "[i for i in d. keys( ) ]"
- self.check(b, a)
- def test_trailing_comment(self):
- b = "d.keys() # foo"
- a = "list(d.keys()) # foo"
- self.check(b, a)
- b = "d.items() # foo"
- a = "list(d.items()) # foo"
- self.check(b, a)
- b = "d.iterkeys() # foo"
- a = "iter(d.keys()) # foo"
- self.check(b, a)
- b = """[i for i in d.iterkeys() # foo
- ]"""
- a = """[i for i in d.keys() # foo
- ]"""
- self.check(b, a)
- b = """[i for i in d.iterkeys() # foo
- ]"""
- a = """[i for i in d.keys() # foo
- ]"""
- self.check(b, a)
- b = "d.viewitems() # foo"
- a = "d.items() # foo"
- self.check(b, a)
- def test_unchanged(self):
- for wrapper in fixer_util.consuming_calls:
- s = "s = %s(d.keys())" % wrapper
- self.unchanged(s)
- s = "s = %s(d.values())" % wrapper
- self.unchanged(s)
- s = "s = %s(d.items())" % wrapper
- self.unchanged(s)
- def test_01(self):
- b = "d.keys()"
- a = "list(d.keys())"
- self.check(b, a)
- b = "a[0].foo().keys()"
- a = "list(a[0].foo().keys())"
- self.check(b, a)
- def test_02(self):
- b = "d.items()"
- a = "list(d.items())"
- self.check(b, a)
- def test_03(self):
- b = "d.values()"
- a = "list(d.values())"
- self.check(b, a)
- def test_04(self):
- b = "d.iterkeys()"
- a = "iter(d.keys())"
- self.check(b, a)
- def test_05(self):
- b = "d.iteritems()"
- a = "iter(d.items())"
- self.check(b, a)
- def test_06(self):
- b = "d.itervalues()"
- a = "iter(d.values())"
- self.check(b, a)
- def test_07(self):
- s = "list(d.keys())"
- self.unchanged(s)
- def test_08(self):
- s = "sorted(d.keys())"
- self.unchanged(s)
- def test_09(self):
- b = "iter(d.keys())"
- a = "iter(list(d.keys()))"
- self.check(b, a)
- def test_10(self):
- b = "foo(d.keys())"
- a = "foo(list(d.keys()))"
- self.check(b, a)
- def test_11(self):
- b = "for i in d.keys(): print i"
- a = "for i in list(d.keys()): print i"
- self.check(b, a)
- def test_12(self):
- b = "for i in d.iterkeys(): print i"
- a = "for i in d.keys(): print i"
- self.check(b, a)
- def test_13(self):
- b = "[i for i in d.keys()]"
- a = "[i for i in list(d.keys())]"
- self.check(b, a)
- def test_14(self):
- b = "[i for i in d.iterkeys()]"
- a = "[i for i in d.keys()]"
- self.check(b, a)
- def test_15(self):
- b = "(i for i in d.keys())"
- a = "(i for i in list(d.keys()))"
- self.check(b, a)
- def test_16(self):
- b = "(i for i in d.iterkeys())"
- a = "(i for i in d.keys())"
- self.check(b, a)
- def test_17(self):
- b = "iter(d.iterkeys())"
- a = "iter(d.keys())"
- self.check(b, a)
- def test_18(self):
- b = "list(d.iterkeys())"
- a = "list(d.keys())"
- self.check(b, a)
- def test_19(self):
- b = "sorted(d.iterkeys())"
- a = "sorted(d.keys())"
- self.check(b, a)
- def test_20(self):
- b = "foo(d.iterkeys())"
- a = "foo(iter(d.keys()))"
- self.check(b, a)
- def test_21(self):
- b = "print h.iterkeys().next()"
- a = "print iter(h.keys()).next()"
- self.check(b, a)
- def test_22(self):
- b = "print h.keys()[0]"
- a = "print list(h.keys())[0]"
- self.check(b, a)
- def test_23(self):
- b = "print list(h.iterkeys().next())"
- a = "print list(iter(h.keys()).next())"
- self.check(b, a)
- def test_24(self):
- b = "for x in h.keys()[0]: print x"
- a = "for x in list(h.keys())[0]: print x"
- self.check(b, a)
- def test_25(self):
- b = "d.viewkeys()"
- a = "d.keys()"
- self.check(b, a)
- def test_26(self):
- b = "d.viewitems()"
- a = "d.items()"
- self.check(b, a)
- def test_27(self):
- b = "d.viewvalues()"
- a = "d.values()"
- self.check(b, a)
- def test_14(self):
- b = "[i for i in d.viewkeys()]"
- a = "[i for i in d.keys()]"
- self.check(b, a)
- def test_15(self):
- b = "(i for i in d.viewkeys())"
- a = "(i for i in d.keys())"
- self.check(b, a)
- def test_17(self):
- b = "iter(d.viewkeys())"
- a = "iter(d.keys())"
- self.check(b, a)
- def test_18(self):
- b = "list(d.viewkeys())"
- a = "list(d.keys())"
- self.check(b, a)
- def test_19(self):
- b = "sorted(d.viewkeys())"
- a = "sorted(d.keys())"
- self.check(b, a)
- class Test_xrange(FixerTestCase):
- fixer = "xrange"
- def test_prefix_preservation(self):
- b = """x = xrange( 10 )"""
- a = """x = range( 10 )"""
- self.check(b, a)
- b = """x = xrange( 1 , 10 )"""
- a = """x = range( 1 , 10 )"""
- self.check(b, a)
- b = """x = xrange( 0 , 10 , 2 )"""
- a = """x = range( 0 , 10 , 2 )"""
- self.check(b, a)
- def test_single_arg(self):
- b = """x = xrange(10)"""
- a = """x = range(10)"""
- self.check(b, a)
- def test_two_args(self):
- b = """x = xrange(1, 10)"""
- a = """x = range(1, 10)"""
- self.check(b, a)
- def test_three_args(self):
- b = """x = xrange(0, 10, 2)"""
- a = """x = range(0, 10, 2)"""
- self.check(b, a)
- def test_wrap_in_list(self):
- b = """x = range(10, 3, 9)"""
- a = """x = list(range(10, 3, 9))"""
- self.check(b, a)
- b = """x = foo(range(10, 3, 9))"""
- a = """x = foo(list(range(10, 3, 9)))"""
- self.check(b, a)
- b = """x = range(10, 3, 9) + [4]"""
- a = """x = list(range(10, 3, 9)) + [4]"""
- self.check(b, a)
- b = """x = range(10)[::-1]"""
- a = """x = list(range(10))[::-1]"""
- self.check(b, a)
- b = """x = range(10) [3]"""
- a = """x = list(range(10)) [3]"""
- self.check(b, a)
- def test_xrange_in_for(self):
- b = """for i in xrange(10):\n j=i"""
- a = """for i in range(10):\n j=i"""
- self.check(b, a)
- b = """[i for i in xrange(10)]"""
- a = """[i for i in range(10)]"""
- self.check(b, a)
- def test_range_in_for(self):
- self.unchanged("for i in range(10): pass")
- self.unchanged("[i for i in range(10)]")
- def test_in_contains_test(self):
- self.unchanged("x in range(10, 3, 9)")
- def test_in_consuming_context(self):
- for call in fixer_util.consuming_calls:
- self.unchanged("a = %s(range(10))" % call)
- class Test_xrange_with_reduce(FixerTestCase):
- def setUp(self):
- super(Test_xrange_with_reduce, self).setUp(["xrange", "reduce"])
- def test_double_transform(self):
- b = """reduce(x, xrange(5))"""
- a = """from functools import reduce
- reduce(x, range(5))"""
- self.check(b, a)
- class Test_raw_input(FixerTestCase):
- fixer = "raw_input"
- def test_prefix_preservation(self):
- b = """x = raw_input( )"""
- a = """x = input( )"""
- self.check(b, a)
- b = """x = raw_input( '' )"""
- a = """x = input( '' )"""
- self.check(b, a)
- def test_1(self):
- b = """x = raw_input()"""
- a = """x = input()"""
- self.check(b, a)
- def test_2(self):
- b = """x = raw_input('')"""
- a = """x = input('')"""
- self.check(b, a)
- def test_3(self):
- b = """x = raw_input('prompt')"""
- a = """x = input('prompt')"""
- self.check(b, a)
- def test_4(self):
- b = """x = raw_input(foo(a) + 6)"""
- a = """x = input(foo(a) + 6)"""
- self.check(b, a)
- def test_5(self):
- b = """x = raw_input(invite).split()"""
- a = """x = input(invite).split()"""
- self.check(b, a)
- def test_6(self):
- b = """x = raw_input(invite) . split ()"""
- a = """x = input(invite) . split ()"""
- self.check(b, a)
- def test_8(self):
- b = "x = int(raw_input())"
- a = "x = int(input())"
- self.check(b, a)
- class Test_funcattrs(FixerTestCase):
- fixer = "funcattrs"
- attrs = ["closure", "doc", "name", "defaults", "code", "globals", "dict"]
- def test(self):
- for attr in self.attrs:
- b = "a.func_%s" % attr
- a = "a.__%s__" % attr
- self.check(b, a)
- b = "self.foo.func_%s.foo_bar" % attr
- a = "self.foo.__%s__.foo_bar" % attr
- self.check(b, a)
- def test_unchanged(self):
- for attr in self.attrs:
- s = "foo(func_%s + 5)" % attr
- self.unchanged(s)
- s = "f(foo.__%s__)" % attr
- self.unchanged(s)
- s = "f(foo.__%s__.foo)" % attr
- self.unchanged(s)
- class Test_xreadlines(FixerTestCase):
- fixer = "xreadlines"
- def test_call(self):
- b = "for x in f.xreadlines(): pass"
- a = "for x in f: pass"
- self.check(b, a)
- b = "for x in foo().xreadlines(): pass"
- a = "for x in foo(): pass"
- self.check(b, a)
- b = "for x in (5 + foo()).xreadlines(): pass"
- a = "for x in (5 + foo()): pass"
- self.check(b, a)
- def test_attr_ref(self):
- b = "foo(f.xreadlines + 5)"
- a = "foo(f.__iter__ + 5)"
- self.check(b, a)
- b = "foo(f().xreadlines + 5)"
- a = "foo(f().__iter__ + 5)"
- self.check(b, a)
- b = "foo((5 + f()).xreadlines + 5)"
- a = "foo((5 + f()).__iter__ + 5)"
- self.check(b, a)
- def test_unchanged(self):
- s = "for x in f.xreadlines(5): pass"
- self.unchanged(s)
- s = "for x in f.xreadlines(k=5): pass"
- self.unchanged(s)
- s = "for x in f.xreadlines(*k, **v): pass"
- self.unchanged(s)
- s = "foo(xreadlines)"
- self.unchanged(s)
- class ImportsFixerTests:
- def test_import_module(self):
- for old, new in self.modules.items():
- b = "import %s" % old
- a = "import %s" % new
- self.check(b, a)
- b = "import foo, %s, bar" % old
- a = "import foo, %s, bar" % new
- self.check(b, a)
- def test_import_from(self):
- for old, new in self.modules.items():
- b = "from %s import foo" % old
- a = "from %s import foo" % new
- self.check(b, a)
- b = "from %s import foo, bar" % old
- a = "from %s import foo, bar" % new
- self.check(b, a)
- b = "from %s import (yes, no)" % old
- a = "from %s import (yes, no)" % new
- self.check(b, a)
- def test_import_module_as(self):
- for old, new in self.modules.items():
- b = "import %s as foo_bar" % old
- a = "import %s as foo_bar" % new
- self.check(b, a)
- b = "import %s as foo_bar" % old
- a = "import %s as foo_bar" % new
- self.check(b, a)
- def test_import_from_as(self):
- for old, new in self.modules.items():
- b = "from %s import foo as bar" % old
- a = "from %s import foo as bar" % new
- self.check(b, a)
- def test_star(self):
- for old, new in self.modules.items():
- b = "from %s import *" % old
- a = "from %s import *" % new
- self.check(b, a)
- def test_import_module_usage(self):
- for old, new in self.modules.items():
- b = """
- import %s
- foo(%s.bar)
- """ % (old, old)
- a = """
- import %s
- foo(%s.bar)
- """ % (new, new)
- self.check(b, a)
- b = """
- from %s import x
- %s = 23
- """ % (old, old)
- a = """
- from %s import x
- %s = 23
- """ % (new, old)
- self.check(b, a)
- s = """
- def f():
- %s.method()
- """ % (old,)
- self.unchanged(s)
- # test nested usage
- b = """
- import %s
- %s.bar(%s.foo)
- """ % (old, old, old)
- a = """
- import %s
- %s.bar(%s.foo)
- """ % (new, new, new)
- self.check(b, a)
- b = """
- import %s
- x.%s
- """ % (old, old)
- a = """
- import %s
- x.%s
- """ % (new, old)
- self.check(b, a)
- class Test_imports(FixerTestCase, ImportsFixerTests):
- fixer = "imports"
- from ..fixes.fix_imports import MAPPING as modules
- def test_multiple_imports(self):
- b = """import urlparse, cStringIO"""
- a = """import urllib.parse, io"""
- self.check(b, a)
- def test_multiple_imports_as(self):
- b = """
- import copy_reg as bar, HTMLParser as foo, urlparse
- s = urlparse.spam(bar.foo())
- """
- a = """
- import copyreg as bar, html.parser as foo, urllib.parse
- s = urllib.parse.spam(bar.foo())
- """
- self.check(b, a)
- class Test_imports2(FixerTestCase, ImportsFixerTests):
- fixer = "imports2"
- from ..fixes.fix_imports2 import MAPPING as modules
- class Test_imports_fixer_order(FixerTestCase, ImportsFixerTests):
- def setUp(self):
- super(Test_imports_fixer_order, self).setUp(['imports', 'imports2'])
- from ..fixes.fix_imports2 import MAPPING as mapping2
- self.modules = mapping2.copy()
- from ..fixes.fix_imports import MAPPING as mapping1
- for key in ('dbhash', 'dumbdbm', 'dbm', 'gdbm'):
- self.modules[key] = mapping1[key]
- def test_after_local_imports_refactoring(self):
- for fix in ("imports", "imports2"):
- self.fixer = fix
- self.assert_runs_after("import")
- class Test_urllib(FixerTestCase):
- fixer = "urllib"
- from ..fixes.fix_urllib import MAPPING as modules
- def test_import_module(self):
- for old, changes in self.modules.items():
- b = "import %s" % old
- a = "import %s" % ", ".join(map(itemgetter(0), changes))
- self.check(b, a)
- def test_import_from(self):
- for old, changes in self.modules.items():
- all_members = []
- for new, members in changes:
- for member in members:
- all_members.append(member)
- b = "from %s import %s" % (old, member)
- a = "from %s import %s" % (new, member)
- self.check(b, a)
- s = "from foo import %s" % member
- self.unchanged(s)
- b = "from %s import %s" % (old, ", ".join(members))
- a = "from %s import %s" % (new, ", ".join(members))
- self.check(b, a)
- s = "from foo import %s" % ", ".join(members)
- self.unchanged(s)
- # test the breaking of a module into multiple replacements
- b = "from %s import %s" % (old, ", ".join(all_members))
- a = "\n".join(["from %s import %s" % (new, ", ".join(members))
- for (new, members) in changes])
- self.check(b, a)
- def test_import_module_as(self):
- for old in self.modules:
- s = "import %s as foo" % old
- self.warns_unchanged(s, "This module is now multiple modules")
- def test_import_from_as(self):
- for old, changes in self.modules.items():
- for new, members in changes:
- for member in members:
- b = "from %s import %s as foo_bar" % (old, member)
- a = "from %s import %s as foo_bar" % (new, member)
- self.check(b, a)
- b = "from %s import %s as blah, %s" % (old, member, member)
- a = "from %s import %s as blah, %s" % (new, member, member)
- self.check(b, a)
- def test_star(self):
- for old in self.modules:
- s = "from %s import *" % old
- self.warns_unchanged(s, "Cannot handle star imports")
- def test_indented(self):
- b = """
- def foo():
- from urllib import urlencode, urlopen
- """
- a = """
- def foo():
- from urllib.parse import urlencode
- from urllib.request import urlopen
- """
- self.check(b, a)
- b = """
- def foo():
- other()
- from urllib import urlencode, urlopen
- """
- a = """
- def foo():
- other()
- from urllib.parse import urlencode
- from urllib.request import urlopen
- """
- self.check(b, a)
- def test_import_module_usage(self):
- for old, changes in self.modules.items():
- for new, members in changes:
- for member in members:
- new_import = ", ".join([n for (n, mems)
- in self.modules[old]])
- b = """
- import %s
- foo(%s.%s)
- """ % (old, old, member)
- a = """
- …
Large files files are truncated, but you can click here to view the full file