Commit 6d911760 authored by R David Murray's avatar R David Murray

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.
parent 988bc970
...@@ -122,8 +122,6 @@ class _AssertRaisesContext(object): ...@@ -122,8 +122,6 @@ class _AssertRaisesContext(object):
return True return True
expected_regexp = self.expected_regexp expected_regexp = self.expected_regexp
if isinstance(expected_regexp, basestring):
expected_regexp = re.compile(expected_regexp)
if not expected_regexp.search(str(exc_value)): if not expected_regexp.search(str(exc_value)):
raise self.failureException('"%s" does not match "%s"' % raise self.failureException('"%s" does not match "%s"' %
(expected_regexp.pattern, str(exc_value))) (expected_regexp.pattern, str(exc_value)))
...@@ -986,6 +984,8 @@ class TestCase(object): ...@@ -986,6 +984,8 @@ class TestCase(object):
args: Extra args. args: Extra args.
kwargs: Extra kwargs. kwargs: Extra kwargs.
""" """
if expected_regexp is not None:
expected_regexp = re.compile(expected_regexp)
context = _AssertRaisesContext(expected_exception, self, expected_regexp) context = _AssertRaisesContext(expected_exception, self, expected_regexp)
if callable_obj is None: if callable_obj is None:
return context return context
......
...@@ -979,6 +979,12 @@ test case ...@@ -979,6 +979,12 @@ test case
self.assertRaisesRegexp, Exception, u'x', self.assertRaisesRegexp, Exception, u'x',
lambda: None) lambda: None)
def testAssertRaisesRegexpInvalidRegexp(self):
# Issue 20145.
class MyExc(Exception):
pass
self.assertRaises(TypeError, self.assertRaisesRegexp, MyExc, lambda: True)
def testAssertRaisesRegexpMismatch(self): def testAssertRaisesRegexpMismatch(self):
def Stub(): def Stub():
raise Exception('Unexpected') raise Exception('Unexpected')
......
...@@ -546,6 +546,7 @@ Stefan Hoffmeister ...@@ -546,6 +546,7 @@ Stefan Hoffmeister
Albert Hofkamp Albert Hofkamp
Tomas Hoger Tomas Hoger
Jonathan Hogg Jonathan Hogg
Kamilla Holanda
Steve Holden Steve Holden
Akintayo Holder Akintayo Holder
Thomas Holenstein Thomas Holenstein
......
...@@ -40,6 +40,9 @@ Core and Builtins ...@@ -40,6 +40,9 @@ Core and Builtins
Library Library
------- -------
- Issue #20145: `assertRaisesRegex` now raises a TypeError if the second
argument is not a string or compiled regex.
- Issue #21058: Fix a leak of file descriptor in tempfile.NamedTemporaryFile(), - Issue #21058: Fix a leak of file descriptor in tempfile.NamedTemporaryFile(),
close the file descriptor if os.fdopen() fails close the file descriptor if os.fdopen() fails
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment