From: Colin Watson <cjwatson@debian.org>
Date: Wed, 16 Apr 2025 11:41:13 +0100
Subject: Compensate for asyncio changes in Python 3.13.3/3.14

In
https://github.com/python/cpython/commit/38a99568763604ccec5d5027f0658100ad76876f
(backported to 3.13 as
https://github.com/python/cpython/commit/7b0543ebe649aea11531e994289293f23f41064e),
Python moved the responsibility for setting the task name from
`asyncio.create_task` to the `Task` constructor.  While `uvloop` itself
is unaffected, this causes `test_set_task_name` to fail when run with
`asyncio`.  Compensate for that in this particular test.

It's possible that `uvloop`'s behaviour should be changed to match
`asyncio`'s depending on the Python version, but that seems like a more
involved change.  For now, this at least gets the tests passing again.

Most of this analysis and patch were from Martin Hostettler in
https://bugs.debian.org/1101258#24; I just tweaked the patch slightly to
ensure it still passes on older Python versions.

Forwarded: https://github.com/MagicStack/uvloop/pull/662
Bug-Debian: https://bugs.debian.org/1101258
Last-Update: 2025-04-16
---
 tests/test_base.py | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/tests/test_base.py b/tests/test_base.py
index 86bbd1d..feac44b 100644
--- a/tests/test_base.py
+++ b/tests/test_base.py
@@ -576,9 +576,14 @@ class _TestBase:
         async def coro():
             pass
 
-        factory = lambda loop, coro, **kwargs: MyTask(
-            coro, loop=loop, **kwargs
-        )
+        def factory(loop, coro, **kwargs):
+            task = MyTask(coro, loop=loop, **kwargs)
+            # Python moved the responsibility to set the name to the Task
+            # class constructor, so MyTask.set_name is never called by
+            # Python's create_task.  Compensate for that here.
+            if self.is_asyncio_loop() and "name" in kwargs:
+                task.set_name(kwargs["name"])
+            return task
 
         self.assertIsNone(self.loop.get_task_factory())
         task = self.loop.create_task(coro(), name="mytask")
