[Python-checkins] cpython (2.7): backport: #20145: assertRaisesRegexp now raises a TypeError on bad regex.
r.david.murray
python-checkins at python.org
Tue Mar 25 20:35:37 CET 2014
http://hg.python.org/cpython/rev/3f8b801e7e76
changeset: 89979:3f8b801e7e76
branch: 2.7
parent: 89976:d09032a9adee
user: R David Murray <rdmurray at bitdance.com>
date: Tue Mar 25 15:29:42 2014 -0400
summary:
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.
files:
Lib/unittest/case.py | 4 ++--
Lib/unittest/test/test_case.py | 6 ++++++
Misc/ACKS | 1 +
Misc/NEWS | 3 +++
4 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py
--- 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
diff --git a/Lib/unittest/test/test_case.py b/Lib/unittest/test/test_case.py
--- a/Lib/unittest/test/test_case.py
+++ b/Lib/unittest/test/test_case.py
@@ -979,6 +979,12 @@
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')
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -546,6 +546,7 @@
Albert Hofkamp
Tomas Hoger
Jonathan Hogg
+Kamilla Holanda
Steve Holden
Akintayo Holder
Thomas Holenstein
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -40,6 +40,9 @@
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
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list