diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2021-06-29 08:27:04 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-29 08:27:04 (GMT) |
commit | 20a88004bae8ead66a205a125e1fe979376fc3ea (patch) | |
tree | 6b8007ba8c981cfdb52cc0a674e0491588ab50e0 /Lib/test | |
parent | 48e3a1d95aee013974121fcafe19816c0e9a41da (diff) | |
download | cpython-20a88004bae8ead66a205a125e1fe979376fc3ea.zip cpython-20a88004bae8ead66a205a125e1fe979376fc3ea.tar.gz cpython-20a88004bae8ead66a205a125e1fe979376fc3ea.tar.bz2 |
bpo-12022: Change error type for bad objects in "with" and "async with" (GH-26809)
A TypeError is now raised instead of an AttributeError in
"with" and "async with" statements for objects which do not
support the context manager or asynchronous context manager
protocols correspondingly.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_contextlib.py | 4 | ||||
-rw-r--r-- | Lib/test/test_coroutines.py | 6 | ||||
-rw-r--r-- | Lib/test/test_with.py | 6 |
3 files changed, 8 insertions, 8 deletions
diff --git a/Lib/test/test_contextlib.py b/Lib/test/test_contextlib.py index 453ef6c..dbc3f5f 100644 --- a/Lib/test/test_contextlib.py +++ b/Lib/test/test_contextlib.py @@ -491,7 +491,7 @@ class TestContextDecorator(unittest.TestCase): def __exit__(self, *exc): pass - with self.assertRaises(AttributeError): + with self.assertRaisesRegex(TypeError, 'the context manager'): with mycontext(): pass @@ -503,7 +503,7 @@ class TestContextDecorator(unittest.TestCase): def __uxit__(self, *exc): pass - with self.assertRaises(AttributeError): + with self.assertRaisesRegex(TypeError, 'the context manager.*__exit__'): with mycontext(): pass diff --git a/Lib/test/test_coroutines.py b/Lib/test/test_coroutines.py index a6a199e..9b83244 100644 --- a/Lib/test/test_coroutines.py +++ b/Lib/test/test_coroutines.py @@ -1212,7 +1212,7 @@ class CoroutineTest(unittest.TestCase): async with CM(): body_executed = True - with self.assertRaisesRegex(AttributeError, '__aexit__'): + with self.assertRaisesRegex(TypeError, 'asynchronous context manager.*__aexit__'): run_async(foo()) self.assertIs(body_executed, False) @@ -1228,7 +1228,7 @@ class CoroutineTest(unittest.TestCase): async with CM(): body_executed = True - with self.assertRaisesRegex(AttributeError, '__aenter__'): + with self.assertRaisesRegex(TypeError, 'asynchronous context manager'): run_async(foo()) self.assertIs(body_executed, False) @@ -1243,7 +1243,7 @@ class CoroutineTest(unittest.TestCase): async with CM(): body_executed = True - with self.assertRaisesRegex(AttributeError, '__aenter__'): + with self.assertRaisesRegex(TypeError, 'asynchronous context manager'): run_async(foo()) self.assertIs(body_executed, False) diff --git a/Lib/test/test_with.py b/Lib/test/test_with.py index f21bf65..07522bd 100644 --- a/Lib/test/test_with.py +++ b/Lib/test/test_with.py @@ -117,7 +117,7 @@ class FailureTestCase(unittest.TestCase): def fooLacksEnter(): foo = LacksEnter() with foo: pass - self.assertRaisesRegex(AttributeError, '__enter__', fooLacksEnter) + self.assertRaisesRegex(TypeError, 'the context manager', fooLacksEnter) def testEnterAttributeError2(self): class LacksEnterAndExit(object): @@ -126,7 +126,7 @@ class FailureTestCase(unittest.TestCase): def fooLacksEnterAndExit(): foo = LacksEnterAndExit() with foo: pass - self.assertRaisesRegex(AttributeError, '__enter__', fooLacksEnterAndExit) + self.assertRaisesRegex(TypeError, 'the context manager', fooLacksEnterAndExit) def testExitAttributeError(self): class LacksExit(object): @@ -136,7 +136,7 @@ class FailureTestCase(unittest.TestCase): def fooLacksExit(): foo = LacksExit() with foo: pass - self.assertRaisesRegex(AttributeError, '__exit__', fooLacksExit) + self.assertRaisesRegex(TypeError, 'the context manager.*__exit__', fooLacksExit) def assertRaisesSyntaxError(self, codestr): def shouldRaiseSyntaxError(s): |