summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorR David Murray <rdmurray@bitdance.com>2014-03-23 19:08:43 (GMT)
committerR David Murray <rdmurray@bitdance.com>2014-03-23 19:08:43 (GMT)
commite1b6f97daedcfab29c1139ddb8c41292cd7de472 (patch)
tree580920419e1db0b677444a7fb33201d44f32b114 /Lib
parent91e7f04fc55523b339209dc6fb7702d3cf6bade4 (diff)
downloadcpython-e1b6f97daedcfab29c1139ddb8c41292cd7de472.zip
cpython-e1b6f97daedcfab29c1139ddb8c41292cd7de472.tar.gz
cpython-e1b6f97daedcfab29c1139ddb8c41292cd7de472.tar.bz2
#20145: assert[Raises|Warns]Regex now raise TypeError on bad regex.
Previously a non-string, non-regex second argument could cause the test to always pass. Initial patch by Kamilla Holanda.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/unittest/case.py2
-rw-r--r--Lib/unittest/test/test_case.py12
2 files changed, 13 insertions, 1 deletions
diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py
index 87fb02b..bedbc67 100644
--- a/Lib/unittest/case.py
+++ b/Lib/unittest/case.py
@@ -143,7 +143,7 @@ class _AssertRaisesBaseContext(_BaseTestCaseContext):
self.obj_name = str(callable_obj)
else:
self.obj_name = None
- if isinstance(expected_regex, (bytes, str)):
+ if expected_regex is not None:
expected_regex = re.compile(expected_regex)
self.expected_regex = expected_regex
self.msg = None
diff --git a/Lib/unittest/test/test_case.py b/Lib/unittest/test/test_case.py
index 363390a..4932578 100644
--- a/Lib/unittest/test/test_case.py
+++ b/Lib/unittest/test/test_case.py
@@ -1126,6 +1126,18 @@ test case
self.assertRaisesRegex, Exception, 'x',
lambda: None)
+ def testAssertRaisesRegexInvalidRegex(self):
+ # Issue 20145.
+ class MyExc(Exception):
+ pass
+ self.assertRaises(TypeError, self.assertRaisesRegex, MyExc, lambda: True)
+
+ def testAssertWarnsRegexInvalidRegex(self):
+ # Issue 20145.
+ class MyWarn(Warning):
+ pass
+ self.assertRaises(TypeError, self.assertWarnsRegex, MyWarn, lambda: True)
+
def testAssertRaisesRegexMismatch(self):
def Stub():
raise Exception('Unexpected')