Skip to content

Commit fe4ea9c

Browse files
ovv1st1
authored andcommitted
bpo-31245: Asyncio unix socket datagram (python#3164)
1 parent a231428 commit fe4ea9c

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

Doc/library/asyncio-eventloop.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,10 @@ Creating connections
341341

342342
.. coroutinemethod:: AbstractEventLoop.create_datagram_endpoint(protocol_factory, local_addr=None, remote_addr=None, \*, family=0, proto=0, flags=0, reuse_address=None, reuse_port=None, allow_broadcast=None, sock=None)
343343

344-
Create datagram connection: socket family :py:data:`~socket.AF_INET` or
345-
:py:data:`~socket.AF_INET6` depending on *host* (or *family* if specified),
346-
socket type :py:data:`~socket.SOCK_DGRAM`. *protocol_factory* must be a
344+
Create datagram connection: socket family :py:data:`~socket.AF_INET`,
345+
:py:data:`~socket.AF_INET6` or :py:data:`~socket.AF_UNIX` depending on
346+
*host* (or *family* if specified), socket type
347+
:py:data:`~socket.SOCK_DGRAM`. *protocol_factory* must be a
347348
callable returning a :ref:`protocol <asyncio-protocol>` instance.
348349

349350
This method is a :ref:`coroutine <coroutine>` which will try to

Lib/asyncio/base_events.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,12 @@ def create_datagram_endpoint(self, protocol_factory,
859859
if family == 0:
860860
raise ValueError('unexpected address family')
861861
addr_pairs_info = (((family, proto), (None, None)),)
862+
elif hasattr(socket, 'AF_UNIX') and family == socket.AF_UNIX:
863+
for addr in (local_addr, remote_addr):
864+
if addr is not None and not isistance(addr, str):
865+
raise TypeError('string is expected')
866+
addr_pairs_info = (((family, proto),
867+
(local_addr, remote_addr)), )
862868
else:
863869
# join address by (family, protocol)
864870
addr_infos = collections.OrderedDict()

Lib/asyncio/events.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,8 @@ def create_datagram_endpoint(self, protocol_factory,
378378
379379
protocol_factory must be a callable returning a protocol instance.
380380
381-
socket family AF_INET or socket.AF_INET6 depending on host (or
382-
family if specified), socket type SOCK_DGRAM.
381+
socket family AF_INET, socket.AF_INET6 or socket.AF_UNIX depending on
382+
host (or family if specified), socket type SOCK_DGRAM.
383383
384384
reuse_address tells the kernel to reuse a local socket in
385385
TIME_WAIT state, without waiting for its natural timeout to

Lib/test/test_asyncio/test_base_events.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,17 @@ def test_create_datagram_endpoint_sock(self):
15281528
self.loop.run_until_complete(protocol.done)
15291529
self.assertEqual('CLOSED', protocol.state)
15301530

1531+
@unittest.skipUnless(hasattr(socket, 'AF_UNIX'), 'No UNIX Sockets')
1532+
def test_create_datagram_endpoint_sock_unix(self):
1533+
fut = self.loop.create_datagram_endpoint(
1534+
lambda: MyDatagramProto(create_future=True, loop=self.loop),
1535+
family=socket.AF_UNIX)
1536+
transport, protocol = self.loop.run_until_complete(fut)
1537+
assert transport._sock.family == socket.AF_UNIX
1538+
transport.close()
1539+
self.loop.run_until_complete(protocol.done)
1540+
self.assertEqual('CLOSED', protocol.state)
1541+
15311542
def test_create_datagram_endpoint_sock_sockopts(self):
15321543
class FakeSock:
15331544
type = socket.SOCK_DGRAM
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added support for AF_UNIX socket in asyncio `create_datagram_endpoint`.

0 commit comments

Comments
 (0)