asyncio: Synchronize with Tulip

* Issue #159: Fix windows_utils.socketpair()

  - Use "127.0.0.1" (IPv4) or "::1" (IPv6) host instead of "localhost", because
    "localhost" may be a different IP address
  - Reject also invalid arguments: only AF_INET/AF_INET6 with SOCK_STREAM (and
    proto=0) are supported

* Reject add/remove reader/writer when event loop is closed.
* Fix ResourceWarning warnings
This commit is contained in:
Victor Stinner 2014-03-06 00:52:53 +01:00
parent c5cc5011ac
commit eeeebcd816
4 changed files with 73 additions and 6 deletions

View file

@ -136,6 +136,8 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
def add_reader(self, fd, callback, *args):
"""Add a reader callback."""
if self._selector is None:
raise RuntimeError('Event loop is closed')
handle = events.Handle(callback, args, self)
try:
key = self._selector.get_key(fd)
@ -151,6 +153,8 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
def remove_reader(self, fd):
"""Remove a reader callback."""
if self._selector is None:
return False
try:
key = self._selector.get_key(fd)
except KeyError:
@ -171,6 +175,8 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
def add_writer(self, fd, callback, *args):
"""Add a writer callback.."""
if self._selector is None:
raise RuntimeError('Event loop is closed')
handle = events.Handle(callback, args, self)
try:
key = self._selector.get_key(fd)
@ -186,6 +192,8 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
def remove_writer(self, fd):
"""Remove a writer callback."""
if self._selector is None:
return False
try:
key = self._selector.get_key(fd)
except KeyError:

View file

@ -36,12 +36,25 @@ def socketpair(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0):
Origin: https://gist.github.com/4325783, by Geert Jansen. Public domain.
"""
if family == socket.AF_INET:
host = '127.0.0.1'
elif family == socket.AF_INET6:
host = '::1'
else:
raise ValueError("Ony AF_INET and AF_INET6 socket address families "
"are supported")
if type != socket.SOCK_STREAM:
raise ValueError("Only SOCK_STREAM socket type is supported")
if proto != 0:
raise ValueError("Only protocol zero is supported")
# We create a connected TCP socket. Note the trick with setblocking(0)
# that prevents us from having to create a thread.
lsock = socket.socket(family, type, proto)
lsock.bind(('localhost', 0))
lsock.bind((host, 0))
lsock.listen(1)
addr, port = lsock.getsockname()
# On IPv6, ignore flow_info and scope_id
addr, port = lsock.getsockname()[:2]
csock = socket.socket(family, type, proto)
csock.setblocking(False)
try: