@@ -40,6 +40,24 @@ include "includes/stdlib.pxi"
4040include " errors.pyx"
4141
4242
43+ cdef _is_sock_ip(sock_family):
44+ return sock_family == uv.AF_INET or sock_family == uv.AF_INET6
45+
46+
47+ cdef _is_sock_stream(sock_type):
48+ # Linux's socket.type is a bitmask that can include extra info
49+ # about socket, therefore we can't do simple
50+ # `sock_type == socket.SOCK_STREAM`.
51+ return (sock_type & uv.SOCK_STREAM) == uv.SOCK_STREAM
52+
53+
54+ cdef _is_sock_dgram(sock_type):
55+ # Linux's socket.type is a bitmask that can include extra info
56+ # about socket, therefore we can't do simple
57+ # `sock_type == socket.SOCK_DGRAM`.
58+ return (sock_type & uv.SOCK_DGRAM) == uv.SOCK_DGRAM
59+
60+
4361cdef isfuture(obj):
4462 if aio_isfuture is None :
4563 return isinstance (obj, aio_Future)
@@ -1322,6 +1340,10 @@ cdef class Loop:
13221340 else :
13231341 if sock is None :
13241342 raise ValueError (' Neither host/port nor sock were specified' )
1343+ if (not _is_sock_stream(sock.type) or
1344+ not _is_sock_ip(sock.family)):
1345+ raise ValueError (
1346+ ' A TCP Stream Socket was expected, got {!r}' .format(sock))
13251347 tcp = TCPServer.new(self , protocol_factory, server, ssl,
13261348 uv.AF_UNSPEC)
13271349
@@ -1505,6 +1527,10 @@ cdef class Loop:
15051527 if sock is None :
15061528 raise ValueError (
15071529 ' host and port was not specified and no sock specified' )
1530+ if (not _is_sock_stream(sock.type) or
1531+ not _is_sock_ip(sock.family)):
1532+ raise ValueError (
1533+ ' A TCP Stream Socket was expected, got {!r}' .format(sock))
15081534
15091535 waiter = self ._new_future()
15101536 tr = TCPTransport.new(self , protocol, None , waiter)
@@ -1578,8 +1604,6 @@ cdef class Loop:
15781604 if ssl is not None and not isinstance (ssl, ssl_SSLContext):
15791605 raise TypeError (' ssl argument must be an SSLContext or None' )
15801606
1581- pipe = UnixServer.new(self , protocol_factory, server, ssl)
1582-
15831607 if path is not None :
15841608 if sock is not None :
15851609 raise ValueError (
@@ -1594,7 +1618,6 @@ cdef class Loop:
15941618 try :
15951619 sock.bind(path)
15961620 except OSError as exc:
1597- pipe._close()
15981621 sock.close()
15991622 if exc.errno == errno.EADDRINUSE:
16001623 # Let's improve the error message by adding
@@ -1604,7 +1627,6 @@ cdef class Loop:
16041627 else :
16051628 raise
16061629 except :
1607- pipe._close()
16081630 sock.close()
16091631 raise
16101632
@@ -1613,11 +1635,13 @@ cdef class Loop:
16131635 raise ValueError (
16141636 ' path was not specified, and no sock specified' )
16151637
1616- if sock.family != uv.AF_UNIX or sock.type ! = uv.SOCK_STREAM :
1638+ if sock.family != uv.AF_UNIX or not _is_sock_stream( sock.type) :
16171639 raise ValueError (
16181640 ' A UNIX Domain Stream Socket was expected, got {!r}'
16191641 .format(sock))
16201642
1643+ pipe = UnixServer.new(self , protocol_factory, server, ssl)
1644+
16211645 try :
16221646 # See a comment on os_dup in create_connection
16231647 fileno = os_dup(sock.fileno())
@@ -1686,7 +1710,7 @@ cdef class Loop:
16861710 if sock is None :
16871711 raise ValueError (' no path and sock were specified' )
16881712
1689- if sock.family != uv.AF_UNIX or sock.type ! = uv.SOCK_STREAM :
1713+ if sock.family != uv.AF_UNIX or not _is_sock_stream( sock.type) :
16901714 raise ValueError (
16911715 ' A UNIX Domain Stream Socket was expected, got {!r}'
16921716 .format(sock))
@@ -1989,9 +2013,9 @@ cdef class Loop:
19892013
19902014 if ssl is not None and not isinstance (ssl, ssl_SSLContext):
19912015 raise TypeError (' ssl argument must be an SSLContext or None' )
1992-
1993- if sock.type ! = uv.SOCK_STREAM:
1994- raise ValueError ( ' invalid socket type, SOCK_STREAM expected' )
2016+ if not _is_sock_stream(sock.type):
2017+ raise ValueError (
2018+ ' A Stream Socket was expected, got {!r} ' .format(sock) )
19952019
19962020 # See a comment on os_dup in create_connection
19972021 fileno = os_dup(sock.fileno())
@@ -2296,6 +2320,9 @@ cdef class Loop:
22962320 system.addrinfo * rai
22972321
22982322 if sock is not None :
2323+ if not _is_sock_dgram(sock.type):
2324+ raise ValueError (
2325+ ' A UDP Socket was expected, got {!r}' .format(sock))
22992326 if (local_addr or remote_addr or
23002327 family or proto or flags or
23012328 reuse_address or reuse_port or allow_broadcast):
0 commit comments