diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2014-01-06 10:34:00 +0100 |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2014-01-06 10:34:00 +0100 |
commit | 295793fdfe3ed14066f458e06e6e7ef8936fe788 (patch) | |
tree | cc9a004c9df2ff932300e798d90eaf74bd3dd683 | |
parent | e33ab1ac6a29ae23a5d859f7babd2067dfa2c982 (diff) | |
download | trollius-295793fdfe3ed14066f458e06e6e7ef8936fe788.tar.gz |
export executor errors and constants in asyncio.futures and asyncio.tasks to
help compatibility with Tulip.
-rw-r--r-- | asyncio/futures.py | 16 | ||||
-rw-r--r-- | asyncio/proactor_events.py | 6 | ||||
-rw-r--r-- | asyncio/tasks.py | 28 | ||||
-rw-r--r-- | asyncio/windows_events.py | 3 | ||||
-rw-r--r-- | tests/test_events.py | 4 | ||||
-rw-r--r-- | tests/test_futures.py | 4 | ||||
-rw-r--r-- | tests/test_locks.py | 9 | ||||
-rw-r--r-- | tests/test_tasks.py | 48 | ||||
-rw-r--r-- | tests/test_windows_events.py | 2 |
9 files changed, 66 insertions, 54 deletions
diff --git a/asyncio/futures.py b/asyncio/futures.py index 50d65b9..2a7c971 100644 --- a/asyncio/futures.py +++ b/asyncio/futures.py @@ -1,6 +1,9 @@ """A Future class similar to the one in PEP 3148.""" -__all__ = ['InvalidStateError', 'Future'] +__all__ = ['CancelledError', 'TimeoutError', + 'InvalidStateError', + 'Future', 'wrap_future', + ] import logging import sys @@ -22,10 +25,14 @@ _FINISHED = 'FINISHED' _PY34 = sys.version_info >= (3, 4) +Error = executor.Error +CancelledError = executor.CancelledError +TimeoutError = executor.TimeoutError + STACK_DEBUG = logging.DEBUG - 1 # heavy-duty debugging -class InvalidStateError(executor.Error): +class InvalidStateError(Error): """The operation is not allowed in this state.""" # TODO: Show the future, its state, the method, and the required state. @@ -209,7 +216,7 @@ class Future(object): the future is done and has an exception set, this exception is raised. """ if self._state == _CANCELLED: - raise executor.CancelledError + raise CancelledError if self._state != _FINISHED: raise InvalidStateError('Result is not ready.') if self._tb_logger is not None: @@ -228,7 +235,7 @@ class Future(object): InvalidStateError. """ if self._state == _CANCELLED: - raise executor.CancelledError + raise CancelledError if self._state != _FINISHED: raise InvalidStateError('Exception is not set.') if self._tb_logger is not None: @@ -346,4 +353,3 @@ def wrap_future(fut, loop=None): lambda future: loop.call_soon_threadsafe( new_future._copy_state, fut)) return new_future -__all__.append('wrap_future') diff --git a/asyncio/proactor_events.py b/asyncio/proactor_events.py index c125aee..7bf9fed 100644 --- a/asyncio/proactor_events.py +++ b/asyncio/proactor_events.py @@ -8,7 +8,7 @@ import socket from . import base_events from . import constants -from . import executor +from . import futures from . import transports from .log import logger @@ -189,7 +189,7 @@ class _ProactorReadPipeTransport(_ProactorBasePipeTransport, self._force_close(exc) except OSError as exc: self._fatal_error(exc) - except executor.CancelledError: + except futures.CancelledError: if not self._closing: raise else: @@ -428,7 +428,7 @@ class BaseProactorEventLoop(base_events.BaseEventLoop): if sock.fileno() != -1: logger.exception('Accept failed') sock.close() - except executor.CancelledError: + except futures.CancelledError: sock.close() else: f.add_done_callback(loop) diff --git a/asyncio/tasks.py b/asyncio/tasks.py index 5cff97b..42f6b9d 100644 --- a/asyncio/tasks.py +++ b/asyncio/tasks.py @@ -3,6 +3,7 @@ from __future__ import print_function __all__ = ['coroutine', 'Task', 'iscoroutinefunction', 'iscoroutine', + 'FIRST_COMPLETED', 'FIRST_EXCEPTION', 'ALL_COMPLETED', 'wait', 'wait_for', 'as_completed', 'sleep', 'async', 'gather', 'shield', 'Return', ] @@ -185,8 +186,8 @@ class Task(futures.Future): assert not self.done(), \ '_step(): already done: {!r}, {!r}, {!r}'.format(self, value, exc) if self._must_cancel: - if not isinstance(exc, executor.CancelledError): - exc = executor.CancelledError() + if not isinstance(exc, futures.CancelledError): + exc = futures.CancelledError() self._must_cancel = False coro = self._coro self._fut_waiter = None @@ -204,7 +205,7 @@ class Task(futures.Future): self.set_result(exc.value) except StopIteration: self.set_result(None) - except executor.CancelledError as exc: + except futures.CancelledError as exc: super(Task, self).cancel() # I.e., Future.cancel(self). except Exception as exc: self.set_exception(exc) @@ -248,9 +249,14 @@ class Task(futures.Future): # wait() and as_completed() similar to those in PEP 3148. +# Export symbols in asyncio.tasks for compatibility with Tulip +FIRST_COMPLETED = executor.FIRST_COMPLETED +FIRST_EXCEPTION = executor.FIRST_EXCEPTION +ALL_COMPLETED = executor.ALL_COMPLETED + @coroutine -def wait(fs, loop=None, timeout=None, return_when=executor.ALL_COMPLETED): +def wait(fs, loop=None, timeout=None, return_when=ALL_COMPLETED): """Wait for the Futures and coroutines given by fs to complete. Coroutines will be wrapped in Tasks. @@ -272,7 +278,7 @@ def wait(fs, loop=None, timeout=None, return_when=executor.ALL_COMPLETED): fs = set(async(f, loop=loop) for f in fs) - if return_when not in (executor.FIRST_COMPLETED, executor.FIRST_EXCEPTION, executor.ALL_COMPLETED): + if return_when not in (FIRST_COMPLETED, FIRST_EXCEPTION, ALL_COMPLETED): raise ValueError('Invalid return_when value: {}'.format(return_when)) result = yield _wait(fs, timeout, return_when, loop) raise Return(result) @@ -312,7 +318,7 @@ def wait_for(fut, timeout, loop=None): raise Return(fut.result()) else: fut.remove_done_callback(cb) - raise executor.TimeoutError() + raise futures.TimeoutError() finally: timeout_handle.cancel() @@ -333,8 +339,8 @@ def _wait(fs, timeout, return_when, loop): def _on_completion(f): non_local['counter'] -= 1 if (non_local['counter'] <= 0 or - return_when == executor.FIRST_COMPLETED or - return_when == executor.FIRST_EXCEPTION and (not f.cancelled() and + return_when == FIRST_COMPLETED or + return_when == FIRST_EXCEPTION and (not f.cancelled() and f.exception() is not None)): if timeout_handle is not None: timeout_handle.cancel() @@ -387,9 +393,9 @@ def as_completed(fs, loop=None, timeout=None): if deadline is not None: timeout = deadline - loop.time() if timeout < 0: - raise executor.TimeoutError() + raise futures.TimeoutError() done, pending = yield _wait( - todo, timeout, executor.FIRST_COMPLETED, loop) + todo, timeout, FIRST_COMPLETED, loop) # Multiple callers might be waiting for the same events # and getting the same outcome. Dedupe by updating todo. for f in done: @@ -497,7 +503,7 @@ def gather(*coros_or_futures, **kw): fut.exception() return if fut._state == futures._CANCELLED: - res = executor.CancelledError() + res = futures.CancelledError() if not return_exceptions: outer.set_exception(res) return diff --git a/asyncio/windows_events.py b/asyncio/windows_events.py index 9bf8868..8102394 100644 --- a/asyncio/windows_events.py +++ b/asyncio/windows_events.py @@ -9,7 +9,6 @@ import _winapi from . import events from . import base_subprocess -from . import executor from . import futures from . import proactor_events from . import selector_events @@ -160,7 +159,7 @@ class ProactorEventLoop(proactor_events.BaseProactorEventLoop): if pipe and pipe.fileno() != -1: logger.exception('Pipe accept failed') pipe.close() - except executor.CancelledError: + except futures.CancelledError: if pipe: pipe.close() else: diff --git a/tests/test_events.py b/tests/test_events.py index 69c2463..9d26430 100644 --- a/tests/test_events.py +++ b/tests/test_events.py @@ -1048,7 +1048,7 @@ class EventLoopTestsMixin(object): try: self.loop.call_soon(f.cancel) yield f - except executor.CancelledError: + except futures.CancelledError: res = 'cancelled' else: res = None @@ -1063,7 +1063,7 @@ class EventLoopTestsMixin(object): self.assertLess(elapsed, 0.1) self.assertEqual(t.result(), 'cancelled') - self.assertRaises(executor.CancelledError, f.result) + self.assertRaises(futures.CancelledError, f.result) if ov is not None: self.assertFalse(ov.pending) self.loop._stop_serving(r) diff --git a/tests/test_futures.py b/tests/test_futures.py index da2de1a..91ae85b 100644 --- a/tests/test_futures.py +++ b/tests/test_futures.py @@ -49,8 +49,8 @@ class FutureTests(unittest.TestCase): self.assertTrue(f.cancel()) self.assertTrue(f.cancelled()) self.assertTrue(f.done()) - self.assertRaises(executor.CancelledError, f.result) - self.assertRaises(executor.CancelledError, f.exception) + self.assertRaises(futures.CancelledError, f.result) + self.assertRaises(futures.CancelledError, f.exception) self.assertRaises(futures.InvalidStateError, f.set_result, None) self.assertRaises(futures.InvalidStateError, f.set_exception, None) self.assertFalse(f.cancel()) diff --git a/tests/test_locks.py b/tests/test_locks.py index 873ecdb..5923203 100644 --- a/tests/test_locks.py +++ b/tests/test_locks.py @@ -6,6 +6,7 @@ import re from asyncio import Return from asyncio import events +from asyncio import executor from asyncio import futures from asyncio import locks from asyncio import tasks @@ -136,7 +137,7 @@ class LockTests(unittest.TestCase): task = tasks.Task(lock.acquire(), loop=self.loop) self.loop.call_soon(task.cancel) self.assertRaises( - executor.CancelledError, + futures.CancelledError, self.loop.run_until_complete, task) self.assertFalse(lock._waiters) @@ -318,7 +319,7 @@ class EventTests(unittest.TestCase): wait = tasks.Task(ev.wait(), loop=self.loop) self.loop.call_soon(wait.cancel) self.assertRaises( - executor.CancelledError, + futures.CancelledError, self.loop.run_until_complete, wait) self.assertFalse(ev._waiters) @@ -460,7 +461,7 @@ class ConditionTests(unittest.TestCase): wait = tasks.Task(cond.wait(), loop=self.loop) self.loop.call_soon(wait.cancel) self.assertRaises( - executor.CancelledError, + futures.CancelledError, self.loop.run_until_complete, wait) self.assertFalse(cond._waiters) self.assertTrue(cond.locked()) @@ -805,7 +806,7 @@ class SemaphoreTests(unittest.TestCase): acquire = tasks.Task(sem.acquire(), loop=self.loop) self.loop.call_soon(acquire.cancel) self.assertRaises( - executor.CancelledError, + futures.CancelledError, self.loop.run_until_complete, acquire) self.assertFalse(sem._waiters) diff --git a/tests/test_tasks.py b/tests/test_tasks.py index 448d0a2..55bb56f 100644 --- a/tests/test_tasks.py +++ b/tests/test_tasks.py @@ -115,7 +115,7 @@ class TaskTests(unittest.TestCase): self.assertEqual(repr(t), 'Task(<notmuch>)<PENDING, [Dummy()]>') t.cancel() # Does not take immediate effect! self.assertEqual(repr(t), 'Task(<notmuch>)<CANCELLING, [Dummy()]>') - self.assertRaises(executor.CancelledError, + self.assertRaises(futures.CancelledError, self.loop.run_until_complete, t) self.assertEqual(repr(t), 'Task(<notmuch>)<CANCELLED>') t = tasks.Task(notmuch(), loop=self.loop) @@ -175,7 +175,7 @@ class TaskTests(unittest.TestCase): t = tasks.Task(task(), loop=loop) loop.call_soon(t.cancel) - with self.assertRaises(executor.CancelledError): + with self.assertRaises(futures.CancelledError): loop.run_until_complete(t) self.assertTrue(t.done()) self.assertTrue(t.cancelled()) @@ -192,7 +192,7 @@ class TaskTests(unittest.TestCase): test_utils.run_briefly(self.loop) # start coro t.cancel() self.assertRaises( - executor.CancelledError, self.loop.run_until_complete, t) + futures.CancelledError, self.loop.run_until_complete, t) self.assertTrue(t.done()) self.assertTrue(t.cancelled()) self.assertFalse(t.cancel()) @@ -208,7 +208,7 @@ class TaskTests(unittest.TestCase): t = tasks.Task(task(), loop=self.loop) test_utils.run_briefly(self.loop) # start task f.cancel() - with self.assertRaises(executor.CancelledError): + with self.assertRaises(futures.CancelledError): self.loop.run_until_complete(t) self.assertTrue(f.cancelled()) self.assertTrue(t.cancelled()) @@ -227,7 +227,7 @@ class TaskTests(unittest.TestCase): f.cancel() t.cancel() - with self.assertRaises(executor.CancelledError): + with self.assertRaises(futures.CancelledError): self.loop.run_until_complete(t) self.assertTrue(t.done()) @@ -243,7 +243,7 @@ class TaskTests(unittest.TestCase): yield fut1 try: yield fut2 - except executor.CancelledError: + except futures.CancelledError: raise tasks.Return(42) t = tasks.Task(task(), loop=self.loop) @@ -268,7 +268,7 @@ class TaskTests(unittest.TestCase): yield fut1 try: yield fut2 - except executor.CancelledError: + except futures.CancelledError: pass res = yield fut3 raise tasks.Return(res) @@ -303,7 +303,7 @@ class TaskTests(unittest.TestCase): t = tasks.Task(task(), loop=loop) self.assertRaises( - executor.CancelledError, loop.run_until_complete, t) + futures.CancelledError, loop.run_until_complete, t) self.assertTrue(t.done()) self.assertFalse(t._must_cancel) # White-box test. self.assertFalse(t.cancel()) @@ -367,7 +367,7 @@ class TaskTests(unittest.TestCase): fut = tasks.Task(foo(), loop=loop) - with self.assertRaises(executor.TimeoutError): + with self.assertRaises(futures.TimeoutError): loop.run_until_complete(tasks.wait_for(fut, 0.1, loop=loop)) self.assertFalse(fut.done()) @@ -399,7 +399,7 @@ class TaskTests(unittest.TestCase): events.set_event_loop(loop) try: fut = tasks.Task(foo(), loop=loop) - with self.assertRaises(executor.TimeoutError): + with self.assertRaises(futures.TimeoutError): loop.run_until_complete(tasks.wait_for(fut, 0.01)) finally: events.set_event_loop(None) @@ -769,7 +769,7 @@ class TaskTests(unittest.TestCase): try: v = yield f values.append((1, v)) - except executor.TimeoutError as exc: + except futures.TimeoutError as exc: values.append((2, exc)) raise tasks.Return(values) @@ -777,7 +777,7 @@ class TaskTests(unittest.TestCase): self.assertEqual(len(res), 2, res) self.assertEqual(res[0], (1, 'a')) self.assertEqual(res[1][0], 2) - self.assertIsInstance(res[1][1], executor.TimeoutError) + self.assertIsInstance(res[1][1], futures.TimeoutError) self.assertAlmostEqual(0.12, loop.time()) # move forward to close generator @@ -907,7 +907,7 @@ class TaskTests(unittest.TestCase): loop.call_later(0.1, sleeper.cancel) try: yield sleeper - except executor.CancelledError: + except futures.CancelledError: raise tasks.Return('cancelled') else: raise tasks.Return('slept in') @@ -930,7 +930,7 @@ class TaskTests(unittest.TestCase): task.cancel() test_utils.run_briefly(self.loop) self.assertRaises( - executor.CancelledError, self.loop.run_until_complete, task) + futures.CancelledError, self.loop.run_until_complete, task) self.assertIsNone(task._fut_waiter) self.assertTrue(fut.cancelled()) @@ -1017,7 +1017,7 @@ class TaskTests(unittest.TestCase): def notmutch(): try: yield sleeper() - except executor.CancelledError: + except futures.CancelledError: raise base_exc task = tasks.Task(notmutch(), loop=loop) @@ -1131,7 +1131,7 @@ class TaskTests(unittest.TestCase): def inner(): try: yield waiter - except executor.CancelledError: + except futures.CancelledError: non_local['proof'] += 1 raise else: @@ -1141,7 +1141,7 @@ class TaskTests(unittest.TestCase): def outer(): try: yield inner() - except executor.CancelledError: + except futures.CancelledError: non_local['proof'] += 100 # Expect this path. else: non_local['proof'] += 10 @@ -1173,7 +1173,7 @@ class TaskTests(unittest.TestCase): test_utils.run_briefly(self.loop) f.cancel() self.assertRaises( - executor.CancelledError, self.loop.run_until_complete, f) + futures.CancelledError, self.loop.run_until_complete, f) waiter.set_result(None) test_utils.run_briefly(self.loop) self.assertEqual(non_local['proof'], 1) @@ -1226,7 +1226,7 @@ class TaskTests(unittest.TestCase): f = tasks.async(outer(), loop=self.loop) test_utils.run_briefly(self.loop) f.cancel() - with self.assertRaises(executor.CancelledError): + with self.assertRaises(futures.CancelledError): self.loop.run_until_complete(f) waiter.set_result(None) test_utils.run_briefly(self.loop) @@ -1256,7 +1256,7 @@ class TaskTests(unittest.TestCase): parent.cancel() # This should cancel inner1 and inner2 but bot child1 and child2. test_utils.run_briefly(self.loop) - self.assertIsInstance(parent.exception(), executor.CancelledError) + self.assertIsInstance(parent.exception(), futures.CancelledError) self.assertTrue(inner1.cancelled()) self.assertTrue(inner2.cancelled()) child1.set_result(1) @@ -1391,7 +1391,7 @@ class FutureGatherTests(GatherTestsBase, unittest.TestCase): self.assertTrue(fut.done()) cb.assert_called_once_with(fut) self.assertFalse(fut.cancelled()) - self.assertIsInstance(fut.exception(), executor.CancelledError) + self.assertIsInstance(fut.exception(), futures.CancelledError) # Does nothing c.set_result(3) d.cancel() @@ -1415,8 +1415,8 @@ class FutureGatherTests(GatherTestsBase, unittest.TestCase): rte = RuntimeError() f.set_exception(rte) res = self.one_loop.run_until_complete(fut) - self.assertIsInstance(res[2], executor.CancelledError) - self.assertIsInstance(res[4], executor.CancelledError) + self.assertIsInstance(res[2], futures.CancelledError) + self.assertIsInstance(res[4], futures.CancelledError) res[2] = res[4] = None self.assertEqual(res, [1, zde, None, 3, None, rte]) cb.assert_called_once_with(fut) @@ -1482,7 +1482,7 @@ class CoroutineGatherTests(GatherTestsBase, unittest.TestCase): f = tasks.async(outer(), loop=self.one_loop) test_utils.run_briefly(self.one_loop) self.assertTrue(f.cancel()) - with self.assertRaises(executor.CancelledError): + with self.assertRaises(futures.CancelledError): self.one_loop.run_until_complete(f) self.assertFalse(non_local['gatherer'].cancel()) self.assertTrue(waiter.cancelled()) diff --git a/tests/test_windows_events.py b/tests/test_windows_events.py index 560bc4f..8170eba 100644 --- a/tests/test_windows_events.py +++ b/tests/test_windows_events.py @@ -130,7 +130,7 @@ class ProactorTests(unittest.TestCase): f = self.loop._proactor.wait_for_handle(event, 10) f.cancel() start = self.loop.time() - with self.assertRaises(executor.CancelledError): + with self.assertRaises(futures.CancelledError): self.loop.run_until_complete(f) elapsed = self.loop.time() - start self.assertTrue(0 <= elapsed < 0.1, elapsed) |