backport: #20145: assertRaisesRegexp now raises a TypeError on bad regex.
Previously a non-string, non-regex second argument and no callable
argument could cause the test to appear to always pass.
diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py
index b0cb44a..644fe5b 100644
--- a/Lib/unittest/case.py
+++ b/Lib/unittest/case.py
@@ -122,8 +122,6 @@
return True
expected_regexp = self.expected_regexp
- if isinstance(expected_regexp, basestring):
- expected_regexp = re.compile(expected_regexp)
if not expected_regexp.search(str(exc_value)):
raise self.failureException('"%s" does not match "%s"' %
(expected_regexp.pattern, str(exc_value)))
@@ -986,6 +984,8 @@
args: Extra args.
kwargs: Extra kwargs.
"""
+ if expected_regexp is not None:
+ expected_regexp = re.compile(expected_regexp)
context = _AssertRaisesContext(expected_exception, self, expected_regexp)
if callable_obj is None:
return context