summaryrefslogtreecommitdiffstats
path: root/Lib/lib2to3/tests
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/lib2to3/tests')
-rw-r--r--Lib/lib2to3/tests/__init__.py24
-rw-r--r--Lib/lib2to3/tests/benchmark.py58
-rw-r--r--Lib/lib2to3/tests/data/py2_test_grammar.py922
-rw-r--r--Lib/lib2to3/tests/data/py3_test_grammar.py863
-rwxr-xr-xLib/lib2to3/tests/pytree_idempotency.py92
-rw-r--r--Lib/lib2to3/tests/support.py47
-rw-r--r--Lib/lib2to3/tests/test_all_fixers.py42
-rwxr-xr-xLib/lib2to3/tests/test_fixers.py2918
-rw-r--r--Lib/lib2to3/tests/test_parser.py202
-rwxr-xr-xLib/lib2to3/tests/test_pytree.py440
-rw-r--r--Lib/lib2to3/tests/test_util.py536
11 files changed, 6144 insertions, 0 deletions
diff --git a/Lib/lib2to3/tests/__init__.py b/Lib/lib2to3/tests/__init__.py
new file mode 100644
index 0000000..cfaea0d
--- /dev/null
+++ b/Lib/lib2to3/tests/__init__.py
@@ -0,0 +1,24 @@
+"""Make tests/ into a package. This allows us to "import tests" and
+have tests.all_tests be a TestSuite representing all test cases
+from all test_*.py files in tests/."""
+# Author: Collin Winter
+
+import os
+import os.path
+import unittest
+import types
+
+from . import support
+
+all_tests = unittest.TestSuite()
+
+tests_dir = os.path.join(os.path.dirname(__file__), '..', 'tests')
+tests = [t[0:-3] for t in os.listdir(tests_dir)
+ if t.startswith('test_') and t.endswith('.py')]
+
+loader = unittest.TestLoader()
+
+for t in tests:
+ __import__("",globals(),locals(),[t],level=1)
+ mod = globals()[t]
+ all_tests.addTests(loader.loadTestsFromModule(mod))
diff --git a/Lib/lib2to3/tests/benchmark.py b/Lib/lib2to3/tests/benchmark.py
new file mode 100644
index 0000000..7ccdd17
--- /dev/null
+++ b/Lib/lib2to3/tests/benchmark.py
@@ -0,0 +1,58 @@
+#!/usr/bin/env python2.5
+"""
+This is a benchmarking script to test the speed of 2to3's pattern matching
+system. It's equivalent to "refactor.py -f all" for every Python module
+in sys.modules, but without engaging the actual transformations.
+"""
+
+__author__ = "Collin Winter <collinw at gmail.com>"
+
+# Python imports
+import os.path
+import sys
+from time import time
+
+# Test imports
+from support import adjust_path
+adjust_path()
+
+# Local imports
+from .. import refactor
+
+### Mock code for refactor.py and the fixers
+###############################################################################
+class Options:
+ def __init__(self, **kwargs):
+ for k, v in kwargs.items():
+ setattr(self, k, v)
+
+ self.verbose = False
+
+def dummy_transform(*args, **kwargs):
+ pass
+
+### Collect list of modules to match against
+###############################################################################
+files = []
+for mod in sys.modules.values():
+ if mod is None or not hasattr(mod, '__file__'):
+ continue
+ f = mod.__file__
+ if f.endswith('.pyc'):
+ f = f[:-1]
+ if f.endswith('.py'):
+ files.append(f)
+
+### Set up refactor and run the benchmark
+###############################################################################
+options = Options(fix=["all"], print_function=False, doctests_only=False)
+refactor = refactor.RefactoringTool(options)
+for fixer in refactor.fixers:
+ # We don't want them to actually fix the tree, just match against it.
+ fixer.transform = dummy_transform
+
+t = time()
+for f in files:
+ print "Matching", f
+ refactor.refactor_file(f)
+print "%d seconds to match %d files" % (time() - t, len(sys.modules))
diff --git a/Lib/lib2to3/tests/data/py2_test_grammar.py b/Lib/lib2to3/tests/data/py2_test_grammar.py
new file mode 100644
index 0000000..e5d8a0f
--- /dev/null
+++ b/Lib/lib2to3/tests/data/py2_test_grammar.py
@@ -0,0 +1,922 @@
+# Python 2's Lib/test/test_grammar.py (r54061)
+
+# Python test set -- part 1, grammar.
+# This just tests whether the parser accepts them all.
+
+# NOTE: When you run this test as a script from the command line, you
+# get warnings about certain hex/oct constants. Since those are
+# issued by the parser, you can't suppress them by adding a
+# filterwarnings() call to this module. Therefore, to shut up the
+# regression test, the filterwarnings() call has been added to
+# regrtest.py.
+
+from test.test_support import run_unittest, check_syntax_error
+import unittest
+import sys
+# testing import *
+from sys import *
+
+class TokenTests(unittest.TestCase):
+
+ def testBackslash(self):
+ # Backslash means line continuation:
+ x = 1 \
+ + 1
+ self.assertEquals(x, 2, 'backslash for line continuation')
+
+ # Backslash does not means continuation in comments :\
+ x = 0
+ self.assertEquals(x, 0, 'backslash ending comment')
+
+ def testPlainIntegers(self):
+ self.assertEquals(0xff, 255)
+ self.assertEquals(0377, 255)
+ self.assertEquals(2147483647, 017777777777)
+ from sys import maxint
+ if maxint == 2147483647:
+ self.assertEquals(-2147483647-1, -020000000000)
+ # XXX -2147483648
+ self.assert_(037777777777 > 0)
+ self.assert_(0xffffffff > 0)
+ for s in '2147483648', '040000000000', '0x100000000':
+ try:
+ x = eval(s)
+ except OverflowError:
+ self.fail("OverflowError on huge integer literal %r" % s)
+ elif maxint == 9223372036854775807:
+ self.assertEquals(-9223372036854775807-1, -01000000000000000000000)
+ self.assert_(01777777777777777777777 > 0)
+ self.assert_(0xffffffffffffffff > 0)
+ for s in '9223372036854775808', '02000000000000000000000', \
+ '0x10000000000000000':
+ try:
+ x = eval(s)
+ except OverflowError:
+ self.fail("OverflowError on huge integer literal %r" % s)
+ else:
+ self.fail('Weird maxint value %r' % maxint)
+
+ def testLongIntegers(self):
+ x = 0L
+ x = 0l
+ x = 0xffffffffffffffffL
+ x = 0xffffffffffffffffl
+ x = 077777777777777777L
+ x = 077777777777777777l
+ x = 123456789012345678901234567890L
+ x = 123456789012345678901234567890l
+
+ def testFloats(self):
+ x = 3.14
+ x = 314.
+ x = 0.314
+ # XXX x = 000.314
+ x = .314
+ x = 3e14
+ x = 3E14
+ x = 3e-14
+ x = 3e+14
+ x = 3.e14
+ x = .3e14
+ x = 3.1e4
+
+ def testStringLiterals(self):
+ x = ''; y = ""; self.assert_(len(x) == 0 and x == y)
+ x = '\''; y = "'"; self.assert_(len(x) == 1 and x == y and ord(x) == 39)
+ x = '"'; y = "\""; self.assert_(len(x) == 1 and x == y and ord(x) == 34)
+ x = "doesn't \"shrink\" does it"
+ y = 'doesn\'t "shrink" does it'
+ self.assert_(len(x) == 24 and x == y)
+ x = "does \"shrink\" doesn't it"
+ y = 'does "shrink" doesn\'t it'
+ self.assert_(len(x) == 24 and x == y)
+ x = """
+The "quick"
+brown fox
+jumps over
+the 'lazy' dog.
+"""
+ y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
+ self.assertEquals(x, y)
+ y = '''
+The "quick"
+brown fox
+jumps over
+the 'lazy' dog.
+'''
+ self.assertEquals(x, y)
+ y = "\n\
+The \"quick\"\n\
+brown fox\n\
+jumps over\n\
+the 'lazy' dog.\n\
+"
+ self.assertEquals(x, y)
+ y = '\n\
+The \"quick\"\n\
+brown fox\n\
+jumps over\n\
+the \'lazy\' dog.\n\
+'
+ self.assertEquals(x, y)
+
+
+class GrammarTests(unittest.TestCase):
+
+ # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
+ # XXX can't test in a script -- this rule is only used when interactive
+
+ # file_input: (NEWLINE | stmt)* ENDMARKER
+ # Being tested as this very moment this very module
+
+ # expr_input: testlist NEWLINE
+ # XXX Hard to test -- used only in calls to input()
+
+ def testEvalInput(self):
+ # testlist ENDMARKER
+ x = eval('1, 0 or 1')
+
+ def testFuncdef(self):
+ ### 'def' NAME parameters ':' suite
+ ### parameters: '(' [varargslist] ')'
+ ### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]
+ ### | ('**'|'*' '*') NAME)
+ ### | fpdef ['=' test] (',' fpdef ['=' test])* [',']
+ ### fpdef: NAME | '(' fplist ')'
+ ### fplist: fpdef (',' fpdef)* [',']
+ ### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test)
+ ### argument: [test '='] test # Really [keyword '='] test
+ def f1(): pass
+ f1()
+ f1(*())
+ f1(*(), **{})
+ def f2(one_argument): pass
+ def f3(two, arguments): pass
+ def f4(two, (compound, (argument, list))): pass
+ def f5((compound, first), two): pass
+ self.assertEquals(f2.func_code.co_varnames, ('one_argument',))
+ self.assertEquals(f3.func_code.co_varnames, ('two', 'arguments'))
+ if sys.platform.startswith('java'):
+ self.assertEquals(f4.func_code.co_varnames,
+ ('two', '(compound, (argument, list))', 'compound', 'argument',
+ 'list',))
+ self.assertEquals(f5.func_code.co_varnames,
+ ('(compound, first)', 'two', 'compound', 'first'))
+ else:
+ self.assertEquals(f4.func_code.co_varnames,
+ ('two', '.1', 'compound', 'argument', 'list'))
+ self.assertEquals(f5.func_code.co_varnames,
+ ('.0', 'two', 'compound', 'first'))
+ def a1(one_arg,): pass
+ def a2(two, args,): pass
+ def v0(*rest): pass
+ def v1(a, *rest): pass
+ def v2(a, b, *rest): pass
+ def v3(a, (b, c), *rest): return a, b, c, rest
+
+ f1()
+ f2(1)
+ f2(1,)
+ f3(1, 2)
+ f3(1, 2,)
+ f4(1, (2, (3, 4)))
+ v0()
+ v0(1)
+ v0(1,)
+ v0(1,2)
+ v0(1,2,3,4,5,6,7,8,9,0)
+ v1(1)
+ v1(1,)
+ v1(1,2)
+ v1(1,2,3)
+ v1(1,2,3,4,5,6,7,8,9,0)
+ v2(1,2)
+ v2(1,2,3)
+ v2(1,2,3,4)
+ v2(1,2,3,4,5,6,7,8,9,0)
+ v3(1,(2,3))
+ v3(1,(2,3),4)
+ v3(1,(2,3),4,5,6,7,8,9,0)
+
+ # ceval unpacks the formal arguments into the first argcount names;
+ # thus, the names nested inside tuples must appear after these names.
+ if sys.platform.startswith('java'):
+ self.assertEquals(v3.func_code.co_varnames, ('a', '(b, c)', 'rest', 'b', 'c'))
+ else:
+ self.assertEquals(v3.func_code.co_varnames, ('a', '.1', 'rest', 'b', 'c'))
+ self.assertEquals(v3(1, (2, 3), 4), (1, 2, 3, (4,)))
+ def d01(a=1): pass
+ d01()
+ d01(1)
+ d01(*(1,))
+ d01(**{'a':2})
+ def d11(a, b=1): pass
+ d11(1)
+ d11(1, 2)
+ d11(1, **{'b':2})
+ def d21(a, b, c=1): pass
+ d21(1, 2)
+ d21(1, 2, 3)
+ d21(*(1, 2, 3))
+ d21(1, *(2, 3))
+ d21(1, 2, *(3,))
+ d21(1, 2, **{'c':3})
+ def d02(a=1, b=2): pass
+ d02()
+ d02(1)
+ d02(1, 2)
+ d02(*(1, 2))
+ d02(1, *(2,))
+ d02(1, **{'b':2})
+ d02(**{'a': 1, 'b': 2})
+ def d12(a, b=1, c=2): pass
+ d12(1)
+ d12(1, 2)
+ d12(1, 2, 3)
+ def d22(a, b, c=1, d=2): pass
+ d22(1, 2)
+ d22(1, 2, 3)
+ d22(1, 2, 3, 4)
+ def d01v(a=1, *rest): pass
+ d01v()
+ d01v(1)
+ d01v(1, 2)
+ d01v(*(1, 2, 3, 4))
+ d01v(*(1,))
+ d01v(**{'a':2})
+ def d11v(a, b=1, *rest): pass
+ d11v(1)
+ d11v(1, 2)
+ d11v(1, 2, 3)
+ def d21v(a, b, c=1, *rest): pass
+ d21v(1, 2)
+ d21v(1, 2, 3)
+ d21v(1, 2, 3, 4)
+ d21v(*(1, 2, 3, 4))
+ d21v(1, 2, **{'c': 3})
+ def d02v(a=1, b=2, *rest): pass
+ d02v()
+ d02v(1)
+ d02v(1, 2)
+ d02v(1, 2, 3)
+ d02v(1, *(2, 3, 4))
+ d02v(**{'a': 1, 'b': 2})
+ def d12v(a, b=1, c=2, *rest): pass
+ d12v(1)
+ d12v(1, 2)
+ d12v(1, 2, 3)
+ d12v(1, 2, 3, 4)
+ d12v(*(1, 2, 3, 4))
+ d12v(1, 2, *(3, 4, 5))
+ d12v(1, *(2,), **{'c': 3})
+ def d22v(a, b, c=1, d=2, *rest): pass
+ d22v(1, 2)
+ d22v(1, 2, 3)
+ d22v(1, 2, 3, 4)
+ d22v(1, 2, 3, 4, 5)
+ d22v(*(1, 2, 3, 4))
+ d22v(1, 2, *(3, 4, 5))
+ d22v(1, *(2, 3), **{'d': 4})
+ def d31v((x)): pass
+ d31v(1)
+ def d32v((x,)): pass
+ d32v((1,))
+
+ def testLambdef(self):
+ ### lambdef: 'lambda' [varargslist] ':' test
+ l1 = lambda : 0
+ self.assertEquals(l1(), 0)
+ l2 = lambda : a[d] # XXX just testing the expression
+ l3 = lambda : [2 < x for x in [-1, 3, 0L]]
+ self.assertEquals(l3(), [0, 1, 0])
+ l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
+ self.assertEquals(l4(), 1)
+ l5 = lambda x, y, z=2: x + y + z
+ self.assertEquals(l5(1, 2), 5)
+ self.assertEquals(l5(1, 2, 3), 6)
+ check_syntax_error(self, "lambda x: x = 2")
+
+ ### stmt: simple_stmt | compound_stmt
+ # Tested below
+
+ def testSimpleStmt(self):
+ ### simple_stmt: small_stmt (';' small_stmt)* [';']
+ x = 1; pass; del x
+ def foo():
+ # verify statments that end with semi-colons
+ x = 1; pass; del x;
+ foo()
+
+ ### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt
+ # Tested below
+
+ def testExprStmt(self):
+ # (exprlist '=')* exprlist
+ 1
+ 1, 2, 3
+ x = 1
+ x = 1, 2, 3
+ x = y = z = 1, 2, 3
+ x, y, z = 1, 2, 3
+ abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
+
+ check_syntax_error(self, "x + 1 = 1")
+ check_syntax_error(self, "a + 1 = b + 2")
+
+ def testPrintStmt(self):
+ # 'print' (test ',')* [test]
+ import StringIO
+
+ # Can't test printing to real stdout without comparing output
+ # which is not available in unittest.
+ save_stdout = sys.stdout
+ sys.stdout = StringIO.StringIO()
+
+ print 1, 2, 3
+ print 1, 2, 3,
+ print
+ print 0 or 1, 0 or 1,
+ print 0 or 1
+
+ # 'print' '>>' test ','
+ print >> sys.stdout, 1, 2, 3
+ print >> sys.stdout, 1, 2, 3,
+ print >> sys.stdout
+ print >> sys.stdout, 0 or 1, 0 or 1,
+ print >> sys.stdout, 0 or 1
+
+ # test printing to an instance
+ class Gulp:
+ def write(self, msg): pass
+
+ gulp = Gulp()
+ print >> gulp, 1, 2, 3
+ print >> gulp, 1, 2, 3,
+ print >> gulp
+ print >> gulp, 0 or 1, 0 or 1,
+ print >> gulp, 0 or 1
+
+ # test print >> None
+ def driver():
+ oldstdout = sys.stdout
+ sys.stdout = Gulp()
+ try:
+ tellme(Gulp())
+ tellme()
+ finally:
+ sys.stdout = oldstdout
+
+ # we should see this once
+ def tellme(file=sys.stdout):
+ print >> file, 'hello world'
+
+ driver()
+
+ # we should not see this at all
+ def tellme(file=None):
+ print >> file, 'goodbye universe'
+
+ driver()
+
+ self.assertEqual(sys.stdout.getvalue(), '''\
+1 2 3
+1 2 3
+1 1 1
+1 2 3
+1 2 3
+1 1 1
+hello world
+''')
+ sys.stdout = save_stdout
+
+ # syntax errors
+ check_syntax_error(self, 'print ,')
+ check_syntax_error(self, 'print >> x,')
+
+ def testDelStmt(self):
+ # 'del' exprlist
+ abc = [1,2,3]
+ x, y, z = abc
+ xyz = x, y, z
+
+ del abc
+ del x, y, (z, xyz)
+
+ def testPassStmt(self):
+ # 'pass'
+ pass
+
+ # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
+ # Tested below
+
+ def testBreakStmt(self):
+ # 'break'
+ while 1: break
+
+ def testContinueStmt(self):
+ # 'continue'
+ i = 1
+ while i: i = 0; continue
+
+ msg = ""
+ while not msg:
+ msg = "ok"
+ try:
+ continue
+ msg = "continue failed to continue inside try"
+ except:
+ msg = "continue inside try called except block"
+ if msg != "ok":
+ self.fail(msg)
+
+ msg = ""
+ while not msg:
+ msg = "finally block not called"
+ try:
+ continue
+ finally:
+ msg = "ok"
+ if msg != "ok":
+ self.fail(msg)
+
+ def test_break_continue_loop(self):
+ # This test warrants an explanation. It is a test specifically for SF bugs
+ # #463359 and #462937. The bug is that a 'break' statement executed or
+ # exception raised inside a try/except inside a loop, *after* a continue
+ # statement has been executed in that loop, will cause the wrong number of
+ # arguments to be popped off the stack and the instruction pointer reset to
+ # a very small number (usually 0.) Because of this, the following test
+ # *must* written as a function, and the tracking vars *must* be function
+ # arguments with default values. Otherwise, the test will loop and loop.
+
+ def test_inner(extra_burning_oil = 1, count=0):
+ big_hippo = 2
+ while big_hippo:
+ count += 1
+ try:
+ if extra_burning_oil and big_hippo == 1:
+ extra_burning_oil -= 1
+ break
+ big_hippo -= 1
+ continue
+ except:
+ raise
+ if count > 2 or big_hippo <> 1:
+ self.fail("continue then break in try/except in loop broken!")
+ test_inner()
+
+ def testReturn(self):
+ # 'return' [testlist]
+ def g1(): return
+ def g2(): return 1
+ g1()
+ x = g2()
+ check_syntax_error(self, "class foo:return 1")
+
+ def testYield(self):
+ check_syntax_error(self, "class foo:yield 1")
+
+ def testRaise(self):
+ # 'raise' test [',' test]
+ try: raise RuntimeError, 'just testing'
+ except RuntimeError: pass
+ try: raise KeyboardInterrupt
+ except KeyboardInterrupt: pass
+
+ def testImport(self):
+ # 'import' dotted_as_names
+ import sys
+ import time, sys
+ # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names)
+ from time import time
+ from time import (time)
+ # not testable inside a function, but already done at top of the module
+ # from sys import *
+ from sys import path, argv
+ from sys import (path, argv)
+ from sys import (path, argv,)
+
+ def testGlobal(self):
+ # 'global' NAME (',' NAME)*
+ global a
+ global a, b
+ global one, two, three, four, five, six, seven, eight, nine, ten
+
+ def testExec(self):
+ # 'exec' expr ['in' expr [',' expr]]
+ z = None
+ del z
+ exec 'z=1+1\n'
+ if z != 2: self.fail('exec \'z=1+1\'\\n')
+ del z
+ exec 'z=1+1'
+ if z != 2: self.fail('exec \'z=1+1\'')
+ z = None
+ del z
+ import types
+ if hasattr(types, "UnicodeType"):
+ exec r"""if 1:
+ exec u'z=1+1\n'
+ if z != 2: self.fail('exec u\'z=1+1\'\\n')
+ del z
+ exec u'z=1+1'
+ if z != 2: self.fail('exec u\'z=1+1\'')"""
+ g = {}
+ exec 'z = 1' in g
+ if g.has_key('__builtins__'): del g['__builtins__']
+ if g != {'z': 1}: self.fail('exec \'z = 1\' in g')
+ g = {}
+ l = {}
+
+ import warnings
+ warnings.filterwarnings("ignore", "global statement", module="<string>")
+ exec 'global a; a = 1; b = 2' in g, l
+ if g.has_key('__builtins__'): del g['__builtins__']
+ if l.has_key('__builtins__'): del l['__builtins__']
+ if (g, l) != ({'a':1}, {'b':2}):
+ self.fail('exec ... in g (%s), l (%s)' %(g,l))
+
+ def testAssert(self):
+ # assert_stmt: 'assert' test [',' test]
+ assert 1
+ assert 1, 1
+ assert lambda x:x
+ assert 1, lambda x:x+1
+ try:
+ assert 0, "msg"
+ except AssertionError, e:
+ self.assertEquals(e.args[0], "msg")
+ else:
+ if __debug__:
+ self.fail("AssertionError not raised by assert 0")
+
+ ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
+ # Tested below
+
+ def testIf(self):
+ # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
+ if 1: pass
+ if 1: pass
+ else: pass
+ if 0: pass
+ elif 0: pass
+ if 0: pass
+ elif 0: pass
+ elif 0: pass
+ elif 0: pass
+ else: pass
+
+ def testWhile(self):
+ # 'while' test ':' suite ['else' ':' suite]
+ while 0: pass
+ while 0: pass
+ else: pass
+
+ def testFor(self):
+ # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
+ for i in 1, 2, 3: pass
+ for i, j, k in (): pass
+ else: pass
+ class Squares:
+ def __init__(self, max):
+ self.max = max
+ self.sofar = []
+ def __len__(self): return len(self.sofar)
+ def __getitem__(self, i):
+ if not 0 <= i < self.max: raise IndexError
+ n = len(self.sofar)
+ while n <= i:
+ self.sofar.append(n*n)
+ n = n+1
+ return self.sofar[i]
+ n = 0
+ for x in Squares(10): n = n+x
+ if n != 285:
+ self.fail('for over growing sequence')
+
+ result = []
+ for x, in [(1,), (2,), (3,)]:
+ result.append(x)
+ self.assertEqual(result, [1, 2, 3])
+
+ def testTry(self):
+ ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
+ ### | 'try' ':' suite 'finally' ':' suite
+ ### except_clause: 'except' [expr [',' expr]]
+ try:
+ 1/0
+ except ZeroDivisionError:
+ pass
+ else:
+ pass
+ try: 1/0
+ except EOFError: pass
+ except TypeError, msg: pass
+ except RuntimeError, msg: pass
+ except: pass
+ else: pass
+ try: 1/0
+ except (EOFError, TypeError, ZeroDivisionError): pass
+ try: 1/0
+ except (EOFError, TypeError, ZeroDivisionError), msg: pass
+ try: pass
+ finally: pass
+
+ def testSuite(self):
+ # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
+ if 1: pass
+ if 1:
+ pass
+ if 1:
+ #
+ #
+ #
+ pass
+ pass
+ #
+ pass
+ #
+
+ def testTest(self):
+ ### and_test ('or' and_test)*
+ ### and_test: not_test ('and' not_test)*
+ ### not_test: 'not' not_test | comparison
+ if not 1: pass
+ if 1 and 1: pass
+ if 1 or 1: pass
+ if not not not 1: pass
+ if not 1 and 1 and 1: pass
+ if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
+
+ def testComparison(self):
+ ### comparison: expr (comp_op expr)*
+ ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
+ if 1: pass
+ x = (1 == 1)
+ if 1 == 1: pass
+ if 1 != 1: pass
+ if 1 <> 1: pass
+ if 1 < 1: pass
+ if 1 > 1: pass
+ if 1 <= 1: pass
+ if 1 >= 1: pass
+ if 1 is 1: pass
+ if 1 is not 1: pass
+ if 1 in (): pass
+ if 1 not in (): pass
+ if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass
+
+ def testBinaryMaskOps(self):
+ x = 1 & 1
+ x = 1 ^ 1
+ x = 1 | 1
+
+ def testShiftOps(self):
+ x = 1 << 1
+ x = 1 >> 1
+ x = 1 << 1 >> 1
+
+ def testAdditiveOps(self):
+ x = 1
+ x = 1 + 1
+ x = 1 - 1 - 1
+ x = 1 - 1 + 1 - 1 + 1
+
+ def testMultiplicativeOps(self):
+ x = 1 * 1
+ x = 1 / 1
+ x = 1 % 1
+ x = 1 / 1 * 1 % 1
+
+ def testUnaryOps(self):
+ x = +1
+ x = -1
+ x = ~1
+ x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
+ x = -1*1/1 + 1*1 - ---1*1
+
+ def testSelectors(self):
+ ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
+ ### subscript: expr | [expr] ':' [expr]
+
+ import sys, time
+ c = sys.path[0]
+ x = time.time()
+ x = sys.modules['time'].time()
+ a = '01234'
+ c = a[0]
+ c = a[-1]
+ s = a[0:5]
+ s = a[:5]
+ s = a[0:]
+ s = a[:]
+ s = a[-5:]
+ s = a[:-1]
+ s = a[-4:-3]
+ # A rough test of SF bug 1333982. http://python.org/sf/1333982
+ # The testing here is fairly incomplete.
+ # Test cases should include: commas with 1 and 2 colons
+ d = {}
+ d[1] = 1
+ d[1,] = 2
+ d[1,2] = 3
+ d[1,2,3] = 4
+ L = list(d)
+ L.sort()
+ self.assertEquals(str(L), '[1, (1,), (1, 2), (1, 2, 3)]')
+
+ def testAtoms(self):
+ ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
+ ### dictmaker: test ':' test (',' test ':' test)* [',']
+
+ x = (1)
+ x = (1 or 2 or 3)
+ x = (1 or 2 or 3, 2, 3)
+
+ x = []
+ x = [1]
+ x = [1 or 2 or 3]
+ x = [1 or 2 or 3, 2, 3]
+ x = []
+
+ x = {}
+ x = {'one': 1}
+ x = {'one': 1,}
+ x = {'one' or 'two': 1 or 2}
+ x = {'one': 1, 'two': 2}
+ x = {'one': 1, 'two': 2,}
+ x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
+
+ x = `x`
+ x = `1 or 2 or 3`
+ self.assertEqual(`1,2`, '(1, 2)')
+
+ x = x
+ x = 'x'
+ x = 123
+
+ ### exprlist: expr (',' expr)* [',']
+ ### testlist: test (',' test)* [',']
+ # These have been exercised enough above
+
+ def testClassdef(self):
+ # 'class' NAME ['(' [testlist] ')'] ':' suite
+ class B: pass
+ class B2(): pass
+ class C1(B): pass
+ class C2(B): pass
+ class D(C1, C2, B): pass
+ class C:
+ def meth1(self): pass
+ def meth2(self, arg): pass
+ def meth3(self, a1, a2): pass
+
+ def testListcomps(self):
+ # list comprehension tests
+ nums = [1, 2, 3, 4, 5]
+ strs = ["Apple", "Banana", "Coconut"]
+ spcs = [" Apple", " Banana ", "Coco nut "]
+
+ self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut'])
+ self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15])
+ self.assertEqual([x for x in nums if x > 2], [3, 4, 5])
+ self.assertEqual([(i, s) for i in nums for s in strs],
+ [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'),
+ (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'),
+ (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'),
+ (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'),
+ (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')])
+ self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]],
+ [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'),
+ (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'),
+ (5, 'Banana'), (5, 'Coconut')])
+ self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)],
+ [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]])
+
+ def test_in_func(l):
+ return [None < x < 3 for x in l if x > 2]
+
+ self.assertEqual(test_in_func(nums), [False, False, False])
+
+ def test_nested_front():
+ self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]],
+ [[1, 2], [3, 4], [5, 6]])
+
+ test_nested_front()
+
+ check_syntax_error(self, "[i, s for i in nums for s in strs]")
+ check_syntax_error(self, "[x if y]")
+
+ suppliers = [
+ (1, "Boeing"),
+ (2, "Ford"),
+ (3, "Macdonalds")
+ ]
+
+ parts = [
+ (10, "Airliner"),
+ (20, "Engine"),
+ (30, "Cheeseburger")
+ ]
+
+ suppart = [
+ (1, 10), (1, 20), (2, 20), (3, 30)
+ ]
+
+ x = [
+ (sname, pname)
+ for (sno, sname) in suppliers
+ for (pno, pname) in parts
+ for (sp_sno, sp_pno) in suppart
+ if sno == sp_sno and pno == sp_pno
+ ]
+
+ self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'),
+ ('Macdonalds', 'Cheeseburger')])
+
+ def testGenexps(self):
+ # generator expression tests
+ g = ([x for x in range(10)] for x in range(1))
+ self.assertEqual(g.next(), [x for x in range(10)])
+ try:
+ g.next()
+ self.fail('should produce StopIteration exception')
+ except StopIteration:
+ pass
+
+ a = 1
+ try:
+ g = (a for d in a)
+ g.next()
+ self.fail('should produce TypeError')
+ except TypeError:
+ pass
+
+ self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd'])
+ self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy'])
+
+ a = [x for x in range(10)]
+ b = (x for x in (y for y in a))
+ self.assertEqual(sum(b), sum([x for x in range(10)]))
+
+ self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)]))
+ self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2]))
+ self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)]))
+ self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)]))
+ self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)]))
+ self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True)) if True), sum([x for x in range(10)]))
+ self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0)
+ check_syntax_error(self, "foo(x for x in range(10), 100)")
+ check_syntax_error(self, "foo(100, x for x in range(10))")
+
+ def testComprehensionSpecials(self):
+ # test for outmost iterable precomputation
+ x = 10; g = (i for i in range(x)); x = 5
+ self.assertEqual(len(list(g)), 10)
+
+ # This should hold, since we're only precomputing outmost iterable.
+ x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x))
+ x = 5; t = True;
+ self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g))
+
+ # Grammar allows multiple adjacent 'if's in listcomps and genexps,
+ # even though it's silly. Make sure it works (ifelse broke this.)
+ self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7])
+ self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7])
+
+ # verify unpacking single element tuples in listcomp/genexp.
+ self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6])
+ self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9])
+
+ def testIfElseExpr(self):
+ # Test ifelse expressions in various cases
+ def _checkeval(msg, ret):
+ "helper to check that evaluation of expressions is done correctly"
+ print x
+ return ret
+
+ self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True])
+ self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True])
+ self.assertEqual([ x(False) for x in (lambda x: False if x else True, lambda x: True if x else False) if x(False) ], [True])
+ self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5)
+ self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5)
+ self.assertEqual((5 and 6 if 0 else 1), 1)
+ self.assertEqual(((5 and 6) if 0 else 1), 1)
+ self.assertEqual((5 and (6 if 1 else 1)), 6)
+ self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3)
+ self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1)
+ self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5)
+ self.assertEqual((not 5 if 1 else 1), False)
+ self.assertEqual((not 5 if 0 else 1), 1)
+ self.assertEqual((6 + 1 if 1 else 2), 7)
+ self.assertEqual((6 - 1 if 1 else 2), 5)
+ self.assertEqual((6 * 2 if 1 else 4), 12)
+ self.assertEqual((6 / 2 if 1 else 3), 3)
+ self.assertEqual((6 < 4 if 0 else 2), 2)
+
+
+def test_main():
+ run_unittest(TokenTests, GrammarTests)
+
+if __name__ == '__main__':
+ test_main()
diff --git a/Lib/lib2to3/tests/data/py3_test_grammar.py b/Lib/lib2to3/tests/data/py3_test_grammar.py
new file mode 100644
index 0000000..bfc77fe
--- /dev/null
+++ b/Lib/lib2to3/tests/data/py3_test_grammar.py
@@ -0,0 +1,863 @@
+# Python test set -- part 1, grammar.
+# This just tests whether the parser accepts them all.
+
+# NOTE: When you run this test as a script from the command line, you
+# get warnings about certain hex/oct constants. Since those are
+# issued by the parser, you can't suppress them by adding a
+# filterwarnings() call to this module. Therefore, to shut up the
+# regression test, the filterwarnings() call has been added to
+# regrtest.py.
+
+from test.test_support import run_unittest, check_syntax_error
+import unittest
+import sys
+# testing import *
+from sys import *
+
+class TokenTests(unittest.TestCase):
+
+ def testBackslash(self):
+ # Backslash means line continuation:
+ x = 1 \
+ + 1
+ self.assertEquals(x, 2, 'backslash for line continuation')
+
+ # Backslash does not means continuation in comments :\
+ x = 0
+ self.assertEquals(x, 0, 'backslash ending comment')
+
+ def testPlainIntegers(self):
+ self.assertEquals(type(000), type(0))
+ self.assertEquals(0xff, 255)
+ self.assertEquals(0o377, 255)
+ self.assertEquals(2147483647, 0o17777777777)
+ self.assertEquals(0b1001, 9)
+ from sys import maxint
+ if maxint == 2147483647:
+ self.assertEquals(-2147483647-1, -0o20000000000)
+ # XXX -2147483648
+ self.assert_(0o37777777777 > 0)
+ self.assert_(0xffffffff > 0)
+ self.assert_(0b1111111111111111111111111111111 > 0)
+ for s in ('2147483648', '0o40000000000', '0x100000000',
+ '0b10000000000000000000000000000000'):
+ try:
+ x = eval(s)
+ except OverflowError:
+ self.fail("OverflowError on huge integer literal %r" % s)
+ elif maxint == 9223372036854775807:
+ self.assertEquals(-9223372036854775807-1, -0o1000000000000000000000)
+ self.assert_(0o1777777777777777777777 > 0)
+ self.assert_(0xffffffffffffffff > 0)
+ self.assert_(0b11111111111111111111111111111111111111111111111111111111111111 > 0)
+ for s in '9223372036854775808', '0o2000000000000000000000', \
+ '0x10000000000000000', \
+ '0b100000000000000000000000000000000000000000000000000000000000000':
+ try:
+ x = eval(s)
+ except OverflowError:
+ self.fail("OverflowError on huge integer literal %r" % s)
+ else:
+ self.fail('Weird maxint value %r' % maxint)
+
+ def testLongIntegers(self):
+ x = 0
+ x = 0xffffffffffffffff
+ x = 0Xffffffffffffffff
+ x = 0o77777777777777777
+ x = 0O77777777777777777
+ x = 123456789012345678901234567890
+ x = 0b100000000000000000000000000000000000000000000000000000000000000000000
+ x = 0B111111111111111111111111111111111111111111111111111111111111111111111
+
+ def testFloats(self):
+ x = 3.14
+ x = 314.
+ x = 0.314
+ # XXX x = 000.314
+ x = .314
+ x = 3e14
+ x = 3E14
+ x = 3e-14
+ x = 3e+14
+ x = 3.e14
+ x = .3e14
+ x = 3.1e4
+
+ def testStringLiterals(self):
+ x = ''; y = ""; self.assert_(len(x) == 0 and x == y)
+ x = '\''; y = "'"; self.assert_(len(x) == 1 and x == y and ord(x) == 39)
+ x = '"'; y = "\""; self.assert_(len(x) == 1 and x == y and ord(x) == 34)
+ x = "doesn't \"shrink\" does it"
+ y = 'doesn\'t "shrink" does it'
+ self.assert_(len(x) == 24 and x == y)
+ x = "does \"shrink\" doesn't it"
+ y = 'does "shrink" doesn\'t it'
+ self.assert_(len(x) == 24 and x == y)
+ x = """
+The "quick"
+brown fox
+jumps over
+the 'lazy' dog.
+"""
+ y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
+ self.assertEquals(x, y)
+ y = '''
+The "quick"
+brown fox
+jumps over
+the 'lazy' dog.
+'''
+ self.assertEquals(x, y)
+ y = "\n\
+The \"quick\"\n\
+brown fox\n\
+jumps over\n\
+the 'lazy' dog.\n\
+"
+ self.assertEquals(x, y)
+ y = '\n\
+The \"quick\"\n\
+brown fox\n\
+jumps over\n\
+the \'lazy\' dog.\n\
+'
+ self.assertEquals(x, y)
+
+ def testEllipsis(self):
+ x = ...
+ self.assert_(x is Ellipsis)
+ self.assertRaises(SyntaxError, eval, ".. .")
+
+class GrammarTests(unittest.TestCase):
+
+ # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
+ # XXX can't test in a script -- this rule is only used when interactive
+
+ # file_input: (NEWLINE | stmt)* ENDMARKER
+ # Being tested as this very moment this very module
+
+ # expr_input: testlist NEWLINE
+ # XXX Hard to test -- used only in calls to input()
+
+ def testEvalInput(self):
+ # testlist ENDMARKER
+ x = eval('1, 0 or 1')
+
+ def testFuncdef(self):
+ ### [decorators] 'def' NAME parameters ['->' test] ':' suite
+ ### decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
+ ### decorators: decorator+
+ ### parameters: '(' [typedargslist] ')'
+ ### typedargslist: ((tfpdef ['=' test] ',')*
+ ### ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef)
+ ### | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
+ ### tfpdef: NAME [':' test]
+ ### varargslist: ((vfpdef ['=' test] ',')*
+ ### ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef)
+ ### | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
+ ### vfpdef: NAME
+ def f1(): pass
+ f1()
+ f1(*())
+ f1(*(), **{})
+ def f2(one_argument): pass
+ def f3(two, arguments): pass
+ self.assertEquals(f2.__code__.co_varnames, ('one_argument',))
+ self.assertEquals(f3.__code__.co_varnames, ('two', 'arguments'))
+ def a1(one_arg,): pass
+ def a2(two, args,): pass
+ def v0(*rest): pass
+ def v1(a, *rest): pass
+ def v2(a, b, *rest): pass
+
+ f1()
+ f2(1)
+ f2(1,)
+ f3(1, 2)
+ f3(1, 2,)
+ v0()
+ v0(1)
+ v0(1,)
+ v0(1,2)
+ v0(1,2,3,4,5,6,7,8,9,0)
+ v1(1)
+ v1(1,)
+ v1(1,2)
+ v1(1,2,3)
+ v1(1,2,3,4,5,6,7,8,9,0)
+ v2(1,2)
+ v2(1,2,3)
+ v2(1,2,3,4)
+ v2(1,2,3,4,5,6,7,8,9,0)
+
+ def d01(a=1): pass
+ d01()
+ d01(1)
+ d01(*(1,))
+ d01(**{'a':2})
+ def d11(a, b=1): pass
+ d11(1)
+ d11(1, 2)
+ d11(1, **{'b':2})
+ def d21(a, b, c=1): pass
+ d21(1, 2)
+ d21(1, 2, 3)
+ d21(*(1, 2, 3))
+ d21(1, *(2, 3))
+ d21(1, 2, *(3,))
+ d21(1, 2, **{'c':3})
+ def d02(a=1, b=2): pass
+ d02()
+ d02(1)
+ d02(1, 2)
+ d02(*(1, 2))
+ d02(1, *(2,))
+ d02(1, **{'b':2})
+ d02(**{'a': 1, 'b': 2})
+ def d12(a, b=1, c=2): pass
+ d12(1)
+ d12(1, 2)
+ d12(1, 2, 3)
+ def d22(a, b, c=1, d=2): pass
+ d22(1, 2)
+ d22(1, 2, 3)
+ d22(1, 2, 3, 4)
+ def d01v(a=1, *rest): pass
+ d01v()
+ d01v(1)
+ d01v(1, 2)
+ d01v(*(1, 2, 3, 4))
+ d01v(*(1,))
+ d01v(**{'a':2})
+ def d11v(a, b=1, *rest): pass
+ d11v(1)
+ d11v(1, 2)
+ d11v(1, 2, 3)
+ def d21v(a, b, c=1, *rest): pass
+ d21v(1, 2)
+ d21v(1, 2, 3)
+ d21v(1, 2, 3, 4)
+ d21v(*(1, 2, 3, 4))
+ d21v(1, 2, **{'c': 3})
+ def d02v(a=1, b=2, *rest): pass
+ d02v()
+ d02v(1)
+ d02v(1, 2)
+ d02v(1, 2, 3)
+ d02v(1, *(2, 3, 4))
+ d02v(**{'a': 1, 'b': 2})
+ def d12v(a, b=1, c=2, *rest): pass
+ d12v(1)
+ d12v(1, 2)
+ d12v(1, 2, 3)
+ d12v(1, 2, 3, 4)
+ d12v(*(1, 2, 3, 4))
+ d12v(1, 2, *(3, 4, 5))
+ d12v(1, *(2,), **{'c': 3})
+ def d22v(a, b, c=1, d=2, *rest): pass
+ d22v(1, 2)
+ d22v(1, 2, 3)
+ d22v(1, 2, 3, 4)
+ d22v(1, 2, 3, 4, 5)
+ d22v(*(1, 2, 3, 4))
+ d22v(1, 2, *(3, 4, 5))
+ d22v(1, *(2, 3), **{'d': 4})
+ # keyword only argument tests
+ def pos0key1(*, key): return key
+ pos0key1(key=100)
+ def pos2key2(p1, p2, *, k1, k2=100): return p1,p2,k1,k2
+ pos2key2(1, 2, k1=100)
+ pos2key2(1, 2, k1=100, k2=200)
+ pos2key2(1, 2, k2=100, k1=200)
+ def pos2key2dict(p1, p2, *, k1=100, k2, **kwarg): return p1,p2,k1,k2,kwarg
+ pos2key2dict(1,2,k2=100,tokwarg1=100,tokwarg2=200)
+ pos2key2dict(1,2,tokwarg1=100,tokwarg2=200, k2=100)
+
+ # argument annotation tests
+ def f(x) -> list: pass
+ self.assertEquals(f.__annotations__, {'return': list})
+ def f(x:int): pass
+ self.assertEquals(f.__annotations__, {'x': int})
+ def f(*x:str): pass
+ self.assertEquals(f.__annotations__, {'x': str})
+ def f(**x:float): pass
+ self.assertEquals(f.__annotations__, {'x': float})
+ def f(x, y:1+2): pass
+ self.assertEquals(f.__annotations__, {'y': 3})
+ def f(a, b:1, c:2, d): pass
+ self.assertEquals(f.__annotations__, {'b': 1, 'c': 2})
+ def f(a, b:1, c:2, d, e:3=4, f=5, *g:6): pass
+ self.assertEquals(f.__annotations__,
+ {'b': 1, 'c': 2, 'e': 3, 'g': 6})
+ def f(a, b:1, c:2, d, e:3=4, f=5, *g:6, h:7, i=8, j:9=10,
+ **k:11) -> 12: pass
+ self.assertEquals(f.__annotations__,
+ {'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9,
+ 'k': 11, 'return': 12})
+ # Check for SF Bug #1697248 - mixing decorators and a return annotation
+ def null(x): return x
+ @null
+ def f(x) -> list: pass
+ self.assertEquals(f.__annotations__, {'return': list})
+
+ # test MAKE_CLOSURE with a variety of oparg's
+ closure = 1
+ def f(): return closure
+ def f(x=1): return closure
+ def f(*, k=1): return closure
+ def f() -> int: return closure
+
+ def testLambdef(self):
+ ### lambdef: 'lambda' [varargslist] ':' test
+ l1 = lambda : 0
+ self.assertEquals(l1(), 0)
+ l2 = lambda : a[d] # XXX just testing the expression
+ l3 = lambda : [2 < x for x in [-1, 3, 0]]
+ self.assertEquals(l3(), [0, 1, 0])
+ l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
+ self.assertEquals(l4(), 1)
+ l5 = lambda x, y, z=2: x + y + z
+ self.assertEquals(l5(1, 2), 5)
+ self.assertEquals(l5(1, 2, 3), 6)
+ check_syntax_error(self, "lambda x: x = 2")
+ l6 = lambda x, y, *, k=20: x+y+k
+ self.assertEquals(l6(1,2), 1+2+20)
+ self.assertEquals(l6(1,2,k=10), 1+2+10)
+
+
+ ### stmt: simple_stmt | compound_stmt
+ # Tested below
+
+ def testSimpleStmt(self):
+ ### simple_stmt: small_stmt (';' small_stmt)* [';']
+ x = 1; pass; del x
+ def foo():
+ # verify statments that end with semi-colons
+ x = 1; pass; del x;
+ foo()
+
+ ### small_stmt: expr_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt
+ # Tested below
+
+ def testExprStmt(self):
+ # (exprlist '=')* exprlist
+ 1
+ 1, 2, 3
+ x = 1
+ x = 1, 2, 3
+ x = y = z = 1, 2, 3
+ x, y, z = 1, 2, 3
+ abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
+
+ check_syntax_error(self, "x + 1 = 1")
+ check_syntax_error(self, "a + 1 = b + 2")
+
+ def testDelStmt(self):
+ # 'del' exprlist
+ abc = [1,2,3]
+ x, y, z = abc
+ xyz = x, y, z
+
+ del abc
+ del x, y, (z, xyz)
+
+ def testPassStmt(self):
+ # 'pass'
+ pass
+
+ # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
+ # Tested below
+
+ def testBreakStmt(self):
+ # 'break'
+ while 1: break
+
+ def testContinueStmt(self):
+ # 'continue'
+ i = 1
+ while i: i = 0; continue
+
+ msg = ""
+ while not msg:
+ msg = "ok"
+ try:
+ continue
+ msg = "continue failed to continue inside try"
+ except:
+ msg = "continue inside try called except block"
+ if msg != "ok":
+ self.fail(msg)
+
+ msg = ""
+ while not msg:
+ msg = "finally block not called"
+ try:
+ continue
+ finally:
+ msg = "ok"
+ if msg != "ok":
+ self.fail(msg)
+
+ def test_break_continue_loop(self):
+ # This test warrants an explanation. It is a test specifically for SF bugs
+ # #463359 and #462937. The bug is that a 'break' statement executed or
+ # exception raised inside a try/except inside a loop, *after* a continue
+ # statement has been executed in that loop, will cause the wrong number of
+ # arguments to be popped off the stack and the instruction pointer reset to
+ # a very small number (usually 0.) Because of this, the following test
+ # *must* written as a function, and the tracking vars *must* be function
+ # arguments with default values. Otherwise, the test will loop and loop.
+
+ def test_inner(extra_burning_oil = 1, count=0):
+ big_hippo = 2
+ while big_hippo:
+ count += 1
+ try:
+ if extra_burning_oil and big_hippo == 1:
+ extra_burning_oil -= 1
+ break
+ big_hippo -= 1
+ continue
+ except:
+ raise
+ if count > 2 or big_hippo != 1:
+ self.fail("continue then break in try/except in loop broken!")
+ test_inner()
+
+ def testReturn(self):
+ # 'return' [testlist]
+ def g1(): return
+ def g2(): return 1
+ g1()
+ x = g2()
+ check_syntax_error(self, "class foo:return 1")
+
+ def testYield(self):
+ check_syntax_error(self, "class foo:yield 1")
+
+ def testRaise(self):
+ # 'raise' test [',' test]
+ try: raise RuntimeError, 'just testing'
+ except RuntimeError: pass
+ try: raise KeyboardInterrupt
+ except KeyboardInterrupt: pass
+
+ def testImport(self):
+ # 'import' dotted_as_names
+ import sys
+ import time, sys
+ # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names)
+ from time import time
+ from time import (time)
+ # not testable inside a function, but already done at top of the module
+ # from sys import *
+ from sys import path, argv
+ from sys import (path, argv)
+ from sys import (path, argv,)
+
+ def testGlobal(self):
+ # 'global' NAME (',' NAME)*
+ global a
+ global a, b
+ global one, two, three, four, five, six, seven, eight, nine, ten
+
+ def testAssert(self):
+ # assert_stmt: 'assert' test [',' test]
+ assert 1
+ assert 1, 1
+ assert lambda x:x
+ assert 1, lambda x:x+1
+ try:
+ assert 0, "msg"
+ except AssertionError as e:
+ self.assertEquals(e.args[0], "msg")
+ else:
+ if __debug__:
+ self.fail("AssertionError not raised by assert 0")
+
+ ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
+ # Tested below
+
+ def testIf(self):
+ # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
+ if 1: pass
+ if 1: pass
+ else: pass
+ if 0: pass
+ elif 0: pass
+ if 0: pass
+ elif 0: pass
+ elif 0: pass
+ elif 0: pass
+ else: pass
+
+ def testWhile(self):
+ # 'while' test ':' suite ['else' ':' suite]
+ while 0: pass
+ while 0: pass
+ else: pass
+
+ def testFor(self):
+ # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
+ for i in 1, 2, 3: pass
+ for i, j, k in (): pass
+ else: pass
+ class Squares:
+ def __init__(self, max):
+ self.max = max
+ self.sofar = []
+ def __len__(self): return len(self.sofar)
+ def __getitem__(self, i):
+ if not 0 <= i < self.max: raise IndexError
+ n = len(self.sofar)
+ while n <= i:
+ self.sofar.append(n*n)
+ n = n+1
+ return self.sofar[i]
+ n = 0
+ for x in Squares(10): n = n+x
+ if n != 285:
+ self.fail('for over growing sequence')
+
+ result = []
+ for x, in [(1,), (2,), (3,)]:
+ result.append(x)
+ self.assertEqual(result, [1, 2, 3])
+
+ def testTry(self):
+ ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
+ ### | 'try' ':' suite 'finally' ':' suite
+ ### except_clause: 'except' [expr ['as' expr]]
+ try:
+ 1/0
+ except ZeroDivisionError:
+ pass
+ else:
+ pass
+ try: 1/0
+ except EOFError: pass
+ except TypeError as msg: pass
+ except RuntimeError as msg: pass
+ except: pass
+ else: pass
+ try: 1/0
+ except (EOFError, TypeError, ZeroDivisionError): pass
+ try: 1/0
+ except (EOFError, TypeError, ZeroDivisionError) as msg: pass
+ try: pass
+ finally: pass
+
+ def testSuite(self):
+ # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
+ if 1: pass
+ if 1:
+ pass
+ if 1:
+ #
+ #
+ #
+ pass
+ pass
+ #
+ pass
+ #
+
+ def testTest(self):
+ ### and_test ('or' and_test)*
+ ### and_test: not_test ('and' not_test)*
+ ### not_test: 'not' not_test | comparison
+ if not 1: pass
+ if 1 and 1: pass
+ if 1 or 1: pass
+ if not not not 1: pass
+ if not 1 and 1 and 1: pass
+ if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
+
+ def testComparison(self):
+ ### comparison: expr (comp_op expr)*
+ ### comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not'
+ if 1: pass
+ x = (1 == 1)
+ if 1 == 1: pass
+ if 1 != 1: pass
+ if 1 < 1: pass
+ if 1 > 1: pass
+ if 1 <= 1: pass
+ if 1 >= 1: pass
+ if 1 is 1: pass
+ if 1 is not 1: pass
+ if 1 in (): pass
+ if 1 not in (): pass
+ if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass
+
+ def testBinaryMaskOps(self):
+ x = 1 & 1
+ x = 1 ^ 1
+ x = 1 | 1
+
+ def testShiftOps(self):
+ x = 1 << 1
+ x = 1 >> 1
+ x = 1 << 1 >> 1
+
+ def testAdditiveOps(self):
+ x = 1
+ x = 1 + 1
+ x = 1 - 1 - 1
+ x = 1 - 1 + 1 - 1 + 1
+
+ def testMultiplicativeOps(self):
+ x = 1 * 1
+ x = 1 / 1
+ x = 1 % 1
+ x = 1 / 1 * 1 % 1
+
+ def testUnaryOps(self):
+ x = +1
+ x = -1
+ x = ~1
+ x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
+ x = -1*1/1 + 1*1 - ---1*1
+
+ def testSelectors(self):
+ ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
+ ### subscript: expr | [expr] ':' [expr]
+
+ import sys, time
+ c = sys.path[0]
+ x = time.time()
+ x = sys.modules['time'].time()
+ a = '01234'
+ c = a[0]
+ c = a[-1]
+ s = a[0:5]
+ s = a[:5]
+ s = a[0:]
+ s = a[:]
+ s = a[-5:]
+ s = a[:-1]
+ s = a[-4:-3]
+ # A rough test of SF bug 1333982. http://python.org/sf/1333982
+ # The testing here is fairly incomplete.
+ # Test cases should include: commas with 1 and 2 colons
+ d = {}
+ d[1] = 1
+ d[1,] = 2
+ d[1,2] = 3
+ d[1,2,3] = 4
+ L = list(d)
+ L.sort(key=lambda x: x if isinstance(x, tuple) else ())
+ self.assertEquals(str(L), '[1, (1,), (1, 2), (1, 2, 3)]')
+
+ def testAtoms(self):
+ ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING
+ ### dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [','])
+
+ x = (1)
+ x = (1 or 2 or 3)
+ x = (1 or 2 or 3, 2, 3)
+
+ x = []
+ x = [1]
+ x = [1 or 2 or 3]
+ x = [1 or 2 or 3, 2, 3]
+ x = []
+
+ x = {}
+ x = {'one': 1}
+ x = {'one': 1,}
+ x = {'one' or 'two': 1 or 2}
+ x = {'one': 1, 'two': 2}
+ x = {'one': 1, 'two': 2,}
+ x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
+
+ x = {'one'}
+ x = {'one', 1,}
+ x = {'one', 'two', 'three'}
+ x = {2, 3, 4,}
+
+ x = x
+ x = 'x'
+ x = 123
+
+ ### exprlist: expr (',' expr)* [',']
+ ### testlist: test (',' test)* [',']
+ # These have been exercised enough above
+
+ def testClassdef(self):
+ # 'class' NAME ['(' [testlist] ')'] ':' suite
+ class B: pass
+ class B2(): pass
+ class C1(B): pass
+ class C2(B): pass
+ class D(C1, C2, B): pass
+ class C:
+ def meth1(self): pass
+ def meth2(self, arg): pass
+ def meth3(self, a1, a2): pass
+
+ # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
+ # decorators: decorator+
+ # decorated: decorators (classdef | funcdef)
+ def class_decorator(x): return x
+ @class_decorator
+ class G: pass
+
+ def testDictcomps(self):
+ # dictorsetmaker: ( (test ':' test (comp_for |
+ # (',' test ':' test)* [','])) |
+ # (test (comp_for | (',' test)* [','])) )
+ nums = [1, 2, 3]
+ self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4})
+
+ def testListcomps(self):
+ # list comprehension tests
+ nums = [1, 2, 3, 4, 5]
+ strs = ["Apple", "Banana", "Coconut"]
+ spcs = [" Apple", " Banana ", "Coco nut "]
+
+ self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut'])
+ self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15])
+ self.assertEqual([x for x in nums if x > 2], [3, 4, 5])
+ self.assertEqual([(i, s) for i in nums for s in strs],
+ [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'),
+ (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'),
+ (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'),
+ (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'),
+ (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')])
+ self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]],
+ [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'),
+ (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'),
+ (5, 'Banana'), (5, 'Coconut')])
+ self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)],
+ [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]])
+
+ def test_in_func(l):
+ return [0 < x < 3 for x in l if x > 2]
+
+ self.assertEqual(test_in_func(nums), [False, False, False])
+
+ def test_nested_front():
+ self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]],
+ [[1, 2], [3, 4], [5, 6]])
+
+ test_nested_front()
+
+ check_syntax_error(self, "[i, s for i in nums for s in strs]")
+ check_syntax_error(self, "[x if y]")
+
+ suppliers = [
+ (1, "Boeing"),
+ (2, "Ford"),
+ (3, "Macdonalds")
+ ]
+
+ parts = [
+ (10, "Airliner"),
+ (20, "Engine"),
+ (30, "Cheeseburger")
+ ]
+
+ suppart = [
+ (1, 10), (1, 20), (2, 20), (3, 30)
+ ]
+
+ x = [
+ (sname, pname)
+ for (sno, sname) in suppliers
+ for (pno, pname) in parts
+ for (sp_sno, sp_pno) in suppart
+ if sno == sp_sno and pno == sp_pno
+ ]
+
+ self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'),
+ ('Macdonalds', 'Cheeseburger')])
+
+ def testGenexps(self):
+ # generator expression tests
+ g = ([x for x in range(10)] for x in range(1))
+ self.assertEqual(next(g), [x for x in range(10)])
+ try:
+ next(g)
+ self.fail('should produce StopIteration exception')
+ except StopIteration:
+ pass
+
+ a = 1
+ try:
+ g = (a for d in a)
+ next(g)
+ self.fail('should produce TypeError')
+ except TypeError:
+ pass
+
+ self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd'])
+ self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy'])
+
+ a = [x for x in range(10)]
+ b = (x for x in (y for y in a))
+ self.assertEqual(sum(b), sum([x for x in range(10)]))
+
+ self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)]))
+ self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2]))
+ self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)]))
+ self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)]))
+ self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)]))
+ self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True)) if True), sum([x for x in range(10)]))
+ self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0)
+ check_syntax_error(self, "foo(x for x in range(10), 100)")
+ check_syntax_error(self, "foo(100, x for x in range(10))")
+
+ def testComprehensionSpecials(self):
+ # test for outmost iterable precomputation
+ x = 10; g = (i for i in range(x)); x = 5
+ self.assertEqual(len(list(g)), 10)
+
+ # This should hold, since we're only precomputing outmost iterable.
+ x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x))
+ x = 5; t = True;
+ self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g))
+
+ # Grammar allows multiple adjacent 'if's in listcomps and genexps,
+ # even though it's silly. Make sure it works (ifelse broke this.)
+ self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7])
+ self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7])
+
+ # verify unpacking single element tuples in listcomp/genexp.
+ self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6])
+ self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9])
+
+ def testIfElseExpr(self):
+ # Test ifelse expressions in various cases
+ def _checkeval(msg, ret):
+ "helper to check that evaluation of expressions is done correctly"
+ print(x)
+ return ret
+
+ # the next line is not allowed anymore
+ #self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True])
+ self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True])
+ self.assertEqual([ x(False) for x in (lambda x: False if x else True, lambda x: True if x else False) if x(False) ], [True])
+ self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5)
+ self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5)
+ self.assertEqual((5 and 6 if 0 else 1), 1)
+ self.assertEqual(((5 and 6) if 0 else 1), 1)
+ self.assertEqual((5 and (6 if 1 else 1)), 6)
+ self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3)
+ self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1)
+ self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5)
+ self.assertEqual((not 5 if 1 else 1), False)
+ self.assertEqual((not 5 if 0 else 1), 1)
+ self.assertEqual((6 + 1 if 1 else 2), 7)
+ self.assertEqual((6 - 1 if 1 else 2), 5)
+ self.assertEqual((6 * 2 if 1 else 4), 12)
+ self.assertEqual((6 / 2 if 1 else 3), 3)
+ self.assertEqual((6 < 4 if 0 else 2), 2)
+
+
+def test_main():
+ run_unittest(TokenTests, GrammarTests)
+
+if __name__ == '__main__':
+ test_main()
diff --git a/Lib/lib2to3/tests/pytree_idempotency.py b/Lib/lib2to3/tests/pytree_idempotency.py
new file mode 100755
index 0000000..d83f5cc
--- /dev/null
+++ b/Lib/lib2to3/tests/pytree_idempotency.py
@@ -0,0 +1,92 @@
+#!/usr/bin/env python2.5
+# Copyright 2006 Google, Inc. All Rights Reserved.
+# Licensed to PSF under a Contributor Agreement.
+
+"""Main program for testing the infrastructure."""
+
+__author__ = "Guido van Rossum <guido@python.org>"
+
+# Support imports (need to be imported first)
+import support
+
+# Python imports
+import os
+import sys
+import logging
+
+# Local imports
+from .. import pytree
+import pgen2
+from pgen2 import driver
+
+logging.basicConfig()
+
+def main():
+ gr = driver.load_grammar("Grammar.txt")
+ dr = driver.Driver(gr, convert=pytree.convert)
+
+ fn = "example.py"
+ tree = dr.parse_file(fn, debug=True)
+ if not diff(fn, tree):
+ print "No diffs."
+ if not sys.argv[1:]:
+ return # Pass a dummy argument to run the complete test suite below
+
+ problems = []
+
+ # Process every imported module
+ for name in sys.modules:
+ mod = sys.modules[name]
+ if mod is None or not hasattr(mod, "__file__"):
+ continue
+ fn = mod.__file__
+ if fn.endswith(".pyc"):
+ fn = fn[:-1]
+ if not fn.endswith(".py"):
+ continue
+ print >>sys.stderr, "Parsing", fn
+ tree = dr.parse_file(fn, debug=True)
+ if diff(fn, tree):
+ problems.append(fn)
+
+ # Process every single module on sys.path (but not in packages)
+ for dir in sys.path:
+ try:
+ names = os.listdir(dir)
+ except os.error:
+ continue
+ print >>sys.stderr, "Scanning", dir, "..."
+ for name in names:
+ if not name.endswith(".py"):
+ continue
+ print >>sys.stderr, "Parsing", name
+ fn = os.path.join(dir, name)
+ try:
+ tree = dr.parse_file(fn, debug=True)
+ except pgen2.parse.ParseError, err:
+ print "ParseError:", err
+ else:
+ if diff(fn, tree):
+ problems.append(fn)
+
+ # Show summary of problem files
+ if not problems:
+ print "No problems. Congratulations!"
+ else:
+ print "Problems in following files:"
+ for fn in problems:
+ print "***", fn
+
+def diff(fn, tree):
+ f = open("@", "w")
+ try:
+ f.write(str(tree))
+ finally:
+ f.close()
+ try:
+ return os.system("diff -u %s @" % fn)
+ finally:
+ os.remove("@")
+
+if __name__ == "__main__":
+ main()
diff --git a/Lib/lib2to3/tests/support.py b/Lib/lib2to3/tests/support.py
new file mode 100644
index 0000000..7789033
--- /dev/null
+++ b/Lib/lib2to3/tests/support.py
@@ -0,0 +1,47 @@
+"""Support code for test_*.py files"""
+# Author: Collin Winter
+
+# Python imports
+import unittest
+import sys
+import os
+import os.path
+import re
+from textwrap import dedent
+
+#sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
+
+# Local imports
+from .. import pytree
+from ..pgen2 import driver
+
+test_dir = os.path.dirname(__file__)
+proj_dir = os.path.normpath(os.path.join(test_dir, ".."))
+grammar_path = os.path.join(test_dir, "..", "Grammar.txt")
+grammar = driver.load_grammar(grammar_path)
+driver = driver.Driver(grammar, convert=pytree.convert)
+
+def parse_string(string):
+ return driver.parse_string(reformat(string), debug=True)
+
+# Python 2.3's TestSuite is not iter()-able
+if sys.version_info < (2, 4):
+ def TestSuite_iter(self):
+ return iter(self._tests)
+ unittest.TestSuite.__iter__ = TestSuite_iter
+
+def run_all_tests(test_mod=None, tests=None):
+ if tests is None:
+ tests = unittest.TestLoader().loadTestsFromModule(test_mod)
+ unittest.TextTestRunner(verbosity=2).run(tests)
+
+def reformat(string):
+ return dedent(string) + "\n\n"
+
+def all_project_files():
+ for dirpath, dirnames, filenames in os.walk(proj_dir):
+ for filename in filenames:
+ if filename.endswith(".py"):
+ yield os.path.join(dirpath, filename)
+
+TestCase = unittest.TestCase
diff --git a/Lib/lib2to3/tests/test_all_fixers.py b/Lib/lib2to3/tests/test_all_fixers.py
new file mode 100644
index 0000000..e8df320
--- /dev/null
+++ b/Lib/lib2to3/tests/test_all_fixers.py
@@ -0,0 +1,42 @@
+#!/usr/bin/env python2.5
+"""Tests that run all fixer modules over an input stream.
+
+This has been broken out into its own test module because of its
+running time.
+"""
+# Author: Collin Winter
+
+# Testing imports
+try:
+ from . import support
+except ImportError:
+ import support
+
+# Python imports
+import unittest
+
+# Local imports
+from .. import pytree
+from .. import refactor
+
+class Options:
+ def __init__(self, **kwargs):
+ for k, v in kwargs.items():
+ setattr(self, k, v)
+ self.verbose = False
+
+class Test_all(support.TestCase):
+ def setUp(self):
+ options = Options(fix=["all", "idioms", "ws_comma"],
+ print_function=False)
+ self.refactor = refactor.RefactoringTool(options)
+
+ def test_all_project_files(self):
+ for filepath in support.all_project_files():
+ print "Fixing %s..." % filepath
+ self.refactor.refactor_string(open(filepath).read(), filepath)
+
+
+if __name__ == "__main__":
+ import __main__
+ support.run_all_tests(__main__)
diff --git a/Lib/lib2to3/tests/test_fixers.py b/Lib/lib2to3/tests/test_fixers.py
new file mode 100755
index 0000000..96c3f59
--- /dev/null
+++ b/Lib/lib2to3/tests/test_fixers.py
@@ -0,0 +1,2918 @@
+#!/usr/bin/env python2.5
+""" Test suite for the fixer modules """
+# Author: Collin Winter
+
+# Testing imports
+try:
+ from tests import support
+except ImportError:
+ import support
+
+# Python imports
+import unittest
+
+# Local imports
+from .. import pygram
+from .. import pytree
+from .. import refactor
+
+class Options:
+ def __init__(self, **kwargs):
+ for k, v in kwargs.items():
+ setattr(self, k, v)
+
+ self.verbose = False
+
+class FixerTestCase(support.TestCase):
+ def setUp(self):
+ options = Options(fix=[self.fixer], print_function=False)
+ self.refactor = refactor.RefactoringTool(options)
+ self.fixer_log = []
+
+ for order in (self.refactor.pre_order, self.refactor.post_order):
+ for fixer in 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, "<string>")
+ self.failUnlessEqual(after, str(tree))
+ return tree
+
+ def check(self, before, after, ignore_warnings=False):
+ tree = self._check(before, after)
+ self.failUnless(tree.was_changed)
+ if not ignore_warnings:
+ self.failUnlessEqual(self.fixer_log, [])
+
+ def warns(self, before, after, message, unchanged=False):
+ tree = self._check(before, after)
+ self.failUnless(message in "".join(self.fixer_log))
+ if not unchanged:
+ self.failUnless(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.failUnlessEqual(self.fixer_log, [])
+
+
+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 = """x = sys.intern( a )"""
+ self.check(b, a)
+
+ b = """y = intern("b" # test
+ )"""
+ a = """y = sys.intern("b" # test
+ )"""
+ self.check(b, a)
+
+ b = """z = intern(a+b+c.d, )"""
+ a = """z = sys.intern(a+b+c.d, )"""
+ self.check(b, a)
+
+ def test(self):
+ b = """x = intern(a)"""
+ a = """x = sys.intern(a)"""
+ self.check(b, a)
+
+ b = """z = intern(a+b+c.d,)"""
+ a = """z = sys.intern(a+b+c.d,)"""
+ self.check(b, a)
+
+ b = """intern("y%s" % 5).replace("y", "")"""
+ a = """sys.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_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):
+ print_stmt = pygram.python_grammar.keywords.pop("print")
+ try:
+ s = """print(1, 1+1, 1+1+1)"""
+ self.unchanged(s)
+
+ s = """print()"""
+ self.unchanged(s)
+
+ s = """print('')"""
+ self.unchanged(s)
+ finally:
+ pygram.python_grammar.keywords["print"] = print_stmt
+
+ 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_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)
+
+
+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)
+
+ # 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_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_4(self):
+ b = """a = 12L"""
+ a = """a = 12"""
+ self.check(b, a)
+
+ def test_5(self):
+ b = """b = 0x12l"""
+ a = """b = 0x12"""
+ self.check(b, a)
+
+ def test_unchanged_1(self):
+ s = """a = 12"""
+ self.unchanged(s)
+
+ def test_unchanged_2(self):
+ s = """b = 0x12"""
+ self.unchanged(s)
+
+ def test_unchanged_3(self):
+ s = """c = 3.14"""
+ self.unchanged(s)
+
+ def test_prefix_preservation(self):
+ b = """x = long( x )"""
+ a = """x = int( x )"""
+ self.check(b, a)
+
+
+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)
+
+ 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)
+
+ def test_unchanged(self):
+ wrappers = ["set", "sorted", "any", "all", "tuple", "sum"]
+ for wrapper in wrappers:
+ 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)
+
+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_1(self):
+ b = """x = xrange(10)"""
+ a = """x = range(10)"""
+ self.check(b, a)
+
+ def test_2(self):
+ b = """x = xrange(1, 10)"""
+ a = """x = range(1, 10)"""
+ self.check(b, a)
+
+ def test_3(self):
+ b = """x = xrange(0, 10, 2)"""
+ a = """x = range(0, 10, 2)"""
+ self.check(b, a)
+
+ def test_4(self):
+ b = """for i in xrange(10):\n j=i"""
+ a = """for i in range(10):\n j=i"""
+ 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)
+
+
+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 Test_imports(FixerTestCase):
+ fixer = "imports"
+
+ modules = {"StringIO": ("io", ["StringIO"]),
+ "cStringIO": ("io", ["StringIO"]),
+ "__builtin__" : ("builtins", ["open", "Exception",
+ "__debug__", "str"]),
+ }
+
+ def test_import_module(self):
+ for old, (new, members) 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, members) in self.modules.items():
+ for member in members:
+ 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)
+
+ def test_import_module_as(self):
+ for old, (new, members) 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, members) in self.modules.items():
+ 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)
+
+ def test_star(self):
+ for old in self.modules:
+ s = "from %s import *" % old
+ self.warns_unchanged(s, "Cannot handle star imports")
+
+ def test_import_module_usage(self):
+ for old, (new, members) in self.modules.items():
+ for member in members:
+ b = """
+ import %s
+ foo(%s, %s.%s)
+ """ % (old, old, old, member)
+ a = """
+ import %s
+ foo(%s, %s.%s)
+ """ % (new, new, new, member)
+ self.check(b, a)
+
+ def test_from_import_usage(self):
+ for old, (new, members) in self.modules.items():
+ for member in members:
+ b = """
+ from %s import %s
+ foo(%s, %s())
+ """ % (old, member, member, member)
+ a = """
+ from %s import %s
+ foo(%s, %s())
+ """ % (new, member, member, member)
+ self.check(b, a)
+
+
+class Test_input(FixerTestCase):
+ fixer = "input"
+
+ def test_prefix_preservation(self):
+ b = """x = input( )"""
+ a = """x = eval(input( ))"""
+ self.check(b, a)
+
+ b = """x = input( '' )"""
+ a = """x = eval(input( '' ))"""
+ self.check(b, a)
+
+ def test_trailing_comment(self):
+ b = """x = input() # foo"""
+ a = """x = eval(input()) # foo"""
+ self.check(b, a)
+
+ def test_idempotency(self):
+ s = """x = eval(input())"""
+ self.unchanged(s)
+
+ s = """x = eval(input(''))"""
+ self.unchanged(s)
+
+ s = """x = eval(input(foo(5) + 9))"""
+ self.unchanged(s)
+
+ def test_1(self):
+ b = """x = input()"""
+ a = """x = eval(input())"""
+ self.check(b, a)
+
+ def test_2(self):
+ b = """x = input('')"""
+ a = """x = eval(input(''))"""
+ self.check(b, a)
+
+ def test_3(self):
+ b = """x = input('prompt')"""
+ a = """x = eval(input('prompt'))"""
+ self.check(b, a)
+
+ def test_4(self):
+ b = """x = input(foo(5) + 9)"""
+ a = """x = eval(input(foo(5) + 9))"""
+ self.check(b, a)
+
+
+class Test_tuple_params(FixerTestCase):
+ fixer = "tuple_params"
+
+ def test_unchanged_1(self):
+ s = """def foo(): pass"""
+ self.unchanged(s)
+
+ def test_unchanged_2(self):
+ s = """def foo(a, b, c): pass"""
+ self.unchanged(s)
+
+ def test_unchanged_3(self):
+ s = """def foo(a=3, b=4, c=5): pass"""
+ self.unchanged(s)
+
+ def test_1(self):
+ b = """
+ def foo(((a, b), c)):
+ x = 5"""
+
+ a = """
+ def foo(xxx_todo_changeme):
+ ((a, b), c) = xxx_todo_changeme
+ x = 5"""
+ self.check(b, a)
+
+ def test_2(self):
+ b = """
+ def foo(((a, b), c), d):
+ x = 5"""
+
+ a = """
+ def foo(xxx_todo_changeme, d):
+ ((a, b), c) = xxx_todo_changeme
+ x = 5"""
+ self.check(b, a)
+
+ def test_3(self):
+ b = """
+ def foo(((a, b), c), d) -> e:
+ x = 5"""
+
+ a = """
+ def foo(xxx_todo_changeme, d) -> e:
+ ((a, b), c) = xxx_todo_changeme
+ x = 5"""
+ self.check(b, a)
+
+ def test_semicolon(self):
+ b = """
+ def foo(((a, b), c)): x = 5; y = 7"""
+
+ a = """
+ def foo(xxx_todo_changeme): ((a, b), c) = xxx_todo_changeme; x = 5; y = 7"""
+ self.check(b, a)
+
+ def test_keywords(self):
+ b = """
+ def foo(((a, b), c), d, e=5) -> z:
+ x = 5"""
+
+ a = """
+ def foo(xxx_todo_changeme, d, e=5) -> z:
+ ((a, b), c) = xxx_todo_changeme
+ x = 5"""
+ self.check(b, a)
+
+ def test_varargs(self):
+ b = """
+ def foo(((a, b), c), d, *vargs, **kwargs) -> z:
+ x = 5"""
+
+ a = """
+ def foo(xxx_todo_changeme, d, *vargs, **kwargs) -> z:
+ ((a, b), c) = xxx_todo_changeme
+ x = 5"""
+ self.check(b, a)
+
+ def test_multi_1(self):
+ b = """
+ def foo(((a, b), c), (d, e, f)) -> z:
+ x = 5"""
+
+ a = """
+ def foo(xxx_todo_changeme, xxx_todo_changeme1) -> z:
+ ((a, b), c) = xxx_todo_changeme
+ (d, e, f) = xxx_todo_changeme1
+ x = 5"""
+ self.check(b, a)
+
+ def test_multi_2(self):
+ b = """
+ def foo(x, ((a, b), c), d, (e, f, g), y) -> z:
+ x = 5"""
+
+ a = """
+ def foo(x, xxx_todo_changeme, d, xxx_todo_changeme1, y) -> z:
+ ((a, b), c) = xxx_todo_changeme
+ (e, f, g) = xxx_todo_changeme1
+ x = 5"""
+ self.check(b, a)
+
+ def test_docstring(self):
+ b = """
+ def foo(((a, b), c), (d, e, f)) -> z:
+ "foo foo foo foo"
+ x = 5"""
+
+ a = """
+ def foo(xxx_todo_changeme, xxx_todo_changeme1) -> z:
+ "foo foo foo foo"
+ ((a, b), c) = xxx_todo_changeme
+ (d, e, f) = xxx_todo_changeme1
+ x = 5"""
+ self.check(b, a)
+
+ def test_lambda_no_change(self):
+ s = """lambda x: x + 5"""
+ self.unchanged(s)
+
+ def test_lambda_parens_single_arg(self):
+ b = """lambda (x): x + 5"""
+ a = """lambda x: x + 5"""
+ self.check(b, a)
+
+ b = """lambda(x): x + 5"""
+ a = """lambda x: x + 5"""
+ self.check(b, a)
+
+ b = """lambda ((((x)))): x + 5"""
+ a = """lambda x: x + 5"""
+ self.check(b, a)
+
+ b = """lambda((((x)))): x + 5"""
+ a = """lambda x: x + 5"""
+ self.check(b, a)
+
+ def test_lambda_simple(self):
+ b = """lambda (x, y): x + f(y)"""
+ a = """lambda x_y: x_y[0] + f(x_y[1])"""
+ self.check(b, a)
+
+ b = """lambda(x, y): x + f(y)"""
+ a = """lambda x_y: x_y[0] + f(x_y[1])"""
+ self.check(b, a)
+
+ b = """lambda (((x, y))): x + f(y)"""
+ a = """lambda x_y: x_y[0] + f(x_y[1])"""
+ self.check(b, a)
+
+ b = """lambda(((x, y))): x + f(y)"""
+ a = """lambda x_y: x_y[0] + f(x_y[1])"""
+ self.check(b, a)
+
+ def test_lambda_one_tuple(self):
+ b = """lambda (x,): x + f(x)"""
+ a = """lambda x1: x1[0] + f(x1[0])"""
+ self.check(b, a)
+
+ b = """lambda (((x,))): x + f(x)"""
+ a = """lambda x1: x1[0] + f(x1[0])"""
+ self.check(b, a)
+
+ def test_lambda_simple_multi_use(self):
+ b = """lambda (x, y): x + x + f(x) + x"""
+ a = """lambda x_y: x_y[0] + x_y[0] + f(x_y[0]) + x_y[0]"""
+ self.check(b, a)
+
+ def test_lambda_simple_reverse(self):
+ b = """lambda (x, y): y + x"""
+ a = """lambda x_y: x_y[1] + x_y[0]"""
+ self.check(b, a)
+
+ def test_lambda_nested(self):
+ b = """lambda (x, (y, z)): x + y + z"""
+ a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + x_y_z[1][1]"""
+ self.check(b, a)
+
+ b = """lambda (((x, (y, z)))): x + y + z"""
+ a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + x_y_z[1][1]"""
+ self.check(b, a)
+
+ def test_lambda_nested_multi_use(self):
+ b = """lambda (x, (y, z)): x + y + f(y)"""
+ a = """lambda x_y_z: x_y_z[0] + x_y_z[1][0] + f(x_y_z[1][0])"""
+ self.check(b, a)
+
+class Test_methodattrs(FixerTestCase):
+ fixer = "methodattrs"
+
+ attrs = ["func", "self", "class"]
+
+ def test(self):
+ for attr in self.attrs:
+ b = "a.im_%s" % attr
+ if attr == "class":
+ a = "a.__self__.__class__"
+ else:
+ a = "a.__%s__" % attr
+ self.check(b, a)
+
+ b = "self.foo.im_%s.foo_bar" % attr
+ if attr == "class":
+ a = "self.foo.__self__.__class__.foo_bar"
+ else:
+ a = "self.foo.__%s__.foo_bar" % attr
+ self.check(b, a)
+
+ def test_unchanged(self):
+ for attr in self.attrs:
+ s = "foo(im_%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_next(FixerTestCase):
+ fixer = "next"
+
+ def test_1(self):
+ b = """it.next()"""
+ a = """next(it)"""
+ self.check(b, a)
+
+ def test_2(self):
+ b = """a.b.c.d.next()"""
+ a = """next(a.b.c.d)"""
+ self.check(b, a)
+
+ def test_3(self):
+ b = """(a + b).next()"""
+ a = """next((a + b))"""
+ self.check(b, a)
+
+ def test_4(self):
+ b = """a().next()"""
+ a = """next(a())"""
+ self.check(b, a)
+
+ def test_5(self):
+ b = """a().next() + b"""
+ a = """next(a()) + b"""
+ self.check(b, a)
+
+ def test_6(self):
+ b = """c( a().next() + b)"""
+ a = """c( next(a()) + b)"""
+ self.check(b, a)
+
+ def test_prefix_preservation_1(self):
+ b = """
+ for a in b:
+ foo(a)
+ a.next()
+ """
+ a = """
+ for a in b:
+ foo(a)
+ next(a)
+ """
+ self.check(b, a)
+
+ def test_prefix_preservation_2(self):
+ b = """
+ for a in b:
+ foo(a) # abc
+ # def
+ a.next()
+ """
+ a = """
+ for a in b:
+ foo(a) # abc
+ # def
+ next(a)
+ """
+ self.check(b, a)
+
+ def test_prefix_preservation_3(self):
+ b = """
+ next = 5
+ for a in b:
+ foo(a)
+ a.next()
+ """
+ a = """
+ next = 5
+ for a in b:
+ foo(a)
+ a.__next__()
+ """
+ self.check(b, a, ignore_warnings=True)
+
+ def test_prefix_preservation_4(self):
+ b = """
+ next = 5
+ for a in b:
+ foo(a) # abc
+ # def
+ a.next()
+ """
+ a = """
+ next = 5
+ for a in b:
+ foo(a) # abc
+ # def
+ a.__next__()
+ """
+ self.check(b, a, ignore_warnings=True)
+
+ def test_prefix_preservation_5(self):
+ b = """
+ next = 5
+ for a in b:
+ foo(foo(a), # abc
+ a.next())
+ """
+ a = """
+ next = 5
+ for a in b:
+ foo(foo(a), # abc
+ a.__next__())
+ """
+ self.check(b, a, ignore_warnings=True)
+
+ def test_prefix_preservation_6(self):
+ b = """
+ for a in b:
+ foo(foo(a), # abc
+ a.next())
+ """
+ a = """
+ for a in b:
+ foo(foo(a), # abc
+ next(a))
+ """
+ self.check(b, a)
+
+ def test_method_1(self):
+ b = """
+ class A:
+ def next(self):
+ pass
+ """
+ a = """
+ class A:
+ def __next__(self):
+ pass
+ """
+ self.check(b, a)
+
+ def test_method_2(self):
+ b = """
+ class A(object):
+ def next(self):
+ pass
+ """
+ a = """
+ class A(object):
+ def __next__(self):
+ pass
+ """
+ self.check(b, a)
+
+ def test_method_3(self):
+ b = """
+ class A:
+ def next(x):
+ pass
+ """
+ a = """
+ class A:
+ def __next__(x):
+ pass
+ """
+ self.check(b, a)
+
+ def test_method_4(self):
+ b = """
+ class A:
+ def __init__(self, foo):
+ self.foo = foo
+
+ def next(self):
+ pass
+
+ def __iter__(self):
+ return self
+ """
+ a = """
+ class A:
+ def __init__(self, foo):
+ self.foo = foo
+
+ def __next__(self):
+ pass
+
+ def __iter__(self):
+ return self
+ """
+ self.check(b, a)
+
+ def test_method_unchanged(self):
+ s = """
+ class A:
+ def next(self, a, b):
+ pass
+ """
+ self.unchanged(s)
+
+ def test_shadowing_assign_simple(self):
+ s = """
+ next = foo
+
+ class A:
+ def next(self, a, b):
+ pass
+ """
+ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
+
+ def test_shadowing_assign_tuple_1(self):
+ s = """
+ (next, a) = foo
+
+ class A:
+ def next(self, a, b):
+ pass
+ """
+ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
+
+ def test_shadowing_assign_tuple_2(self):
+ s = """
+ (a, (b, (next, c)), a) = foo
+
+ class A:
+ def next(self, a, b):
+ pass
+ """
+ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
+
+ def test_shadowing_assign_list_1(self):
+ s = """
+ [next, a] = foo
+
+ class A:
+ def next(self, a, b):
+ pass
+ """
+ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
+
+ def test_shadowing_assign_list_2(self):
+ s = """
+ [a, [b, [next, c]], a] = foo
+
+ class A:
+ def next(self, a, b):
+ pass
+ """
+ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
+
+ def test_builtin_assign(self):
+ s = """
+ def foo():
+ __builtin__.next = foo
+
+ class A:
+ def next(self, a, b):
+ pass
+ """
+ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
+
+ def test_builtin_assign_in_tuple(self):
+ s = """
+ def foo():
+ (a, __builtin__.next) = foo
+
+ class A:
+ def next(self, a, b):
+ pass
+ """
+ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
+
+ def test_builtin_assign_in_list(self):
+ s = """
+ def foo():
+ [a, __builtin__.next] = foo
+
+ class A:
+ def next(self, a, b):
+ pass
+ """
+ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
+
+ def test_assign_to_next(self):
+ s = """
+ def foo():
+ A.next = foo
+
+ class A:
+ def next(self, a, b):
+ pass
+ """
+ self.unchanged(s)
+
+ def test_assign_to_next_in_tuple(self):
+ s = """
+ def foo():
+ (a, A.next) = foo
+
+ class A:
+ def next(self, a, b):
+ pass
+ """
+ self.unchanged(s)
+
+ def test_assign_to_next_in_list(self):
+ s = """
+ def foo():
+ [a, A.next] = foo
+
+ class A:
+ def next(self, a, b):
+ pass
+ """
+ self.unchanged(s)
+
+ def test_shadowing_import_1(self):
+ s = """
+ import foo.bar as next
+
+ class A:
+ def next(self, a, b):
+ pass
+ """
+ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
+
+ def test_shadowing_import_2(self):
+ s = """
+ import bar, bar.foo as next
+
+ class A:
+ def next(self, a, b):
+ pass
+ """
+ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
+
+ def test_shadowing_import_3(self):
+ s = """
+ import bar, bar.foo as next, baz
+
+ class A:
+ def next(self, a, b):
+ pass
+ """
+ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
+
+ def test_shadowing_import_from_1(self):
+ s = """
+ from x import next
+
+ class A:
+ def next(self, a, b):
+ pass
+ """
+ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
+
+ def test_shadowing_import_from_2(self):
+ s = """
+ from x.a import next
+
+ class A:
+ def next(self, a, b):
+ pass
+ """
+ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
+
+ def test_shadowing_import_from_3(self):
+ s = """
+ from x import a, next, b
+
+ class A:
+ def next(self, a, b):
+ pass
+ """
+ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
+
+ def test_shadowing_import_from_4(self):
+ s = """
+ from x.a import a, next, b
+
+ class A:
+ def next(self, a, b):
+ pass
+ """
+ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
+
+ def test_shadowing_funcdef_1(self):
+ s = """
+ def next(a):
+ pass
+
+ class A:
+ def next(self, a, b):
+ pass
+ """
+ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
+
+ def test_shadowing_funcdef_2(self):
+ b = """
+ def next(a):
+ pass
+
+ class A:
+ def next(self):
+ pass
+
+ it.next()
+ """
+ a = """
+ def next(a):
+ pass
+
+ class A:
+ def __next__(self):
+ pass
+
+ it.__next__()
+ """
+ self.warns(b, a, "Calls to builtin next() possibly shadowed")
+
+ def test_shadowing_global_1(self):
+ s = """
+ def f():
+ global next
+ next = 5
+ """
+ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
+
+ def test_shadowing_global_2(self):
+ s = """
+ def f():
+ global a, next, b
+ next = 5
+ """
+ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
+
+ def test_shadowing_for_simple(self):
+ s = """
+ for next in it():
+ pass
+
+ b = 5
+ c = 6
+ """
+ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
+
+ def test_shadowing_for_tuple_1(self):
+ s = """
+ for next, b in it():
+ pass
+
+ b = 5
+ c = 6
+ """
+ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
+
+ def test_shadowing_for_tuple_2(self):
+ s = """
+ for a, (next, c), b in it():
+ pass
+
+ b = 5
+ c = 6
+ """
+ self.warns_unchanged(s, "Calls to builtin next() possibly shadowed")
+
+ def test_noncall_access_1(self):
+ b = """gnext = g.next"""
+ a = """gnext = g.__next__"""
+ self.check(b, a)
+
+ def test_noncall_access_2(self):
+ b = """f(g.next + 5)"""
+ a = """f(g.__next__ + 5)"""
+ self.check(b, a)
+
+ def test_noncall_access_3(self):
+ b = """f(g().next + 5)"""
+ a = """f(g().__next__ + 5)"""
+ self.check(b, a)
+
+class Test_nonzero(FixerTestCase):
+ fixer = "nonzero"
+
+ def test_1(self):
+ b = """
+ class A:
+ def __nonzero__(self):
+ pass
+ """
+ a = """
+ class A:
+ def __bool__(self):
+ pass
+ """
+ self.check(b, a)
+
+ def test_2(self):
+ b = """
+ class A(object):
+ def __nonzero__(self):
+ pass
+ """
+ a = """
+ class A(object):
+ def __bool__(self):
+ pass
+ """
+ self.check(b, a)
+
+ def test_unchanged_1(self):
+ s = """
+ class A(object):
+ def __bool__(self):
+ pass
+ """
+ self.unchanged(s)
+
+ def test_unchanged_2(self):
+ s = """
+ class A(object):
+ def __nonzero__(self, a):
+ pass
+ """
+ self.unchanged(s)
+
+ def test_unchanged_func(self):
+ s = """
+ def __nonzero__(self):
+ pass
+ """
+ self.unchanged(s)
+
+class Test_numliterals(FixerTestCase):
+ fixer = "numliterals"
+
+ def test_octal_1(self):
+ b = """0755"""
+ a = """0o755"""
+ self.check(b, a)
+
+ def test_long_int_1(self):
+ b = """a = 12L"""
+ a = """a = 12"""
+ self.check(b, a)
+
+ def test_long_int_2(self):
+ b = """a = 12l"""
+ a = """a = 12"""
+ self.check(b, a)
+
+ def test_long_hex(self):
+ b = """b = 0x12l"""
+ a = """b = 0x12"""
+ self.check(b, a)
+
+ def test_unchanged_int(self):
+ s = """5"""
+ self.unchanged(s)
+
+ def test_unchanged_float(self):
+ s = """5.0"""
+ self.unchanged(s)
+
+ def test_unchanged_octal(self):
+ s = """0o755"""
+ self.unchanged(s)
+
+ def test_unchanged_hex(self):
+ s = """0xABC"""
+ self.unchanged(s)
+
+ def test_unchanged_exp(self):
+ s = """5.0e10"""
+ self.unchanged(s)
+
+ def test_unchanged_complex_int(self):
+ s = """5 + 4j"""
+ self.unchanged(s)
+
+ def test_unchanged_complex_float(self):
+ s = """5.4 + 4.9j"""
+ self.unchanged(s)
+
+ def test_unchanged_complex_bare(self):
+ s = """4j"""
+ self.unchanged(s)
+ s = """4.4j"""
+ self.unchanged(s)
+
+class Test_renames(FixerTestCase):
+ fixer = "renames"
+
+ modules = {"sys": ("maxint", "maxsize"),
+ }
+
+ def test_import_from(self):
+ for mod, (old, new) in self.modules.items():
+ b = "from %s import %s" % (mod, old)
+ a = "from %s import %s" % (mod, new)
+ self.check(b, a)
+
+ s = "from foo import %s" % old
+ self.unchanged(s)
+
+ def test_import_from_as(self):
+ for mod, (old, new) in self.modules.items():
+ b = "from %s import %s as foo_bar" % (mod, old)
+ a = "from %s import %s as foo_bar" % (mod, new)
+ self.check(b, a)
+
+ def test_import_module_usage(self):
+ for mod, (old, new) in self.modules.items():
+ b = """
+ import %s
+ foo(%s, %s.%s)
+ """ % (mod, mod, mod, old)
+ a = """
+ import %s
+ foo(%s, %s.%s)
+ """ % (mod, mod, mod, new)
+ self.check(b, a)
+
+ def XXX_test_from_import_usage(self):
+ # not implemented yet
+ for mod, (old, new) in self.modules.items():
+ b = """
+ from %s import %s
+ foo(%s, %s)
+ """ % (mod, old, mod, old)
+ a = """
+ from %s import %s
+ foo(%s, %s)
+ """ % (mod, new, mod, new)
+ self.check(b, a)
+
+
+class Test_unicode(FixerTestCase):
+ fixer = "unicode"
+
+ def test_unicode_call(self):
+ b = """unicode(x, y, z)"""
+ a = """str(x, y, z)"""
+ self.check(b, a)
+
+ def test_unicode_literal_1(self):
+ b = '''u"x"'''
+ a = '''"x"'''
+ self.check(b, a)
+
+ def test_unicode_literal_2(self):
+ b = """ur'x'"""
+ a = """r'x'"""
+ self.check(b, a)
+
+ def test_unicode_literal_3(self):
+ b = """UR'''x'''"""
+ a = """R'''x'''"""
+ self.check(b, a)
+
+class Test_callable(FixerTestCase):
+ fixer = "callable"
+
+ def test_prefix_preservation(self):
+ b = """callable( x)"""
+ a = """hasattr( x, '__call__')"""
+ self.check(b, a)
+
+ b = """if callable(x): pass"""
+ a = """if hasattr(x, '__call__'): pass"""
+ self.check(b, a)
+
+ def test_callable_call(self):
+ b = """callable(x)"""
+ a = """hasattr(x, '__call__')"""
+ self.check(b, a)
+
+ def test_callable_should_not_change(self):
+ a = """callable(*x)"""
+ self.unchanged(a)
+
+ a = """callable(x, y)"""
+ self.unchanged(a)
+
+ a = """callable(x, kw=y)"""
+ self.unchanged(a)
+
+ a = """callable()"""
+ self.unchanged(a)
+
+class Test_filter(FixerTestCase):
+ fixer = "filter"
+
+ def test_prefix_preservation(self):
+ b = """x = filter( foo, 'abc' )"""
+ a = """x = list(filter( foo, 'abc' ))"""
+ self.check(b, a)
+
+ b = """x = filter( None , 'abc' )"""
+ a = """x = [_f for _f in 'abc' if _f]"""
+ self.check(b, a)
+
+ def test_filter_basic(self):
+ b = """x = filter(None, 'abc')"""
+ a = """x = [_f for _f in 'abc' if _f]"""
+ self.check(b, a)
+
+ b = """x = len(filter(f, 'abc'))"""
+ a = """x = len(list(filter(f, 'abc')))"""
+ self.check(b, a)
+
+ b = """x = filter(lambda x: x%2 == 0, range(10))"""
+ a = """x = [x for x in range(10) if x%2 == 0]"""
+ self.check(b, a)
+
+ # Note the parens around x
+ b = """x = filter(lambda (x): x%2 == 0, range(10))"""
+ a = """x = [x for x in range(10) if x%2 == 0]"""
+ self.check(b, a)
+
+ # XXX This (rare) case is not supported
+## b = """x = filter(f, 'abc')[0]"""
+## a = """x = list(filter(f, 'abc'))[0]"""
+## self.check(b, a)
+
+ def test_filter_nochange(self):
+ a = """b.join(filter(f, 'abc'))"""
+ self.unchanged(a)
+ a = """(a + foo(5)).join(filter(f, 'abc'))"""
+ self.unchanged(a)
+ a = """iter(filter(f, 'abc'))"""
+ self.unchanged(a)
+ a = """list(filter(f, 'abc'))"""
+ self.unchanged(a)
+ a = """list(filter(f, 'abc'))[0]"""
+ self.unchanged(a)
+ a = """set(filter(f, 'abc'))"""
+ self.unchanged(a)
+ a = """set(filter(f, 'abc')).pop()"""
+ self.unchanged(a)
+ a = """tuple(filter(f, 'abc'))"""
+ self.unchanged(a)
+ a = """any(filter(f, 'abc'))"""
+ self.unchanged(a)
+ a = """all(filter(f, 'abc'))"""
+ self.unchanged(a)
+ a = """sum(filter(f, 'abc'))"""
+ self.unchanged(a)
+ a = """sorted(filter(f, 'abc'))"""
+ self.unchanged(a)
+ a = """sorted(filter(f, 'abc'), key=blah)"""
+ self.unchanged(a)
+ a = """sorted(filter(f, 'abc'), key=blah)[0]"""
+ self.unchanged(a)
+ a = """for i in filter(f, 'abc'): pass"""
+ self.unchanged(a)
+ a = """[x for x in filter(f, 'abc')]"""
+ self.unchanged(a)
+ a = """(x for x in filter(f, 'abc'))"""
+ self.unchanged(a)
+
+ def test_future_builtins(self):
+ a = "from future_builtins import spam, filter; filter(f, 'ham')"
+ self.unchanged(a)
+
+ b = """from future_builtins import spam; x = filter(f, 'abc')"""
+ a = """from future_builtins import spam; x = list(filter(f, 'abc'))"""
+ self.check(b, a)
+
+ a = "from future_builtins import *; filter(f, 'ham')"
+ self.unchanged(a)
+
+class Test_map(FixerTestCase):
+ fixer = "map"
+
+ def check(self, b, a):
+ self.unchanged("from future_builtins import map; " + b, a)
+ FixerTestCase.check(self, b, a)
+
+ def test_prefix_preservation(self):
+ b = """x = map( f, 'abc' )"""
+ a = """x = list(map( f, 'abc' ))"""
+ self.check(b, a)
+
+ def test_trailing_comment(self):
+ b = """x = map(f, 'abc') # foo"""
+ a = """x = list(map(f, 'abc')) # foo"""
+ self.check(b, a)
+
+ def test_map_basic(self):
+ b = """x = map(f, 'abc')"""
+ a = """x = list(map(f, 'abc'))"""
+ self.check(b, a)
+
+ b = """x = len(map(f, 'abc', 'def'))"""
+ a = """x = len(list(map(f, 'abc', 'def')))"""
+ self.check(b, a)
+
+ b = """x = map(None, 'abc')"""
+ a = """x = list('abc')"""
+ self.check(b, a)
+
+ b = """x = map(None, 'abc', 'def')"""
+ a = """x = list(map(None, 'abc', 'def'))"""
+ self.check(b, a)
+
+ b = """x = map(lambda x: x+1, range(4))"""
+ a = """x = [x+1 for x in range(4)]"""
+ self.check(b, a)
+
+ # Note the parens around x
+ b = """x = map(lambda (x): x+1, range(4))"""
+ a = """x = [x+1 for x in range(4)]"""
+ self.check(b, a)
+
+ b = """
+ foo()
+ # foo
+ map(f, x)
+ """
+ a = """
+ foo()
+ # foo
+ list(map(f, x))
+ """
+ self.warns(b, a, "You should use a for loop here")
+
+ # XXX This (rare) case is not supported
+## b = """x = map(f, 'abc')[0]"""
+## a = """x = list(map(f, 'abc'))[0]"""
+## self.check(b, a)
+
+ def test_map_nochange(self):
+ a = """b.join(map(f, 'abc'))"""
+ self.unchanged(a)
+ a = """(a + foo(5)).join(map(f, 'abc'))"""
+ self.unchanged(a)
+ a = """iter(map(f, 'abc'))"""
+ self.unchanged(a)
+ a = """list(map(f, 'abc'))"""
+ self.unchanged(a)
+ a = """list(map(f, 'abc'))[0]"""
+ self.unchanged(a)
+ a = """set(map(f, 'abc'))"""
+ self.unchanged(a)
+ a = """set(map(f, 'abc')).pop()"""
+ self.unchanged(a)
+ a = """tuple(map(f, 'abc'))"""
+ self.unchanged(a)
+ a = """any(map(f, 'abc'))"""
+ self.unchanged(a)
+ a = """all(map(f, 'abc'))"""
+ self.unchanged(a)
+ a = """sum(map(f, 'abc'))"""
+ self.unchanged(a)
+ a = """sorted(map(f, 'abc'))"""
+ self.unchanged(a)
+ a = """sorted(map(f, 'abc'), key=blah)"""
+ self.unchanged(a)
+ a = """sorted(map(f, 'abc'), key=blah)[0]"""
+ self.unchanged(a)
+ a = """for i in map(f, 'abc'): pass"""
+ self.unchanged(a)
+ a = """[x for x in map(f, 'abc')]"""
+ self.unchanged(a)
+ a = """(x for x in map(f, 'abc'))"""
+ self.unchanged(a)
+
+ def test_future_builtins(self):
+ a = "from future_builtins import spam, map, eggs; map(f, 'ham')"
+ self.unchanged(a)
+
+ b = """from future_builtins import spam, eggs; x = map(f, 'abc')"""
+ a = """from future_builtins import spam, eggs; x = list(map(f, 'abc'))"""
+ self.check(b, a)
+
+ a = "from future_builtins import *; map(f, 'ham')"
+ self.unchanged(a)
+
+class Test_standarderror(FixerTestCase):
+ fixer = "standarderror"
+
+ def test(self):
+ b = """x = StandardError()"""
+ a = """x = Exception()"""
+ self.check(b, a)
+
+ b = """x = StandardError(a, b, c)"""
+ a = """x = Exception(a, b, c)"""
+ self.check(b, a)
+
+ b = """f(2 + StandardError(a, b, c))"""
+ a = """f(2 + Exception(a, b, c))"""
+ self.check(b, a)
+
+class Test_types(FixerTestCase):
+ fixer = "types"
+
+ def test_basic_types_convert(self):
+ b = """types.StringType"""
+ a = """bytes"""
+ self.check(b, a)
+
+ b = """types.DictType"""
+ a = """dict"""
+ self.check(b, a)
+
+ b = """types . IntType"""
+ a = """int"""
+ self.check(b, a)
+
+ b = """types.ListType"""
+ a = """list"""
+ self.check(b, a)
+
+ b = """types.LongType"""
+ a = """int"""
+ self.check(b, a)
+
+ b = """types.NoneType"""
+ a = """type(None)"""
+ self.check(b, a)
+
+class Test_idioms(FixerTestCase):
+ fixer = "idioms"
+
+ def test_while(self):
+ b = """while 1: foo()"""
+ a = """while True: foo()"""
+ self.check(b, a)
+
+ b = """while 1: foo()"""
+ a = """while True: foo()"""
+ self.check(b, a)
+
+ b = """
+ while 1:
+ foo()
+ """
+ a = """
+ while True:
+ foo()
+ """
+ self.check(b, a)
+
+ def test_while_unchanged(self):
+ s = """while 11: foo()"""
+ self.unchanged(s)
+
+ s = """while 0: foo()"""
+ self.unchanged(s)
+
+ s = """while foo(): foo()"""
+ self.unchanged(s)
+
+ s = """while []: foo()"""
+ self.unchanged(s)
+
+ def test_eq_simple(self):
+ b = """type(x) == T"""
+ a = """isinstance(x, T)"""
+ self.check(b, a)
+
+ b = """if type(x) == T: pass"""
+ a = """if isinstance(x, T): pass"""
+ self.check(b, a)
+
+ def test_eq_reverse(self):
+ b = """T == type(x)"""
+ a = """isinstance(x, T)"""
+ self.check(b, a)
+
+ b = """if T == type(x): pass"""
+ a = """if isinstance(x, T): pass"""
+ self.check(b, a)
+
+ def test_eq_expression(self):
+ b = """type(x+y) == d.get('T')"""
+ a = """isinstance(x+y, d.get('T'))"""
+ self.check(b, a)
+
+ b = """type( x + y) == d.get('T')"""
+ a = """isinstance(x + y, d.get('T'))"""
+ self.check(b, a)
+
+ def test_is_simple(self):
+ b = """type(x) is T"""
+ a = """isinstance(x, T)"""
+ self.check(b, a)
+
+ b = """if type(x) is T: pass"""
+ a = """if isinstance(x, T): pass"""
+ self.check(b, a)
+
+ def test_is_reverse(self):
+ b = """T is type(x)"""
+ a = """isinstance(x, T)"""
+ self.check(b, a)
+
+ b = """if T is type(x): pass"""
+ a = """if isinstance(x, T): pass"""
+ self.check(b, a)
+
+ def test_is_expression(self):
+ b = """type(x+y) is d.get('T')"""
+ a = """isinstance(x+y, d.get('T'))"""
+ self.check(b, a)
+
+ b = """type( x + y) is d.get('T')"""
+ a = """isinstance(x + y, d.get('T'))"""
+ self.check(b, a)
+
+ def test_is_not_simple(self):
+ b = """type(x) is not T"""
+ a = """not isinstance(x, T)"""
+ self.check(b, a)
+
+ b = """if type(x) is not T: pass"""
+ a = """if not isinstance(x, T): pass"""
+ self.check(b, a)
+
+ def test_is_not_reverse(self):
+ b = """T is not type(x)"""
+ a = """not isinstance(x, T)"""
+ self.check(b, a)
+
+ b = """if T is not type(x): pass"""
+ a = """if not isinstance(x, T): pass"""
+ self.check(b, a)
+
+ def test_is_not_expression(self):
+ b = """type(x+y) is not d.get('T')"""
+ a = """not isinstance(x+y, d.get('T'))"""
+ self.check(b, a)
+
+ b = """type( x + y) is not d.get('T')"""
+ a = """not isinstance(x + y, d.get('T'))"""
+ self.check(b, a)
+
+ def test_ne_simple(self):
+ b = """type(x) != T"""
+ a = """not isinstance(x, T)"""
+ self.check(b, a)
+
+ b = """if type(x) != T: pass"""
+ a = """if not isinstance(x, T): pass"""
+ self.check(b, a)
+
+ def test_ne_reverse(self):
+ b = """T != type(x)"""
+ a = """not isinstance(x, T)"""
+ self.check(b, a)
+
+ b = """if T != type(x): pass"""
+ a = """if not isinstance(x, T): pass"""
+ self.check(b, a)
+
+ def test_ne_expression(self):
+ b = """type(x+y) != d.get('T')"""
+ a = """not isinstance(x+y, d.get('T'))"""
+ self.check(b, a)
+
+ b = """type( x + y) != d.get('T')"""
+ a = """not isinstance(x + y, d.get('T'))"""
+ self.check(b, a)
+
+ def test_type_unchanged(self):
+ a = """type(x).__name__"""
+ self.unchanged(a)
+
+ def test_sort_list_call(self):
+ b = """
+ v = list(t)
+ v.sort()
+ foo(v)
+ """
+ a = """
+ v = sorted(t)
+ foo(v)
+ """
+ self.check(b, a)
+
+ b = """
+ v = list(foo(b) + d)
+ v.sort()
+ foo(v)
+ """
+ a = """
+ v = sorted(foo(b) + d)
+ foo(v)
+ """
+ self.check(b, a)
+
+ b = """
+ while x:
+ v = list(t)
+ v.sort()
+ foo(v)
+ """
+ a = """
+ while x:
+ v = sorted(t)
+ foo(v)
+ """
+ self.check(b, a)
+
+ b = """
+ v = list(t)
+ # foo
+ v.sort()
+ foo(v)
+ """
+ a = """
+ v = sorted(t)
+ # foo
+ foo(v)
+ """
+ self.check(b, a)
+
+ b = r"""
+ v = list( t)
+ v.sort()
+ foo(v)
+ """
+ a = r"""
+ v = sorted( t)
+ foo(v)
+ """
+ self.check(b, a)
+
+ def test_sort_simple_expr(self):
+ b = """
+ v = t
+ v.sort()
+ foo(v)
+ """
+ a = """
+ v = sorted(t)
+ foo(v)
+ """
+ self.check(b, a)
+
+ b = """
+ v = foo(b)
+ v.sort()
+ foo(v)
+ """
+ a = """
+ v = sorted(foo(b))
+ foo(v)
+ """
+ self.check(b, a)
+
+ b = """
+ v = b.keys()
+ v.sort()
+ foo(v)
+ """
+ a = """
+ v = sorted(b.keys())
+ foo(v)
+ """
+ self.check(b, a)
+
+ b = """
+ v = foo(b) + d
+ v.sort()
+ foo(v)
+ """
+ a = """
+ v = sorted(foo(b) + d)
+ foo(v)
+ """
+ self.check(b, a)
+
+ b = """
+ while x:
+ v = t
+ v.sort()
+ foo(v)
+ """
+ a = """
+ while x:
+ v = sorted(t)
+ foo(v)
+ """
+ self.check(b, a)
+
+ b = """
+ v = t
+ # foo
+ v.sort()
+ foo(v)
+ """
+ a = """
+ v = sorted(t)
+ # foo
+ foo(v)
+ """
+ self.check(b, a)
+
+ b = r"""
+ v = t
+ v.sort()
+ foo(v)
+ """
+ a = r"""
+ v = sorted(t)
+ foo(v)
+ """
+ self.check(b, a)
+
+ def test_sort_unchanged(self):
+ s = """
+ v = list(t)
+ w.sort()
+ foo(w)
+ """
+ self.unchanged(s)
+
+ s = """
+ v = list(t)
+ v.sort(u)
+ foo(v)
+ """
+ self.unchanged(s)
+
+
+class Test_basestring(FixerTestCase):
+ fixer = "basestring"
+
+ def test_basestring(self):
+ b = """isinstance(x, basestring)"""
+ a = """isinstance(x, str)"""
+ self.check(b, a)
+
+
+class Test_buffer(FixerTestCase):
+ fixer = "buffer"
+
+ def test_buffer(self):
+ b = """x = buffer(y)"""
+ a = """x = memoryview(y)"""
+ self.check(b, a)
+
+class Test_future(FixerTestCase):
+ fixer = "future"
+
+ def test_future(self):
+ b = """from __future__ import braces"""
+ a = """"""
+ self.check(b, a)
+
+class Test_itertools(FixerTestCase):
+ fixer = "itertools"
+
+ def checkall(self, before, after):
+ # Because we need to check with and without the itertools prefix
+ # and on each of the three functions, these loops make it all
+ # much easier
+ for i in ('itertools.', ''):
+ for f in ('map', 'filter', 'zip'):
+ b = before %(i+'i'+f)
+ a = after %(f)
+ self.check(b, a)
+
+ def test_0(self):
+ # A simple example -- test_1 covers exactly the same thing,
+ # but it's not quite as clear.
+ b = "itertools.izip(a, b)"
+ a = "zip(a, b)"
+ self.check(b, a)
+
+ def test_1(self):
+ b = """%s(f, a)"""
+ a = """%s(f, a)"""
+ self.checkall(b, a)
+
+ def test_2(self):
+ b = """itertools.ifilterfalse(a, b)"""
+ a = """itertools.filterfalse(a, b)"""
+ self.check(b, a)
+
+ def test_4(self):
+ b = """ifilterfalse(a, b)"""
+ a = """filterfalse(a, b)"""
+ self.check(b, a)
+
+ def test_space_1(self):
+ b = """ %s(f, a)"""
+ a = """ %s(f, a)"""
+ self.checkall(b, a)
+
+ def test_space_2(self):
+ b = """ itertools.ifilterfalse(a, b)"""
+ a = """ itertools.filterfalse(a, b)"""
+ self.check(b, a)
+
+
+if __name__ == "__main__":
+ import __main__
+ support.run_all_tests(__main__)
diff --git a/Lib/lib2to3/tests/test_parser.py b/Lib/lib2to3/tests/test_parser.py
new file mode 100644
index 0000000..33ddc92
--- /dev/null
+++ b/Lib/lib2to3/tests/test_parser.py
@@ -0,0 +1,202 @@
+#!/usr/bin/env python2.5
+"""Test suite for 2to3's parser and grammar files.
+
+This is the place to add tests for changes to 2to3's grammar, such as those
+merging the grammars for Python 2 and 3. In addition to specific tests for
+parts of the grammar we've changed, we also make sure we can parse the
+test_grammar.py files from both Python 2 and Python 3.
+"""
+# Author: Collin Winter
+
+# Testing imports
+from . import support
+from .support import driver, test_dir
+
+# Python imports
+import os
+import os.path
+
+# Local imports
+from ..pgen2.parse import ParseError
+
+
+class GrammarTest(support.TestCase):
+ def validate(self, code):
+ support.parse_string(code)
+
+ def invalid_syntax(self, code):
+ try:
+ self.validate(code)
+ except ParseError:
+ pass
+ else:
+ raise AssertionError("Syntax shouldn't have been valid")
+
+
+class TestRaiseChanges(GrammarTest):
+ def test_2x_style_1(self):
+ self.validate("raise")
+
+ def test_2x_style_2(self):
+ self.validate("raise E, V")
+
+ def test_2x_style_3(self):
+ self.validate("raise E, V, T")
+
+ def test_2x_style_invalid_1(self):
+ self.invalid_syntax("raise E, V, T, Z")
+
+ def test_3x_style(self):
+ self.validate("raise E1 from E2")
+
+ def test_3x_style_invalid_1(self):
+ self.invalid_syntax("raise E, V from E1")
+
+ def test_3x_style_invalid_2(self):
+ self.invalid_syntax("raise E from E1, E2")
+
+ def test_3x_style_invalid_3(self):
+ self.invalid_syntax("raise from E1, E2")
+
+ def test_3x_style_invalid_4(self):
+ self.invalid_syntax("raise E from")
+
+
+# Adapated from Python 3's Lib/test/test_grammar.py:GrammarTests.testFuncdef
+class TestFunctionAnnotations(GrammarTest):
+ def test_1(self):
+ self.validate("""def f(x) -> list: pass""")
+
+ def test_2(self):
+ self.validate("""def f(x:int): pass""")
+
+ def test_3(self):
+ self.validate("""def f(*x:str): pass""")
+
+ def test_4(self):
+ self.validate("""def f(**x:float): pass""")
+
+ def test_5(self):
+ self.validate("""def f(x, y:1+2): pass""")
+
+ def test_6(self):
+ self.validate("""def f(a, (b:1, c:2, d)): pass""")
+
+ def test_7(self):
+ self.validate("""def f(a, (b:1, c:2, d), e:3=4, f=5, *g:6): pass""")
+
+ def test_8(self):
+ s = """def f(a, (b:1, c:2, d), e:3=4, f=5,
+ *g:6, h:7, i=8, j:9=10, **k:11) -> 12: pass"""
+ self.validate(s)
+
+
+class TestExcept(GrammarTest):
+ def test_new(self):
+ s = """
+ try:
+ x
+ except E as N:
+ y"""
+ self.validate(s)
+
+ def test_old(self):
+ s = """
+ try:
+ x
+ except E, N:
+ y"""
+ self.validate(s)
+
+
+# Adapted from Python 3's Lib/test/test_grammar.py:GrammarTests.testAtoms
+class TestSetLiteral(GrammarTest):
+ def test_1(self):
+ self.validate("""x = {'one'}""")
+
+ def test_2(self):
+ self.validate("""x = {'one', 1,}""")
+
+ def test_3(self):
+ self.validate("""x = {'one', 'two', 'three'}""")
+
+ def test_4(self):
+ self.validate("""x = {2, 3, 4,}""")
+
+
+class TestNumericLiterals(GrammarTest):
+ def test_new_octal_notation(self):
+ self.validate("""0o7777777777777""")
+ self.invalid_syntax("""0o7324528887""")
+
+ def test_new_binary_notation(self):
+ self.validate("""0b101010""")
+ self.invalid_syntax("""0b0101021""")
+
+
+class TestClassDef(GrammarTest):
+ def test_new_syntax(self):
+ self.validate("class B(t=7): pass")
+ self.validate("class B(t, *args): pass")
+ self.validate("class B(t, **kwargs): pass")
+ self.validate("class B(t, *args, **kwargs): pass")
+ self.validate("class B(t, y=9, *args, **kwargs): pass")
+
+
+class TestParserIdempotency(support.TestCase):
+
+ """A cut-down version of pytree_idempotency.py."""
+
+ def test_all_project_files(self):
+ for filepath in support.all_project_files():
+ print "Parsing %s..." % filepath
+ tree = driver.parse_file(filepath, debug=True)
+ if diff(filepath, tree):
+ self.fail("Idempotency failed: %s" % filepath)
+
+
+class TestLiterals(GrammarTest):
+
+ def test_multiline_bytes_literals(self):
+ s = """
+ md5test(b"\xaa" * 80,
+ (b"Test Using Larger Than Block-Size Key "
+ b"and Larger Than One Block-Size Data"),
+ "6f630fad67cda0ee1fb1f562db3aa53e")
+ """
+ self.validate(s)
+
+ def test_multiline_bytes_tripquote_literals(self):
+ s = '''
+ b"""
+ <?xml version="1.0" encoding="UTF-8"?>
+ <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN">
+ """
+ '''
+ self.validate(s)
+
+ def test_multiline_str_literals(self):
+ s = """
+ md5test("\xaa" * 80,
+ ("Test Using Larger Than Block-Size Key "
+ "and Larger Than One Block-Size Data"),
+ "6f630fad67cda0ee1fb1f562db3aa53e")
+ """
+ self.validate(s)
+
+
+def diff(fn, tree):
+ f = open("@", "w")
+ try:
+ f.write(str(tree))
+ finally:
+ f.close()
+ try:
+ return os.system("diff -u %s @" % fn)
+ finally:
+ os.remove("@")
+
+
+if __name__ == "__main__":
+ import __main__
+ support.run_all_tests(__main__)
diff --git a/Lib/lib2to3/tests/test_pytree.py b/Lib/lib2to3/tests/test_pytree.py
new file mode 100755
index 0000000..7a94423
--- /dev/null
+++ b/Lib/lib2to3/tests/test_pytree.py
@@ -0,0 +1,440 @@
+#!/usr/bin/env python2.5
+# Copyright 2006 Google, Inc. All Rights Reserved.
+# Licensed to PSF under a Contributor Agreement.
+
+"""Unit tests for pytree.py.
+
+NOTE: Please *don't* add doc strings to individual test methods!
+In verbose mode, printing of the module, class and method name is much
+more helpful than printing of (the first line of) the docstring,
+especially when debugging a test.
+"""
+
+# Testing imports
+from . import support
+
+# Local imports (XXX should become a package)
+from .. import pytree
+
+try:
+ sorted
+except NameError:
+ def sorted(lst):
+ l = list(lst)
+ l.sort()
+ return l
+
+class TestNodes(support.TestCase):
+
+ """Unit tests for nodes (Base, Leaf, Node)."""
+
+ def testBaseCantConstruct(self):
+ if __debug__:
+ # Test that instantiating Base() raises an AssertionError
+ self.assertRaises(AssertionError, pytree.Base)
+
+ def testLeaf(self):
+ l1 = pytree.Leaf(100, "foo")
+ self.assertEqual(l1.type, 100)
+ self.assertEqual(l1.value, "foo")
+
+ def testLeafRepr(self):
+ l1 = pytree.Leaf(100, "foo")
+ self.assertEqual(repr(l1), "Leaf(100, 'foo')")
+
+ def testLeafStr(self):
+ l1 = pytree.Leaf(100, "foo")
+ self.assertEqual(str(l1), "foo")
+ l2 = pytree.Leaf(100, "foo", context=(" ", (10, 1)))
+ self.assertEqual(str(l2), " foo")
+
+ def testLeafStrNumericValue(self):
+ # Make sure that the Leaf's value is stringified. Failing to
+ # do this can cause a TypeError in certain situations.
+ l1 = pytree.Leaf(2, 5)
+ l1.set_prefix("foo_")
+ self.assertEqual(str(l1), "foo_5")
+
+ def testLeafEq(self):
+ l1 = pytree.Leaf(100, "foo")
+ l2 = pytree.Leaf(100, "foo", context=(" ", (1, 0)))
+ self.assertEqual(l1, l2)
+ l3 = pytree.Leaf(101, "foo")
+ l4 = pytree.Leaf(100, "bar")
+ self.assertNotEqual(l1, l3)
+ self.assertNotEqual(l1, l4)
+
+ def testLeafPrefix(self):
+ l1 = pytree.Leaf(100, "foo")
+ self.assertEqual(l1.get_prefix(), "")
+ self.failIf(l1.was_changed)
+ l1.set_prefix(" ##\n\n")
+ self.assertEqual(l1.get_prefix(), " ##\n\n")
+ self.failUnless(l1.was_changed)
+
+ def testNode(self):
+ l1 = pytree.Leaf(100, "foo")
+ l2 = pytree.Leaf(200, "bar")
+ n1 = pytree.Node(1000, [l1, l2])
+ self.assertEqual(n1.type, 1000)
+ self.assertEqual(n1.children, [l1, l2])
+
+ def testNodeRepr(self):
+ l1 = pytree.Leaf(100, "foo")
+ l2 = pytree.Leaf(100, "bar", context=(" ", (1, 0)))
+ n1 = pytree.Node(1000, [l1, l2])
+ self.assertEqual(repr(n1),
+ "Node(1000, [%s, %s])" % (repr(l1), repr(l2)))
+
+ def testNodeStr(self):
+ l1 = pytree.Leaf(100, "foo")
+ l2 = pytree.Leaf(100, "bar", context=(" ", (1, 0)))
+ n1 = pytree.Node(1000, [l1, l2])
+ self.assertEqual(str(n1), "foo bar")
+
+ def testNodePrefix(self):
+ l1 = pytree.Leaf(100, "foo")
+ self.assertEqual(l1.get_prefix(), "")
+ n1 = pytree.Node(1000, [l1])
+ self.assertEqual(n1.get_prefix(), "")
+ n1.set_prefix(" ")
+ self.assertEqual(n1.get_prefix(), " ")
+ self.assertEqual(l1.get_prefix(), " ")
+
+ def testGetSuffix(self):
+ l1 = pytree.Leaf(100, "foo", prefix="a")
+ l2 = pytree.Leaf(100, "bar", prefix="b")
+ n1 = pytree.Node(1000, [l1, l2])
+
+ self.assertEqual(l1.get_suffix(), l2.get_prefix())
+ self.assertEqual(l2.get_suffix(), "")
+ self.assertEqual(n1.get_suffix(), "")
+
+ l3 = pytree.Leaf(100, "bar", prefix="c")
+ n2 = pytree.Node(1000, [n1, l3])
+
+ self.assertEqual(n1.get_suffix(), l3.get_prefix())
+ self.assertEqual(l3.get_suffix(), "")
+ self.assertEqual(n2.get_suffix(), "")
+
+ def testNodeEq(self):
+ n1 = pytree.Node(1000, ())
+ n2 = pytree.Node(1000, [], context=(" ", (1, 0)))
+ self.assertEqual(n1, n2)
+ n3 = pytree.Node(1001, ())
+ self.assertNotEqual(n1, n3)
+
+ def testNodeEqRecursive(self):
+ l1 = pytree.Leaf(100, "foo")
+ l2 = pytree.Leaf(100, "foo")
+ n1 = pytree.Node(1000, [l1])
+ n2 = pytree.Node(1000, [l2])
+ self.assertEqual(n1, n2)
+ l3 = pytree.Leaf(100, "bar")
+ n3 = pytree.Node(1000, [l3])
+ self.assertNotEqual(n1, n3)
+
+ def testReplace(self):
+ l1 = pytree.Leaf(100, "foo")
+ l2 = pytree.Leaf(100, "+")
+ l3 = pytree.Leaf(100, "bar")
+ n1 = pytree.Node(1000, [l1, l2, l3])
+ self.assertEqual(n1.children, [l1, l2, l3])
+ self.failUnless(isinstance(n1.children, list))
+ self.failIf(n1.was_changed)
+ l2new = pytree.Leaf(100, "-")
+ l2.replace(l2new)
+ self.assertEqual(n1.children, [l1, l2new, l3])
+ self.failUnless(isinstance(n1.children, list))
+ self.failUnless(n1.was_changed)
+
+ def testReplaceWithList(self):
+ l1 = pytree.Leaf(100, "foo")
+ l2 = pytree.Leaf(100, "+")
+ l3 = pytree.Leaf(100, "bar")
+ n1 = pytree.Node(1000, [l1, l2, l3])
+
+ l2.replace([pytree.Leaf(100, "*"), pytree.Leaf(100, "*")])
+ self.assertEqual(str(n1), "foo**bar")
+ self.failUnless(isinstance(n1.children, list))
+
+ def testPostOrder(self):
+ l1 = pytree.Leaf(100, "foo")
+ l2 = pytree.Leaf(100, "bar")
+ n1 = pytree.Node(1000, [l1, l2])
+ self.assertEqual(list(n1.post_order()), [l1, l2, n1])
+
+ def testPreOrder(self):
+ l1 = pytree.Leaf(100, "foo")
+ l2 = pytree.Leaf(100, "bar")
+ n1 = pytree.Node(1000, [l1, l2])
+ self.assertEqual(list(n1.pre_order()), [n1, l1, l2])
+
+ def testChangedLeaf(self):
+ l1 = pytree.Leaf(100, "f")
+ self.failIf(l1.was_changed)
+
+ l1.changed()
+ self.failUnless(l1.was_changed)
+
+ def testChangedNode(self):
+ l1 = pytree.Leaf(100, "f")
+ n1 = pytree.Node(1000, [l1])
+ self.failIf(n1.was_changed)
+
+ n1.changed()
+ self.failUnless(n1.was_changed)
+
+ def testChangedRecursive(self):
+ l1 = pytree.Leaf(100, "foo")
+ l2 = pytree.Leaf(100, "+")
+ l3 = pytree.Leaf(100, "bar")
+ n1 = pytree.Node(1000, [l1, l2, l3])
+ n2 = pytree.Node(1000, [n1])
+ self.failIf(l1.was_changed)
+ self.failIf(n1.was_changed)
+ self.failIf(n2.was_changed)
+
+ n1.changed()
+ self.failUnless(n1.was_changed)
+ self.failUnless(n2.was_changed)
+ self.failIf(l1.was_changed)
+
+ def testLeafConstructorPrefix(self):
+ for prefix in ("xyz_", ""):
+ l1 = pytree.Leaf(100, "self", prefix=prefix)
+ self.failUnless(str(l1), prefix + "self")
+ self.assertEqual(l1.get_prefix(), prefix)
+
+ def testNodeConstructorPrefix(self):
+ for prefix in ("xyz_", ""):
+ l1 = pytree.Leaf(100, "self")
+ l2 = pytree.Leaf(100, "foo", prefix="_")
+ n1 = pytree.Node(1000, [l1, l2], prefix=prefix)
+ self.failUnless(str(n1), prefix + "self_foo")
+ self.assertEqual(n1.get_prefix(), prefix)
+ self.assertEqual(l1.get_prefix(), prefix)
+ self.assertEqual(l2.get_prefix(), "_")
+
+ def testRemove(self):
+ l1 = pytree.Leaf(100, "foo")
+ l2 = pytree.Leaf(100, "foo")
+ n1 = pytree.Node(1000, [l1, l2])
+ n2 = pytree.Node(1000, [n1])
+
+ self.assertEqual(n1.remove(), 0)
+ self.assertEqual(n2.children, [])
+ self.assertEqual(l1.parent, n1)
+ self.assertEqual(n1.parent, None)
+ self.assertEqual(n2.parent, None)
+ self.failIf(n1.was_changed)
+ self.failUnless(n2.was_changed)
+
+ self.assertEqual(l2.remove(), 1)
+ self.assertEqual(l1.remove(), 0)
+ self.assertEqual(n1.children, [])
+ self.assertEqual(l1.parent, None)
+ self.assertEqual(n1.parent, None)
+ self.assertEqual(n2.parent, None)
+ self.failUnless(n1.was_changed)
+ self.failUnless(n2.was_changed)
+
+ def testRemoveParentless(self):
+ n1 = pytree.Node(1000, [])
+ n1.remove()
+ self.assertEqual(n1.parent, None)
+
+ l1 = pytree.Leaf(100, "foo")
+ l1.remove()
+ self.assertEqual(l1.parent, None)
+
+ def testNodeSetChild(self):
+ l1 = pytree.Leaf(100, "foo")
+ n1 = pytree.Node(1000, [l1])
+
+ l2 = pytree.Leaf(100, "bar")
+ n1.set_child(0, l2)
+ self.assertEqual(l1.parent, None)
+ self.assertEqual(l2.parent, n1)
+ self.assertEqual(n1.children, [l2])
+
+ n2 = pytree.Node(1000, [l1])
+ n2.set_child(0, n1)
+ self.assertEqual(l1.parent, None)
+ self.assertEqual(n1.parent, n2)
+ self.assertEqual(n2.parent, None)
+ self.assertEqual(n2.children, [n1])
+
+ self.assertRaises(IndexError, n1.set_child, 4, l2)
+ # I don't care what it raises, so long as it's an exception
+ self.assertRaises(Exception, n1.set_child, 0, list)
+
+ def testNodeInsertChild(self):
+ l1 = pytree.Leaf(100, "foo")
+ n1 = pytree.Node(1000, [l1])
+
+ l2 = pytree.Leaf(100, "bar")
+ n1.insert_child(0, l2)
+ self.assertEqual(l2.parent, n1)
+ self.assertEqual(n1.children, [l2, l1])
+
+ l3 = pytree.Leaf(100, "abc")
+ n1.insert_child(2, l3)
+ self.assertEqual(n1.children, [l2, l1, l3])
+
+ # I don't care what it raises, so long as it's an exception
+ self.assertRaises(Exception, n1.insert_child, 0, list)
+
+ def testNodeAppendChild(self):
+ n1 = pytree.Node(1000, [])
+
+ l1 = pytree.Leaf(100, "foo")
+ n1.append_child(l1)
+ self.assertEqual(l1.parent, n1)
+ self.assertEqual(n1.children, [l1])
+
+ l2 = pytree.Leaf(100, "bar")
+ n1.append_child(l2)
+ self.assertEqual(l2.parent, n1)
+ self.assertEqual(n1.children, [l1, l2])
+
+ # I don't care what it raises, so long as it's an exception
+ self.assertRaises(Exception, n1.append_child, list)
+
+ def testNodeNextSibling(self):
+ n1 = pytree.Node(1000, [])
+ n2 = pytree.Node(1000, [])
+ p1 = pytree.Node(1000, [n1, n2])
+
+ self.failUnless(n1.get_next_sibling() is n2)
+ self.assertEqual(n2.get_next_sibling(), None)
+ self.assertEqual(p1.get_next_sibling(), None)
+
+ def testLeafNextSibling(self):
+ l1 = pytree.Leaf(100, "a")
+ l2 = pytree.Leaf(100, "b")
+ p1 = pytree.Node(1000, [l1, l2])
+
+ self.failUnless(l1.get_next_sibling() is l2)
+ self.assertEqual(l2.get_next_sibling(), None)
+ self.assertEqual(p1.get_next_sibling(), None)
+
+
+class TestPatterns(support.TestCase):
+
+ """Unit tests for tree matching patterns."""
+
+ def testBasicPatterns(self):
+ # Build a tree
+ l1 = pytree.Leaf(100, "foo")
+ l2 = pytree.Leaf(100, "bar")
+ l3 = pytree.Leaf(100, "foo")
+ n1 = pytree.Node(1000, [l1, l2])
+ n2 = pytree.Node(1000, [l3])
+ root = pytree.Node(1000, [n1, n2])
+ # Build a pattern matching a leaf
+ pl = pytree.LeafPattern(100, "foo", name="pl")
+ r = {}
+ self.assertEqual(pl.match(root, results=r), False)
+ self.assertEqual(r, {})
+ self.assertEqual(pl.match(n1, results=r), False)
+ self.assertEqual(r, {})
+ self.assertEqual(pl.match(n2, results=r), False)
+ self.assertEqual(r, {})
+ self.assertEqual(pl.match(l1, results=r), True)
+ self.assertEqual(r, {"pl": l1})
+ r = {}
+ self.assertEqual(pl.match(l2, results=r), False)
+ self.assertEqual(r, {})
+ # Build a pattern matching a node
+ pn = pytree.NodePattern(1000, [pl], name="pn")
+ self.assertEqual(pn.match(root, results=r), False)
+ self.assertEqual(r, {})
+ self.assertEqual(pn.match(n1, results=r), False)
+ self.assertEqual(r, {})
+ self.assertEqual(pn.match(n2, results=r), True)
+ self.assertEqual(r, {"pn": n2, "pl": l3})
+ r = {}
+ self.assertEqual(pn.match(l1, results=r), False)
+ self.assertEqual(r, {})
+ self.assertEqual(pn.match(l2, results=r), False)
+ self.assertEqual(r, {})
+
+ def testWildcardPatterns(self):
+ # Build a tree for testing
+ l1 = pytree.Leaf(100, "foo")
+ l2 = pytree.Leaf(100, "bar")
+ l3 = pytree.Leaf(100, "foo")
+ n1 = pytree.Node(1000, [l1, l2])
+ n2 = pytree.Node(1000, [l3])
+ root = pytree.Node(1000, [n1, n2])
+ # Build a pattern
+ pl = pytree.LeafPattern(100, "foo", name="pl")
+ pn = pytree.NodePattern(1000, [pl], name="pn")
+ pw = pytree.WildcardPattern([[pn], [pl, pl]], name="pw")
+ r = {}
+ self.assertEqual(pw.match_seq([root], r), False)
+ self.assertEqual(r, {})
+ self.assertEqual(pw.match_seq([n1], r), False)
+ self.assertEqual(r, {})
+ self.assertEqual(pw.match_seq([n2], r), True)
+ # These are easier to debug
+ self.assertEqual(sorted(r.keys()), ["pl", "pn", "pw"])
+ self.assertEqual(r["pl"], l1)
+ self.assertEqual(r["pn"], n2)
+ self.assertEqual(r["pw"], [n2])
+ # But this is equivalent
+ self.assertEqual(r, {"pl": l1, "pn": n2, "pw": [n2]})
+ r = {}
+ self.assertEqual(pw.match_seq([l1, l3], r), True)
+ self.assertEqual(r, {"pl": l3, "pw": [l1, l3]})
+ self.assert_(r["pl"] is l3)
+ r = {}
+
+ def testGenerateMatches(self):
+ la = pytree.Leaf(1, "a")
+ lb = pytree.Leaf(1, "b")
+ lc = pytree.Leaf(1, "c")
+ ld = pytree.Leaf(1, "d")
+ le = pytree.Leaf(1, "e")
+ lf = pytree.Leaf(1, "f")
+ leaves = [la, lb, lc, ld, le, lf]
+ root = pytree.Node(1000, leaves)
+ pa = pytree.LeafPattern(1, "a", "pa")
+ pb = pytree.LeafPattern(1, "b", "pb")
+ pc = pytree.LeafPattern(1, "c", "pc")
+ pd = pytree.LeafPattern(1, "d", "pd")
+ pe = pytree.LeafPattern(1, "e", "pe")
+ pf = pytree.LeafPattern(1, "f", "pf")
+ pw = pytree.WildcardPattern([[pa, pb, pc], [pd, pe],
+ [pa, pb], [pc, pd], [pe, pf]],
+ min=1, max=4, name="pw")
+ self.assertEqual([x[0] for x in pw.generate_matches(leaves)],
+ [3, 5, 2, 4, 6])
+ pr = pytree.NodePattern(type=1000, content=[pw], name="pr")
+ matches = list(pytree.generate_matches([pr], [root]))
+ self.assertEqual(len(matches), 1)
+ c, r = matches[0]
+ self.assertEqual(c, 1)
+ self.assertEqual(str(r["pr"]), "abcdef")
+ self.assertEqual(r["pw"], [la, lb, lc, ld, le, lf])
+ for c in "abcdef":
+ self.assertEqual(r["p" + c], pytree.Leaf(1, c))
+
+ def testHasKeyExample(self):
+ pattern = pytree.NodePattern(331,
+ (pytree.LeafPattern(7),
+ pytree.WildcardPattern(name="args"),
+ pytree.LeafPattern(8)))
+ l1 = pytree.Leaf(7, "(")
+ l2 = pytree.Leaf(3, "x")
+ l3 = pytree.Leaf(8, ")")
+ node = pytree.Node(331, [l1, l2, l3])
+ r = {}
+ self.assert_(pattern.match(node, r))
+ self.assertEqual(r["args"], [l2])
+
+
+if __name__ == "__main__":
+ import __main__
+ support.run_all_tests(__main__)
diff --git a/Lib/lib2to3/tests/test_util.py b/Lib/lib2to3/tests/test_util.py
new file mode 100644
index 0000000..2c22378
--- /dev/null
+++ b/Lib/lib2to3/tests/test_util.py
@@ -0,0 +1,536 @@
+#!/usr/bin/env python2.5
+""" Test suite for the code in fixes.util """
+# Author: Collin Winter
+
+# Testing imports
+from . import support
+
+# Python imports
+import os.path
+
+# Local imports
+from .. import pytree
+from ..fixes import util
+
+
+def parse(code, strip_levels=0):
+ # The topmost node is file_input, which we don't care about.
+ # The next-topmost node is a *_stmt node, which we also don't care about
+ tree = support.parse_string(code)
+ for i in range(strip_levels):
+ tree = tree.children[0]
+ tree.parent = None
+ return tree
+
+class MacroTestCase(support.TestCase):
+ def assertStr(self, node, string):
+ if isinstance(node, (tuple, list)):
+ node = pytree.Node(util.syms.simple_stmt, node)
+ self.assertEqual(str(node), string)
+
+
+class Test_is_tuple(support.TestCase):
+ def is_tuple(self, string):
+ return util.is_tuple(parse(string, strip_levels=2))
+
+ def test_valid(self):
+ self.failUnless(self.is_tuple("(a, b)"))
+ self.failUnless(self.is_tuple("(a, (b, c))"))
+ self.failUnless(self.is_tuple("((a, (b, c)),)"))
+ self.failUnless(self.is_tuple("(a,)"))
+ self.failUnless(self.is_tuple("()"))
+
+ def test_invalid(self):
+ self.failIf(self.is_tuple("(a)"))
+ self.failIf(self.is_tuple("('foo') % (b, c)"))
+
+
+class Test_is_list(support.TestCase):
+ def is_list(self, string):
+ return util.is_list(parse(string, strip_levels=2))
+
+ def test_valid(self):
+ self.failUnless(self.is_list("[]"))
+ self.failUnless(self.is_list("[a]"))
+ self.failUnless(self.is_list("[a, b]"))
+ self.failUnless(self.is_list("[a, [b, c]]"))
+ self.failUnless(self.is_list("[[a, [b, c]],]"))
+
+ def test_invalid(self):
+ self.failIf(self.is_list("[]+[]"))
+
+
+class Test_Attr(MacroTestCase):
+ def test(self):
+ from ..fixes.util import Attr, Name
+ call = parse("foo()", strip_levels=2)
+
+ self.assertStr(Attr(Name("a"), Name("b")), "a.b")
+ self.assertStr(Attr(call, Name("b")), "foo().b")
+
+ def test_returns(self):
+ from ..fixes.util import Attr, Name
+
+ attr = Attr(Name("a"), Name("b"))
+ self.assertEqual(type(attr), list)
+
+
+class Test_Name(MacroTestCase):
+ def test(self):
+ from ..fixes.util import Name
+
+ self.assertStr(Name("a"), "a")
+ self.assertStr(Name("foo.foo().bar"), "foo.foo().bar")
+ self.assertStr(Name("a", prefix="b"), "ba")
+
+
+class Test_does_tree_import(support.TestCase):
+ def _find_bind_rec(self, name, node):
+ # Search a tree for a binding -- used to find the starting
+ # point for these tests.
+ c = util.find_binding(name, node)
+ if c: return c
+ for child in node.children:
+ c = self._find_bind_rec(name, child)
+ if c: return c
+
+ def does_tree_import(self, package, name, string):
+ node = parse(string)
+ # Find the binding of start -- that's what we'll go from
+ node = self._find_bind_rec('start', node)
+ return util.does_tree_import(package, name, node)
+
+ def try_with(self, string):
+ failing_tests = (("a", "a", "from a import b"),
+ ("a.d", "a", "from a.d import b"),
+ ("d.a", "a", "from d.a import b"),
+ (None, "a", "import b"),
+ (None, "a", "import b, c, d"))
+ for package, name, import_ in failing_tests:
+ n = self.does_tree_import(package, name, import_ + "\n" + string)
+ self.failIf(n)
+ n = self.does_tree_import(package, name, string + "\n" + import_)
+ self.failIf(n)
+
+ passing_tests = (("a", "a", "from a import a"),
+ ("x", "a", "from x import a"),
+ ("x", "a", "from x import b, c, a, d"),
+ ("x.b", "a", "from x.b import a"),
+ ("x.b", "a", "from x.b import b, c, a, d"),
+ (None, "a", "import a"),
+ (None, "a", "import b, c, a, d"))
+ for package, name, import_ in passing_tests:
+ n = self.does_tree_import(package, name, import_ + "\n" + string)
+ self.failUnless(n)
+ n = self.does_tree_import(package, name, string + "\n" + import_)
+ self.failUnless(n)
+
+ def test_in_function(self):
+ self.try_with("def foo():\n\tbar.baz()\n\tstart=3")
+
+class Test_find_binding(support.TestCase):
+ def find_binding(self, name, string, package=None):
+ return util.find_binding(name, parse(string), package)
+
+ def test_simple_assignment(self):
+ self.failUnless(self.find_binding("a", "a = b"))
+ self.failUnless(self.find_binding("a", "a = [b, c, d]"))
+ self.failUnless(self.find_binding("a", "a = foo()"))
+ self.failUnless(self.find_binding("a", "a = foo().foo.foo[6][foo]"))
+ self.failIf(self.find_binding("a", "foo = a"))
+ self.failIf(self.find_binding("a", "foo = (a, b, c)"))
+
+ def test_tuple_assignment(self):
+ self.failUnless(self.find_binding("a", "(a,) = b"))
+ self.failUnless(self.find_binding("a", "(a, b, c) = [b, c, d]"))
+ self.failUnless(self.find_binding("a", "(c, (d, a), b) = foo()"))
+ self.failUnless(self.find_binding("a", "(a, b) = foo().foo[6][foo]"))
+ self.failIf(self.find_binding("a", "(foo, b) = (b, a)"))
+ self.failIf(self.find_binding("a", "(foo, (b, c)) = (a, b, c)"))
+
+ def test_list_assignment(self):
+ self.failUnless(self.find_binding("a", "[a] = b"))
+ self.failUnless(self.find_binding("a", "[a, b, c] = [b, c, d]"))
+ self.failUnless(self.find_binding("a", "[c, [d, a], b] = foo()"))
+ self.failUnless(self.find_binding("a", "[a, b] = foo().foo[a][foo]"))
+ self.failIf(self.find_binding("a", "[foo, b] = (b, a)"))
+ self.failIf(self.find_binding("a", "[foo, [b, c]] = (a, b, c)"))
+
+ def test_invalid_assignments(self):
+ self.failIf(self.find_binding("a", "foo.a = 5"))
+ self.failIf(self.find_binding("a", "foo[a] = 5"))
+ self.failIf(self.find_binding("a", "foo(a) = 5"))
+ self.failIf(self.find_binding("a", "foo(a, b) = 5"))
+
+ def test_simple_import(self):
+ self.failUnless(self.find_binding("a", "import a"))
+ self.failUnless(self.find_binding("a", "import b, c, a, d"))
+ self.failIf(self.find_binding("a", "import b"))
+ self.failIf(self.find_binding("a", "import b, c, d"))
+
+ def test_from_import(self):
+ self.failUnless(self.find_binding("a", "from x import a"))
+ self.failUnless(self.find_binding("a", "from a import a"))
+ self.failUnless(self.find_binding("a", "from x import b, c, a, d"))
+ self.failUnless(self.find_binding("a", "from x.b import a"))
+ self.failUnless(self.find_binding("a", "from x.b import b, c, a, d"))
+ self.failIf(self.find_binding("a", "from a import b"))
+ self.failIf(self.find_binding("a", "from a.d import b"))
+ self.failIf(self.find_binding("a", "from d.a import b"))
+
+ def test_import_as(self):
+ self.failUnless(self.find_binding("a", "import b as a"))
+ self.failUnless(self.find_binding("a", "import b as a, c, a as f, d"))
+ self.failIf(self.find_binding("a", "import a as f"))
+ self.failIf(self.find_binding("a", "import b, c as f, d as e"))
+
+ def test_from_import_as(self):
+ self.failUnless(self.find_binding("a", "from x import b as a"))
+ self.failUnless(self.find_binding("a", "from x import g as a, d as b"))
+ self.failUnless(self.find_binding("a", "from x.b import t as a"))
+ self.failUnless(self.find_binding("a", "from x.b import g as a, d"))
+ self.failIf(self.find_binding("a", "from a import b as t"))
+ self.failIf(self.find_binding("a", "from a.d import b as t"))
+ self.failIf(self.find_binding("a", "from d.a import b as t"))
+
+ def test_simple_import_with_package(self):
+ self.failUnless(self.find_binding("b", "import b"))
+ self.failUnless(self.find_binding("b", "import b, c, d"))
+ self.failIf(self.find_binding("b", "import b", "b"))
+ self.failIf(self.find_binding("b", "import b, c, d", "c"))
+
+ def test_from_import_with_package(self):
+ self.failUnless(self.find_binding("a", "from x import a", "x"))
+ self.failUnless(self.find_binding("a", "from a import a", "a"))
+ self.failUnless(self.find_binding("a", "from x import *", "x"))
+ self.failUnless(self.find_binding("a", "from x import b, c, a, d", "x"))
+ self.failUnless(self.find_binding("a", "from x.b import a", "x.b"))
+ self.failUnless(self.find_binding("a", "from x.b import *", "x.b"))
+ self.failUnless(self.find_binding("a", "from x.b import b, c, a, d", "x.b"))
+ self.failIf(self.find_binding("a", "from a import b", "a"))
+ self.failIf(self.find_binding("a", "from a.d import b", "a.d"))
+ self.failIf(self.find_binding("a", "from d.a import b", "a.d"))
+ self.failIf(self.find_binding("a", "from x.y import *", "a.b"))
+
+ def test_import_as_with_package(self):
+ self.failIf(self.find_binding("a", "import b.c as a", "b.c"))
+ self.failIf(self.find_binding("a", "import a as f", "f"))
+ self.failIf(self.find_binding("a", "import a as f", "a"))
+
+ def test_from_import_as_with_package(self):
+ # Because it would take a lot of special-case code in the fixers
+ # to deal with from foo import bar as baz, we'll simply always
+ # fail if there is an "from ... import ... as ..."
+ self.failIf(self.find_binding("a", "from x import b as a", "x"))
+ self.failIf(self.find_binding("a", "from x import g as a, d as b", "x"))
+ self.failIf(self.find_binding("a", "from x.b import t as a", "x.b"))
+ self.failIf(self.find_binding("a", "from x.b import g as a, d", "x.b"))
+ self.failIf(self.find_binding("a", "from a import b as t", "a"))
+ self.failIf(self.find_binding("a", "from a import b as t", "b"))
+ self.failIf(self.find_binding("a", "from a import b as t", "t"))
+
+ def test_function_def(self):
+ self.failUnless(self.find_binding("a", "def a(): pass"))
+ self.failUnless(self.find_binding("a", "def a(b, c, d): pass"))
+ self.failUnless(self.find_binding("a", "def a(): b = 7"))
+ self.failIf(self.find_binding("a", "def d(b, (c, a), e): pass"))
+ self.failIf(self.find_binding("a", "def d(a=7): pass"))
+ self.failIf(self.find_binding("a", "def d(a): pass"))
+ self.failIf(self.find_binding("a", "def d(): a = 7"))
+
+ s = """
+ def d():
+ def a():
+ pass"""
+ self.failIf(self.find_binding("a", s))
+
+ def test_class_def(self):
+ self.failUnless(self.find_binding("a", "class a: pass"))
+ self.failUnless(self.find_binding("a", "class a(): pass"))
+ self.failUnless(self.find_binding("a", "class a(b): pass"))
+ self.failUnless(self.find_binding("a", "class a(b, c=8): pass"))
+ self.failIf(self.find_binding("a", "class d: pass"))
+ self.failIf(self.find_binding("a", "class d(a): pass"))
+ self.failIf(self.find_binding("a", "class d(b, a=7): pass"))
+ self.failIf(self.find_binding("a", "class d(b, *a): pass"))
+ self.failIf(self.find_binding("a", "class d(b, **a): pass"))
+ self.failIf(self.find_binding("a", "class d: a = 7"))
+
+ s = """
+ class d():
+ class a():
+ pass"""
+ self.failIf(self.find_binding("a", s))
+
+ def test_for(self):
+ self.failUnless(self.find_binding("a", "for a in r: pass"))
+ self.failUnless(self.find_binding("a", "for a, b in r: pass"))
+ self.failUnless(self.find_binding("a", "for (a, b) in r: pass"))
+ self.failUnless(self.find_binding("a", "for c, (a,) in r: pass"))
+ self.failUnless(self.find_binding("a", "for c, (a, b) in r: pass"))
+ self.failUnless(self.find_binding("a", "for c in r: a = c"))
+ self.failIf(self.find_binding("a", "for c in a: pass"))
+
+ def test_for_nested(self):
+ s = """
+ for b in r:
+ for a in b:
+ pass"""
+ self.failUnless(self.find_binding("a", s))
+
+ s = """
+ for b in r:
+ for a, c in b:
+ pass"""
+ self.failUnless(self.find_binding("a", s))
+
+ s = """
+ for b in r:
+ for (a, c) in b:
+ pass"""
+ self.failUnless(self.find_binding("a", s))
+
+ s = """
+ for b in r:
+ for (a,) in b:
+ pass"""
+ self.failUnless(self.find_binding("a", s))
+
+ s = """
+ for b in r:
+ for c, (a, d) in b:
+ pass"""
+ self.failUnless(self.find_binding("a", s))
+
+ s = """
+ for b in r:
+ for c in b:
+ a = 7"""
+ self.failUnless(self.find_binding("a", s))
+
+ s = """
+ for b in r:
+ for c in b:
+ d = a"""
+ self.failIf(self.find_binding("a", s))
+
+ s = """
+ for b in r:
+ for c in a:
+ d = 7"""
+ self.failIf(self.find_binding("a", s))
+
+ def test_if(self):
+ self.failUnless(self.find_binding("a", "if b in r: a = c"))
+ self.failIf(self.find_binding("a", "if a in r: d = e"))
+
+ def test_if_nested(self):
+ s = """
+ if b in r:
+ if c in d:
+ a = c"""
+ self.failUnless(self.find_binding("a", s))
+
+ s = """
+ if b in r:
+ if c in d:
+ c = a"""
+ self.failIf(self.find_binding("a", s))
+
+ def test_while(self):
+ self.failUnless(self.find_binding("a", "while b in r: a = c"))
+ self.failIf(self.find_binding("a", "while a in r: d = e"))
+
+ def test_while_nested(self):
+ s = """
+ while b in r:
+ while c in d:
+ a = c"""
+ self.failUnless(self.find_binding("a", s))
+
+ s = """
+ while b in r:
+ while c in d:
+ c = a"""
+ self.failIf(self.find_binding("a", s))
+
+ def test_try_except(self):
+ s = """
+ try:
+ a = 6
+ except:
+ b = 8"""
+ self.failUnless(self.find_binding("a", s))
+
+ s = """
+ try:
+ b = 8
+ except:
+ a = 6"""
+ self.failUnless(self.find_binding("a", s))
+
+ s = """
+ try:
+ b = 8
+ except KeyError:
+ pass
+ except:
+ a = 6"""
+ self.failUnless(self.find_binding("a", s))
+
+ s = """
+ try:
+ b = 8
+ except:
+ b = 6"""
+ self.failIf(self.find_binding("a", s))
+
+ def test_try_except_nested(self):
+ s = """
+ try:
+ try:
+ a = 6
+ except:
+ pass
+ except:
+ b = 8"""
+ self.failUnless(self.find_binding("a", s))
+
+ s = """
+ try:
+ b = 8
+ except:
+ try:
+ a = 6
+ except:
+ pass"""
+ self.failUnless(self.find_binding("a", s))
+
+ s = """
+ try:
+ b = 8
+ except:
+ try:
+ pass
+ except:
+ a = 6"""
+ self.failUnless(self.find_binding("a", s))
+
+ s = """
+ try:
+ try:
+ b = 8
+ except KeyError:
+ pass
+ except:
+ a = 6
+ except:
+ pass"""
+ self.failUnless(self.find_binding("a", s))
+
+ s = """
+ try:
+ pass
+ except:
+ try:
+ b = 8
+ except KeyError:
+ pass
+ except:
+ a = 6"""
+ self.failUnless(self.find_binding("a", s))
+
+ s = """
+ try:
+ b = 8
+ except:
+ b = 6"""
+ self.failIf(self.find_binding("a", s))
+
+ s = """
+ try:
+ try:
+ b = 8
+ except:
+ c = d
+ except:
+ try:
+ b = 6
+ except:
+ t = 8
+ except:
+ o = y"""
+ self.failIf(self.find_binding("a", s))
+
+ def test_try_except_finally(self):
+ s = """
+ try:
+ c = 6
+ except:
+ b = 8
+ finally:
+ a = 9"""
+ self.failUnless(self.find_binding("a", s))
+
+ s = """
+ try:
+ b = 8
+ finally:
+ a = 6"""
+ self.failUnless(self.find_binding("a", s))
+
+ s = """
+ try:
+ b = 8
+ finally:
+ b = 6"""
+ self.failIf(self.find_binding("a", s))
+
+ s = """
+ try:
+ b = 8
+ except:
+ b = 9
+ finally:
+ b = 6"""
+ self.failIf(self.find_binding("a", s))
+
+ def test_try_except_finally_nested(self):
+ s = """
+ try:
+ c = 6
+ except:
+ b = 8
+ finally:
+ try:
+ a = 9
+ except:
+ b = 9
+ finally:
+ c = 9"""
+ self.failUnless(self.find_binding("a", s))
+
+ s = """
+ try:
+ b = 8
+ finally:
+ try:
+ pass
+ finally:
+ a = 6"""
+ self.failUnless(self.find_binding("a", s))
+
+ s = """
+ try:
+ b = 8
+ finally:
+ try:
+ b = 6
+ finally:
+ b = 7"""
+ self.failIf(self.find_binding("a", s))
+
+
+if __name__ == "__main__":
+ import __main__
+ support.run_all_tests(__main__)