Update asyncio from the Tulip project

Major changes:

- StreamReader.readexactly() now raises an IncompleteReadError if the
  end of stream is reached before we received enough bytes, instead of
  returning less bytes than requested.

- Unit tests use the main asyncio module instead of submodules like events

- _UnixWritePipeTransport now also supports character devices, as
  _UnixReadPipeTransport. Patch written by Jonathan Slenders.

- Export more symbols: BaseEventLoop, BaseProactorEventLoop,
  BaseSelectorEventLoop, Queue and Queue sublasses, Empty, Full
This commit is contained in:
Victor Stinner 2014-01-25 15:32:06 +01:00
parent 75a5ec88ff
commit 8dffc456d7
17 changed files with 931 additions and 887 deletions

View file

@ -4,10 +4,7 @@ import unittest
import unittest.mock
import re
from asyncio import events
from asyncio import futures
from asyncio import locks
from asyncio import tasks
import asyncio
from asyncio import test_utils
@ -24,33 +21,33 @@ class LockTests(unittest.TestCase):
def setUp(self):
self.loop = test_utils.TestLoop()
events.set_event_loop(None)
asyncio.set_event_loop(None)
def tearDown(self):
self.loop.close()
def test_ctor_loop(self):
loop = unittest.mock.Mock()
lock = locks.Lock(loop=loop)
lock = asyncio.Lock(loop=loop)
self.assertIs(lock._loop, loop)
lock = locks.Lock(loop=self.loop)
lock = asyncio.Lock(loop=self.loop)
self.assertIs(lock._loop, self.loop)
def test_ctor_noloop(self):
try:
events.set_event_loop(self.loop)
lock = locks.Lock()
asyncio.set_event_loop(self.loop)
lock = asyncio.Lock()
self.assertIs(lock._loop, self.loop)
finally:
events.set_event_loop(None)
asyncio.set_event_loop(None)
def test_repr(self):
lock = locks.Lock(loop=self.loop)
lock = asyncio.Lock(loop=self.loop)
self.assertTrue(repr(lock).endswith('[unlocked]>'))
self.assertTrue(RGX_REPR.match(repr(lock)))
@tasks.coroutine
@asyncio.coroutine
def acquire_lock():
yield from lock
@ -59,9 +56,9 @@ class LockTests(unittest.TestCase):
self.assertTrue(RGX_REPR.match(repr(lock)))
def test_lock(self):
lock = locks.Lock(loop=self.loop)
lock = asyncio.Lock(loop=self.loop)
@tasks.coroutine
@asyncio.coroutine
def acquire_lock():
return (yield from lock)
@ -74,31 +71,31 @@ class LockTests(unittest.TestCase):
self.assertFalse(lock.locked())
def test_acquire(self):
lock = locks.Lock(loop=self.loop)
lock = asyncio.Lock(loop=self.loop)
result = []
self.assertTrue(self.loop.run_until_complete(lock.acquire()))
@tasks.coroutine
@asyncio.coroutine
def c1(result):
if (yield from lock.acquire()):
result.append(1)
return True
@tasks.coroutine
@asyncio.coroutine
def c2(result):
if (yield from lock.acquire()):
result.append(2)
return True
@tasks.coroutine
@asyncio.coroutine
def c3(result):
if (yield from lock.acquire()):
result.append(3)
return True
t1 = tasks.Task(c1(result), loop=self.loop)
t2 = tasks.Task(c2(result), loop=self.loop)
t1 = asyncio.Task(c1(result), loop=self.loop)
t2 = asyncio.Task(c2(result), loop=self.loop)
test_utils.run_briefly(self.loop)
self.assertEqual([], result)
@ -110,7 +107,7 @@ class LockTests(unittest.TestCase):
test_utils.run_briefly(self.loop)
self.assertEqual([1], result)
t3 = tasks.Task(c3(result), loop=self.loop)
t3 = asyncio.Task(c3(result), loop=self.loop)
lock.release()
test_utils.run_briefly(self.loop)
@ -128,13 +125,13 @@ class LockTests(unittest.TestCase):
self.assertTrue(t3.result())
def test_acquire_cancel(self):
lock = locks.Lock(loop=self.loop)
lock = asyncio.Lock(loop=self.loop)
self.assertTrue(self.loop.run_until_complete(lock.acquire()))
task = tasks.Task(lock.acquire(), loop=self.loop)
task = asyncio.Task(lock.acquire(), loop=self.loop)
self.loop.call_soon(task.cancel)
self.assertRaises(
futures.CancelledError,
asyncio.CancelledError,
self.loop.run_until_complete, task)
self.assertFalse(lock._waiters)
@ -153,9 +150,9 @@ class LockTests(unittest.TestCase):
# B's waiter; instead, it should move on to C's waiter.
# Setup: A has the lock, b and c are waiting.
lock = locks.Lock(loop=self.loop)
lock = asyncio.Lock(loop=self.loop)
@tasks.coroutine
@asyncio.coroutine
def lockit(name, blocker):
yield from lock.acquire()
try:
@ -164,14 +161,14 @@ class LockTests(unittest.TestCase):
finally:
lock.release()
fa = futures.Future(loop=self.loop)
ta = tasks.Task(lockit('A', fa), loop=self.loop)
fa = asyncio.Future(loop=self.loop)
ta = asyncio.Task(lockit('A', fa), loop=self.loop)
test_utils.run_briefly(self.loop)
self.assertTrue(lock.locked())
tb = tasks.Task(lockit('B', None), loop=self.loop)
tb = asyncio.Task(lockit('B', None), loop=self.loop)
test_utils.run_briefly(self.loop)
self.assertEqual(len(lock._waiters), 1)
tc = tasks.Task(lockit('C', None), loop=self.loop)
tc = asyncio.Task(lockit('C', None), loop=self.loop)
test_utils.run_briefly(self.loop)
self.assertEqual(len(lock._waiters), 2)
@ -187,12 +184,12 @@ class LockTests(unittest.TestCase):
self.assertTrue(tc.done())
def test_release_not_acquired(self):
lock = locks.Lock(loop=self.loop)
lock = asyncio.Lock(loop=self.loop)
self.assertRaises(RuntimeError, lock.release)
def test_release_no_waiters(self):
lock = locks.Lock(loop=self.loop)
lock = asyncio.Lock(loop=self.loop)
self.loop.run_until_complete(lock.acquire())
self.assertTrue(lock.locked())
@ -200,9 +197,9 @@ class LockTests(unittest.TestCase):
self.assertFalse(lock.locked())
def test_context_manager(self):
lock = locks.Lock(loop=self.loop)
lock = asyncio.Lock(loop=self.loop)
@tasks.coroutine
@asyncio.coroutine
def acquire_lock():
return (yield from lock)
@ -212,7 +209,7 @@ class LockTests(unittest.TestCase):
self.assertFalse(lock.locked())
def test_context_manager_no_yield(self):
lock = locks.Lock(loop=self.loop)
lock = asyncio.Lock(loop=self.loop)
try:
with lock:
@ -227,29 +224,29 @@ class EventTests(unittest.TestCase):
def setUp(self):
self.loop = test_utils.TestLoop()
events.set_event_loop(None)
asyncio.set_event_loop(None)
def tearDown(self):
self.loop.close()
def test_ctor_loop(self):
loop = unittest.mock.Mock()
ev = locks.Event(loop=loop)
ev = asyncio.Event(loop=loop)
self.assertIs(ev._loop, loop)
ev = locks.Event(loop=self.loop)
ev = asyncio.Event(loop=self.loop)
self.assertIs(ev._loop, self.loop)
def test_ctor_noloop(self):
try:
events.set_event_loop(self.loop)
ev = locks.Event()
asyncio.set_event_loop(self.loop)
ev = asyncio.Event()
self.assertIs(ev._loop, self.loop)
finally:
events.set_event_loop(None)
asyncio.set_event_loop(None)
def test_repr(self):
ev = locks.Event(loop=self.loop)
ev = asyncio.Event(loop=self.loop)
self.assertTrue(repr(ev).endswith('[unset]>'))
match = RGX_REPR.match(repr(ev))
self.assertEqual(match.group('extras'), 'unset')
@ -263,33 +260,33 @@ class EventTests(unittest.TestCase):
self.assertTrue(RGX_REPR.match(repr(ev)))
def test_wait(self):
ev = locks.Event(loop=self.loop)
ev = asyncio.Event(loop=self.loop)
self.assertFalse(ev.is_set())
result = []
@tasks.coroutine
@asyncio.coroutine
def c1(result):
if (yield from ev.wait()):
result.append(1)
@tasks.coroutine
@asyncio.coroutine
def c2(result):
if (yield from ev.wait()):
result.append(2)
@tasks.coroutine
@asyncio.coroutine
def c3(result):
if (yield from ev.wait()):
result.append(3)
t1 = tasks.Task(c1(result), loop=self.loop)
t2 = tasks.Task(c2(result), loop=self.loop)
t1 = asyncio.Task(c1(result), loop=self.loop)
t2 = asyncio.Task(c2(result), loop=self.loop)
test_utils.run_briefly(self.loop)
self.assertEqual([], result)
t3 = tasks.Task(c3(result), loop=self.loop)
t3 = asyncio.Task(c3(result), loop=self.loop)
ev.set()
test_utils.run_briefly(self.loop)
@ -303,24 +300,24 @@ class EventTests(unittest.TestCase):
self.assertIsNone(t3.result())
def test_wait_on_set(self):
ev = locks.Event(loop=self.loop)
ev = asyncio.Event(loop=self.loop)
ev.set()
res = self.loop.run_until_complete(ev.wait())
self.assertTrue(res)
def test_wait_cancel(self):
ev = locks.Event(loop=self.loop)
ev = asyncio.Event(loop=self.loop)
wait = tasks.Task(ev.wait(), loop=self.loop)
wait = asyncio.Task(ev.wait(), loop=self.loop)
self.loop.call_soon(wait.cancel)
self.assertRaises(
futures.CancelledError,
asyncio.CancelledError,
self.loop.run_until_complete, wait)
self.assertFalse(ev._waiters)
def test_clear(self):
ev = locks.Event(loop=self.loop)
ev = asyncio.Event(loop=self.loop)
self.assertFalse(ev.is_set())
ev.set()
@ -330,16 +327,16 @@ class EventTests(unittest.TestCase):
self.assertFalse(ev.is_set())
def test_clear_with_waiters(self):
ev = locks.Event(loop=self.loop)
ev = asyncio.Event(loop=self.loop)
result = []
@tasks.coroutine
@asyncio.coroutine
def c1(result):
if (yield from ev.wait()):
result.append(1)
return True
t = tasks.Task(c1(result), loop=self.loop)
t = asyncio.Task(c1(result), loop=self.loop)
test_utils.run_briefly(self.loop)
self.assertEqual([], result)
@ -363,55 +360,55 @@ class ConditionTests(unittest.TestCase):
def setUp(self):
self.loop = test_utils.TestLoop()
events.set_event_loop(None)
asyncio.set_event_loop(None)
def tearDown(self):
self.loop.close()
def test_ctor_loop(self):
loop = unittest.mock.Mock()
cond = locks.Condition(loop=loop)
cond = asyncio.Condition(loop=loop)
self.assertIs(cond._loop, loop)
cond = locks.Condition(loop=self.loop)
cond = asyncio.Condition(loop=self.loop)
self.assertIs(cond._loop, self.loop)
def test_ctor_noloop(self):
try:
events.set_event_loop(self.loop)
cond = locks.Condition()
asyncio.set_event_loop(self.loop)
cond = asyncio.Condition()
self.assertIs(cond._loop, self.loop)
finally:
events.set_event_loop(None)
asyncio.set_event_loop(None)
def test_wait(self):
cond = locks.Condition(loop=self.loop)
cond = asyncio.Condition(loop=self.loop)
result = []
@tasks.coroutine
@asyncio.coroutine
def c1(result):
yield from cond.acquire()
if (yield from cond.wait()):
result.append(1)
return True
@tasks.coroutine
@asyncio.coroutine
def c2(result):
yield from cond.acquire()
if (yield from cond.wait()):
result.append(2)
return True
@tasks.coroutine
@asyncio.coroutine
def c3(result):
yield from cond.acquire()
if (yield from cond.wait()):
result.append(3)
return True
t1 = tasks.Task(c1(result), loop=self.loop)
t2 = tasks.Task(c2(result), loop=self.loop)
t3 = tasks.Task(c3(result), loop=self.loop)
t1 = asyncio.Task(c1(result), loop=self.loop)
t2 = asyncio.Task(c2(result), loop=self.loop)
t3 = asyncio.Task(c3(result), loop=self.loop)
test_utils.run_briefly(self.loop)
self.assertEqual([], result)
@ -451,25 +448,25 @@ class ConditionTests(unittest.TestCase):
self.assertTrue(t3.result())
def test_wait_cancel(self):
cond = locks.Condition(loop=self.loop)
cond = asyncio.Condition(loop=self.loop)
self.loop.run_until_complete(cond.acquire())
wait = tasks.Task(cond.wait(), loop=self.loop)
wait = asyncio.Task(cond.wait(), loop=self.loop)
self.loop.call_soon(wait.cancel)
self.assertRaises(
futures.CancelledError,
asyncio.CancelledError,
self.loop.run_until_complete, wait)
self.assertFalse(cond._waiters)
self.assertTrue(cond.locked())
def test_wait_unacquired(self):
cond = locks.Condition(loop=self.loop)
cond = asyncio.Condition(loop=self.loop)
self.assertRaises(
RuntimeError,
self.loop.run_until_complete, cond.wait())
def test_wait_for(self):
cond = locks.Condition(loop=self.loop)
cond = asyncio.Condition(loop=self.loop)
presult = False
def predicate():
@ -477,7 +474,7 @@ class ConditionTests(unittest.TestCase):
result = []
@tasks.coroutine
@asyncio.coroutine
def c1(result):
yield from cond.acquire()
if (yield from cond.wait_for(predicate)):
@ -485,7 +482,7 @@ class ConditionTests(unittest.TestCase):
cond.release()
return True
t = tasks.Task(c1(result), loop=self.loop)
t = asyncio.Task(c1(result), loop=self.loop)
test_utils.run_briefly(self.loop)
self.assertEqual([], result)
@ -507,7 +504,7 @@ class ConditionTests(unittest.TestCase):
self.assertTrue(t.result())
def test_wait_for_unacquired(self):
cond = locks.Condition(loop=self.loop)
cond = asyncio.Condition(loop=self.loop)
# predicate can return true immediately
res = self.loop.run_until_complete(cond.wait_for(lambda: [1, 2, 3]))
@ -519,10 +516,10 @@ class ConditionTests(unittest.TestCase):
cond.wait_for(lambda: False))
def test_notify(self):
cond = locks.Condition(loop=self.loop)
cond = asyncio.Condition(loop=self.loop)
result = []
@tasks.coroutine
@asyncio.coroutine
def c1(result):
yield from cond.acquire()
if (yield from cond.wait()):
@ -530,7 +527,7 @@ class ConditionTests(unittest.TestCase):
cond.release()
return True
@tasks.coroutine
@asyncio.coroutine
def c2(result):
yield from cond.acquire()
if (yield from cond.wait()):
@ -538,7 +535,7 @@ class ConditionTests(unittest.TestCase):
cond.release()
return True
@tasks.coroutine
@asyncio.coroutine
def c3(result):
yield from cond.acquire()
if (yield from cond.wait()):
@ -546,9 +543,9 @@ class ConditionTests(unittest.TestCase):
cond.release()
return True
t1 = tasks.Task(c1(result), loop=self.loop)
t2 = tasks.Task(c2(result), loop=self.loop)
t3 = tasks.Task(c3(result), loop=self.loop)
t1 = asyncio.Task(c1(result), loop=self.loop)
t2 = asyncio.Task(c2(result), loop=self.loop)
t3 = asyncio.Task(c3(result), loop=self.loop)
test_utils.run_briefly(self.loop)
self.assertEqual([], result)
@ -574,11 +571,11 @@ class ConditionTests(unittest.TestCase):
self.assertTrue(t3.result())
def test_notify_all(self):
cond = locks.Condition(loop=self.loop)
cond = asyncio.Condition(loop=self.loop)
result = []
@tasks.coroutine
@asyncio.coroutine
def c1(result):
yield from cond.acquire()
if (yield from cond.wait()):
@ -586,7 +583,7 @@ class ConditionTests(unittest.TestCase):
cond.release()
return True
@tasks.coroutine
@asyncio.coroutine
def c2(result):
yield from cond.acquire()
if (yield from cond.wait()):
@ -594,8 +591,8 @@ class ConditionTests(unittest.TestCase):
cond.release()
return True
t1 = tasks.Task(c1(result), loop=self.loop)
t2 = tasks.Task(c2(result), loop=self.loop)
t1 = asyncio.Task(c1(result), loop=self.loop)
t2 = asyncio.Task(c2(result), loop=self.loop)
test_utils.run_briefly(self.loop)
self.assertEqual([], result)
@ -612,15 +609,15 @@ class ConditionTests(unittest.TestCase):
self.assertTrue(t2.result())
def test_notify_unacquired(self):
cond = locks.Condition(loop=self.loop)
cond = asyncio.Condition(loop=self.loop)
self.assertRaises(RuntimeError, cond.notify)
def test_notify_all_unacquired(self):
cond = locks.Condition(loop=self.loop)
cond = asyncio.Condition(loop=self.loop)
self.assertRaises(RuntimeError, cond.notify_all)
def test_repr(self):
cond = locks.Condition(loop=self.loop)
cond = asyncio.Condition(loop=self.loop)
self.assertTrue('unlocked' in repr(cond))
self.assertTrue(RGX_REPR.match(repr(cond)))
@ -636,9 +633,9 @@ class ConditionTests(unittest.TestCase):
self.assertTrue(RGX_REPR.match(repr(cond)))
def test_context_manager(self):
cond = locks.Condition(loop=self.loop)
cond = asyncio.Condition(loop=self.loop)
@tasks.coroutine
@asyncio.coroutine
def acquire_cond():
return (yield from cond)
@ -648,7 +645,7 @@ class ConditionTests(unittest.TestCase):
self.assertFalse(cond.locked())
def test_context_manager_no_yield(self):
cond = locks.Condition(loop=self.loop)
cond = asyncio.Condition(loop=self.loop)
try:
with cond:
@ -663,33 +660,33 @@ class SemaphoreTests(unittest.TestCase):
def setUp(self):
self.loop = test_utils.TestLoop()
events.set_event_loop(None)
asyncio.set_event_loop(None)
def tearDown(self):
self.loop.close()
def test_ctor_loop(self):
loop = unittest.mock.Mock()
sem = locks.Semaphore(loop=loop)
sem = asyncio.Semaphore(loop=loop)
self.assertIs(sem._loop, loop)
sem = locks.Semaphore(loop=self.loop)
sem = asyncio.Semaphore(loop=self.loop)
self.assertIs(sem._loop, self.loop)
def test_ctor_noloop(self):
try:
events.set_event_loop(self.loop)
sem = locks.Semaphore()
asyncio.set_event_loop(self.loop)
sem = asyncio.Semaphore()
self.assertIs(sem._loop, self.loop)
finally:
events.set_event_loop(None)
asyncio.set_event_loop(None)
def test_initial_value_zero(self):
sem = locks.Semaphore(0, loop=self.loop)
sem = asyncio.Semaphore(0, loop=self.loop)
self.assertTrue(sem.locked())
def test_repr(self):
sem = locks.Semaphore(loop=self.loop)
sem = asyncio.Semaphore(loop=self.loop)
self.assertTrue(repr(sem).endswith('[unlocked,value:1]>'))
self.assertTrue(RGX_REPR.match(repr(sem)))
@ -707,10 +704,10 @@ class SemaphoreTests(unittest.TestCase):
self.assertTrue(RGX_REPR.match(repr(sem)))
def test_semaphore(self):
sem = locks.Semaphore(loop=self.loop)
sem = asyncio.Semaphore(loop=self.loop)
self.assertEqual(1, sem._value)
@tasks.coroutine
@asyncio.coroutine
def acquire_lock():
return (yield from sem)
@ -725,43 +722,43 @@ class SemaphoreTests(unittest.TestCase):
self.assertEqual(1, sem._value)
def test_semaphore_value(self):
self.assertRaises(ValueError, locks.Semaphore, -1)
self.assertRaises(ValueError, asyncio.Semaphore, -1)
def test_acquire(self):
sem = locks.Semaphore(3, loop=self.loop)
sem = asyncio.Semaphore(3, loop=self.loop)
result = []
self.assertTrue(self.loop.run_until_complete(sem.acquire()))
self.assertTrue(self.loop.run_until_complete(sem.acquire()))
self.assertFalse(sem.locked())
@tasks.coroutine
@asyncio.coroutine
def c1(result):
yield from sem.acquire()
result.append(1)
return True
@tasks.coroutine
@asyncio.coroutine
def c2(result):
yield from sem.acquire()
result.append(2)
return True
@tasks.coroutine
@asyncio.coroutine
def c3(result):
yield from sem.acquire()
result.append(3)
return True
@tasks.coroutine
@asyncio.coroutine
def c4(result):
yield from sem.acquire()
result.append(4)
return True
t1 = tasks.Task(c1(result), loop=self.loop)
t2 = tasks.Task(c2(result), loop=self.loop)
t3 = tasks.Task(c3(result), loop=self.loop)
t1 = asyncio.Task(c1(result), loop=self.loop)
t2 = asyncio.Task(c2(result), loop=self.loop)
t3 = asyncio.Task(c3(result), loop=self.loop)
test_utils.run_briefly(self.loop)
self.assertEqual([1], result)
@ -769,7 +766,7 @@ class SemaphoreTests(unittest.TestCase):
self.assertEqual(2, len(sem._waiters))
self.assertEqual(0, sem._value)
t4 = tasks.Task(c4(result), loop=self.loop)
t4 = asyncio.Task(c4(result), loop=self.loop)
sem.release()
sem.release()
@ -794,23 +791,23 @@ class SemaphoreTests(unittest.TestCase):
sem.release()
def test_acquire_cancel(self):
sem = locks.Semaphore(loop=self.loop)
sem = asyncio.Semaphore(loop=self.loop)
self.loop.run_until_complete(sem.acquire())
acquire = tasks.Task(sem.acquire(), loop=self.loop)
acquire = asyncio.Task(sem.acquire(), loop=self.loop)
self.loop.call_soon(acquire.cancel)
self.assertRaises(
futures.CancelledError,
asyncio.CancelledError,
self.loop.run_until_complete, acquire)
self.assertFalse(sem._waiters)
def test_release_not_acquired(self):
sem = locks.BoundedSemaphore(loop=self.loop)
sem = asyncio.BoundedSemaphore(loop=self.loop)
self.assertRaises(ValueError, sem.release)
def test_release_no_waiters(self):
sem = locks.Semaphore(loop=self.loop)
sem = asyncio.Semaphore(loop=self.loop)
self.loop.run_until_complete(sem.acquire())
self.assertTrue(sem.locked())
@ -818,9 +815,9 @@ class SemaphoreTests(unittest.TestCase):
self.assertFalse(sem.locked())
def test_context_manager(self):
sem = locks.Semaphore(2, loop=self.loop)
sem = asyncio.Semaphore(2, loop=self.loop)
@tasks.coroutine
@asyncio.coroutine
def acquire_lock():
return (yield from sem)