diff options
author | R David Murray <rdmurray@bitdance.com> | 2014-03-25 19:29:42 (GMT) |
---|---|---|
committer | R David Murray <rdmurray@bitdance.com> | 2014-03-25 19:29:42 (GMT) |
commit | 6d9117604f9198044add00c71bc672c2ee54b7b7 (patch) | |
tree | c53b1c18d9b2eb6c8035b9942bd0f4226ed3db9f | |
parent | 988bc970eb5465537eb2d60d4c5a694f0be2b6d8 (diff) | |
download | cpython-6d9117604f9198044add00c71bc672c2ee54b7b7.zip cpython-6d9117604f9198044add00c71bc672c2ee54b7b7.tar.gz cpython-6d9117604f9198044add00c71bc672c2ee54b7b7.tar.bz2 |
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.
-rw-r--r-- | Lib/unittest/case.py | 4 | ||||
-rw-r--r-- | Lib/unittest/test/test_case.py | 6 | ||||
-rw-r--r-- | Misc/ACKS | 1 | ||||
-rw-r--r-- | Misc/NEWS | 3 |
4 files changed, 12 insertions, 2 deletions
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 @@ class _AssertRaisesContext(object): 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 @@ class TestCase(object): 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 diff --git a/Lib/unittest/test/test_case.py b/Lib/unittest/test/test_case.py index 42a753b..b69fdb3 100644 --- a/Lib/unittest/test/test_case.py +++ b/Lib/unittest/test/test_case.py @@ -979,6 +979,12 @@ test case self.assertRaisesRegexp, Exception, u'x', lambda: None) + def testAssertRaisesRegexpInvalidRegexp(self): + # Issue 20145. + class MyExc(Exception): + pass + self.assertRaises(TypeError, self.assertRaisesRegexp, MyExc, lambda: True) + def testAssertRaisesRegexpMismatch(self): def Stub(): raise Exception('Unexpected') @@ -546,6 +546,7 @@ Stefan Hoffmeister Albert Hofkamp Tomas Hoger Jonathan Hogg +Kamilla Holanda Steve Holden Akintayo Holder Thomas Holenstein @@ -40,6 +40,9 @@ Core and Builtins 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(), close the file descriptor if os.fdopen() fails |