import concurrent.futures._base
import logging
+import reprlib
import sys
import traceback
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):
+ def _repr_info(self):
info = [self._state.lower()]
if self._state == _FINISHED:
- info.append(self._format_result())
+ if self._exception is not None:
+ info.append('exception={!r}'.format(self._exception))
+ else:
+ # use reprlib to limit the length of the output, especially
+ # for very long strings
+ result = reprlib.repr(self._result)
+ info.append('result={}'.format(result))
if self._callbacks:
info.append(self._format_callbacks())
+ if self._source_traceback:
+ frame = self._source_traceback[-1]
+ info.append('created at %s:%s' % (frame[0], frame[1]))
+ return info
+
+ def __repr__(self):
+ info = self._repr_info()
return '<%s %s>' % (self.__class__.__name__, ' '.join(info))
# On Python 3.3 or older, objects with a destructor part of a reference
self._loop.call_exception_handler(context)
futures.Future.__del__(self)
- def __repr__(self):
- info = []
+ def _repr_info(self):
+ info = super()._repr_info()
+
if self._must_cancel:
- info.append('cancelling')
- else:
- info.append(self._state.lower())
+ # replace status
+ info[0] = 'cancelling'
coro = coroutines._format_coroutine(self._coro)
- info.append('coro=<%s>' % coro)
-
- if self._source_traceback:
- frame = self._source_traceback[-1]
- info.append('created at %s:%s' % (frame[0], frame[1]))
-
- if self._state == futures._FINISHED:
- info.append(self._format_result())
-
- if self._callbacks:
- info.append(self._format_callbacks())
+ info.insert(1, 'coro=<%s>' % coro)
if self._fut_waiter is not None:
- info.append('wait_for=%r' % self._fut_waiter)
-
- return '<%s %s>' % (self.__class__.__name__, ' '.join(info))
+ info.insert(2, 'wait_for=%r' % self._fut_waiter)
+ return info
def get_stack(self, *, limit=None):
"""Return the list of stack frames for this task's coroutine.
del self._source_traceback[-1]
self._ov = ov
- def __repr__(self):
- info = [self._state.lower()]
+ def _repr_info(self):
+ info = super()._repr_info()
if self._ov is not None:
state = 'pending' if self._ov.pending else 'completed'
- info.append('overlapped=<%s, %#x>' % (state, self._ov.address))
- if self._state == futures._FINISHED:
- info.append(self._format_result())
- if self._callbacks:
- info.append(self._format_callbacks())
- return '<%s %s>' % (self.__class__.__name__, ' '.join(info))
+ info.insert(1, 'overlapped=<%s, %#x>' % (state, self._ov.address))
+ return info
def _cancel_overlapped(self):
if self._ov is None:
class _WaitHandleFuture(futures.Future):
"""Subclass of Future which represents a wait handle."""
- def __init__(self, handle, wait_handle, *, loop=None):
+ def __init__(self, iocp, ov, handle, wait_handle, *, loop=None):
super().__init__(loop=loop)
+ if self._source_traceback:
+ del self._source_traceback[-1]
+ # iocp and ov are only used by cancel() to notify IocpProactor
+ # that the wait was cancelled
+ self._iocp = iocp
+ self._ov = ov
self._handle = handle
self._wait_handle = wait_handle
return (_winapi.WaitForSingleObject(self._handle, 0) ==
_winapi.WAIT_OBJECT_0)
- def __repr__(self):
- info = [self._state.lower()]
+ def _repr_info(self):
+ info = super()._repr_info()
+ info.insert(1, 'handle=%#x' % self._handle)
if self._wait_handle:
- state = 'pending' if self._poll() else 'completed'
- info.append('wait_handle=<%s, %#x>' % (state, self._wait_handle))
- info.append('handle=<%#x>' % self._handle)
- if self._state == futures._FINISHED:
- info.append(self._format_result())
- if self._callbacks:
- info.append(self._format_callbacks())
- return '<%s %s>' % (self.__class__.__name__, ' '.join(info))
-
- def _unregister(self):
+ state = 'signaled' if self._poll() else 'waiting'
+ info.insert(1, 'wait_handle=<%s, %#x>'
+ % (state, self._wait_handle))
+ return info
+
+ def _unregister_wait(self):
if self._wait_handle is None:
return
try:
raise
# ERROR_IO_PENDING is not an error, the wait was unregistered
self._wait_handle = None
+ self._iocp = None
+ self._ov = None
def cancel(self):
- self._unregister()
- return super().cancel()
+ result = super().cancel()
+ if self._ov is not None:
+ # signal the cancellation to the overlapped object
+ _overlapped.PostQueuedCompletionStatus(self._iocp, True,
+ 0, self._ov.address)
+ self._unregister_wait()
+ return result
+
+ def set_exception(self, exception):
+ super().set_exception(exception)
+ self._unregister_wait()
+
+ def set_result(self, result):
+ super().set_result(result)
+ self._unregister_wait()
class PipeServer(object):
ov = _overlapped.Overlapped(NULL)
wh = _overlapped.RegisterWaitWithQueue(
handle, self._iocp, ov.address, ms)
- f = _WaitHandleFuture(handle, wh, loop=self._loop)
+ f = _WaitHandleFuture(self._iocp, ov, handle, wh, loop=self._loop)
+ if f._source_traceback:
+ del f._source_traceback[-1]
def finish_wait_for_handle(trans, key, ov):
# Note that this second wait means that we should only use
# or semaphores are not. Also note if the handle is
# signalled and then quickly reset, then we may return
# False even though we have not timed out.
+ return f._poll()
+
+ if f._poll():
try:
- return f._poll()
- finally:
- f._unregister()
+ result = f._poll()
+ except OSError as exc:
+ f.set_exception(exc)
+ else:
+ f.set_result(result)
- self._cache[ov.address] = (f, ov, None, finish_wait_for_handle)
+ self._cache[ov.address] = (f, ov, 0, finish_wait_for_handle)
return f
def _register_with_iocp(self, obj):
# operation when it completes. The future's value is actually
# the value returned by callback().
f = _OverlappedFuture(ov, loop=self._loop)
+ if f._source_traceback:
+ del f._source_traceback[-1]
if not ov.pending and not wait_for_post:
# The operation has completed, so no need to postpone the
# work. We cannot take this short cut if we need the
ms = math.ceil(timeout * 1e3)
if ms >= INFINITE:
raise ValueError("timeout too big")
+
while True:
status = _overlapped.GetQueuedCompletionStatus(self._iocp, ms)
if status is None:
return
+ ms = 0
+
err, transferred, key, address = status
try:
f, ov, obj, callback = self._cache.pop(address)
# handle which should be closed to avoid a leak.
if key not in (0, _overlapped.INVALID_HANDLE_VALUE):
_winapi.CloseHandle(key)
- ms = 0
continue
if obj in self._stopped_serving:
else:
f.set_result(value)
self._results.append(f)
- ms = 0
def _stop_serving(self, obj):
# obj is a socket or pipe handle. It will be closed in
self.assertEqual(next(g), ('C', 42)) # yield 'C', y.
def test_future_repr(self):
+ self.loop.set_debug(True)
+ f_pending_debug = asyncio.Future(loop=self.loop)
+ frame = f_pending_debug._source_traceback[-1]
+ self.assertEqual(repr(f_pending_debug),
+ '<Future pending created at %s:%s>'
+ % (frame[0], frame[1]))
+ f_pending_debug.cancel()
+
+ self.loop.set_debug(False)
f_pending = asyncio.Future(loop=self.loop)
self.assertEqual(repr(f_pending), '<Future pending>')
f_pending.cancel()
@mock.patch('asyncio.base_events.logger')
def test_future_exception_never_retrieved(self, m_log):
- # FIXME: Python issue #21163, other tests may "leak" pending task which
- # emit a warning when they are destroyed by the GC
- support.gc_collect()
- m_log.error.reset_mock()
- # ---
-
self.loop.set_debug(True)
def memory_error():
if sys.version_info >= (3, 4):
frame = source_traceback[-1]
regex = (r'^Future exception was never retrieved\n'
- r'future: <Future finished exception=MemoryError\(\)>\n'
+ r'future: <Future finished exception=MemoryError\(\) created at {filename}:{lineno}>\n'
r'source_traceback: Object created at \(most recent call last\):\n'
r' File'
r'.*\n'