Skip to content
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Lib/asyncio/locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ def __init__(self, *, loop=None):
"and scheduled for removal in Python 3.10.",
DeprecationWarning, stacklevel=2)

if not self._loop.is_running():
warnings.warn("The creation of asyncio objects without a running "
"event loop is deprecated as of Python 3.9.",
DeprecationWarning, stacklevel=2)

def __repr__(self):
res = super().__repr__()
extra = 'locked' if self._locked else 'unlocked'
Expand Down Expand Up @@ -181,6 +186,11 @@ def __init__(self, *, loop=None):
"and scheduled for removal in Python 3.10.",
DeprecationWarning, stacklevel=2)

if not self._loop.is_running():
warnings.warn("The creation of asyncio objects without a running "
"event loop is deprecated as of Python 3.9.",
DeprecationWarning, stacklevel=2)

def __repr__(self):
res = super().__repr__()
extra = 'set' if self._value else 'unset'
Expand Down Expand Up @@ -248,6 +258,11 @@ def __init__(self, lock=None, *, loop=None):
"and scheduled for removal in Python 3.10.",
DeprecationWarning, stacklevel=2)

if not self._loop.is_running():
warnings.warn("The creation of asyncio objects without a running "
"event loop is deprecated as of Python 3.9.",
DeprecationWarning, stacklevel=2)

if lock is None:
lock = Lock(loop=loop)
elif lock._loop is not self._loop:
Expand Down Expand Up @@ -379,6 +394,11 @@ def __init__(self, value=1, *, loop=None):
"and scheduled for removal in Python 3.10.",
DeprecationWarning, stacklevel=2)

if not self._loop.is_running():
warnings.warn("The creation of asyncio objects without a running "
"event loop is deprecated as of Python 3.9.",
DeprecationWarning, stacklevel=2)

def __repr__(self):
res = super().__repr__()
extra = 'locked' if self.locked() else f'unlocked, value:{self._value}'
Expand Down
5 changes: 5 additions & 0 deletions Lib/asyncio/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ def __init__(self, maxsize=0, *, loop=None):
DeprecationWarning, stacklevel=2)
self._maxsize = maxsize

if not self._loop.is_running():
warnings.warn("The creation of asyncio objects without a running "
"event loop is deprecated as of Python 3.9.",
DeprecationWarning, stacklevel=2)

# Futures.
self._getters = collections.deque()
# 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):
Comment thread
eamanu marked this conversation as 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()

Comment thread
eamanu marked this conversation as resolved.

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):
Comment thread
eamanu marked this conversation as 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_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.