diff options
-rw-r--r-- | Lib/lib2to3/fixes/fix_except.py | 13 | ||||
-rw-r--r-- | Lib/lib2to3/tests/test_all_fixers.py | 2 | ||||
-rwxr-xr-x | Lib/lib2to3/tests/test_fixers.py | 68 | ||||
-rw-r--r-- | Lib/test/test_lib2to3.py | 15 |
4 files changed, 92 insertions, 6 deletions
diff --git a/Lib/lib2to3/fixes/fix_except.py b/Lib/lib2to3/fixes/fix_except.py index 34281a0..5d6d153 100644 --- a/Lib/lib2to3/fixes/fix_except.py +++ b/Lib/lib2to3/fixes/fix_except.py @@ -37,15 +37,18 @@ class FixExcept(basefix.BaseFix): PATTERN = """ try_stmt< 'try' ':' suite - cleanup=((except_clause ':' suite)+ ['else' ':' suite] - ['finally' ':' suite] - | 'finally' ':' suite) > + cleanup=(except_clause ':' suite)+ + tail=(['except' ':' suite] + ['else' ':' suite] + ['finally' ':' suite]) > """ def transform(self, node, results): syms = self.syms - try_cleanup = [ch.clone() for ch in results['cleanup']] + tail = [n.clone() for n in results["tail"]] + + try_cleanup = [ch.clone() for ch in results["cleanup"]] for except_clause, e_suite in find_excepts(try_cleanup): if len(except_clause.children) == 4: (E, comma, N) = except_clause.children[1:4] @@ -85,5 +88,5 @@ class FixExcept(basefix.BaseFix): N.set_prefix(" ") #TODO(cwinter) fix this when children becomes a smart list - children = [c.clone() for c in node.children[:3]] + try_cleanup + children = [c.clone() for c in node.children[:3]] + try_cleanup + tail return pytree.Node(node.type, children) diff --git a/Lib/lib2to3/tests/test_all_fixers.py b/Lib/lib2to3/tests/test_all_fixers.py index 19c1af6..5f4a9fc 100644 --- a/Lib/lib2to3/tests/test_all_fixers.py +++ b/Lib/lib2to3/tests/test_all_fixers.py @@ -27,7 +27,7 @@ class Options: class Test_all(support.TestCase): def setUp(self): - options = Options(fix=["all", "idioms", "ws_comma"], + options = Options(fix=["all", "idioms", "ws_comma", "buffer"], print_function=False) self.refactor = refactor.RefactoringTool(options) diff --git a/Lib/lib2to3/tests/test_fixers.py b/Lib/lib2to3/tests/test_fixers.py index 34ea3e5..6d33672 100755 --- a/Lib/lib2to3/tests/test_fixers.py +++ b/Lib/lib2to3/tests/test_fixers.py @@ -435,6 +435,8 @@ class Test_print(FixerTestCase): # is fixed so it won't crash when it sees print(x=y). # When #2412 is fixed, the try/except block can be taken # out and the tests can be run like normal. + # MvL: disable entirely for now, so that it doesn't print to stdout + return try: s = "from __future__ import print_function\n"\ "print('Hai!', end=' ')" @@ -679,6 +681,72 @@ class Test_except(FixerTestCase): pass""" self.check(b, a) + def test_bare_except(self): + b = """ + try: + pass + except Exception, a: + pass + except: + pass""" + + a = """ + try: + pass + except Exception as a: + pass + except: + pass""" + self.check(b, a) + + def test_bare_except_and_else_finally(self): + b = """ + try: + pass + except Exception, a: + pass + except: + pass + else: + pass + finally: + pass""" + + a = """ + try: + pass + except Exception as a: + pass + except: + pass + else: + pass + finally: + pass""" + self.check(b, a) + + def test_multi_fixed_excepts_before_bare_except(self): + b = """ + try: + pass + except TypeError, b: + pass + except Exception, a: + pass + except: + pass""" + + a = """ + try: + pass + except TypeError as b: + pass + except Exception as a: + pass + except: + pass""" + self.check(b, a) + # These should not be touched: def test_unchanged_1(self): diff --git a/Lib/test/test_lib2to3.py b/Lib/test/test_lib2to3.py new file mode 100644 index 0000000..beda695 --- /dev/null +++ b/Lib/test/test_lib2to3.py @@ -0,0 +1,15 @@ +# Skipping test_parser and test_all_fixers +# because of running +from lib2to3.tests import test_fixers, test_pytree, test_util +import unittest +from test.test_support import run_unittest + +def suite(): + tests = unittest.TestSuite() + loader = unittest.TestLoader() + for m in (test_fixers,test_pytree,test_util): + tests.addTests(loader.loadTestsFromModule(m)) + return tests + +def test_main(): + run_unittest(suite()) |