From d99daafd3e201066c22e7d880294ae09baacb9f4 Mon Sep 17 00:00:00 2001 From: harsha Date: Sat, 14 Nov 2026 17:50:10 +0530 Subject: [PATCH] Add Path.lchmod for pathlib compatibility --- newsfragments/214.feature.rst | 1 + path/__init__.py | 15 +++++++ tests/test_lchmod.py | 79 +++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 newsfragments/214.feature.rst create mode 100644 tests/test_lchmod.py diff --git a/newsfragments/214.feature.rst b/newsfragments/214.feature.rst new file mode 100644 index 0000000..c645980 --- /dev/null +++ b/newsfragments/214.feature.rst @@ -0,0 +1 @@ +Add ``Path.lchmod()`` to change permissions without following symbolic links. diff --git a/path/__init__.py b/path/__init__.py index 731e88e..b03a7f4 100644 --- a/path/__init__.py +++ b/path/__init__.py @@ -1294,6 +1294,21 @@ def chmod(self, mode: str | int) -> Self: os.chmod(self, mode) return self + def lchmod(self, mode: int) -> Self: + """ + Change permissions without following symbolic links. + + If this path is a symbolic link, change the link's permissions + rather than those of its target. Support depends on the platform + and filesystem; errors from :func:`os.chmod` are propagated. + + Return self, like :meth:`chmod`. + + .. seealso:: :meth:`pathlib.Path.lchmod`, :func:`os.chmod` + """ + os.chmod(self, mode, follow_symlinks=False) + return self + if sys.platform != "win32": def chown(self, uid: str | int = -1, gid: str | int = -1) -> Self: diff --git a/tests/test_lchmod.py b/tests/test_lchmod.py new file mode 100644 index 0000000..c0228db --- /dev/null +++ b/tests/test_lchmod.py @@ -0,0 +1,79 @@ +import errno +import os +import stat + +import pytest + +from path import Path + + +def test_lchmod_does_not_follow_symlinks(monkeypatch, tmp_path): + item = Path(tmp_path / 'link') + calls = [] + + def fake_chmod(filename, mode, *, follow_symlinks=True): + calls.append((filename, mode, follow_symlinks)) + + monkeypatch.setattr(os, 'chmod', fake_chmod) + + assert item.lchmod(0o600) is item + assert calls == [(item, 0o600, False)] + + +@pytest.mark.parametrize( + 'error', + [PermissionError('denied'), NotImplementedError('unsupported')], +) +def test_lchmod_propagates_errors(monkeypatch, error): + def fail(*args, **kwargs): + raise error + + monkeypatch.setattr(os, 'chmod', fail) + + with pytest.raises(type(error)) as caught: + Path('link').lchmod(0o600) + + assert caught.value is error + + +def test_lchmod_regular_file(tmp_path): + item = Path(tmp_path / 'file.txt') + item.write_text('content') + + assert item.lchmod(0o600) is item + assert item.stat().st_mode & stat.S_IWRITE + + if os.name != 'nt': + assert stat.S_IMODE(item.stat().st_mode) == 0o600 + + +def test_lchmod_symlink_leaves_target_unchanged(tmp_path): + target = tmp_path / 'target.txt' + target.write_text('content') + target.chmod(0o600) + link = Path(tmp_path / 'link') + + try: + os.symlink(target, link) + except OSError as error: + if getattr(error, 'winerror', None) == 1314: + pytest.skip('Creating symlinks requires Windows privileges') + raise + + before = stat.S_IMODE(target.stat().st_mode) + + try: + result = link.lchmod(0o700) + except NotImplementedError: + pytest.skip('Symlink permissions are unsupported on this platform') + except OSError as error: + if error.errno in {errno.ENOTSUP, errno.EOPNOTSUPP}: + pytest.skip('Symlink permissions are unsupported on this filesystem') + raise + + assert result is link + assert stat.S_IMODE(target.stat().st_mode) == before + assert target.read_text() == 'content' + + if os.name != 'nt': + assert stat.S_IMODE(link.lstat().st_mode) == 0o700