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

@ -18,13 +18,17 @@ if sys.platform == 'win32':
import _overlapped # Will also be exported.
# This relies on each of the submodules having an __all__ variable.
from .futures import *
from .base_events import *
from .events import *
from .futures import *
from .locks import *
from .transports import *
from .proactor_events import *
from .protocols import *
from .queues import *
from .selector_events import *
from .streams import *
from .tasks import *
from .transports import *
if sys.platform == 'win32': # pragma: no cover
from .windows_events import *
@ -32,10 +36,14 @@ else:
from .unix_events import * # pragma: no cover
__all__ = (futures.__all__ +
__all__ = (base_events.__all__ +
events.__all__ +
futures.__all__ +
locks.__all__ +
transports.__all__ +
proactor_events.__all__ +
protocols.__all__ +
queues.__all__ +
selector_events.__all__ +
streams.__all__ +
tasks.__all__)
tasks.__all__ +
transports.__all__)

View file

@ -4,6 +4,8 @@ A proactor is a "notify-on-completion" multiplexer. Currently a
proactor is only implemented on Windows with IOCP.
"""
__all__ = ['BaseProactorEventLoop']
import socket
from . import base_events

View file

@ -4,6 +4,8 @@ A selector is a "notify-when-ready" multiplexer. For a subclass which
also includes support for signal handling, see the unix_events sub-module.
"""
__all__ = ['BaseSelectorEventLoop']
import collections
import errno
import socket

View file

@ -1,7 +1,7 @@
"""Stream-related things."""
__all__ = ['StreamReader', 'StreamWriter', 'StreamReaderProtocol',
'open_connection', 'start_server',
'open_connection', 'start_server', 'IncompleteReadError',
]
import collections
@ -14,6 +14,19 @@ from . import tasks
_DEFAULT_LIMIT = 2**16
class IncompleteReadError(EOFError):
"""
Incomplete read error. Attributes:
- partial: read bytes string before the end of stream was reached
- expected: total number of expected bytes
"""
def __init__(self, partial, expected):
EOFError.__init__(self, "%s bytes read on a total of %s expected bytes"
% (len(partial), expected))
self.partial = partial
self.expected = expected
@tasks.coroutine
def open_connection(host=None, port=None, *,
@ -403,12 +416,9 @@ class StreamReader:
while n > 0:
block = yield from self.read(n)
if not block:
break
partial = b''.join(blocks)
raise IncompleteReadError(partial, len(partial) + n)
blocks.append(block)
n -= len(block)
# TODO: Raise EOFError if we break before n == 0? (That would
# be a change in specification, but I've always had to add an
# explicit size check to the caller.)
return b''.join(blocks)

View file

@ -259,9 +259,11 @@ class _UnixWritePipeTransport(transports.WriteTransport):
self._fileno = pipe.fileno()
mode = os.fstat(self._fileno).st_mode
is_socket = stat.S_ISSOCK(mode)
is_pipe = stat.S_ISFIFO(mode)
if not (is_socket or is_pipe):
raise ValueError("Pipe transport is for pipes/sockets only.")
if not (is_socket or
stat.S_ISFIFO(mode) or
stat.S_ISCHR(mode)):
raise ValueError("Pipe transport is only for "
"pipes, sockets and character devices")
_set_nonblocking(self._fileno)
self._protocol = protocol
self._buffer = []