summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2014-07-25 12:53:47 +0200
committerVictor Stinner <victor.stinner@gmail.com>2014-07-25 12:53:47 +0200
commit77c53439cadd07728ba62a308f5b2f5bfe255f46 (patch)
treeaca5770a782836bce338bbebfd500c1877cd869f
parent0ad9f3e5c2364cfaf69294defd99b85e5b02fae1 (diff)
parent5b3ed14ed61f14e01ac0d1b11757e64119a14084 (diff)
downloadtrollius-77c53439cadd07728ba62a308f5b2f5bfe255f46.tar.gz
Merge Tulip into Trollius
-rw-r--r--trollius/proactor_events.py8
-rw-r--r--trollius/windows_events.py69
2 files changed, 51 insertions, 26 deletions
diff --git a/trollius/proactor_events.py b/trollius/proactor_events.py
index 206ffc0..5b73c93 100644
--- a/trollius/proactor_events.py
+++ b/trollius/proactor_events.py
@@ -47,13 +47,9 @@ class _ProactorBasePipeTransport(transports._FlowControlMixin,
def __repr__(self):
info = [self.__class__.__name__, 'fd=%s' % self._sock.fileno()]
if self._read_fut is not None:
- ov = "pending" if self._read_fut.ov.pending else "completed"
- info.append('read=%s' % ov)
+ info.append('read=%s' % self._read_fut)
if self._write_fut is not None:
- if self._write_fut.ov.pending:
- info.append("write=pending=%s" % self._pending_write)
- else:
- info.append("write=completed")
+ info.append("write=%r" % self._write_fut)
if self._buffer:
bufsize = len(self._buffer)
info.append('write_bufsize=%s' % bufsize)
diff --git a/trollius/windows_events.py b/trollius/windows_events.py
index 2a7a996..8362a3a 100644
--- a/trollius/windows_events.py
+++ b/trollius/windows_events.py
@@ -40,41 +40,69 @@ class _OverlappedFuture(futures.Future):
super(_OverlappedFuture, self).__init__(loop=loop)
if self._source_traceback:
del self._source_traceback[-1]
- self.ov = ov
+ self._ov = ov
def __repr__(self):
info = [self._state.lower()]
- state = 'pending' if self.ov.pending else 'completed'
- info.append('overlapped=<%s, %#x>' % (state, self.ov.address))
+ 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))
+ def _cancel_overlapped(self):
+ if self._ov is None:
+ return
+ try:
+ self._ov.cancel()
+ except OSError as exc:
+ context = {
+ 'message': 'Cancelling an overlapped future failed',
+ 'exception': exc,
+ 'future': self,
+ }
+ if self._source_traceback:
+ context['source_traceback'] = self._source_traceback
+ self._loop.call_exception_handler(context)
+ self._ov = None
+
def cancel(self):
- if not self.done():
- try:
- self.ov.cancel()
- except OSError as exc:
- context = {
- 'message': 'Cancelling an overlapped future failed',
- 'exception': exc,
- 'future': self,
- }
- if self._source_traceback:
- context['source_traceback'] = self._source_traceback
- self._loop.call_exception_handler(context)
+ self._cancel_overlapped()
return super(_OverlappedFuture, self).cancel()
+ def set_exception(self, exception):
+ super(_OverlappedFuture, self).set_exception(exception)
+ self._cancel_overlapped()
+
class _WaitHandleFuture(futures.Future):
"""Subclass of Future which represents a wait handle."""
- def __init__(self, wait_handle, loop=None):
+ def __init__(self, handle, wait_handle, loop=None):
super(_WaitHandleFuture, self).__init__(loop=loop)
+ self._handle = handle
self._wait_handle = wait_handle
+ def _poll(self):
+ # non-blocking wait: use a timeout of 0 millisecond
+ return (_winapi.WaitForSingleObject(self._handle, 0) ==
+ _winapi.WAIT_OBJECT_0)
+
+ def __repr__(self):
+ info = [self._state.lower()]
+ 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):
if self._wait_handle is None:
return
@@ -362,18 +390,19 @@ class IocpProactor(object):
ov = _overlapped.Overlapped(NULL)
wh = _overlapped.RegisterWaitWithQueue(
handle, self._iocp, ov.address, ms)
- f = _WaitHandleFuture(wh, loop=self._loop)
+ f = _WaitHandleFuture(handle, wh, loop=self._loop)
def finish_wait_for_handle(trans, key, ov):
- f._unregister()
# Note that this second wait means that we should only use
# this with handles types where a successful wait has no
# effect. So events or processes are all right, but locks
# 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 (_winapi.WaitForSingleObject(handle, 0) ==
- _winapi.WAIT_OBJECT_0)
+ try:
+ return f._poll()
+ finally:
+ f._unregister()
self._cache[ov.address] = (f, ov, None, finish_wait_for_handle)
return f