Skip to content

Commit b362804

Browse files
committed
Add tests for raise translation and raise_() functionality.
1. Uncomment the Test_raise tests now that support for tracebacks has been added. Add test_unknown_value() to test for #455. 2. Run the test_raise_() test now that #455 is fixed. Add a test for #387 (raise_(E, V, T) now detects if V is an instance of E and no longer blindly calls E(V)).
1 parent df3a9dc commit b362804

File tree

2 files changed

+151
-131
lines changed

2 files changed

+151
-131
lines changed

tests/test_future/test_libfuturize_fixers.py

Lines changed: 134 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -702,133 +702,140 @@ def test_with_future_print_function(self):
702702
# except (Exception, SystemExit):
703703
# pass"""
704704
# self.unchanged(s)
705-
#
706-
# class Test_raise(FixerTestCase):
707-
# fixer = "raise"
708-
#
709-
# def test_basic(self):
710-
# b = """raise Exception, 5"""
711-
# a = """raise Exception(5)"""
712-
# self.check(b, a)
713-
#
714-
# def test_prefix_preservation(self):
715-
# b = """raise Exception,5"""
716-
# a = """raise Exception(5)"""
717-
# self.check(b, a)
718-
#
719-
# b = """raise Exception, 5"""
720-
# a = """raise Exception(5)"""
721-
# self.check(b, a)
722-
#
723-
# def test_with_comments(self):
724-
# b = """raise Exception, 5 # foo"""
725-
# a = """raise Exception(5) # foo"""
726-
# self.check(b, a)
727-
#
728-
# b = """raise E, (5, 6) % (a, b) # foo"""
729-
# a = """raise E((5, 6) % (a, b)) # foo"""
730-
# self.check(b, a)
731-
#
732-
# b = """def foo():
733-
# raise Exception, 5, 6 # foo"""
734-
# a = """def foo():
735-
# raise Exception(5).with_traceback(6) # foo"""
736-
# self.check(b, a)
737-
#
738-
# def test_None_value(self):
739-
# b = """raise Exception(5), None, tb"""
740-
# a = """raise Exception(5).with_traceback(tb)"""
741-
# self.check(b, a)
742-
#
743-
# def test_tuple_value(self):
744-
# b = """raise Exception, (5, 6, 7)"""
745-
# a = """raise Exception(5, 6, 7)"""
746-
# self.check(b, a)
747-
#
748-
# def test_tuple_detection(self):
749-
# b = """raise E, (5, 6) % (a, b)"""
750-
# a = """raise E((5, 6) % (a, b))"""
751-
# self.check(b, a)
752-
#
753-
# def test_tuple_exc_1(self):
754-
# b = """raise (((E1, E2), E3), E4), V"""
755-
# a = """raise E1(V)"""
756-
# self.check(b, a)
757-
#
758-
# def test_tuple_exc_2(self):
759-
# b = """raise (E1, (E2, E3), E4), V"""
760-
# a = """raise E1(V)"""
761-
# self.check(b, a)
762-
#
763-
# # These should produce a warning
764-
#
765-
# def test_string_exc(self):
766-
# s = """raise 'foo'"""
767-
# self.warns_unchanged(s, "Python 3 does not support string exceptions")
768-
#
769-
# def test_string_exc_val(self):
770-
# s = """raise "foo", 5"""
771-
# self.warns_unchanged(s, "Python 3 does not support string exceptions")
772-
#
773-
# def test_string_exc_val_tb(self):
774-
# s = """raise "foo", 5, 6"""
775-
# self.warns_unchanged(s, "Python 3 does not support string exceptions")
776-
#
777-
# # These should result in traceback-assignment
778-
#
779-
# def test_tb_1(self):
780-
# b = """def foo():
781-
# raise Exception, 5, 6"""
782-
# a = """def foo():
783-
# raise Exception(5).with_traceback(6)"""
784-
# self.check(b, a)
785-
#
786-
# def test_tb_2(self):
787-
# b = """def foo():
788-
# a = 5
789-
# raise Exception, 5, 6
790-
# b = 6"""
791-
# a = """def foo():
792-
# a = 5
793-
# raise Exception(5).with_traceback(6)
794-
# b = 6"""
795-
# self.check(b, a)
796-
#
797-
# def test_tb_3(self):
798-
# b = """def foo():
799-
# raise Exception,5,6"""
800-
# a = """def foo():
801-
# raise Exception(5).with_traceback(6)"""
802-
# self.check(b, a)
803-
#
804-
# def test_tb_4(self):
805-
# b = """def foo():
806-
# a = 5
807-
# raise Exception,5,6
808-
# b = 6"""
809-
# a = """def foo():
810-
# a = 5
811-
# raise Exception(5).with_traceback(6)
812-
# b = 6"""
813-
# self.check(b, a)
814-
#
815-
# def test_tb_5(self):
816-
# b = """def foo():
817-
# raise Exception, (5, 6, 7), 6"""
818-
# a = """def foo():
819-
# raise Exception(5, 6, 7).with_traceback(6)"""
820-
# self.check(b, a)
821-
#
822-
# def test_tb_6(self):
823-
# b = """def foo():
824-
# a = 5
825-
# raise Exception, (5, 6, 7), 6
826-
# b = 6"""
827-
# a = """def foo():
828-
# a = 5
829-
# raise Exception(5, 6, 7).with_traceback(6)
830-
# b = 6"""
831-
# self.check(b, a)
705+
706+
class Test_raise(FixerTestCase):
707+
fixer = "raise"
708+
709+
def test_basic(self):
710+
b = """raise Exception, 5"""
711+
a = """raise Exception(5)"""
712+
self.check(b, a)
713+
714+
def test_prefix_preservation(self):
715+
b = """raise Exception,5"""
716+
a = """raise Exception(5)"""
717+
self.check(b, a)
718+
719+
b = """raise Exception, 5"""
720+
a = """raise Exception(5)"""
721+
self.check(b, a)
722+
723+
def test_with_comments(self):
724+
b = """raise Exception, 5 # foo"""
725+
a = """raise Exception(5) # foo"""
726+
self.check(b, a)
727+
728+
b = """def foo():
729+
raise Exception, 5, 6 # foo"""
730+
a = """def foo():
731+
raise Exception(5).with_traceback(6) # foo"""
732+
self.check(b, a)
733+
734+
def test_None_value(self):
735+
b = """raise Exception(5), None, tb"""
736+
a = """raise Exception(5).with_traceback(tb)"""
737+
self.check(b, a)
738+
739+
def test_tuple_value(self):
740+
b = """raise Exception, (5, 6, 7)"""
741+
a = """raise Exception(5, 6, 7)"""
742+
self.check(b, a)
743+
744+
def test_tuple_exc_1(self):
745+
b = """raise (((E1, E2), E3), E4), 5"""
746+
a = """raise E1(5)"""
747+
self.check(b, a)
748+
749+
def test_tuple_exc_2(self):
750+
b = """raise (E1, (E2, E3), E4), 5"""
751+
a = """raise E1(5)"""
752+
self.check(b, a)
753+
754+
def test_unknown_value(self):
755+
b = """
756+
raise E, V"""
757+
a = """
758+
from future.utils import raise_
759+
raise_(E, V)"""
760+
self.check(b, a)
761+
762+
def test_unknown_value_with_traceback_with_comments(self):
763+
b = """
764+
raise E, Func(arg1, arg2, arg3), tb # foo"""
765+
a = """
766+
from future.utils import raise_
767+
raise_(E, Func(arg1, arg2, arg3), tb) # foo"""
768+
self.check(b, a)
769+
770+
# These should produce a warning
771+
772+
def test_string_exc(self):
773+
s = """raise 'foo'"""
774+
self.warns_unchanged(s, "Python 3 does not support string exceptions")
775+
776+
def test_string_exc_val(self):
777+
s = """raise "foo", 5"""
778+
self.warns_unchanged(s, "Python 3 does not support string exceptions")
779+
780+
def test_string_exc_val_tb(self):
781+
s = """raise "foo", 5, 6"""
782+
self.warns_unchanged(s, "Python 3 does not support string exceptions")
783+
784+
# These should result in traceback-assignment
785+
786+
def test_tb_1(self):
787+
b = """def foo():
788+
raise Exception, 5, 6"""
789+
a = """def foo():
790+
raise Exception(5).with_traceback(6)"""
791+
self.check(b, a)
792+
793+
def test_tb_2(self):
794+
b = """def foo():
795+
a = 5
796+
raise Exception, 5, 6
797+
b = 6"""
798+
a = """def foo():
799+
a = 5
800+
raise Exception(5).with_traceback(6)
801+
b = 6"""
802+
self.check(b, a)
803+
804+
def test_tb_3(self):
805+
b = """def foo():
806+
raise Exception,5,6"""
807+
a = """def foo():
808+
raise Exception(5).with_traceback(6)"""
809+
self.check(b, a)
810+
811+
def test_tb_4(self):
812+
b = """def foo():
813+
a = 5
814+
raise Exception,5,6
815+
b = 6"""
816+
a = """def foo():
817+
a = 5
818+
raise Exception(5).with_traceback(6)
819+
b = 6"""
820+
self.check(b, a)
821+
822+
def test_tb_5(self):
823+
b = """def foo():
824+
raise Exception, (5, 6, 7), 6"""
825+
a = """def foo():
826+
raise Exception(5, 6, 7).with_traceback(6)"""
827+
self.check(b, a)
828+
829+
def test_tb_6(self):
830+
b = """def foo():
831+
a = 5
832+
raise Exception, (5, 6, 7), 6
833+
b = 6"""
834+
a = """def foo():
835+
a = 5
836+
raise Exception(5, 6, 7).with_traceback(6)
837+
b = 6"""
838+
self.check(b, a)
832839
#
833840
# class Test_throw(FixerTestCase):
834841
# fixer = "throw"

tests/test_future/test_utils.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,7 @@ def test_isbytes(self):
110110
self.assertFalse(isbytes(self.s))
111111
self.assertFalse(isbytes(self.s2))
112112

113-
@unittest.skipIf(PY3, 'test_raise_ currently fails on Py3')
114113
def test_raise_(self):
115-
"""
116-
The with_value() test currently fails on Py3
117-
"""
118114
def valerror():
119115
try:
120116
raise ValueError("Apples!")
@@ -173,6 +169,23 @@ def bar():
173169
pass
174170
# incorrectly raises a TypeError on Py3 as of v0.15.2.
175171

172+
def test_raise_custom_exception(self):
173+
"""
174+
Test issue #387.
175+
"""
176+
class CustomException(Exception):
177+
def __init__(self, severity, message):
178+
super().__init__("custom message of severity %d: %s" % (
179+
severity, message))
180+
181+
def raise_custom_exception():
182+
try:
183+
raise CustomException(1, "hello")
184+
except CustomException:
185+
raise_(*sys.exc_info())
186+
187+
self.assertRaises(CustomException, raise_custom_exception)
188+
176189
@skip26
177190
def test_as_native_str(self):
178191
"""

0 commit comments

Comments
 (0)