Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[3.13] gh-133745: Fix asyncio task factory name/context kwarg breaks#133948

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
17 commits
Select commitHold shift + click to select a range
5c3c953
gh-133745: Fix asyncio task factory name/context kwarg breaks
graingertMay 12, 2025
6247a57
expect failure on named eager tasks
graingertMay 12, 2025
ddfbeb2
remove failing test
graingertMay 13, 2025
9dcf0e8
use only one try/finally/del block
graingertMay 13, 2025
d607230
remove extra whitespace
graingertMay 13, 2025
f9786ce
Merge branch '3.13' into fix-name-passed-to-task-factory
graingertMay 13, 2025
36e8f04
Merge branch '3.13' into fix-name-passed-to-task-factory
graingertMay 14, 2025
ac5759a
📜🤖 Added by blurb_it.
blurb-it[bot]May 14, 2025
363d336
copy editing for news
graingertMay 14, 2025
2fd6255
Merge branch '3.13' into fix-name-passed-to-task-factory
graingertMay 17, 2025
2ba0d19
update docs about create_task
graingertMay 17, 2025
2e7556d
Discard changes to Doc/library/asyncio-task.rst
graingertMay 17, 2025
d3021ae
add pass through docs
graingertMay 17, 2025
b26c1ff
Update Doc/library/asyncio-eventloop.rst
graingertMay 17, 2025
8088dac
Update Doc/library/asyncio-eventloop.rst
graingertMay 17, 2025
5c26ed7
Apply suggestions from code review
graingertMay 18, 2025
d600dd7
Apply suggestions from code review
graingertMay 18, 2025
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
21 changes: 20 additions & 1 deletionDoc/library/asyncio-eventloop.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -355,7 +355,7 @@ Creating Futures and Tasks

.. versionadded:: 3.5.2

.. method:: loop.create_task(coro, *, name=None, context=None)
.. method:: loop.create_task(coro, *, name=None, context=None, **kwargs)

Schedule the execution of :ref:`coroutine <coroutine>` *coro*.
Return a :class:`Task` object.
Expand All@@ -364,6 +364,11 @@ Creating Futures and Tasks
for interoperability. In this case, the result type is a subclass
of :class:`Task`.

The full function signature is largely the same as that of the
:class:`Task` constructor (or factory) - all of the keyword arguments to
this function are passed through to that interface, except *name*,
or *context* if it is ``None``.

If the *name* argument is provided and not ``None``, it is set as
the name of the task using :meth:`Task.set_name`.

Expand All@@ -377,6 +382,13 @@ Creating Futures and Tasks
.. versionchanged:: 3.11
Added the *context* parameter.

.. versionchanged:: 3.13.3
Added ``kwargs`` which passes on arbitrary extra parameters, including ``name`` and ``context``.

.. versionchanged:: 3.13.4
Rolled back the change that passes on *name* and *context* (if it is None),
while still passing on other arbitrary keyword arguments (to avoid breaking backwards compatibility with 3.13.3).

.. method:: loop.set_task_factory(factory)

Set a task factory that will be used by
Expand All@@ -388,6 +400,13 @@ Creating Futures and Tasks
event loop, and *coro* is a coroutine object. The callable
must pass on all *kwargs*, and return a :class:`asyncio.Task`-compatible object.

.. versionchanged:: 3.13.3
Required that all *kwargs* are passed on to :class:`asyncio.Task`.

.. versionchanged:: 3.13.4
*name* is no longer passed to task factories. *context* is no longer passed
to task factories if it is ``None``.

.. method:: loop.get_task_factory()

Return a task factory or ``None`` if the default one is in use.
Expand Down
16 changes: 11 additions & 5 deletionsLib/asyncio/base_events.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -458,18 +458,24 @@ def create_future(self):
"""Create a Future object attached to the loop."""
return futures.Future(loop=self)

def create_task(self, coro, **kwargs):
def create_task(self, coro, *, name=None, context=None, **kwargs):
"""Schedule a coroutine object.

Return a task object.
"""
self._check_closed()
if self._task_factory is not None:
return self._task_factory(self, coro, **kwargs)
if context is not None:
kwargs["context"] = context

task = self._task_factory(self, coro, **kwargs)
task.set_name(name)

else:
task = tasks.Task(coro, loop=self, name=name, context=context, **kwargs)
if task._source_traceback:
del task._source_traceback[-1]

task = tasks.Task(coro, loop=self, **kwargs)
if task._source_traceback:
del task._source_traceback[-1]
try:
return task
finally:
Expand Down
12 changes: 0 additions & 12 deletionsLib/test/test_asyncio/test_taskgroups.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1081,18 +1081,6 @@ async def throw_error():
# cancellation happens here and error is more understandable
await asyncio.sleep(0)

async def test_name(self):
name = None

async def asyncfn():
nonlocal name
name = asyncio.current_task().get_name()

async with asyncio.TaskGroup() as tg:
tg.create_task(asyncfn(), name="example name")

self.assertEqual(name, "example name")


class TestTaskGroup(BaseTestTaskGroup, unittest.IsolatedAsyncioTestCase):
loop_factory = asyncio.EventLoop
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
In 3.13.3 we accidentally changed the signature of the asyncio ``create_task()`` family of methods and how it calls a custom task factory in a backwards incompatible way. Since some 3rd party libraries have already made changes to work around the issue that might break if we simply reverted the changes, we're instead changing things to be backwards compatible with 3.13.2 while still supporting those workarounds for 3.13.3. In particular, the special-casing of ``name`` and ``context`` is back (until 3.14) and consequently eager tasks may still find that their name hasn't been set before they execute their first yielding await.
Loading

[8]ページ先頭

©2009-2025 Movatter.jp