Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use BaseException in raise_() #515

Merged
merged 1 commit into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/future/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,17 +408,17 @@ def raise_(tp, value=None, tb=None):
allows re-raising exceptions with the cls value and traceback on
Python 2 and 3.
"""
if isinstance(tp, Exception):
if isinstance(tp, BaseException):
# If the first object is an instance, the type of the exception
# is the class of the instance, the instance itself is the value,
# and the second object must be None.
if value is not None:
raise TypeError("instance exception may not have a separate value")
exc = tp
elif isinstance(tp, type) and not issubclass(tp, Exception):
elif isinstance(tp, type) and not issubclass(tp, BaseException):
# If the first object is a class, it becomes the type of the
# exception.
raise TypeError("class must derive from Exception")
raise TypeError("class must derive from BaseException, not %s" % tp.__name__)
else:
# The second object is used to determine the exception value: If it
# is an instance of the class, the instance becomes the exception
Expand Down
15 changes: 13 additions & 2 deletions tests/test_future/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ def test_isbytes(self):
self.assertFalse(isbytes(self.s2))

def test_raise_(self):
def valerror():
def valuerror():
try:
raise ValueError("Apples!")
except Exception as e:
raise_(e)

self.assertRaises(ValueError, valerror)
self.assertRaises(ValueError, valuerror)

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

class Timeout(BaseException):
pass

self.assertRaises(Timeout, raise_, Timeout)
self.assertRaises(Timeout, raise_, Timeout())

if PY3:
self.assertRaisesRegexp(
TypeError, "class must derive from BaseException",
raise_, int)

def test_raise_from_None(self):
try:
try:
Expand Down