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/+readlink-unc.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Preserve the leading double backslash when normalizing extended UNC targets in ``Path.readlink()``.
7 changes: 6 additions & 1 deletion path/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1480,7 +1480,12 @@ def readlink(self) -> Self:

.. seealso:: :meth:`readlinkabs`, :func:`os.readlink`
"""
return self._next_class(os.readlink(self).removeprefix('\\\\?\\'))
target = os.readlink(self)
if target.startswith('\\\\?\\UNC\\'):
target = '\\\\' + target.removeprefix('\\\\?\\UNC\\')
else:
target = target.removeprefix('\\\\?\\')
return self._next_class(target)

def readlinkabs(self) -> Self:
"""Return the path to which this symbolic link points.
Expand Down
15 changes: 15 additions & 0 deletions tests/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,21 @@ def test_get_owner(self):


class TestLinks:
@pytest.mark.parametrize(
('target', 'expected'),
[
(r'\\?\UNC\server\share\file', r'\\server\share\file'),
(r'\\?\C:\folder\file', r'C:\folder\file'),
(r'\\server\share\file', r'\\server\share\file'),
('relative-file', 'relative-file'),
],
)
def test_readlink_windows_prefixes(self, monkeypatch, target, expected):
monkeypatch.setattr(os, 'readlink', lambda _: target)
result = Path('link').readlink()
assert result == expected
assert isinstance(result, Path)

def test_hardlink_to(self, tmpdir):
target = Path(tmpdir) / 'target'
target.write_text('hello', encoding='utf-8')
Expand Down