mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
backport: #20145: assert[Raises|Warns]Regex now raise TypeError on bad regex.
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.
This commit is contained in:
parent
87d13ea56d
commit
ef1c26798c
4 changed files with 17 additions and 1 deletions
|
@ -143,7 +143,7 @@ class _AssertRaisesBaseContext(_BaseTestCaseContext):
|
||||||
self.obj_name = str(callable_obj)
|
self.obj_name = str(callable_obj)
|
||||||
else:
|
else:
|
||||||
self.obj_name = None
|
self.obj_name = None
|
||||||
if isinstance(expected_regex, (bytes, str)):
|
if expected_regex is not None:
|
||||||
expected_regex = re.compile(expected_regex)
|
expected_regex = re.compile(expected_regex)
|
||||||
self.expected_regex = expected_regex
|
self.expected_regex = expected_regex
|
||||||
self.msg = None
|
self.msg = None
|
||||||
|
|
|
@ -1126,6 +1126,18 @@ test case
|
||||||
self.assertRaisesRegex, Exception, 'x',
|
self.assertRaisesRegex, Exception, 'x',
|
||||||
lambda: None)
|
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 testAssertRaisesRegexMismatch(self):
|
||||||
def Stub():
|
def Stub():
|
||||||
raise Exception('Unexpected')
|
raise Exception('Unexpected')
|
||||||
|
|
|
@ -546,6 +546,7 @@ Stefan Hoffmeister
|
||||||
Albert Hofkamp
|
Albert Hofkamp
|
||||||
Tomas Hoger
|
Tomas Hoger
|
||||||
Jonathan Hogg
|
Jonathan Hogg
|
||||||
|
Kamilla Holanda
|
||||||
Steve Holden
|
Steve Holden
|
||||||
Akintayo Holder
|
Akintayo Holder
|
||||||
Thomas Holenstein
|
Thomas Holenstein
|
||||||
|
|
|
@ -24,6 +24,9 @@ Core and Builtins
|
||||||
Library
|
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
|
- Issue #21058: Fix a leak of file descriptor in
|
||||||
:func:`tempfile.NamedTemporaryFile`, close the file descriptor if
|
:func:`tempfile.NamedTemporaryFile`, close the file descriptor if
|
||||||
:func:`io.open` fails
|
:func:`io.open` fails
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue