summaryrefslogtreecommitdiff
path: root/Lib/asyncio/events.py
diff options
context:
space:
mode:
authorYury Selivanov <yury@magic.io>2018-01-22 19:11:18 -0500
committerGitHub <noreply@github.com>2018-01-22 19:11:18 -0500
commitf23746a934177c48eff754411aba54c31d6be2f0 (patch)
tree4b32964b53fa87701f71c71937792f2489b7bbb4 /Lib/asyncio/events.py
parent9089a265918754d95e105a7c4c409ac9352c87bb (diff)
downloadcpython-git-f23746a934177c48eff754411aba54c31d6be2f0.tar.gz
bpo-32436: Implement PEP 567 (#5027)
Diffstat (limited to 'Lib/asyncio/events.py')
-rw-r--r--Lib/asyncio/events.py15
1 files changed, 10 insertions, 5 deletions
diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py
index d5365dc480..5c68d4cb97 100644
--- a/Lib/asyncio/events.py
+++ b/Lib/asyncio/events.py
@@ -11,6 +11,7 @@ __all__ = (
'_get_running_loop',
)
+import contextvars
import os
import socket
import subprocess
@@ -32,9 +33,13 @@ class Handle:
"""Object returned by callback registration methods."""
__slots__ = ('_callback', '_args', '_cancelled', '_loop',
- '_source_traceback', '_repr', '__weakref__')
+ '_source_traceback', '_repr', '__weakref__',
+ '_context')
- def __init__(self, callback, args, loop):
+ def __init__(self, callback, args, loop, context=None):
+ if context is None:
+ context = contextvars.copy_context()
+ self._context = context
self._loop = loop
self._callback = callback
self._args = args
@@ -80,7 +85,7 @@ class Handle:
def _run(self):
try:
- self._callback(*self._args)
+ self._context.run(self._callback, *self._args)
except Exception as exc:
cb = format_helpers._format_callback_source(
self._callback, self._args)
@@ -101,9 +106,9 @@ class TimerHandle(Handle):
__slots__ = ['_scheduled', '_when']
- def __init__(self, when, callback, args, loop):
+ def __init__(self, when, callback, args, loop, context=None):
assert when is not None
- super().__init__(callback, args, loop)
+ super().__init__(callback, args, loop, context)
if self._source_traceback:
del self._source_traceback[-1]
self._when = when