summaryrefslogtreecommitdiff
path: root/Lib/asyncio/futures.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2014-06-25 21:41:58 +0200
committerVictor Stinner <victor.stinner@gmail.com>2014-06-25 21:41:58 +0200
commit050d2d4b3272e17934e253263a1cb99bae211b80 (patch)
tree0b5ec39e15cf2b494480ec37f75e39c10a929125 /Lib/asyncio/futures.py
parent8c4f230cfd830d1605892463c6e64b6cd64a0164 (diff)
downloadcpython-050d2d4b3272e17934e253263a1cb99bae211b80.tar.gz
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.py48
1 files changed, 32 insertions, 16 deletions
diff --git a/Lib/asyncio/futures.py b/Lib/asyncio/futures.py
index 4edd2e5059..3103fe11bd 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