GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GH-96704: Add task.get_context(), use it in call_exception_handler() by gvanrossum · Pull Request #96756 · python/cpython

GH-96704: Add task.get_context(), use it in call_exception_handler() #96756

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.

Sign up for GitHub

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

Merged
gvanrossum merged 7 commits into python:main from gvanrossum:exc-handler-context
Oct 5, 2022

Uh oh!

There was an error while loading. Please reload this page.

Merged

GH-96704: Add task.get_context(), use it in call_exception_handler() #96756

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
7 commits
Select commit Hold shift + click to select a range
1d2d081
GH-96704: Add task.get_context(), use it in call_exception_handler()
gvanrossum Sep 11, 2022
9fbc1d8
add two tests
kumaraditya303 Sep 17, 2022
bc3000b
📜🤖 Added by blurb_it.
blurb-it[bot] Sep 18, 2022
bcd1a4a
Also look in handle for contextvars
gvanrossum Oct 4, 2022
d9ccfe3
Update NEWS
gvanrossum Oct 4, 2022
a362fc0
Document the {Task,Handle}.get_context() methods
gvanrossum Oct 4, 2022
0ca5023
Document change for exception handler
gvanrossum Oct 4, 2022
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

16 changes: 16 additions & 0 deletions 16 Doc/library/asyncio-eventloop.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,15 @@ Allows customizing how exceptions are handled in the event loop.
(see :meth:`call_exception_handler` documentation for details
about context).

If the handler is called on behalf of a :class:`~asyncio.Task` or
:class:`~asyncio.Handle`, it is run in the
:class:`contextvars.Context` of that task or callback handle.

.. versionchanged:: 3.12

The handler may be called in the :class:`~contextvars.Context`
of the task or handle where the exception originated.

.. method:: loop.get_exception_handler()

Return the current exception handler, or ``None`` if no custom
Expand Down Expand Up @@ -1474,6 +1483,13 @@ Callback Handles
A callback wrapper object returned by :meth:`loop.call_soon`,
:meth:`loop.call_soon_threadsafe`.

.. method:: get_context()

Return the :class:`contextvars.Context` object
associated with the handle.

.. versionadded:: 3.12

.. method:: cancel()

Cancel the callback. If the callback has already been canceled
Expand Down
7 changes: 7 additions & 0 deletions 7 Doc/library/asyncio-task.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,13 @@ Task Object

.. versionadded:: 3.8

.. method:: get_context()

Return the :class:`contextvars.Context` object
associated with the task.

.. versionadded:: 3.12

.. method:: get_name()

Return the name of the Task.
Expand Down
17 changes: 16 additions & 1 deletion 17 Lib/asyncio/base_events.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1808,7 +1808,22 @@ def call_exception_handler(self, context):
exc_info=True)
else:
try:
self._exception_handler(self, context)
ctx = None
thing = context.get("task")
if thing is None:
# Even though Futures don't have a context,
# Task is a subclass of Future,
# and sometimes the 'future' key holds a Task.
thing = context.get("future")
if thing is None:
# Handles also have a context.
thing = context.get("handle")
if thing is not None and hasattr(thing, "get_context"):
ctx = thing.get_context()
if ctx is not None and hasattr(ctx, "run"):
ctx.run(self._exception_handler, self, context)
else:
self._exception_handler(self, context)
except (SystemExit, KeyboardInterrupt):
raise
except BaseException as exc:
Expand Down
3 changes: 3 additions & 0 deletions 3 Lib/asyncio/events.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ def __repr__(self):
info = self._repr_info()
return '<{}>'.format(' '.join(info))

def get_context(self):
return self._context

def cancel(self):
if not self._cancelled:
self._cancelled = True
Expand Down
3 changes: 3 additions & 0 deletions 3 Lib/asyncio/tasks.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ def __repr__(self):
def get_coro(self):
return self._coro

def get_context(self):
return self._context

def get_name(self):
return self._name

Expand Down
41 changes: 41 additions & 0 deletions 41 Lib/test/test_asyncio/test_futures2.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# IsolatedAsyncioTestCase based tests
import asyncio
import contextvars
import traceback
import unittest
from asyncio import tasks
Expand Down Expand Up @@ -27,6 +28,46 @@ async def raise_exc():
else:
self.fail('TypeError was not raised')

async def test_task_exc_handler_correct_context(self):
# see https://github.com/python/cpython/issues/96704
name = contextvars.ContextVar('name', default='foo')
exc_handler_called = False

def exc_handler(*args):
self.assertEqual(name.get(), 'bar')
nonlocal exc_handler_called
exc_handler_called = True

async def task():
name.set('bar')
1/0

loop = asyncio.get_running_loop()
loop.set_exception_handler(exc_handler)
self.cls(task())
await asyncio.sleep(0)
self.assertTrue(exc_handler_called)

async def test_handle_exc_handler_correct_context(self):
# see https://github.com/python/cpython/issues/96704
name = contextvars.ContextVar('name', default='foo')
exc_handler_called = False

def exc_handler(*args):
self.assertEqual(name.get(), 'bar')
nonlocal exc_handler_called
exc_handler_called = True

def callback():
name.set('bar')
1/0

loop = asyncio.get_running_loop()
loop.set_exception_handler(exc_handler)
loop.call_soon(callback)
await asyncio.sleep(0)
self.assertTrue(exc_handler_called)

@unittest.skipUnless(hasattr(tasks, '_CTask'),
'requires the C _asyncio module')
class CFutureTests(FutureTests, unittest.IsolatedAsyncioTestCase):
Expand Down
11 changes: 11 additions & 0 deletions 11 Lib/test/test_asyncio/test_tasks.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2482,6 +2482,17 @@ def test_get_coro(self):
finally:
loop.close()

def test_get_context(self):
loop = asyncio.new_event_loop()
coro = coroutine_function()
context = contextvars.copy_context()
try:
task = self.new_task(loop, coro, context=context)
loop.run_until_complete(task)
self.assertIs(task.get_context(), context)
finally:
loop.close()


def add_subclass_tests(cls):
BaseTask = cls.Task
Expand Down
1 change: 1 addition & 0 deletions 1 Misc/NEWS.d/next/Library/2022-09-18-04-51-30.gh-issue-96704.DmamRX.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pass the correct ``contextvars.Context`` when a ``asyncio`` exception handler is called on behalf of a task or callback handle. This adds a new ``Task`` method, ``get_context``, and also a new ``Handle`` method with the same name. If this method is not found on a task object (perhaps because it is a third-party library that does not yet provide this method), the context prevailing at the time the exception handler is called is used.
13 changes: 13 additions & 0 deletions 13 Modules/_asynciomodule.c
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2409,6 +2409,18 @@ _asyncio_Task_get_coro_impl(TaskObj *self)
return self->task_coro;
}

/*[clinic input]
_asyncio.Task.get_context
[clinic start generated code]*/

static PyObject *
_asyncio_Task_get_context_impl(TaskObj *self)
/*[clinic end generated code: output=6996f53d3dc01aef input=87c0b209b8fceeeb]*/
{
Py_INCREF(self->task_context);
return self->task_context;
}

/*[clinic input]
_asyncio.Task.get_name
[clinic start generated code]*/
Expand Down Expand Up @@ -2536,6 +2548,7 @@ static PyMethodDef TaskType_methods[] = {
_ASYNCIO_TASK_GET_NAME_METHODDEF
_ASYNCIO_TASK_SET_NAME_METHODDEF
_ASYNCIO_TASK_GET_CORO_METHODDEF
_ASYNCIO_TASK_GET_CONTEXT_METHODDEF
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
{NULL, NULL} /* Sentinel */
};
Expand Down
19 changes: 18 additions & 1 deletion 19 Modules/clinic/_asynciomodule.c.h
Show comments
View file
Open in desktop

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.