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.lchmod()`` to change permissions without following symbolic links.
15 changes: 15 additions & 0 deletions path/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
79 changes: 79 additions & 0 deletions tests/test_lchmod.py
Original file line number Diff line number Diff line change
@@ -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