Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/214.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``Path.is_fifo()``, ``Path.is_socket()``, ``Path.is_block_device()``, and ``Path.is_char_device()``.
16 changes: 16 additions & 0 deletions path/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
88 changes: 88 additions & 0 deletions tests/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down