diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2014-06-25 19:41:58 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2014-06-25 19:41:58 (GMT) |
commit | 975735f729df6b7767556d2d389b560dbc7500ac (patch) | |
tree | 8579740ca947c8323088a452d659e4c0cb130a2e /Lib/asyncio/futures.py | |
parent | 65c623de74cba3eff6d8f5f4c37cfec89c0fde43 (diff) | |
download | cpython-975735f729df6b7767556d2d389b560dbc7500ac.zip cpython-975735f729df6b7767556d2d389b560dbc7500ac.tar.gz cpython-975735f729df6b7767556d2d389b560dbc7500ac.tar.bz2 |
asyncio, Tulip issue 177: Rewite repr() of Future, Task, Handle and TimerHandle
- Uniformize repr() output to format "<Class ...>"
- On Python 3.5+, repr(Task) uses the qualified name instead of the short name
of the coroutine
Diffstat (limited to 'Lib/asyncio/futures.py')
-rw-r--r-- | Lib/asyncio/futures.py | 48 |
1 files changed, 32 insertions, 16 deletions
diff --git a/Lib/asyncio/futures.py b/Lib/asyncio/futures.py index 4edd2e5..3103fe1 100644 --- a/Lib/asyncio/futures.py +++ b/Lib/asyncio/futures.py @@ -150,24 +150,40 @@ class Future: self._loop = loop self._callbacks = [] + def _format_callbacks(self): + cb = self._callbacks + size = len(cb) + if not size: + cb = '' + + def format_cb(callback): + return events._format_callback(callback, ()) + + if size == 1: + cb = format_cb(cb[0]) + elif size == 2: + cb = '{}, {}'.format(format_cb(cb[0]), format_cb(cb[1])) + elif size > 2: + cb = '{}, <{} more>, {}'.format(format_cb(cb[0]), + size-2, + format_cb(cb[-1])) + return 'cb=[%s]' % cb + + def _format_result(self): + if self._state != _FINISHED: + return None + elif self._exception is not None: + return 'exception={!r}'.format(self._exception) + else: + return 'result={!r}'.format(self._result) + def __repr__(self): - res = self.__class__.__name__ + info = [self._state.lower()] if self._state == _FINISHED: - if self._exception is not None: - res += '<exception={!r}>'.format(self._exception) - else: - res += '<result={!r}>'.format(self._result) - elif self._callbacks: - size = len(self._callbacks) - if size > 2: - res += '<{}, [{}, <{} more>, {}]>'.format( - self._state, self._callbacks[0], - size-2, self._callbacks[-1]) - else: - res += '<{}, {}>'.format(self._state, self._callbacks) - else: - res += '<{}>'.format(self._state) - return res + info.append(self._format_result()) + if self._callbacks: + info.append(self._format_callbacks()) + return '<%s %s>' % (self.__class__.__name__, ' '.join(info)) # On Python 3.3 or older, objects with a destructor part of a reference # cycle are never destroyed. It's not more the case on Python 3.4 thanks to |