]> granicus.if.org Git - python/commitdiff
backport: #20145: assert[Raises|Warns]Regex now raise TypeError on bad regex.
authorR David Murray <rdmurray@bitdance.com>
Tue, 25 Mar 2014 19:31:50 +0000 (15:31 -0400)
committerR David Murray <rdmurray@bitdance.com>
Tue, 25 Mar 2014 19:31:50 +0000 (15:31 -0400)
Previously a non-string, non-regex second argument and missing callable
argument could cause the test to appear to always pass.

Initial patch by Kamilla Holanda.

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

index 87fb02bd2ef0074d4b8177b1c6a6d6be64b9cfcf..bedbc670d21688934a31d63885b82da848ac67f0 100644 (file)
@@ -143,7 +143,7 @@ class _AssertRaisesBaseContext(_BaseTestCaseContext):
                 self.obj_name = str(callable_obj)
         else:
             self.obj_name = None
-        if isinstance(expected_regex, (bytes, str)):
+        if expected_regex is not None:
             expected_regex = re.compile(expected_regex)
         self.expected_regex = expected_regex
         self.msg = None
index 363390af09c096a2b14832cb988bf66c2a1acdc6..49325784d2db4876cfcb88b728f80bd49007d086 100644 (file)
@@ -1126,6 +1126,18 @@ test case
                 self.assertRaisesRegex, Exception, 'x',
                 lambda: None)
 
+    def testAssertRaisesRegexInvalidRegex(self):
+        # Issue 20145.
+        class MyExc(Exception):
+            pass
+        self.assertRaises(TypeError, self.assertRaisesRegex, MyExc, lambda: True)
+
+    def testAssertWarnsRegexInvalidRegex(self):
+        # Issue 20145.
+        class MyWarn(Warning):
+            pass
+        self.assertRaises(TypeError, self.assertWarnsRegex, MyWarn, lambda: True)
+
     def testAssertRaisesRegexMismatch(self):
         def Stub():
             raise Exception('Unexpected')
index 68617ce1b9407f36e3fd1961a1ff0b9685adcad1..0eb826715a5ef58ee4f56035bf34639489db51f5 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 2a7cd3bf9e233d664cfdbe5b2096f0f6b926d1c2..b00a01708ccce74da82eb00d0763986908584ed1 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -24,6 +24,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #20145: `assertRaisesRegex` and `assertWarnsRegex` now raise a
+  TypeError if the second argument is not a string or compiled regex.
+
 - Issue #21058: Fix a leak of file descriptor in
   :func:`tempfile.NamedTemporaryFile`, close the file descriptor if
   :func:`io.open` fails