]> granicus.if.org Git - python/commitdiff
backport: #20145: assertRaisesRegexp now raises a TypeError on bad regex.
authorR David Murray <rdmurray@bitdance.com>
Tue, 25 Mar 2014 19:29:42 +0000 (15:29 -0400)
committerR David Murray <rdmurray@bitdance.com>
Tue, 25 Mar 2014 19:29:42 +0000 (15:29 -0400)
Previously a non-string, non-regex second argument and no callable
argument could cause the test to appear to always pass.

Lib/unittest/case.py
Lib/unittest/test/test_case.py
Misc/ACKS
Misc/NEWS

index b0cb44a2fc33ff76e1574d2d6c022c59485ace3f..644fe5b5c5a8e37fa12f4db54f1145436cfe3bf9 100644 (file)
@@ -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
index 42a753b289fa23238ac1f566a7736a1f0ed3d8ea..b69fdb3e0849691e978b504ffcad377631a38ece 100644 (file)
@@ -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')
index 88753892137f67b0799c041886359350347f1064..b7404ca563af85c00e1f888e2c930bc4a2785473 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -546,6 +546,7 @@ Stefan Hoffmeister
 Albert Hofkamp
 Tomas Hoger
 Jonathan Hogg
+Kamilla Holanda
 Steve Holden
 Akintayo Holder
 Thomas Holenstein
index 0f455557c216a1338b4da7dfadf1bfb112e4a9b3..711c70ce27a66cfb8877e877a16265bfcb70b9eb 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -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