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
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index 67b94e0..825533d 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -1244,6 +1244,15 @@
with self.open(mode='w', encoding=encoding, errors=errors) as f:
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):
"""
Create this file with the given access mode, if it doesn't exist.
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index 221c272..058a201 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -1812,6 +1812,16 @@
self.assertEqual(os.stat(r).st_size, size)
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):
P = self.cls(BASE)
p = P / 'newfileA'