FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Use BaseException in raise_() by jparise · Pull Request #515 · PythonCharmers/python-future · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
6 changes: 3 additions & 3 deletions src/future/utils/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL