mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
asyncio: Tulip issue 173: Enhance repr(Handle) and repr(Task)
repr(Handle) is shorter for function: "foo" instead of "<function foo at 0x...>". It now also includes the source of the callback, filename and line number where it was defined, if available. repr(Task) now also includes the current position in the code, filename and line number, if available. If the coroutine (generator) is done, the line number is omitted and "done" is added.
This commit is contained in:
parent
f54432e2a1
commit
307bccc6ff
5 changed files with 123 additions and 31 deletions
|
@ -8,9 +8,29 @@ __all__ = ['AbstractEventLoopPolicy',
|
|||
'get_child_watcher', 'set_child_watcher',
|
||||
]
|
||||
|
||||
import functools
|
||||
import inspect
|
||||
import subprocess
|
||||
import threading
|
||||
import socket
|
||||
import sys
|
||||
|
||||
|
||||
_PY34 = sys.version_info >= (3, 4)
|
||||
|
||||
def _get_function_source(func):
|
||||
if _PY34:
|
||||
func = inspect.unwrap(func)
|
||||
elif hasattr(func, '__wrapped__'):
|
||||
func = func.__wrapped__
|
||||
if inspect.isfunction(func):
|
||||
code = func.__code__
|
||||
return (code.co_filename, code.co_firstlineno)
|
||||
if isinstance(func, functools.partial):
|
||||
return _get_function_source(func.func)
|
||||
if _PY34 and isinstance(func, functools.partialmethod):
|
||||
return _get_function_source(func.func)
|
||||
return None
|
||||
|
||||
|
||||
class Handle:
|
||||
|
@ -26,7 +46,15 @@ class Handle:
|
|||
self._cancelled = False
|
||||
|
||||
def __repr__(self):
|
||||
res = 'Handle({}, {})'.format(self._callback, self._args)
|
||||
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)
|
||||
if self._cancelled:
|
||||
res += '<cancelled>'
|
||||
return res
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue