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

Initial patch by Kamilla Holanda.

Doc/whatsnew/3.5.rst
Lib/unittest/case.py
Lib/unittest/test/test_case.py
Misc/ACKS
Misc/NEWS

index b18bcd2d79ddb9da4fa15131410037166e2e3463..2447898f88f35a55491eaaa6ac18913f5b6627ee 100644 (file)
@@ -192,3 +192,9 @@ that may require changes to your code.
   if it represented midnight in UTC.  This behavior was considered obscure and
   error-prone and has been removed in Python 3.5.  See :issue:`13936` for full
   details.
+
+* :meth:`unittest.TestCase.assertRaisesRegex` and
+  :meth:`~unittest.TestCase.assertWarnsRegex` now raise a :exc:`TypeError` if
+  the second argument is not a string or a compiled :mod:`regex`.  You may have
+  tests with an invalid second argument that have until 3.5 been falsely
+  passing which will now raise TypeErrors (:issue:`20145`).
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 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 17e6ea5ec936f7f08d3ca15068437fced574ca99..0736f410f259df947e6de077d9c551cf3b56ef78 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -23,6 +23,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 #20633: Replace relative import by absolute import.
 
 - Issue #20980: Stop wrapping exception when using ThreadPool.