Skip to content
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-38599: Deprecate creation of asyncio object when the loop is not running #18195

Closed
wants to merge 14 commits into from
40 changes: 24 additions & 16 deletions Lib/asyncio/locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ def __init__(self, *, loop=None):
self._waiters = None
self._locked = False
if loop is None:
self._loop = events.get_event_loop()
self._loop = events._get_running_loop()
if self._loop is None:
warnings.warn("The creation of asyncio objects outside a running "
eamanu marked this conversation as resolved.
Show resolved Hide resolved
"event loop is deprecated as of Python 3.9.",
DeprecationWarning, stacklevel=2)
self._loop = events.get_event_loop()
else:
self._loop = loop
warnings.warn("The loop argument is deprecated since Python 3.8, "
"and scheduled for removal in Python 3.10.",
DeprecationWarning, stacklevel=2)
Comment on lines -84 to -86
Copy link
Contributor

@aeros aeros Mar 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be very confusing to users to add a deprecation warning in 3.8, and then remove the warning from certain asyncio objects entirely in 3.9. That doesn't make much sense to me.

(also applies to the other warning removals)


def __repr__(self):
res = super().__repr__()
Expand Down Expand Up @@ -174,12 +176,14 @@ def __init__(self, *, loop=None):
self._waiters = collections.deque()
self._value = False
if loop is None:
self._loop = events.get_event_loop()
self._loop = events._get_running_loop()
if self._loop is None:
warnings.warn("The creation of asyncio objects outside a running "
"event loop is deprecated as of Python 3.9.",
DeprecationWarning, stacklevel=2)
self._loop = events.get_event_loop()
else:
self._loop = loop
warnings.warn("The loop argument is deprecated since Python 3.8, "
"and scheduled for removal in Python 3.10.",
DeprecationWarning, stacklevel=2)

def __repr__(self):
res = super().__repr__()
Expand Down Expand Up @@ -241,12 +245,14 @@ class Condition(_ContextManagerMixin):

def __init__(self, lock=None, *, loop=None):
if loop is None:
self._loop = events.get_event_loop()
self._loop = events._get_running_loop()
if self._loop is None:
warnings.warn("The creation of asyncio objects outside a running "
"event loop is deprecated as of Python 3.9.",
DeprecationWarning, stacklevel=2)
self._loop = events.get_event_loop()
else:
self._loop = loop
warnings.warn("The loop argument is deprecated since Python 3.8, "
"and scheduled for removal in Python 3.10.",
DeprecationWarning, stacklevel=2)

if lock is None:
lock = Lock(loop=loop)
Expand Down Expand Up @@ -372,12 +378,14 @@ def __init__(self, value=1, *, loop=None):
self._value = value
self._waiters = collections.deque()
if loop is None:
self._loop = events.get_event_loop()
self._loop = events._get_running_loop()
if self._loop is None:
warnings.warn("The creation of asyncio objects outside a running "
"event loop is deprecated as of Python 3.9.",
DeprecationWarning, stacklevel=2)
self._loop = events.get_event_loop()
else:
self._loop = loop
warnings.warn("The loop argument is deprecated since Python 3.8, "
"and scheduled for removal in Python 3.10.",
DeprecationWarning, stacklevel=2)

def __repr__(self):
res = super().__repr__()
Expand Down
11 changes: 7 additions & 4 deletions Lib/asyncio/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ class Queue:

def __init__(self, maxsize=0, *, loop=None):
if loop is None:
self._loop = events.get_event_loop()
self._loop = events._get_running_loop()
if self._loop is None:
warnings.warn("The creation of asyncio objects outside a running "
"event loop is deprecated as of Python 3.9.",
DeprecationWarning, stacklevel=2)
self._loop = events.get_event_loop()
else:
self._loop = loop
warnings.warn("The loop argument is deprecated since Python 3.8, "
"and scheduled for removal in Python 3.10.",
DeprecationWarning, stacklevel=2)

self._maxsize = maxsize

# Futures.
Expand Down
29 changes: 29 additions & 0 deletions Lib/test/test_asyncio/test_locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,5 +1000,34 @@ def test_release_no_waiters(self):
self.assertFalse(sem.locked())


class LockLoopTests:
"""Tests the deprecation of creating asyncio objects outside of a
running event loop."""

def setUp(self):
eamanu marked this conversation as resolved.
Show resolved Hide resolved
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)

def tearDown(self):
asyncio.set_event_loop(None)
self.loop.close()

def test_lock_loop_not_running(self):
with self.assertWarns(DeprecationWarning):
asyncio.Lock()

def test_event_loop_not_running(self):
with self.assertWarns(DeprecationWarning):
asyncio.Event()

def test_condition_loop_not_running(self):
with self.assertWarns(DeprecationWarning):
asyncio.Condition()

def test_semaphore_loop_not_running(self):
with self.assertWarns(DeprecationWarning):
asyncio.Semaphore()


if __name__ == '__main__':
unittest.main()
17 changes: 17 additions & 0 deletions Lib/test/test_asyncio/test_queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,5 +714,22 @@ class PriorityQueueJoinTests(_QueueJoinTestMixin, _QueueTestBase):
q_class = asyncio.PriorityQueue


class QueueLoopTests:
"""Tests the deprecation of creating asyncio objects outside of a
running event loop."""

def setUp(self):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)

def tearDown(self):
asyncio.set_event_loop(None)
self.loop.close()

def test_queue_loop_not_running(self):
with self.assertWarns(DeprecationWarning):
asyncio.Queue()


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The creation of :class:`asyncio.Queue`, :class:`asyncio.Lock`,
:class:`asyncio.Event`, :class:`asyncio.Condition`,
:class:`asyncio.Semaphore`, and :class:`asyncio.BoundedSemaphore`
objects outside of a running event loop has been deprecated.