@@ -50,6 +50,7 @@ include "errors.pyx"
5050
5151cdef:
5252 int PY39 = PY_VERSION_HEX >= 0x03090000
53+ int PY311 = PY_VERSION_HEX >= 0x030b0000
5354 uint64_t MAX_SLEEP = 3600 * 24 * 365 * 100
5455
5556
@@ -1413,30 +1414,64 @@ cdef class Loop:
14131414 """ Create a Future object attached to the loop."""
14141415 return self ._new_future()
14151416
1416- def create_task (self , coro , *, name = None ):
1417- """ Schedule a coroutine object.
1417+ if PY311:
14181418
1419- Return a task object.
1419+ def create_task (self , coro , *, name = None , context = None ):
1420+ """ Schedule a coroutine object.
14201421
1421- If name is not None, task.set_name(name) will be called if the task
1422- object has the set_name attribute, true for default Task in Python 3.8.
1423- """
1424- self ._check_closed()
1425- if self ._task_factory is None :
1426- task = aio_Task(coro, loop = self )
1427- else :
1428- task = self ._task_factory(self , coro)
1422+ Return a task object.
14291423
1430- # copied from asyncio.tasks._set_task_name (bpo-34270)
1431- if name is not None :
1432- try :
1433- set_name = task.set_name
1434- except AttributeError :
1435- pass
1424+ If name is not None, task.set_name(name) will be called if the task
1425+ object has the set_name attribute, true for default Task in Python
1426+ 3.8.
1427+
1428+ An optional keyword-only context argument allows specifying a
1429+ custom contextvars.Context for the coro to run in. The current
1430+ context copy is created when no context is provided.
1431+ """
1432+ self ._check_closed()
1433+ if self ._task_factory is None :
1434+ task = aio_Task(coro, loop = self , context = context)
1435+ else :
1436+ task = self ._task_factory(self , coro, context = context)
1437+
1438+ # copied from asyncio.tasks._set_task_name (bpo-34270)
1439+ if name is not None :
1440+ try :
1441+ set_name = task.set_name
1442+ except AttributeError :
1443+ pass
1444+ else :
1445+ set_name(name)
1446+
1447+ return task
1448+ else :
1449+
1450+ def create_task (self , coro , *, name = None ):
1451+ """ Schedule a coroutine object.
1452+
1453+ Return a task object.
1454+
1455+ If name is not None, task.set_name(name) will be called if the task
1456+ object has the set_name attribute, true for default Task in Python
1457+ 3.8.
1458+ """
1459+ self ._check_closed()
1460+ if self ._task_factory is None :
1461+ task = aio_Task(coro, loop = self )
14361462 else :
1437- set_name(name)
1463+ task = self ._task_factory(self , coro)
1464+
1465+ # copied from asyncio.tasks._set_task_name (bpo-34270)
1466+ if name is not None :
1467+ try :
1468+ set_name = task.set_name
1469+ except AttributeError :
1470+ pass
1471+ else :
1472+ set_name(name)
14381473
1439- return task
1474+ return task
14401475
14411476 def set_task_factory (self , factory ):
14421477 """ Set a task factory that will be used by loop.create_task().
0 commit comments