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/+is-junction.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``Path.is_junction()`` on Python 3.12 and later for pathlib compatibility.
12 changes: 12 additions & 0 deletions path/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,18 @@ def islink(self) -> bool:
""".. seealso:: :func:`os.path.islink`"""
return self.module.islink(self)

if sys.version_info >= (3, 12):

def is_junction(self) -> bool:
"""Return whether this path is a Windows junction.

Available on Python 3.12 and later. Returns False on
platforms that do not support junctions.

.. seealso:: :func:`os.path.isjunction`
"""
return self.module.isjunction(self)

def ismount(self) -> bool:
"""
>>> Path('.').ismount()
Expand Down
29 changes: 29 additions & 0 deletions tests/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,35 @@ def test_group(self, tmpdir):
assert isinstance(file.group(), str)


@pytest.mark.skipif(
sys.version_info < (3, 12), reason='junction API requires Python 3.12'
)
class TestJunctions:
def test_non_junctions(self, tmp_path):
root = Path(tmp_path)
file = (root / 'file').touch()
assert not root.is_junction()
assert not file.is_junction()
assert not (root / 'missing').is_junction()
assert not Path('invalid\0path').is_junction()

@pytest.mark.skipif(sys.platform != 'win32', reason='Windows junctions')
def test_junction(self, tmp_path):
winapi = pytest.importorskip('_winapi')
root = Path(tmp_path)
target = (root / 'target').mkdir()
junction = root / 'junction'
winapi.CreateJunction(str(target), str(junction))
try:
assert junction.is_junction()
assert not target.is_junction()
target.rmdir()
assert junction.is_junction()
assert not junction.exists()
finally:
junction.rmdir()


class TestReadWriteText:
def test_read_write(self, tmpdir):
file = path.Path(tmpdir) / 'filename'
Expand Down