-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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
bpo-34793: Drop old-style context managers in asyncio.locks #17533
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 |
---|---|---|
|
@@ -3,96 +3,13 @@ | |
__all__ = ('Lock', 'Event', 'Condition', 'Semaphore', 'BoundedSemaphore') | ||
|
||
import collections | ||
import types | ||
import warnings | ||
|
||
from . import events | ||
from . import futures | ||
from . import exceptions | ||
from .import coroutines | ||
|
||
|
||
class _ContextManager: | ||
"""Context manager. | ||
|
||
This enables the following idiom for acquiring and releasing a | ||
lock around a block: | ||
|
||
with (yield from lock): | ||
<block> | ||
|
||
while failing loudly when accidentally using: | ||
|
||
with lock: | ||
<block> | ||
|
||
Deprecated, use 'async with' statement: | ||
async with lock: | ||
<block> | ||
""" | ||
|
||
def __init__(self, lock): | ||
self._lock = lock | ||
|
||
def __enter__(self): | ||
# We have no use for the "as ..." clause in the with | ||
# statement for locks. | ||
return None | ||
|
||
def __exit__(self, *args): | ||
try: | ||
self._lock.release() | ||
finally: | ||
self._lock = None # Crudely prevent reuse. | ||
|
||
|
||
class _ContextManagerMixin: | ||
def __enter__(self): | ||
raise RuntimeError( | ||
'"yield from" should be used as context manager expression') | ||
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. Should we keep this? We can change the error message to say that 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. I think the motivation was emphasizing that context managers were supported but in a slightly unusual way: After switching to Maybe the standard text can be improved globally, but it is another story, I guess. |
||
|
||
def __exit__(self, *args): | ||
# This must exist because __enter__ exists, even though that | ||
# always raises; that's how the with-statement works. | ||
pass | ||
|
||
@types.coroutine | ||
def __iter__(self): | ||
# This is not a coroutine. It is meant to enable the idiom: | ||
# | ||
# with (yield from lock): | ||
# <block> | ||
# | ||
# as an alternative to: | ||
# | ||
# yield from lock.acquire() | ||
# try: | ||
# <block> | ||
# finally: | ||
# lock.release() | ||
# Deprecated, use 'async with' statement: | ||
# async with lock: | ||
# <block> | ||
warnings.warn("'with (yield from lock)' is deprecated " | ||
"use 'async with lock' instead", | ||
DeprecationWarning, stacklevel=2) | ||
yield from self.acquire() | ||
return _ContextManager(self) | ||
|
||
# The flag is needed for legacy asyncio.iscoroutine() | ||
__iter__._is_coroutine = coroutines._is_coroutine | ||
|
||
async def __acquire_ctx(self): | ||
await self.acquire() | ||
return _ContextManager(self) | ||
|
||
def __await__(self): | ||
warnings.warn("'with await lock' is deprecated " | ||
"use 'async with lock' instead", | ||
DeprecationWarning, stacklevel=2) | ||
# To make "with await lock" work. | ||
return self.__acquire_ctx().__await__() | ||
|
||
async def __aenter__(self): | ||
await self.acquire() | ||
# We have no use for the "as ..." clause in the with | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Remove support for ``with (await asyncio.lock):`` and ``with (yield from | ||
asyncio.lock):``. The same is correct for ``asyncio.Condition`` and | ||
``asyncio.Semaphore``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also replace the deprecation warning with changedversion in https://docs.python.org/3/library/asyncio-sync.html#boundedsemaphore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done