Skip to content

Commit e783561

Browse files
authored
Merge pull request #515 from jparise/raise-base-exception
Use BaseException in raise_()
2 parents 1bbae12 + 51509ad commit e783561

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/future/utils/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -408,17 +408,17 @@ def raise_(tp, value=None, tb=None):
408408
allows re-raising exceptions with the cls value and traceback on
409409
Python 2 and 3.
410410
"""
411-
if isinstance(tp, Exception):
411+
if isinstance(tp, BaseException):
412412
# If the first object is an instance, the type of the exception
413413
# is the class of the instance, the instance itself is the value,
414414
# and the second object must be None.
415415
if value is not None:
416416
raise TypeError("instance exception may not have a separate value")
417417
exc = tp
418-
elif isinstance(tp, type) and not issubclass(tp, Exception):
418+
elif isinstance(tp, type) and not issubclass(tp, BaseException):
419419
# If the first object is a class, it becomes the type of the
420420
# exception.
421-
raise TypeError("class must derive from Exception")
421+
raise TypeError("class must derive from BaseException, not %s" % tp.__name__)
422422
else:
423423
# The second object is used to determine the exception value: If it
424424
# is an instance of the class, the instance becomes the exception

tests/test_future/test_utils.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ def test_isbytes(self):
111111
self.assertFalse(isbytes(self.s2))
112112

113113
def test_raise_(self):
114-
def valerror():
114+
def valuerror():
115115
try:
116116
raise ValueError("Apples!")
117117
except Exception as e:
118118
raise_(e)
119119

120-
self.assertRaises(ValueError, valerror)
120+
self.assertRaises(ValueError, valuerror)
121121

122122
def with_value():
123123
raise_(IOError, "This is an error")
@@ -143,6 +143,17 @@ def with_traceback():
143143
except IOError as e:
144144
self.assertEqual(str(e), "An error")
145145

146+
class Timeout(BaseException):
147+
pass
148+
149+
self.assertRaises(Timeout, raise_, Timeout)
150+
self.assertRaises(Timeout, raise_, Timeout())
151+
152+
if PY3:
153+
self.assertRaisesRegexp(
154+
TypeError, "class must derive from BaseException",
155+
raise_, int)
156+
146157
def test_raise_from_None(self):
147158
try:
148159
try:

0 commit comments

Comments
 (0)