Skip to content

Commit 7950f36

Browse files
committed
Merge pull request #82 from takluyver/fix-raise-subclass-2to3
Subclass FixRaise from 2to3
2 parents bb72bc6 + bdd46af commit 7950f36

File tree

2 files changed

+17
-51
lines changed

2 files changed

+17
-51
lines changed

libmodernize/fixes/fix_raise.py

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,53 +20,11 @@
2020
# Author: Collin Winter, Armin Ronacher
2121
from __future__ import absolute_import
2222

23-
# Local imports
24-
from lib2to3 import pytree, fixer_base
25-
from lib2to3.pgen2 import token
26-
from lib2to3.fixer_util import Name, Call, is_tuple
23+
from lib2to3.fixes import fix_raise
2724

28-
class FixRaise(fixer_base.BaseFix):
29-
30-
BM_compatible = True
25+
class FixRaise(fix_raise.FixRaise):
26+
# We don't want to match 3-argument raise, with a traceback;
27+
# that is handled separately by fix_raise_six
3128
PATTERN = """
3229
raise_stmt< 'raise' exc=any [',' val=any] >
33-
"""
34-
35-
def transform(self, node, results):
36-
syms = self.syms
37-
38-
exc = results["exc"].clone()
39-
if exc.type == token.STRING:
40-
msg = "Python 3 does not support string exceptions"
41-
self.cannot_convert(node, msg)
42-
return
43-
44-
# Python 2 supports
45-
# raise ((((E1, E2), E3), E4), E5), V
46-
# as a synonym for
47-
# raise E1, V
48-
# Since Python 3 will not support this, we recurse down any tuple
49-
# literals, always taking the first element.
50-
if is_tuple(exc):
51-
while is_tuple(exc):
52-
# exc.children[1:-1] is the unparenthesized tuple
53-
# exc.children[1].children[0] is the first element of the tuple
54-
exc = exc.children[1].children[0].clone()
55-
exc.prefix = u" "
56-
57-
if "val" not in results:
58-
# One-argument raise
59-
new = pytree.Node(syms.raise_stmt, [Name(u"raise"), exc])
60-
new.prefix = node.prefix
61-
return new
62-
63-
val = results["val"].clone()
64-
if is_tuple(val):
65-
args = [c.clone() for c in val.children[1:-1]]
66-
else:
67-
val.prefix = u""
68-
args = [val]
69-
70-
return pytree.Node(syms.raise_stmt,
71-
[Name(u"raise"), Call(exc, args)],
72-
prefix=node.prefix)
30+
"""

tests/test_fix_imports_six.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import absolute_import
22

33
import sys
4+
import unittest
45

56
try:
67
from six.moves import tkinter
@@ -35,13 +36,20 @@ def test_moved_module():
3536
def test_moved_module_fromlist():
3637
check_on_input(*MOVED_MODULE_FROMLIST)
3738

39+
@unittest.skipIf(sys.version_info[0] >= 3, "Test only runs on Python 2")
3840
def test_validate_mapping():
3941
for py2_name, six_name in fix_imports_six.FixImportsSix.mapping.items():
4042
try:
4143
__import__(py2_name)
4244
__import__(six_name)
43-
except ImportError as exc:
44-
if 'tkinter' in six_name and tkinter is not None:
45-
raise
46-
elif 'winreg' in six_name and sys.platform.startswith('win'):
45+
except ImportError:
46+
if 'tkinter' in six_name:
47+
# Ignore error if tkinter not installed
48+
if tkinter is not None:
49+
raise
50+
elif 'winreg' in six_name:
51+
# Ignore error if we're not on Windows
52+
if sys.platform.startswith('win'):
53+
raise
54+
else:
4755
raise

0 commit comments

Comments
 (0)