-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
bpo-44471: Change error type for bad objects in ExitStack.enter_context() #26820
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -483,7 +483,7 @@ async def __aexit__(self, *exc_details): | |
| 1/0 | ||
|
|
||
| @_async_test | ||
| async def test_async_enter_context(self): | ||
| async def test_enter_async_context(self): | ||
| class TestCM(object): | ||
| async def __aenter__(self): | ||
| result.append(1) | ||
|
|
@@ -504,6 +504,26 @@ async def _exit(): | |
|
|
||
| self.assertEqual(result, [1, 2, 3, 4]) | ||
|
|
||
| @_async_test | ||
| async def test_enter_async_context_errors(self): | ||
| class LacksEnterAndExit: | ||
| pass | ||
| class LacksEnter: | ||
| async def __aexit__(self, *exc_info): | ||
| pass | ||
| class LacksExit: | ||
| async def __aenter__(self): | ||
| pass | ||
|
|
||
| async with self.exit_stack() as stack: | ||
| with self.assertRaisesRegex(TypeError, 'asynchronous context manager'): | ||
| await stack.enter_async_context(LacksEnterAndExit()) | ||
| with self.assertRaisesRegex(TypeError, 'asynchronous context manager'): | ||
| await stack.enter_async_context(LacksEnter()) | ||
| with self.assertRaisesRegex(TypeError, 'asynchronous context manager'): | ||
| await stack.enter_async_context(LacksExit()) | ||
| self.assertFalse(stack._exit_callbacks) | ||
|
|
||
| @_async_test | ||
| async def test_async_exit_exception_chaining(self): | ||
| # Ensure exception chaining matches the reference behaviour | ||
|
|
@@ -536,6 +556,18 @@ async def suppress_exc(*exc_details): | |
| self.assertIsInstance(inner_exc, ValueError) | ||
| self.assertIsInstance(inner_exc.__context__, ZeroDivisionError) | ||
|
|
||
| @_async_test | ||
| async def test_instance_bypass_async(self): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this test doing?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is a copy of similar test for synchronous context managers ( |
||
| class Example(object): pass | ||
| cm = Example() | ||
| cm.__aenter__ = object() | ||
| cm.__aexit__ = object() | ||
| stack = self.exit_stack() | ||
| with self.assertRaisesRegex(TypeError, "asynchronous context manager"): | ||
| await stack.enter_async_context(cm) | ||
| stack.push_async_exit(cm) | ||
| self.assertIs(stack._exit_callbacks[-1][1], cm) | ||
|
|
||
|
|
||
| class TestAsyncNullcontext(unittest.TestCase): | ||
| @_async_test | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| A :exc:`TypeError` is now raised instead of an :exc:`AttributeError` in | ||
| :meth:`contextlib.ExitStack.enter_context` and | ||
| :meth:`contextlib.AsyncExitStack.enter_async_context` for objects which do | ||
| not support the :term:`context manager` or :term:`asynchronous context | ||
| manager` protocols correspondingly. |
Uh oh!
There was an error while loading. Please reload this page.