summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio/events.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/asyncio/events.py')
-rw-r--r--Lib/asyncio/events.py56
1 files changed, 39 insertions, 17 deletions
diff --git a/Lib/asyncio/events.py b/Lib/asyncio/events.py
index de161df..4054482 100644
--- a/Lib/asyncio/events.py
+++ b/Lib/asyncio/events.py
@@ -18,6 +18,7 @@ import sys
_PY34 = sys.version_info >= (3, 4)
+
def _get_function_source(func):
if _PY34:
func = inspect.unwrap(func)
@@ -33,6 +34,35 @@ def _get_function_source(func):
return None
+def _format_args(args):
+ # function formatting ('hello',) as ('hello')
+ args_repr = repr(args)
+ if len(args) == 1 and args_repr.endswith(',)'):
+ args_repr = args_repr[:-2] + ')'
+ return args_repr
+
+
+def _format_callback(func, args, suffix=''):
+ if isinstance(func, functools.partial):
+ if args is not None:
+ suffix = _format_args(args) + suffix
+ return _format_callback(func.func, func.args, suffix)
+
+ func_repr = getattr(func, '__qualname__', None)
+ if not func_repr:
+ func_repr = repr(func)
+
+ if args is not None:
+ func_repr += _format_args(args)
+ if suffix:
+ func_repr += suffix
+
+ source = _get_function_source(func)
+ if source:
+ func_repr += ' at %s:%s' % source
+ return func_repr
+
+
class Handle:
"""Object returned by callback registration methods."""
@@ -46,18 +76,11 @@ class Handle:
self._cancelled = False
def __repr__(self):
- cb_repr = getattr(self._callback, '__qualname__', None)
- if not cb_repr:
- cb_repr = str(self._callback)
-
- source = _get_function_source(self._callback)
- if source:
- cb_repr += ' at %s:%s' % source
-
- res = 'Handle({}, {})'.format(cb_repr, self._args)
+ info = []
if self._cancelled:
- res += '<cancelled>'
- return res
+ info.append('cancelled')
+ info.append(_format_callback(self._callback, self._args))
+ return '<%s %s>' % (self.__class__.__name__, ' '.join(info))
def cancel(self):
self._cancelled = True
@@ -88,13 +111,12 @@ class TimerHandle(Handle):
self._when = when
def __repr__(self):
- res = 'TimerHandle({}, {}, {})'.format(self._when,
- self._callback,
- self._args)
+ info = []
if self._cancelled:
- res += '<cancelled>'
-
- return res
+ info.append('cancelled')
+ info.append('when=%s' % self._when)
+ info.append(_format_callback(self._callback, self._args))
+ return '<%s %s>' % (self.__class__.__name__, ' '.join(info))
def __hash__(self):
return hash(self._when)