Skip to content

Commit

Permalink
Avoid manual async loop management
Browse files Browse the repository at this point in the history
Python 3.14 removes the implicit creation of an event loop when running
asyncio.get_event_loop().  Rather than manually managing the loop
creation, switch the async tests to use the higher level asyncio.run()
interface, as recommended by the standard library docs.

https://docs.python.org/dev/whatsnew/3.14.html#id4

Resolves #741
  • Loading branch information
carlwgeorge committed Dec 7, 2024
1 parent c767bc4 commit 5db92db
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions tests/sh_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1722,9 +1722,10 @@ async def consumer(q):
self.assertEqual(msg, "hello")
alternating.append(2)

loop = asyncio.get_event_loop()
fut = asyncio.gather(producer(q), consumer(q))
loop.run_until_complete(fut)
async def main():
await asyncio.gather(producer(q), consumer(q))

asyncio.run(main())
self.assertListEqual(alternating, [1, 2, 1, 2])

def test_async_exc(self):
Expand All @@ -1733,8 +1734,7 @@ def test_async_exc(self):
async def producer():
await python(py.name, _async=True)

loop = asyncio.get_event_loop()
self.assertRaises(sh.ErrorReturnCode_34, loop.run_until_complete, producer())
self.assertRaises(sh.ErrorReturnCode_34, asyncio.run, producer())

def test_async_iter(self):
py = create_tmp_test(
Expand Down Expand Up @@ -1763,9 +1763,10 @@ async def consumer(q):
return
alternating.append(2)

loop = asyncio.get_event_loop()
res = asyncio.gather(producer(q), consumer(q))
loop.run_until_complete(res)
async def main():
await asyncio.gather(producer(q), consumer(q))

asyncio.run(main())
self.assertListEqual(alternating, [1, 2, 1, 2, 1, 2, 1, 2, 1, 2])

def test_async_iter_exc(self):
Expand All @@ -1783,8 +1784,7 @@ async def producer():
async for line in python(py.name, _async=True):
lines.append(int(line.strip()))

loop = asyncio.get_event_loop()
self.assertRaises(sh.ErrorReturnCode_34, loop.run_until_complete, producer())
self.assertRaises(sh.ErrorReturnCode_34, asyncio.run, producer())

def test_handle_both_out_and_err(self):
py = create_tmp_test(
Expand Down

0 comments on commit 5db92db

Please sign in to comment.