From 68734d3105bd6a1b10d25f01319815e8a7238572 Mon Sep 17 00:00:00 2001 From: polio <99978136+polootyyyyy@users.noreply.github.com> Date: Wed, 21 Oct 2026 12:34:05 -0400 Subject: [PATCH 1/3] Add pathlib-compatible special-file predicates --- path/__init__.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/path/__init__.py b/path/__init__.py index 731e88e..614ea0b 100644 --- a/path/__init__.py +++ b/path/__init__.py @@ -1023,6 +1023,22 @@ def is_file(self) -> bool: """.. seealso:: :func:`os.path.isfile`""" return self.module.isfile(self) + def is_fifo(self) -> bool: + """.. seealso:: :meth:`pathlib.Path.is_fifo`""" + return pathlib.Path(self).is_fifo() + + def is_socket(self) -> bool: + """.. seealso:: :meth:`pathlib.Path.is_socket`""" + return pathlib.Path(self).is_socket() + + def is_block_device(self) -> bool: + """.. seealso:: :meth:`pathlib.Path.is_block_device`""" + return pathlib.Path(self).is_block_device() + + def is_char_device(self) -> bool: + """.. seealso:: :meth:`pathlib.Path.is_char_device`""" + return pathlib.Path(self).is_char_device() + def islink(self) -> bool: """.. seealso:: :func:`os.path.islink`""" return self.module.islink(self) From 71e98374db791708a51ece37917d55a4f8794c12 Mon Sep 17 00:00:00 2001 From: polio <99978136+polootyyyyy@users.noreply.github.com> Date: Wed, 21 Oct 2026 12:34:59 -0400 Subject: [PATCH 2/3] Test special-file predicates and symlink behavior --- tests/test_path.py | 88 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/tests/test_path.py b/tests/test_path.py index 55d733c..cfa9f14 100644 --- a/tests/test_path.py +++ b/tests/test_path.py @@ -15,16 +15,20 @@ import contextlib import datetime +import errno import importlib import ntpath import os +import pathlib import platform import posixpath import re import shutil +import socket import stat import subprocess import sys +import tempfile import textwrap import time import types @@ -341,6 +345,90 @@ def test_import_time(self, monkeypatch): assert duration < limit +@pytest.mark.parametrize( + 'predicate', ('is_fifo', 'is_socket', 'is_block_device', 'is_char_device') +) +class TestSpecialFilePredicates: + @pytest.mark.parametrize('kind', ('file', 'directory', 'missing', 'file_parent')) + def test_non_special(self, tmp_path, predicate, kind): + file = tmp_path / 'file' + file.touch() + candidate = dict( + file=file, + directory=tmp_path, + missing=tmp_path / 'missing', + file_parent=file / 'child', + )[kind] + assert getattr(Path(candidate), predicate)() is False + + @pytest.fixture + def symlink(self): + def create(target, link): + try: + link.symlink_to(target) + except NotImplementedError as exc: + pytest.skip(f'Symbolic links unavailable: {exc}') + except OSError as exc: + if exc.errno not in (errno.EPERM, errno.EACCES, errno.ENOSYS): + raise + pytest.skip(f'Symbolic links unavailable: {exc}') + return Path(link) + + return create + + def test_broken_symlink(self, tmp_path, predicate, symlink): + link = symlink(tmp_path / 'missing', tmp_path / 'link') + assert getattr(link, predicate)() is False + + @pytest.mark.skipif(not hasattr(os, 'mkfifo'), reason='FIFOs unavailable') + def test_fifo(self, tmp_path, predicate): + fifo = tmp_path / 'fifo' + os.mkfifo(fifo) + assert getattr(Path(fifo), predicate)() is (predicate == 'is_fifo') + + @pytest.mark.skipif(not hasattr(os, 'mkfifo'), reason='FIFOs unavailable') + def test_fifo_symlink(self, tmp_path, predicate, symlink): + fifo = tmp_path / 'fifo' + os.mkfifo(fifo) + link = symlink(fifo, tmp_path / 'link') + assert getattr(link, predicate)() is (predicate == 'is_fifo') + + @pytest.fixture + def unix_socket(self): + if not hasattr(socket, 'AF_UNIX'): + pytest.skip('Unix-domain sockets unavailable') + with tempfile.TemporaryDirectory(prefix='p-') as directory: + candidate = pathlib.Path(directory) / 'socket' + with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as server: + server.bind(str(candidate)) + yield candidate + + def test_socket(self, unix_socket, predicate): + assert getattr(Path(unix_socket), predicate)() is (predicate == 'is_socket') + + def test_socket_symlink(self, unix_socket, predicate, symlink): + link = symlink(unix_socket, unix_socket.with_name('link')) + assert getattr(link, predicate)() is (predicate == 'is_socket') + + def test_char_device(self, predicate): + device = pathlib.Path('/dev/null') + if not device.is_char_device(): + pytest.skip('/dev/null is not a character device on this platform') + assert getattr(Path(device), predicate)() is (predicate == 'is_char_device') + + def test_block_device(self, tmp_path, monkeypatch, predicate): + candidate = tmp_path / 'block' + original_stat = pathlib.Path.stat + + def stat_result(self, *args, **kwargs): + if self == candidate: + return os.stat_result((stat.S_IFBLK | 0o600,) + (0,) * 9) + return original_stat(self, *args, **kwargs) + + monkeypatch.setattr(pathlib.Path, 'stat', stat_result) + assert getattr(Path(candidate), predicate)() is (predicate == 'is_block_device') + + class TestOwnership: @pytest.mark.skipif('platform.system() == "Windows" and sys.version_info > (3, 12)') def test_get_owner(self): From d6183de4c175b514e1ccecf8e57510e7c6a3719c Mon Sep 17 00:00:00 2001 From: polio <99978136+polootyyyyy@users.noreply.github.com> Date: Wed, 21 Oct 2026 12:36:00 -0400 Subject: [PATCH 3/3] Document the new special-file predicates --- newsfragments/214.feature.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 newsfragments/214.feature.rst diff --git a/newsfragments/214.feature.rst b/newsfragments/214.feature.rst new file mode 100644 index 0000000..5d62213 --- /dev/null +++ b/newsfragments/214.feature.rst @@ -0,0 +1 @@ +Add ``Path.is_fifo()``, ``Path.is_socket()``, ``Path.is_block_device()``, and ``Path.is_char_device()``.