Skip to content
_anyio.py 1.38 KiB
Newer Older
#  Copyright 2021, 2024  Dom Sekotill <dom.sekotill@kodo.org.uk>
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

"""
Work-arounds for lack of AF_UNIX datagram socket support in Anyio
"""

from __future__ import annotations

import errno
import tempfile
from contextlib import suppress
from os import PathLike
from anyio import create_connected_unix_datagram_socket
from anyio.abc import ConnectedUNIXDatagramSocket as DatagramSocket
Dom Sekotill's avatar
Dom Sekotill committed
async def connect_unix_datagram(path: str | PathLike[str]) -> DatagramSocket:
	"""
	Return an AnyIO socket connected to a Unix datagram socket

	This behaviour is currently missing from AnyIO.
	"""
	for _ in range(10):
		fname = tempfile.mktemp(suffix=".sock", prefix="wpa_ctrl.")
		with suppress(FileExistsError):
			return await create_connected_unix_datagram_socket(path, local_path=fname)
	raise FileExistsError(
		errno.EEXIST, "No usable temporary filename found",
	)