Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(39644)

Unified Diff: Lib/unittest/case.py

Issue 15836: unittest assertRaises should verify excClass is actually a BaseException class
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Lib/test/test_importlib/builtin/test_loader.py ('k') | Lib/unittest/test/test_case.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
--- a/Lib/unittest/case.py Tue May 19 11:00:07 2015 +0300
+++ b/Lib/unittest/case.py Tue May 19 12:07:06 2015 +0300
@@ -119,6 +119,10 @@ def expectedFailure(test_item):
test_item.__unittest_expecting_failure__ = True
return test_item
+def _is_subtype(expected, basetype):
+ if isinstance(expected, tuple):
+ return all(_is_subtype(e, basetype) for e in expected)
+ return isinstance(expected, type) and issubclass(expected, basetype)
class _BaseTestCaseContext:
@@ -148,6 +152,9 @@ class _AssertRaisesBaseContext(_BaseTest
If args is not empty, call a callable passing positional and keyword
arguments.
"""
+ if not _is_subtype(self.expected, self._base_type):
Martin Panter 2015/05/19 12:37:58 Why is this check moved from __init__() to handle(
storchaka 2015/05/19 12:47:33 Only because the name parameter is passed to handl
Martin Panter 2015/05/19 13:27:57 Fair enough, that makes sense
+ raise TypeError('%s() arg 1 must be %s' %
+ (name, self._base_type_str))
if args and args[0] is None:
warnings.warn("callable is None",
DeprecationWarning, 3)
@@ -172,6 +179,9 @@ class _AssertRaisesBaseContext(_BaseTest
class _AssertRaisesContext(_AssertRaisesBaseContext):
"""A context manager used to implement TestCase.assertRaises* methods."""
+ _base_type = BaseException
+ _base_type_str = 'an exception type or tuple of exception types'
+
def __enter__(self):
return self
@@ -206,6 +216,9 @@ class _AssertRaisesContext(_AssertRaises
class _AssertWarnsContext(_AssertRaisesBaseContext):
"""A context manager used to implement TestCase.assertWarns* methods."""
+ _base_type = Warning
+ _base_type_str = 'a warning type or tuple of warning types'
+
def __enter__(self):
# The __warningregistry__'s need to be in a pristine state for tests
# to work properly.
« no previous file with comments | « Lib/test/test_importlib/builtin/test_loader.py ('k') | Lib/unittest/test/test_case.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+