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
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
24 changes: 24 additions & 0 deletions Lib/test/test_asyncio/test_locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,12 @@ async def f():

self.loop.run_until_complete(f())

def test_lock_loop_not_running(self):
loop = asyncio.new_event_loop()
eamanu marked this conversation as resolved.
Show resolved Hide resolved
loop.stop()
eamanu marked this conversation as resolved.
Show resolved Hide resolved
with self.assertWarns(DeprecationWarning):
q = asyncio.Lock(loop=loop)
eamanu marked this conversation as resolved.
Show resolved Hide resolved


class EventTests(test_utils.TestCase):

Expand Down Expand Up @@ -439,6 +445,12 @@ async def c1(result):
self.assertTrue(t.done())
self.assertTrue(t.result())

def test_event_loop_not_running(self):
loop = asyncio.new_event_loop()
loop.stop()
with self.assertWarns(DeprecationWarning):
q = asyncio.Event(loop=loop)


class ConditionTests(test_utils.TestCase):

Expand Down Expand Up @@ -803,6 +815,12 @@ async def task_timeout():
with self.assertWarns(DeprecationWarning):
loop.run_until_complete(task_timeout())

def test_condition_loop_not_running(self):
loop = asyncio.new_event_loop()
loop.stop()
with self.assertWarns(DeprecationWarning):
q = asyncio.Condition(loop=loop)


class SemaphoreTests(test_utils.TestCase):

Expand Down Expand Up @@ -999,6 +1017,12 @@ def test_release_no_waiters(self):
sem.release()
self.assertFalse(sem.locked())

def test_semaphore_loop_not_running(self):
loop = asyncio.new_event_loop()
loop.stop()
with self.assertWarns(DeprecationWarning):
q = asyncio.Semaphore(loop=loop)

eamanu marked this conversation as resolved.
Show resolved Hide resolved

if __name__ == '__main__':
unittest.main()
6 changes: 6 additions & 0 deletions Lib/test/test_asyncio/test_queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ async def test():
loop.run_until_complete(test())
self.assertAlmostEqual(0.02, loop.time())

def test_queue_loop_not_running(self):
loop = asyncio.new_event_loop()
loop.stop()
with self.assertWarns(DeprecationWarning):
q = asyncio.Queue(loop=loop)


class QueueGetTests(_QueueTestBase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add Deprecation Warning message when an Queue and Locks objects are created
and the loop is not running.
eamanu marked this conversation as resolved.
Show resolved Hide resolved