util/jsoncomm: support PathLike
Add support for `util.types.PathLike` paths for socket addresses, instead of just plain strings. Test it by using pathlib.Path to create the address in the corresponding test.
This commit is contained in:
parent
0aa44c23bb
commit
28947f3bae
2 changed files with 7 additions and 5 deletions
|
|
@ -14,6 +14,7 @@ import os
|
||||||
import socket
|
import socket
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
from .types import PathLike
|
||||||
|
|
||||||
|
|
||||||
class FdSet():
|
class FdSet():
|
||||||
|
|
@ -137,7 +138,7 @@ class Socket(contextlib.AbstractContextManager):
|
||||||
self._unlink = None
|
self._unlink = None
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def new_client(cls, connect_to: Optional[str] = None):
|
def new_client(cls, connect_to: Optional[PathLike] = None):
|
||||||
"""Create Client
|
"""Create Client
|
||||||
|
|
||||||
Create a new client socket.
|
Create a new client socket.
|
||||||
|
|
@ -163,7 +164,7 @@ class Socket(contextlib.AbstractContextManager):
|
||||||
# Connect the socket. This has no effect other than specifying the
|
# Connect the socket. This has no effect other than specifying the
|
||||||
# default destination for send operations.
|
# default destination for send operations.
|
||||||
if connect_to is not None:
|
if connect_to is not None:
|
||||||
sock.connect(connect_to)
|
sock.connect(os.fspath(connect_to))
|
||||||
except:
|
except:
|
||||||
if sock is not None:
|
if sock is not None:
|
||||||
sock.close()
|
sock.close()
|
||||||
|
|
@ -172,7 +173,7 @@ class Socket(contextlib.AbstractContextManager):
|
||||||
return cls(sock, None)
|
return cls(sock, None)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def new_server(cls, bind_to: str):
|
def new_server(cls, bind_to: PathLike):
|
||||||
"""Create Server
|
"""Create Server
|
||||||
|
|
||||||
Create a new listener socket.
|
Create a new listener socket.
|
||||||
|
|
@ -199,7 +200,7 @@ class Socket(contextlib.AbstractContextManager):
|
||||||
# as well. We do not guarantee atomicity, so you better make sure
|
# as well. We do not guarantee atomicity, so you better make sure
|
||||||
# you do not rely on it.
|
# you do not rely on it.
|
||||||
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
|
sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
|
||||||
sock.bind(bind_to)
|
sock.bind(os.fspath(bind_to))
|
||||||
unlink = os.open(os.path.join(".", path[0]), os.O_CLOEXEC | os.O_PATH)
|
unlink = os.open(os.path.join(".", path[0]), os.O_CLOEXEC | os.O_PATH)
|
||||||
except:
|
except:
|
||||||
if unlink is not None:
|
if unlink is not None:
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import os
|
import os
|
||||||
|
import pathlib
|
||||||
import tempfile
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
|
@ -13,7 +14,7 @@ from osbuild.util import jsoncomm
|
||||||
class TestUtilJsonComm(unittest.TestCase):
|
class TestUtilJsonComm(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.dir = tempfile.TemporaryDirectory()
|
self.dir = tempfile.TemporaryDirectory()
|
||||||
self.address = os.path.join(self.dir.name, "listener")
|
self.address = pathlib.Path(self.dir.name, "listener")
|
||||||
self.server = jsoncomm.Socket.new_server(self.address)
|
self.server = jsoncomm.Socket.new_server(self.address)
|
||||||
self.client = jsoncomm.Socket.new_client(self.address)
|
self.client = jsoncomm.Socket.new_client(self.address)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue