Commit a01ba333 authored by Girts's avatar Girts Committed by Miss Skeleton (bot)

bpo-30618: add readlink to pathlib.Path (GH-8285)



This adds a "readlink" method to pathlib.Path objects that calls through
to os.readlink.


https://bugs.python.org/issue30618



Automerge-Triggered-By: @gpshead
parent 01659ca6
...@@ -944,6 +944,19 @@ call fails (for example because the path doesn't exist). ...@@ -944,6 +944,19 @@ call fails (for example because the path doesn't exist).
.. versionadded:: 3.5 .. versionadded:: 3.5
.. method:: Path.readlink()
Return the path to which the symbolic link points (as returned by
:func:`os.readlink`)::
>>> p = Path('mylink')
>>> p.symlink_to('setup.py')
>>> p.readlink()
PosixPath('setup.py')
.. versionadded:: 3.9
.. method:: Path.rename(target) .. method:: Path.rename(target)
Rename this file or directory to the given *target*, and return a new Path Rename this file or directory to the given *target*, and return a new Path
...@@ -1153,6 +1166,7 @@ os and os.path pathlib ...@@ -1153,6 +1166,7 @@ os and os.path pathlib
:func:`os.path.isdir` :meth:`Path.is_dir` :func:`os.path.isdir` :meth:`Path.is_dir`
:func:`os.path.isfile` :meth:`Path.is_file` :func:`os.path.isfile` :meth:`Path.is_file`
:func:`os.path.islink` :meth:`Path.is_symlink` :func:`os.path.islink` :meth:`Path.is_symlink`
:func:`os.readlink` :meth:`Path.readlink`
:func:`os.stat` :meth:`Path.stat`, :func:`os.stat` :meth:`Path.stat`,
:meth:`Path.owner`, :meth:`Path.owner`,
:meth:`Path.group` :meth:`Path.group`
......
...@@ -150,6 +150,13 @@ customization consistently by always using the value specified by ...@@ -150,6 +150,13 @@ customization consistently by always using the value specified by
case), and one used ``__VENV_NAME__`` instead. case), and one used ``__VENV_NAME__`` instead.
(Contributed by Brett Cannon in :issue:`37663`.) (Contributed by Brett Cannon in :issue:`37663`.)
pathlib
-------
Added :meth:`~pathlib.Path.readlink()` which acts similar to
:func:`~os.readlink`.
(Contributed by Girts Folkmanis in :issue:`30618`)
pprint pprint
------ ------
......
...@@ -1244,6 +1244,15 @@ class Path(PurePath): ...@@ -1244,6 +1244,15 @@ class Path(PurePath):
with self.open(mode='w', encoding=encoding, errors=errors) as f: with self.open(mode='w', encoding=encoding, errors=errors) as f:
return f.write(data) return f.write(data)
def readlink(self):
"""
Return the path to which the symbolic link points.
"""
path = self._accessor.readlink(self)
obj = self._from_parts((path,), init=False)
obj._init(template=self)
return obj
def touch(self, mode=0o666, exist_ok=True): def touch(self, mode=0o666, exist_ok=True):
""" """
Create this file with the given access mode, if it doesn't exist. Create this file with the given access mode, if it doesn't exist.
......
...@@ -1812,6 +1812,16 @@ class _BasePathTest(object): ...@@ -1812,6 +1812,16 @@ class _BasePathTest(object):
self.assertEqual(os.stat(r).st_size, size) self.assertEqual(os.stat(r).st_size, size)
self.assertFileNotFound(q.stat) self.assertFileNotFound(q.stat)
@support.skip_unless_symlink
def test_readlink(self):
P = self.cls(BASE)
self.assertEqual((P / 'linkA').readlink(), self.cls('fileA'))
self.assertEqual((P / 'brokenLink').readlink(),
self.cls('non-existing'))
self.assertEqual((P / 'linkB').readlink(), self.cls('dirB'))
with self.assertRaises(OSError):
(P / 'fileA').readlink()
def test_touch_common(self): def test_touch_common(self):
P = self.cls(BASE) P = self.cls(BASE)
p = P / 'newfileA' p = P / 'newfileA'
......
Add :meth:`~pathlib.Path.readlink`. Patch by Girts Folkmanis.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment